[88] | 1 | /* |
---|
| 2 | * $Id: Instance_fromXMLLight.cpp 88 2008-12-10 18:31:39Z rosiere $ |
---|
| 3 | * |
---|
| 4 | * [ Description ] |
---|
| 5 | * |
---|
| 6 | */ |
---|
| 7 | |
---|
| 8 | #include "Behavioural/Configuration/include/Instance.h" |
---|
| 9 | #include "Common/include/FromString.h" |
---|
| 10 | #include <fstream> |
---|
| 11 | |
---|
| 12 | namespace morpheo { |
---|
| 13 | namespace behavioural { |
---|
| 14 | namespace configuration { |
---|
| 15 | |
---|
| 16 | using namespace XMLUtils; |
---|
| 17 | |
---|
| 18 | #undef FUNCTION |
---|
| 19 | #define FUNCTION "Instance::fromXMLLight" |
---|
| 20 | void Instance::fromXMLLight (XML_t * xml, |
---|
| 21 | attribute_t id, |
---|
| 22 | list_parameters_t * list_parameters, |
---|
| 23 | list_links_t * list_links, |
---|
| 24 | list_components_t * list_components) |
---|
| 25 | { |
---|
| 26 | |
---|
| 27 | XMLLightVector<XML_t> vect = xml->getNodes(); |
---|
| 28 | |
---|
| 29 | for (uint32_t i=0; i<vect.size(); ++i) |
---|
| 30 | { |
---|
| 31 | XML_t * child = vect[i]; |
---|
| 32 | |
---|
| 33 | std::string child_name = child->getName(); |
---|
| 34 | |
---|
| 35 | //-------------------------------------------- |
---|
| 36 | // Child : Parameter |
---|
| 37 | //-------------------------------------------- |
---|
| 38 | if (child_name == "parameter") |
---|
| 39 | { |
---|
| 40 | // Is a parameter |
---|
| 41 | testSingleton (child,true); |
---|
| 42 | |
---|
| 43 | attributes_t attributes = child->getAttributes(); |
---|
| 44 | attribute_t value_name = getAttribute(child,attributes,"name" ); |
---|
| 45 | attribute_t value = getAttribute(child,attributes,"value"); |
---|
| 46 | |
---|
| 47 | testAttributesEmpty(child,attributes); |
---|
| 48 | |
---|
| 49 | // Test the parameter : |
---|
| 50 | // * name is correct |
---|
| 51 | // * value is correct |
---|
| 52 | test (value_name, value); |
---|
| 53 | |
---|
| 54 | // Insert in array |
---|
| 55 | Parameter_affectation * param = new Parameter_affectation(value_name, // type |
---|
| 56 | value); |
---|
| 57 | |
---|
| 58 | // insert parameter |
---|
| 59 | (*list_parameters)[param->_name] = param; |
---|
| 60 | } |
---|
| 61 | //-------------------------------------------- |
---|
| 62 | // Child : Link |
---|
| 63 | //-------------------------------------------- |
---|
| 64 | else |
---|
| 65 | if (child_name == "link") |
---|
| 66 | { |
---|
| 67 | // Is a parameter |
---|
| 68 | testSingleton (child,true); |
---|
| 69 | |
---|
| 70 | attributes_t attributes = child->getAttributes(); |
---|
| 71 | attribute_t value_name = getAttribute(child,attributes,"name"); |
---|
| 72 | |
---|
| 73 | // Test, must have src or dest (or twice) |
---|
| 74 | bool have_src = child->containsAttribute("src" ); |
---|
| 75 | bool have_dest = child->containsAttribute("dest"); |
---|
| 76 | if (not have_src and |
---|
| 77 | not have_dest) |
---|
| 78 | throw ERRORMORPHEO(FUNCTION,toString(_("Syntax error, node \"link\" must have one or twince of this attributes : src, dest\n"))); |
---|
| 79 | |
---|
| 80 | attribute_t value_src = (have_src )?getAttribute(child,attributes,"src" ):id; |
---|
| 81 | attribute_t value_dest = (have_dest)?getAttribute(child,attributes,"dest"):id; |
---|
| 82 | |
---|
| 83 | testAttributesEmpty(child,attributes); |
---|
| 84 | |
---|
| 85 | // Test the link : |
---|
| 86 | // * name is correct |
---|
| 87 | // * if not have_src , test if src is correct |
---|
| 88 | // * if not have_dest, test if dest is correct |
---|
| 89 | test (value_name, |
---|
| 90 | xml->getName(), |
---|
| 91 | not have_src, |
---|
| 92 | not have_dest); |
---|
| 93 | |
---|
| 94 | // Create a new link |
---|
| 95 | { |
---|
| 96 | Link_affectation * link = new Link_affectation(value_name, // type |
---|
| 97 | value_src, |
---|
| 98 | value_dest); |
---|
| 99 | |
---|
| 100 | // Test if this id is previously used |
---|
| 101 | if (((*list_links)[value_name]).find(value_src) != ((*list_links)[value_name]).end()) |
---|
| 102 | throw ERRORMORPHEO(FUNCTION,toString(_("A Link \"%s\" with the source \"%s\" is previously declared.\n"),value_name.c_str(),value_src.c_str())); |
---|
| 103 | |
---|
| 104 | ((*list_links)[value_name])[value_src] = link; |
---|
| 105 | } |
---|
| 106 | } |
---|
| 107 | //-------------------------------------------- |
---|
| 108 | // Child : Timing |
---|
| 109 | //-------------------------------------------- |
---|
| 110 | else |
---|
| 111 | if (child_name == "timing") |
---|
| 112 | { |
---|
| 113 | // Notation : |
---|
| 114 | // <timing type="5" operation="8" latence="2" delay="1" /> |
---|
| 115 | // |
---|
| 116 | // is equivalent at : |
---|
| 117 | // <type id="5" > |
---|
| 118 | // <operation id="8" > |
---|
| 119 | // <parameter name="delay" value="1" /> |
---|
| 120 | // <parameter name="latence" value="2" /> |
---|
| 121 | // </operation> |
---|
| 122 | // </type> |
---|
| 123 | |
---|
| 124 | // Is a parameter |
---|
| 125 | testSingleton (child,true); |
---|
| 126 | |
---|
| 127 | attributes_t attributes = child->getAttributes(); |
---|
| 128 | attribute_t value_type = getAttribute(child,attributes,"type"); |
---|
| 129 | bool have_operation = (child->containsAttribute("operation")); |
---|
| 130 | attribute_t value_operation ; |
---|
| 131 | attribute_t value_latence = (child->containsAttribute("latence" ))?getAttribute(child,attributes,"latence" ):"1"; |
---|
| 132 | attribute_t value_delay = (child->containsAttribute("delay" ))?getAttribute(child,attributes,"delay" ):"1"; |
---|
| 133 | |
---|
| 134 | { |
---|
| 135 | // Find component "type", if don't exist : create |
---|
| 136 | if (((*list_components)["type"]).find(value_type) == ((*list_components)["type"]).end()) |
---|
| 137 | { |
---|
| 138 | // Create instance |
---|
| 139 | Instance_component * param = new Instance_component("type", // type |
---|
| 140 | value_type); |
---|
| 141 | ((*list_components)["type"])[value_type] = param; |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | list_components_t * list_type = ((*list_components)["type"])[value_type]->_list_components; |
---|
| 145 | |
---|
| 146 | // Find component "operation", if exist : error, else create |
---|
| 147 | |
---|
| 148 | bool stop = false; |
---|
| 149 | Toperation_t operation = 0; |
---|
| 150 | attribute_t value_operation = (have_operation)?getAttribute(child,attributes,"operation"):toString(operation); |
---|
| 151 | |
---|
| 152 | while (not stop) |
---|
| 153 | { |
---|
| 154 | if (((*list_type)["operation"]).find(value_operation) != ((*list_type)["operation"]).end()) |
---|
| 155 | throw ERRORMORPHEO(FUNCTION,toString(_("A Component \"%s\" with id \"%s\" is previously declared.\n"),"operation",value_operation.c_str())); |
---|
| 156 | else |
---|
| 157 | { |
---|
| 158 | // Create instance |
---|
| 159 | Instance_component * param = new Instance_component("operation", // type |
---|
| 160 | value_operation); |
---|
| 161 | ((*list_type)["operation"])[value_operation] = param; |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | list_parameters_t * list_operation = ((*list_type)["operation"])[value_operation]->_list_parameters; |
---|
| 165 | |
---|
| 166 | // insert parameter "latence" and "delay" |
---|
| 167 | Parameter_affectation * param_latence = new Parameter_affectation("latence", |
---|
| 168 | value_latence); |
---|
| 169 | Parameter_affectation * param_delay = new Parameter_affectation("delay", |
---|
| 170 | value_delay); |
---|
| 171 | |
---|
| 172 | (*list_operation)["latence"] = param_latence; |
---|
| 173 | (*list_operation)["delay" ] = param_delay ; |
---|
| 174 | |
---|
| 175 | operation ++; |
---|
| 176 | value_operation = toString(operation); |
---|
| 177 | stop = have_operation or (operation == MAX_OPERATION); |
---|
| 178 | }; |
---|
| 179 | |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | testAttributesEmpty(child,attributes); |
---|
| 183 | } |
---|
| 184 | //-------------------------------------------- |
---|
| 185 | // Child : Other (node) |
---|
| 186 | //-------------------------------------------- |
---|
| 187 | else |
---|
| 188 | { |
---|
| 189 | testSingleton (child,false); |
---|
| 190 | |
---|
| 191 | attributes_t attributes = child->getAttributes(); |
---|
| 192 | attribute_t value_id = getAttribute(child,attributes,"id"); |
---|
| 193 | attribute_t value_type = child->getName(); |
---|
| 194 | |
---|
| 195 | testAttributesEmpty(child,attributes); |
---|
| 196 | |
---|
| 197 | // Create a new component |
---|
| 198 | { |
---|
| 199 | Instance_component * param = new Instance_component(value_type, // type |
---|
| 200 | value_id); |
---|
| 201 | |
---|
| 202 | // log_printf(TRACE,Configuration,FUNCTION,"%s.%s",value_type.c_str(),value_id.c_str()); |
---|
| 203 | // log_printf(TRACE,Configuration,FUNCTION," * %d",((*list_components)[value_type]).size()); |
---|
| 204 | |
---|
| 205 | // Test if this id is previously used |
---|
| 206 | if (((*list_components)[value_type]).find(value_id) != ((*list_components)[value_type]).end()) |
---|
| 207 | throw ERRORMORPHEO(FUNCTION,toString(_("A Component \"%s\" with id \"%s\" is previously declared.\n"),value_type.c_str(),value_id.c_str())); |
---|
| 208 | |
---|
| 209 | ((*list_components)[value_type])[value_id] = param; |
---|
| 210 | } |
---|
| 211 | |
---|
| 212 | // Test if counter is an parameters |
---|
| 213 | { |
---|
| 214 | std::string name_counter = "nb_"+value_type; |
---|
| 215 | // std::cout << name_counter << std::endl; |
---|
| 216 | |
---|
| 217 | if (list_parameters->find(name_counter) == list_parameters->end()) |
---|
| 218 | { |
---|
| 219 | // Scearch all occurante of this type |
---|
| 220 | uint32_t nb_counter = xml->getNodes(value_type).size(); |
---|
| 221 | // std::cout << " * " << nb_counter << std::endl; |
---|
| 222 | |
---|
| 223 | // Insert in array |
---|
| 224 | Parameter_affectation * param = new Parameter_affectation(name_counter, // type |
---|
| 225 | toString(nb_counter)); |
---|
| 226 | |
---|
| 227 | // insert parameter |
---|
| 228 | (*list_parameters)[name_counter] = param; |
---|
| 229 | } |
---|
| 230 | } |
---|
| 231 | |
---|
| 232 | // Recursive function |
---|
| 233 | fromXMLLight (child, |
---|
| 234 | ((id=="")?value_id:(id+"."+value_id)), // construction of identificator |
---|
| 235 | ((*list_components)[value_type])[value_id]->_list_parameters, |
---|
| 236 | // ((*list_components)[value_type])[value_id]->_list_links, |
---|
| 237 | list_links, |
---|
| 238 | ((*list_components)[value_type])[value_id]->_list_components); |
---|
| 239 | } |
---|
| 240 | } |
---|
| 241 | }; |
---|
| 242 | |
---|
| 243 | }; // end namespace configuration |
---|
| 244 | }; // end namespace behavioural |
---|
| 245 | }; // end namespace morpheo |
---|