Dev Essential
Loading...
Searching...
No Matches
datamodel_xml_configurationfromxml.h
Go to the documentation of this file.
1
20
21#ifndef MAPPING_CONFIGURATION_FROM_XML_FACTORY_H_INCLUDED
22#define MAPPING_CONFIGURATION_FROM_XML_FACTORY_H_INCLUDED
23
25
26namespace ddl {
27namespace mapping {
40template <typename DomNodeType>
53 static constexpr auto mapping_reading_version = "Mapping20::";
54
63 template <typename T>
64 static utility::Optional<T> getAttributeMandatory(const DomNodeType& dom_node,
65 const std::string& attribute_name)
66 {
67 return xml_base_type::template getAttribute<std::string>(
68 dom_node,
69 attribute_name,
70 {},
71 true,
72 dom_node.getName() + " tag has no mandatory '" + attribute_name + "' attribute",
74 }
75
83 template <typename T>
84 static utility::Optional<T> getAttribute(const DomNodeType& dom_node,
85 const std::string& attribute_name)
86 {
87 return xml_base_type::template getAttribute<std::string>(dom_node, attribute_name);
88 }
89
99 template <typename T>
100 static utility::Optional<T> getDataSubNodeMandatory(const DomNodeType& dom_node,
101 const std::string& tag_name,
102 const std::string& additional_info = {})
103 {
104 return xml_base_type::template getDataSubNode<std::string>(
105 dom_node,
106 tag_name,
107 {},
108 true,
109 dom_node.getName() + additional_info + " tag has no mandatory <" + tag_name + "/> tag",
111 }
112
120 template <typename T>
121 static utility::Optional<T> getDataSubNode(const DomNodeType& dom_node,
122 const std::string& tag_name)
123 {
124 return xml_base_type::template getDataSubNode<std::string>(dom_node, tag_name);
125 }
126
133 static datamodel::Header createHeader(const DomNodeType& dom_node, const Version& version)
134 {
135 datamodel::Header created_header(version);
136 auto file_mapping_version = getDataSubNode<std::string>(dom_node, "language_version");
137 if (file_mapping_version) {
138 Version version_to_set = VersionConversion::fromString(*file_mapping_version);
139 created_header.setMappingVersion(version_to_set);
140 }
141
142 auto author = getDataSubNode<std::string>(dom_node, "author");
143 if (author) {
144 created_header.setAuthor(author);
145 }
146 auto date_crea = getDataSubNode<std::string>(dom_node, "date_creation");
147 if (date_crea) {
148 created_header.setDateCreation(date_crea);
149 }
150 auto date_cha = getDataSubNode<std::string>(dom_node, "date_change");
151 if (date_cha) {
152 created_header.setDateChange(date_cha);
153 }
154 auto description = getDataSubNode<std::string>(dom_node, "description");
155 if (description) {
156 created_header.setDescription(description);
157 }
158 return created_header;
159 }
160
167 template <typename PropertiesType>
168 static void setDataOrSubNodesAsProperty(const DomNodeType& dom_node, PropertiesType& properties)
169 {
170 const auto name = getAttributeMandatory<std::string>(dom_node, "name");
171 xml_base_type::template setDataOrSubNodesAsProperty<PropertiesType>(
172 dom_node, *name, properties);
173 }
174
180 static datamodel::Source createSource(const DomNodeType& dom_node)
181 {
182 const auto name = getAttributeMandatory<std::string>(dom_node, "name");
183 const auto source_type = getAttribute<std::string>(dom_node, "source_type");
184
185 datamodel::Source source(*name, *source_type);
186 // we have have properties
187 std::list<DomNodeType> properties;
188 if (dom_node.findNodes("properties//property", properties)) {
189 for (const auto& current_property: properties) {
190 setDataOrSubNodesAsProperty(current_property, source.getProperties());
191 }
192 }
193 return source;
194 }
195
202 static datamodel::ParameterValue createParameterValue(const DomNodeType& dom_node)
203 {
204 const auto name = getAttributeMandatory<std::string>(dom_node, "name");
205 const auto type = getAttributeMandatory<std::string>(dom_node, "type");
206 if (*type == "source") {
207 const auto source_name = getDataSubNodeMandatory<std::string>(
208 dom_node, "source_name", " with type='source' ");
209 const auto value_name = getDataSubNode<std::string>(dom_node, "value_name");
210 return {name, datamodel::ParameterValue::SourceValue{*source_name, *value_name}};
211 }
212 else if (*type == "const") {
213 const auto value =
214 getDataSubNodeMandatory<std::string>(dom_node, "value", " with type='const' ");
215 return {name, datamodel::ParameterValue::ConstValue{*value}};
216 }
217 else if (*type == "internal") {
218 const auto internal_name = getDataSubNode<std::string>(dom_node, "internal_name");
219 const auto value_name = getDataSubNodeMandatory<std::string>(
220 dom_node, "value_name", " with type='internal'");
221 return {name, datamodel::ParameterValue::InternalValue{*internal_name, *value_name}};
222 }
223 else {
224 throw Error("createParameterValue", {*name, *type}, "unsupported type for assignment");
225 }
226 }
227
233 static datamodel::Assignment createAssignment(const DomNodeType& dom_node)
234 {
235 const auto to_name = getDataSubNodeMandatory<std::string>(dom_node, "to");
236 const auto assignment_type = getAttributeMandatory<std::string>(dom_node, "type");
237
238 if (*assignment_type == "source") {
239 const auto source_name =
240 getDataSubNodeMandatory<std::string>(dom_node, "source_name", " with type=source");
241 const auto value_name = getDataSubNode<std::string>(dom_node, "value_name");
243 *to_name, datamodel::Assignment::SourceValue{*source_name, *value_name});
244 }
245 else if (*assignment_type == "const") {
246 const auto value =
247 getDataSubNodeMandatory<std::string>(dom_node, "value", " with type=const");
249 }
250 else if (*assignment_type == "call") {
251 const auto function_name =
252 getDataSubNodeMandatory<std::string>(dom_node, "function", " with type=call");
253
254 datamodel::Assignment assignment(
255 *to_name, datamodel::Assignment::FunctionCall{*function_name});
256
257 std::list<DomNodeType> parameter_values;
258 dom_node.findNodes("parameter_value", parameter_values);
259 for (const auto& current_param_node: parameter_values) {
260 assignment.getParameterValues().emplace(createParameterValue(current_param_node));
261 }
262 return assignment;
263 }
264 throw Error("Mapping20::createAssignment",
265 {dom_node.getName()},
266 "assignment type='" + *assignment_type + "' is unsupported");
267 }
268
274 static datamodel::Trigger createPeriodTrigger(const DomNodeType& dom_node)
275 {
276 const auto time_value =
277 getDataSubNodeMandatory<std::string>(dom_node, "time", " with type=periodic");
278 const auto unit_value =
279 getDataSubNodeMandatory<std::string>(dom_node, "unit", " with type=periodic");
280 return datamodel::Trigger(datamodel::Trigger::Period{*time_value, *unit_value});
281 }
282
288 static datamodel::Trigger createDataTrigger(const DomNodeType& dom_node)
289 {
290 const auto source =
291 getDataSubNodeMandatory<std::string>(dom_node, "source_name", " with type=data");
292 const auto value_name =
293 getDataSubNodeMandatory<std::string>(dom_node, "value_name", " with type=data");
294 const auto operator_name =
295 getDataSubNodeMandatory<std::string>(dom_node, "operator", " with type=data");
296 const auto value =
297 getDataSubNodeMandatory<std::string>(dom_node, "value", " with type=data");
298 return datamodel::Trigger(
299 datamodel::Trigger::Data{*source, *value_name, *operator_name, *value});
300 }
301
307 static datamodel::Trigger createSourceTrigger(const DomNodeType& dom_node)
308 {
309 const auto source =
310 getDataSubNodeMandatory<std::string>(dom_node, "source_name", " with type=source");
312 }
313
320 static datamodel::Trigger createTrigger(const DomNodeType& dom_node, const Version& version)
321 {
322 const auto trigger_type = getAttributeMandatory<std::string>(dom_node, "type");
323 const auto parse_trigger_node = [&]() {
324 if (*trigger_type == "source") {
325 return createSourceTrigger(dom_node);
326 }
327 else if (*trigger_type == "data") {
328 return createDataTrigger(dom_node);
329 }
330 else if (*trigger_type == "periodic") {
331 return createPeriodTrigger(dom_node);
332 }
333 else {
334 throw Error("Mapping20::createTrigger",
335 {dom_node.getName()},
336 "trigger type='" + *trigger_type + "' is unsupported");
337 }
338 };
339 datamodel::Trigger created_trigger = parse_trigger_node();
340 if (version > Version(2, 0)) {
341 const auto time_strategy = getDataSubNode<std::string>(dom_node, "time_strategy");
342 if (time_strategy) {
343 created_trigger.setTimeStrategy(*time_strategy);
344 }
345 }
346 return created_trigger;
347 }
348
355 static datamodel::Trigger createTrigger(const DomNodeType& dom_node)
356 {
357 return createTrigger(dom_node, Version(2, 0));
358 }
359
367 static datamodel::Target createTarget(const DomNodeType& dom_node, const Version& version)
368 {
369 const auto name = getAttributeMandatory<std::string>(dom_node, "name");
370 const auto target_type =
371 xml_base_type::template getAttribute<std::string>(dom_node, "target_type");
372
373 datamodel::Target target(*name, *target_type);
374
375 // we have properties
376 std::list<DomNodeType> properties;
377 if (dom_node.findNodes("properties//property", properties)) {
378 for (const auto& current_property: properties) {
379 setDataOrSubNodesAsProperty(current_property, target.getProperties());
380 }
381 }
382
383 std::list<DomNodeType> assignments;
384 if (dom_node.findNodes("assignments//assignment", assignments)) {
385 for (const auto& current_assignment: assignments) {
386 target.getAssignments().emplace(createAssignment(current_assignment));
387 }
388 }
389 std::list<DomNodeType> triggers;
390 if (dom_node.findNodes("triggers//trigger", triggers)) {
391 for (const auto& current_trigger: triggers) {
392 target.getTriggers().emplace(createTrigger(current_trigger, version));
393 }
394 }
395 return target;
396 }
397
405 static datamodel::Target createTarget(const DomNodeType& dom_node)
406 {
407 return createTarget(dom_node, Version(2, 0));
408 }
409
415 static datamodel::Function createFunction(const DomNodeType& dom_node)
416 {
417 const auto name = getAttributeMandatory<std::string>(dom_node, "name");
418 const auto module_type = getAttributeMandatory<std::string>(dom_node, "function_type");
419
420 datamodel::Function module_instance(*name, *module_type);
421
422 // we have properties
423 std::list<DomNodeType> properties;
424 if (dom_node.findNodes("properties//property", properties)) {
425 for (const auto& current_property: properties) {
426 setDataOrSubNodesAsProperty(current_property, module_instance.getProperties());
427 }
428 }
429 return module_instance;
430 }
431
441 const DomNodeType& dom_node, const Version& mapping_version = Version::getLatestVersion())
442 {
443 DomNodeType header_element;
444 datamodel::Header header(mapping_version);
445 if (dom_node.findNode("header", header_element)) {
446 header = createHeader(header_element, mapping_version);
447 }
448 const auto current_version = header.getMappingVersion();
449 if (current_version < Version(2, 0)) {
450 return xml_mapping10_type::createConfiguration(dom_node, header);
451 }
452 else {
453 datamodel::MappingConfiguration new_config(current_version);
454 new_config.setHeader(header);
455
456 // read sources
457 std::list<DomNodeType> source_nodes;
458 if (dom_node.findNodes("//sources/source", source_nodes)) {
459 for (const auto& current_source_node: source_nodes) {
460 new_config.getSources().emplace(createSource(current_source_node));
461 }
462 }
463 // read targets
464 std::list<DomNodeType> target_nodes;
465 if (dom_node.findNodes("//targets/target", target_nodes)) {
466 for (const auto& current_target_node: target_nodes) {
467 new_config.getTargets().emplace(
468 createTarget(current_target_node, current_version));
469 }
470 }
471 // read functions
472 std::list<DomNodeType> function_nodes;
473 if (dom_node.findNodes("//functions/function", function_nodes)) {
474 for (const auto& current_func_node: function_nodes) {
475 new_config.getFunctions().emplace(createFunction(current_func_node));
476 }
477 }
478 return new_config;
479 }
480 }
481};
482
483} // namespace mapping
484} // namespace ddl
485
486#endif // MAPPING_CONFIGURATION_FROM_XML_FACTORY_H_INCLUDED
static Version fromString(const std::string &version, const Version &default_val=Version(0, 0))
Creates the Version from a string.
void emplace(DDL_TYPE_TO_ACCESS &&type_to_add)
emplace the given item
Definition access_list.h:432
void emplace(DDL_TYPE_TO_ACCESS &&type_to_add)
emplaces the given item
Definition access_map.h:368
Mapping Version.
Definition mapping_version.h:32
static const Version getLatestVersion()
Get the Latest Version (this is 2.0)
Datamodel assignment class used in Target.
Definition datamodel_configuration_items.h:912
ParameterValue::ConstValue ConstValue
ConstValue type.
Definition datamodel_configuration_items.h:941
ParameterValue::SourceValue SourceValue
SourceValue type.
Definition datamodel_configuration_items.h:937
Datamodel function class used in MappingConfiguration.
Definition datamodel_configuration_items.h:1314
const Properties & getProperties() const
Get the Properties.
Datamodel header class This class is observable.
Definition datamodel_configuration_items.h:41
void setAuthor(const std::string &author)
Set the author.
void setDescription(const std::string &description)
Set the description.
Version getMappingVersion() const
Get the mapping version.
void setDateChange(const std::string &date_change)
Set the date of last change.
void setMappingVersion(const Version &mapping_version)
Set the mapping version.
void setDateCreation(const std::string &date_creation)
Set the date of creation.
The mapping configuration datamodel.
Definition datamodel_configuration.h:38
const Functions & getFunctions() const
Get the Functions.
const Targets & getTargets() const
Get the Targets.
void setHeader(const Header &header)
Set the Header object.
const Sources & getSources() const
Get the Sources.
Datamodel parameter value class used within Assignment.
Definition datamodel_configuration_items.h:654
Datamodel source class used in MappingConfiguration.
Definition datamodel_configuration_items.h:262
const Properties & getProperties() const
Get the Properties.
Datamodel target class within MappingConfiguration.
Definition datamodel_configuration_items.h:1155
const Assignments & getAssignments() const
Get the Assignments.
const Triggers & getTriggers() const
Get the Triggers.
const Properties & getProperties() const
Get the Properties.
Datamodel trigger class use within Target.
Definition datamodel_configuration_items.h:376
void setTimeStrategy(const std::string &time_strategy)
void emplace(DDL_TYPE_TO_ACCESS &&type_to_add)
emplace the given item
Definition access_vector.h:276
definition of the mapping namespace
Definition ddl.h:50
ddl::utility::Error Error
mapping error definition
Definition mapping_configuration_types.h:35
definition of the ddl namespace
Definition codec.h:21
An optional template as long as the std::optional is not available here.
Definition access_optional.h:31
Factory to create a MappingConfiguration out of a XML based description. This factory uses a template...
Definition datamodel_xml_configurationfromxml_10.h:44
static datamodel::MappingConfiguration createConfiguration(const DomNodeType &dom_node, const datamodel::Header &header)
Creates a MappingConfiguration and read all sub node.
Definition datamodel_xml_configurationfromxml_10.h:450
Factory helper to create a MappingConfiguration out of a XML based description. This factory uses a t...
Definition datamodel_xml_configurationfromxml_base.h:48
Factory to create a MappingConfiguration out of a XML based description. This factory uses a template...
Definition datamodel_xml_configurationfromxml.h:41
static datamodel::Trigger createTrigger(const DomNodeType &dom_node)
Create a Trigger object.
Definition datamodel_xml_configurationfromxml.h:355
static datamodel::Trigger createSourceTrigger(const DomNodeType &dom_node)
Create a Trigger object as Trigger(Source)
Definition datamodel_xml_configurationfromxml.h:307
static datamodel::Target createTarget(const DomNodeType &dom_node, const Version &version)
Create a Target object.
Definition datamodel_xml_configurationfromxml.h:367
static datamodel::MappingConfiguration createConfiguration(const DomNodeType &dom_node, const Version &mapping_version=Version::getLatestVersion())
Creates a MappingConfiguration and read all sub node.
Definition datamodel_xml_configurationfromxml.h:440
static datamodel::Function createFunction(const DomNodeType &dom_node)
Create a Function.
Definition datamodel_xml_configurationfromxml.h:415
static datamodel::Trigger createPeriodTrigger(const DomNodeType &dom_node)
Create a Trigger object as Trigger(Period)
Definition datamodel_xml_configurationfromxml.h:274
static datamodel::ParameterValue createParameterValue(const DomNodeType &dom_node)
Create a ParameterValue object.
Definition datamodel_xml_configurationfromxml.h:202
static datamodel::Header createHeader(const DomNodeType &dom_node, const Version &version)
Create a Header object.
Definition datamodel_xml_configurationfromxml.h:133
static utility::Optional< T > getAttributeMandatory(const DomNodeType &dom_node, const std::string &attribute_name)
Get the mandatory attribute helper function.
Definition datamodel_xml_configurationfromxml.h:64
static constexpr auto mapping_reading_version
Error message definition string.
Definition datamodel_xml_configurationfromxml.h:53
static datamodel::Target createTarget(const DomNodeType &dom_node)
Create a Target object.
Definition datamodel_xml_configurationfromxml.h:405
MappingConfigurationFromXMLFactoryBase< DomNodeType > xml_base_type
base factory type for xml reading
Definition datamodel_xml_configurationfromxml.h:45
static utility::Optional< T > getDataSubNode(const DomNodeType &dom_node, const std::string &tag_name)
Get the mandatory sub node data helper function.
Definition datamodel_xml_configurationfromxml.h:121
static datamodel::Assignment createAssignment(const DomNodeType &dom_node)
Create a Assignment object.
Definition datamodel_xml_configurationfromxml.h:233
static utility::Optional< T > getDataSubNodeMandatory(const DomNodeType &dom_node, const std::string &tag_name, const std::string &additional_info={})
Get the mandatory sub node data helper function.
Definition datamodel_xml_configurationfromxml.h:100
static datamodel::Trigger createTrigger(const DomNodeType &dom_node, const Version &version)
Create a Trigger object.
Definition datamodel_xml_configurationfromxml.h:320
static void setDataOrSubNodesAsProperty(const DomNodeType &dom_node, PropertiesType &properties)
Set the Data Or Sub Nodes As Property.
Definition datamodel_xml_configurationfromxml.h:168
static datamodel::Source createSource(const DomNodeType &dom_node)
Create a Source object.
Definition datamodel_xml_configurationfromxml.h:180
static datamodel::Trigger createDataTrigger(const DomNodeType &dom_node)
Create a Trigger object as Trigger(Data)
Definition datamodel_xml_configurationfromxml.h:288
static utility::Optional< T > getAttribute(const DomNodeType &dom_node, const std::string &attribute_name)
Get the attribute.
Definition datamodel_xml_configurationfromxml.h:84
MappingConfigurationFromXMLFactory10< DomNodeType > xml_mapping10_type
mapping10 xml reading factory type for compatibility of mapping 1.0 definition
Definition datamodel_xml_configurationfromxml.h:49
FunctionCall type.
Definition datamodel_configuration_items.h:945
ConstValue type of the parameter.
Definition datamodel_configuration_items.h:694
InternalValue type of the parameter.
Definition datamodel_configuration_items.h:707
SourceValue type of the parameter.
Definition datamodel_configuration_items.h:680
Data type of the trigger.
Definition datamodel_configuration_items.h:414
Period type of the trigger.
Definition datamodel_configuration_items.h:437
Source type of the trigger.
Definition datamodel_configuration_items.h:403