1 #include "parameterset.h"
2 #include "comment.h"
3 #include "modcontext.h"
4 #include "expression.h"
5 #include "printutils.h"
6 #include <boost/property_tree/json_parser.hpp>
7 #include "boost-utils.h"
8 
9 std::string ParameterSet::parameterSetsKey("parameterSets");
10 std::string ParameterSet::fileFormatVersionKey("fileFormatVersion");
11 std::string ParameterSet::fileFormatVersionValue("1");
12 
clear()13 void ParameterSet::clear()
14 {
15 	root.clear();
16 }
17 
isEmpty() const18 bool ParameterSet::isEmpty() const
19 {
20 	const boost::optional<const pt::ptree &> sets{root.get_child_optional(ParameterSet::parameterSetsKey)};
21 	const bool hasEntries = sets.is_initialized() && !sets.get().empty();
22 	return !hasEntries;
23 }
24 
parameterSets()25 boost::optional<pt::ptree &> ParameterSet::parameterSets()
26 {
27 	return root.get_child_optional(ParameterSet::parameterSetsKey);
28 }
29 
getParameterNames()30 std::vector<std::string> ParameterSet::getParameterNames()
31 {
32 	std::vector<std::string> names;
33 
34 	boost::optional<pt::ptree &> sets = parameterSets();
35 	if (sets.is_initialized()) {
36 		for (const auto &v : sets.get()) {
37 			names.push_back(v.first);
38 		}
39 	}
40 	return names;
41 }
42 
setNameExists(const std::string & setName)43 bool ParameterSet::setNameExists(const std::string &setName){
44 	boost::optional<pt::ptree &> sets = parameterSets();
45 	if (sets.is_initialized()) {
46 		for (const auto &v : sets.get()) {
47 			if(v.first == setName) return true;
48 		}
49 	}
50 	return false;
51 }
52 
getParameterSet(const std::string & setName)53 boost::optional<pt::ptree &> ParameterSet::getParameterSet(const std::string &setName)
54 {
55 	boost::optional<pt::ptree &> sets = parameterSets();
56 	if (!sets.is_initialized()) {
57 		return sets;
58 	}
59 
60 	pt::ptree::assoc_iterator set = sets.get().find(pt::ptree::key_type(setName));
61 	if(set!=sets.get().not_found()) {
62 		return set->second;
63 	}
64 	return sets;
65 }
66 
addParameterSet(const std::string setName,const pt::ptree & set)67 void ParameterSet::addParameterSet(const std::string setName, const pt::ptree & set)
68 {
69 	boost::optional<pt::ptree &> sets = parameterSets();
70 	if (sets.is_initialized()) {
71 		sets.get().erase(pt::ptree::key_type(setName));
72 		sets.get().push_back(pt::ptree::value_type(setName,set));
73 	}
74 	else {
75 		pt::ptree child;
76 		child.push_back(pt::ptree::value_type(setName,set));
77 		root.push_back(pt::ptree::value_type(ParameterSet::parameterSetsKey,child));
78 	}
79 }
80 
81 /*!
82 	Returns true if the file was successfully read
83 */
readParameterSet(const std::string & filename)84 bool ParameterSet::readParameterSet(const std::string &filename)
85 {
86 	try {
87 		pt::read_json(filename, this->root);
88 		return true;
89 	}
90 	catch (const pt::json_parser_error &e) {
91 		LOG(message_group::Error,Location::NONE,"","Cannot open Parameter Set '%1$s': %2$s",filename,e.what());
92 	}
93 	return false;
94 }
95 
writeParameterSet(const std::string & filename)96 void ParameterSet::writeParameterSet(const std::string &filename)
97 {
98 	// Always force default version for now
99 	root.put<std::string>(fileFormatVersionKey, fileFormatVersionValue);
100 	try {
101 		pt::write_json(filename, this->root);
102 	}
103 	catch (const pt::json_parser_error &e) {
104 		LOG(message_group::Error,Location::NONE,"","Cannot write Parameter Set '%1$s': %2$s",filename,e.what());
105 	}
106 }
107 
applyParameterSet(FileModule * fileModule,const std::string & setName)108 void ParameterSet::applyParameterSet(FileModule *fileModule, const std::string &setName)
109 {
110 	if (fileModule == nullptr || this->root.empty()) return;
111 	try {
112 		ContextHandle<Context> ctx{Context::create<Context>()};
113 		boost::optional<pt::ptree &> set = getParameterSet(setName);
114 		for (auto &assignment : fileModule->scope.assignments) {
115 			for (auto &v : set.get()) {
116 				if (v.first == assignment->getName()) {
117 					const Value defaultValue = assignment->getExpr()->evaluate(ctx.ctx);
118 					if (defaultValue.type() == Value::Type::STRING) {
119 						assignment->setExpr(make_shared<Literal>(v.second.data()));
120 					}
121 					else if (defaultValue.type() == Value::Type::BOOL) {
122 						assignment->setExpr(make_shared<Literal>(Value(v.second.get_value<bool>())));
123 					} else {
124 						shared_ptr<Expression> params = CommentParser::parser(v.second.data().c_str());
125 						if (!params) continue;
126 						ContextHandle<Context> ctx{Context::create<Context>()};
127 						if (defaultValue.type() == params->evaluate(ctx.ctx).type()) {
128 							assignment->setExpr(params);
129 						}
130 					}
131 				}
132 			}
133 		}
134 	}
135 	catch (std::exception const& e) {
136 		LOG(message_group::Error,Location::NONE,"","Cannot apply Parameter Set '%1$s'",e.what());
137 	}
138 }
139 
140