Config::ChildrenMapType getAllElementChildren(const rapidjson::Document& doc, const std::vector<SvgElement>& all_elements_, const Config::ElementGroupType& element_groups_) { Config::ChildrenMapType all_element_children; all_element_children.set_empty_key(std::string{""}); // load elements const auto element_children_it = doc.FindMember("element_children"); assert(element_children_it != doc.MemberEnd()); assert(element_children_it->value.IsArray()); const auto raw_element_children_array = element_children_it->value.GetArray(); for (const auto& raw_element_children_obj : raw_element_children_array) { assert(raw_element_children_obj.IsObject()); //parent const auto raw_parent_element = raw_element_children_obj.FindMember("parent"); assert(raw_parent_element != raw_element_children_obj.MemberEnd()); assert(raw_parent_element->value.IsString()); const auto& parent_element_name = raw_parent_element->value.GetString(); //children std::vector<std::string> all_children; const auto raw_children_array_it = raw_element_children_obj.FindMember("children"); assert(raw_children_array_it != raw_element_children_obj.MemberEnd()); assert(raw_children_array_it->value.IsArray()); const auto raw_children_array = (raw_children_array_it->value.GetArray()); for (const auto& raw_child : raw_children_array) { assert(raw_child.IsString()); auto child = std::string{raw_child.GetString(), raw_child.GetStringLength()}; if (element_groups_.count(child) > 0) // the name is a group name { const auto& group_with_current_name = const_cast<Config::ElementGroupType&>(element_groups_)[child]; all_children.insert(std::end(all_children), std::begin(group_with_current_name), std::end(group_with_current_name));//we replace the group with it's elements } else // otherwise it's a simple element { all_children.emplace_back(std::move(child)); } std::sort(std::begin(all_children), std::end(all_children)); all_element_children[parent_element_name] = std::move(all_children); } } return all_element_children; }
void Parameters::json_simpars(rapidjson::Document& jpars) { try { rapidjson::Value::MemberIterator it; it = jpars.FindMember("simulation"); if (it != jpars.MemberEnd()) { if (! it->value.IsObject()) throw 201; rapidjson::Value& d = it->value; it = d.FindMember("maxTime"); if (! it->value.IsDouble()) throw 202; sim_pars.maxTime = it->value.GetDouble(); } } catch (int e) { cerr << "Exception while reading SimPars: " << e << endl; return; } }
template<typename T> T get(const std::string & key) const { const rapidjson::Value::ConstMemberIterator & member = find_member_from_key(document, key); if(member == document.MemberEnd()) { auto alternative = fallbacks.find(key); if(alternative != fallbacks.end()) { // Recursive call return get<T>(alternative->second); } else { auto msg = boost::format("Unable to find %1% in the configuration file") % key; throw Configuration_error(boost::str(msg)); } } else { return conf_getter<T>(member->value); } }
void Parameters::from_json(rapidjson::Document& jpars) { try { rapidjson::Value::MemberIterator m1; rapidjson::SizeType z; m1 = jpars.FindMember("model_type"); if (m1 != jpars.MemberEnd()) { if (! m1->value.IsString()) throw 101; model_type = m1->value.GetString(); } m1 = jpars.FindMember("nroot"); if (m1 != jpars.MemberEnd()) { if (! m1->value.IsInt()) throw 102; nroot = m1->value.GetInt(); } m1 = jpars.FindMember("shifts"); if (m1 != jpars.MemberEnd()) { if (! m1->value.IsArray()) throw 103; shifts.resize(m1->value.Size()); for (rapidjson::SizeType i = 0; i < m1->value.Size(); ++i) { shifts[i] = m1->value[i].GetDouble(); } } rapidjson::Value::MemberIterator _d = jpars.FindMember("pars"); if (_d == jpars.MemberEnd()) throw 1; rapidjson::Value& d = _d->value; pars.resize(d.Size()); for (rapidjson::SizeType i = 0; i < d.Size(); ++i) { rapidjson::Value& a = d[i]; m1 = a.FindMember("name"); if (m1 != a.MemberEnd()) { if (! m1->value.IsString()) throw 5; pars[i].name = m1->value.GetString(); name_map.insert(make_pair(pars[i].name,i)); } m1 = a.FindMember("limits"); if (m1 != a.MemberEnd()) { if (! m1->value.IsArray()) throw 2; if (m1->value.Size() != 2) throw 3; z = 0; pars[i].lo = m1->value[z].GetDouble(); z = 1; pars[i].hi = m1->value[z].GetDouble(); } m1 = a.FindMember("value"); if (m1 == a.MemberEnd()) m1 = a.FindMember("init"); if (m1 != a.MemberEnd()) { if (m1->value.IsDouble()) { pars[i].init.assign(1,m1->value.GetDouble()); } else if (m1->value.IsArray()) { pars[i].init.assign(m1->value.Size(),0.0); for (size_t j = 0; j < m1->value.Size(); ++j) { pars[i].init[j] = m1->value[j].GetDouble(); } } else { throw 2; } } m1 = a.FindMember("lock"); if (m1 != a.MemberEnd()) { if (! m1->value.IsDouble()) throw 4; pars[i].lock = 1; pars[i].lo = m1->value.GetDouble(); pars[i].hi = pars[i].lo; pars[i].init.assign(1,0.0); pars[i].init[0] = pars[i].hi; } m1 = a.FindMember("scale"); if (m1 != a.MemberEnd()) { if (! m1->value.IsString()) throw 5; pars[i].scale = m1->value.GetString()[0]; } } } catch (int e) { cerr << "Exception while reading pars: " << e << endl; return; } json_simpars(jpars); }