source: trunk/IPs/systemC/processor/Morpheo/Behavioural/Configuration/src/Instance_fromXMLLight.cpp

Last change on this file was 137, checked in by rosiere, 14 years ago

Various modif (add test, and vhdl)

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