]> git.llucax.com Git - z.facultad/75.42/plaqui.git/blob - Model/src/simulator.cpp
* Se arregla un bug en el constructor al verificar conexiones
[z.facultad/75.42/plaqui.git] / Model / src / simulator.cpp
1
2 #include "simulator.h"
3
4 using namespace PlaQui::Model;
5
6 Simulator::Simulator(const std::string &filename)
7 {
8         frame = 0;
9         /* Parseo de ejemplo de un XML desde archivo */
10         xmlDocPtr document;
11         document = xmlParseFile(filename.c_str());
12         if (document == NULL) {
13                 is_load_ok = false;
14                 std::cout << "Error cargando XML" << std::endl;
15                 return;
16         }
17
18         is_load_ok = true;
19
20         /* bien, el archivo se parseo bien! */
21         xmlNodePtr nodo, items;
22         nodo = document->children;
23
24         if (strcmp((char *)nodo->name, "planta") == 0) {
25                 items = nodo->children;
26                 while (items != NULL) {
27                         if (items->type == XML_ELEMENT_NODE) {
28                                 if (xmlStrcmp(items->name, BAD_CAST"bomba")==0) {
29                                         loadBomba(items);
30                                 } else if ((xmlStrcmp(items->name, BAD_CAST"tubo")==0)||(xmlStrcmp(items->name, BAD_CAST"codo")==0)) {
31                                         loadConduct(items);
32                                 } else if (xmlStrcmp(items->name, BAD_CAST"exclusa")==0) {
33                                         loadExclusa(items);
34                                 } else if (xmlStrcmp(items->name, BAD_CAST"tanque")==0) {
35                                         loadTank(items);
36                                 } else if (xmlStrcmp(items->name, BAD_CAST"empalme")==0) {
37                                         loadUnion(items);
38                                 } else if (xmlStrcmp(items->name, BAD_CAST"drenaje")==0) {
39                                         loadDrain(items);
40                                 }
41                         }
42                         items = items->next;
43                 }
44                 // Bien, la planta esta cargada, conectemos todo!!
45                 do_connections(nodo->children);
46         }
47         xmlFreeDoc(document);
48 }
49
50 Simulator::~Simulator()
51 {
52         std::list<PlantItem *>::iterator i = items.begin();
53         PlantItem *o;
54
55         while (i != items.end()) {
56                 o = (*i);
57                 items.remove(o);
58                 delete o;
59                 i = items.begin();
60         }
61 }
62
63 void Simulator::add_pump(const std::string &name, float max_flow, RGB color)
64 {
65         Pump *p = new Pump(name);
66         p->set_max_flow(max_flow);
67         p->set_color(color);
68         pump_lst.push_back(p);
69         items.push_back(p);
70 }
71
72 void Simulator::add_union(const std::string &name, float max_flow)
73 {
74         Union *u = new Union(name);
75         u->set_max_flow(max_flow);
76         union_lst.push_back(u);
77         items.push_back(u);
78 }
79
80 void Simulator::add_splitter(const std::string &name, float max_flow)
81 {
82         Splitter *p = new Splitter(name);
83         p->set_max_flow(max_flow);
84         split_lst.push_back(p);
85         items.push_back(p);
86 }
87
88 void Simulator::add_conduct(const std::string &name, float flujo)
89 {
90         Conduct *p = new Conduct(name);
91         p->set_max_flow(flujo);
92         conduct_lst.push_back(p);
93         items.push_back(p);
94 }
95
96 void Simulator::add_exclusa(const std::string &name, bool open)
97 {
98         Exclusa *p = new Exclusa(name);
99         if (!open)
100                 p->close();
101         exclusa_lst.push_back(p);
102         items.push_back(p);
103 }
104
105 void Simulator::add_tank(const std::string &name, float capacity, float initial, RGB color)
106 {
107         Tank *p = new Tank(name);
108         p->set_capacity(capacity);
109         p->set_max_flow(initial);
110         p->set_litros(initial);
111         p->set_color(color);
112         tank_lst.push_back(p);
113         items.push_back(p);
114 }
115
116 void Simulator::add_drainage(const std::string &name)
117 {
118         Drainage *p = new Drainage(name);
119         drainage_lst.push_back(p);
120         items.push_back(p);
121 }
122
123 bool Simulator::connect(const std::string &name1, const std::string &name2, int flag)
124 {
125         IConector *o1, *o2;
126         o1 = find(name1);
127         o2 = find(name2);
128
129         if ((o1 == NULL) || (o2 == NULL)) {
130                 // NO SE PUDO CONECTAR!, FALTAN ITEMS!!
131                 return false;
132         }
133
134         bool b;
135         if (flag == IConector::OUT) {
136                 b = o1->connect(o2, IConector::OUT);
137                 b = b && o2->connect(o1, IConector::IN);
138         } else {
139                 b = o1->connect(o2, IConector::IN);
140                 b = b && o2->connect(o1, IConector::OUT);
141         }
142         
143         return b;
144 }
145
146 void Simulator::simulate()
147 {
148         // Actualizo
149         std::list<Pump *>::iterator i1;
150         for(i1=pump_lst.begin(); i1!=pump_lst.end(); i1++)
151                 (*i1)->update();
152
153         std::list<PlantItem *>::iterator i2;
154         for(i2=items.begin(); i2!=items.end(); i2++) 
155                 (*i2)->simulate();
156
157         frame++;
158 }
159
160 IConector *Simulator::find(const std::string &name)
161 {
162         // Busco el item, aca no me importa de que tipo es!
163         std::list<PlantItem *>::iterator i;
164         for(i=items.begin(); i!=items.end(); i++)
165                 if ((*i)->get_name() == name)
166                         return *i;
167         return NULL;
168 }
169
170 bool Simulator::set_open(const std::string &name, bool open)
171 {
172         // Busco el elemento, usando RTTI :-(
173         IConector *tmp = find(name);
174         Pump *p;
175         Exclusa *e;
176         if ((p = dynamic_cast<Pump*>(tmp))) {
177                 if (open) {
178                         p->activate();
179                 } else {
180                         p->deactivate();
181                 }
182         } else if ((e = dynamic_cast<Exclusa*>(tmp))) {
183                 if (open) {
184                         e->open();
185                 } else {
186                         e->close();
187                 }
188         } else {
189                 return false;
190         }
191 }
192
193 void Simulator::loadBomba(xmlNodePtr nodo)
194 {
195         std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
196         int orientacion=0, x, y;
197         RGB color;
198         float flujo;
199
200         nodo = nodo->children;
201         while (nodo != NULL) {
202                 if (nodo->type == XML_ELEMENT_NODE) {
203                         if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
204                                 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
205                         } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
206                                 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
207                         } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
208                                 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
209                         } else if (xmlStrcmp(nodo->name, BAD_CAST"entrega") == 0) {
210                                 flujo = atof( (char *)XML_GET_CONTENT(nodo->children) );
211                         } else if (xmlStrcmp(nodo->name, BAD_CAST"color") == 0) {
212                                 color = loadRGB(nodo->children);
213                         }
214                 }
215                 nodo = nodo->next;
216         }
217
218         add_pump(name, flujo, color);
219 }
220
221 void Simulator::loadConduct(xmlNodePtr nodo)
222 {
223         std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
224         int orientacion=0, x, y;
225         float flujo;
226
227         nodo = nodo->children;
228         while (nodo != NULL) {
229                 if (nodo->type == XML_ELEMENT_NODE) {
230                         if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
231                                 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
232                         } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
233                                 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
234                         } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
235                                 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
236                         } else if (xmlStrcmp(nodo->name, BAD_CAST"caudal") == 0) {
237                                 flujo = atof( (char *)XML_GET_CONTENT(nodo->children) );
238                         }
239                         
240
241                 }
242                 nodo = nodo->next;
243         }
244
245         // listo, ya recolecte todos los datos, ahora creo el objeto!
246         add_conduct(name, flujo);
247 }
248
249 void Simulator::loadExclusa(xmlNodePtr nodo)
250 {
251         std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
252         int orientacion=0, x, y;
253         std::string open;
254
255         nodo = nodo->children;
256         while (nodo != NULL) {
257                 if (nodo->type == XML_ELEMENT_NODE) {
258                         if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
259                                 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
260                         } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
261                                 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
262                         } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
263                                 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
264                         } else if (xmlStrcmp(nodo->name, BAD_CAST"estado") == 0) {
265                                 open = (char *)XML_GET_CONTENT(nodo->children);
266                         }
267                 }
268                 nodo = nodo->next;
269         }
270
271         // listo, ya recolecte todos los datos, ahora creo el objeto!
272         add_exclusa(name, open == "1");
273 }
274
275 void Simulator::loadTank(xmlNodePtr nodo)
276 {
277         std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
278         int orientacion=0, x, y;
279         float capacidad, inicial;
280         RGB color;
281
282         nodo = nodo->children;
283         while (nodo != NULL) {
284                 if (nodo->type == XML_ELEMENT_NODE) {
285                         if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
286                                 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
287                         } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
288                                 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
289                         } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
290                                 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
291                         } else if (xmlStrcmp(nodo->name, BAD_CAST"capacidad") == 0) {
292                                 capacidad = atoi( (char *)XML_GET_CONTENT(nodo->children) );
293                         } else if (xmlStrcmp(nodo->name, BAD_CAST"inicial") == 0) {
294                                 inicial = atof( (char *)XML_GET_CONTENT(nodo->children) );
295                         } else if (xmlStrcmp(nodo->name, BAD_CAST"") == 0) {
296                                 color = loadRGB(nodo->children);
297                         }
298                 }
299                 nodo = nodo->next;
300         }
301
302         // listo, ya recolecte todos los datos, ahora creo el objeto!
303         add_tank(name, capacidad, inicial, color);
304 }
305
306 void Simulator::loadUnion(xmlNodePtr nodo)
307 {
308         std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
309         int orientacion=0, x, y;
310         float flow;
311         std::string type;
312
313         nodo = nodo->children;
314         while (nodo != NULL) {
315                 if (nodo->type == XML_ELEMENT_NODE) {
316                         if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
317                                 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
318                         } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
319                                 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
320                         } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
321                                 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
322                         } else if (xmlStrcmp(nodo->name, BAD_CAST"caudal") == 0) {
323                                 flow = atof( (char *)XML_GET_CONTENT(nodo->children) );
324                         } else if (xmlStrcmp(nodo->name, BAD_CAST"tipo") == 0) {
325                                 type = (char *)XML_GET_CONTENT(nodo->children);
326                         }
327                 }
328                 nodo = nodo->next;
329         }
330
331         // listo, ya recolecte todos los datos, ahora creo el objeto!
332         if (type == "union")
333                 add_union(name, flow);
334         else
335                 add_splitter(name, flow);
336 }
337
338 void Simulator::loadDrain(xmlNodePtr nodo)
339 {
340         std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
341
342         add_drainage(name);
343 }
344
345 void Simulator::do_connections(xmlNodePtr nodo)
346 {
347         // Intengo conectar los elementos :)
348         IConector *current_item, *to_connect;
349         xmlNodePtr props; // propiedades del item actual
350         xmlNodePtr conector1, conector2, conector3;
351
352         while (nodo != NULL) {
353                 if (nodo->type != XML_ELEMENT_NODE) {
354                         nodo = nodo->next;
355                         continue;
356                 }
357                 // obtengo el items actual por su nombre
358                 std::string s = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
359                 current_item = find((char *)xmlGetProp(nodo, BAD_CAST"nombre"));
360                 props = nodo->children;
361                 conector3 = conector2 = conector1 = NULL;
362                 while (props != NULL) {
363                         if (nodo->type == XML_ELEMENT_NODE) {
364                                 if (xmlStrcmp(props->name, BAD_CAST"conector") == 0) {
365                                         xmlNodePtr temp = props->children;
366                                         while ((temp != NULL) && (conector1 == NULL))
367                                                 if (temp->type == XML_ELEMENT_NODE) {
368                                                         conector1 = temp;
369                                                         temp = temp->next;
370                                                         break;
371                                                 } else {
372                                                         temp = temp->next;
373                                                 }
374                                         while ((temp != NULL) && (conector2 == NULL))
375                                                 if (temp->type == XML_ELEMENT_NODE) {
376                                                         conector2 = temp;
377                                                         temp = temp->next;
378                                                         break;
379                                                 } else {
380                                                         temp = temp->next;
381                                                 }
382                                         while ((temp != NULL) && (conector3 == NULL))
383                                                 if (temp->type == XML_ELEMENT_NODE) {
384                                                         conector3 = temp;
385                                                         temp = temp->next;
386                                                         break;
387                                                 } else {
388                                                         temp = temp->next;
389                                                 }
390                                 }
391                         }
392                         props = props->next;
393                 }
394                 // Bien, conector1 y 2 deberian estar apuntando a <entrada> y/o <salida>
395                 if (conector1 != NULL) {
396                         // si, aca hay un conector!, veamos cual es
397                         if (xmlStrcmp(conector1->name, BAD_CAST"entrada") == 0) {
398                                 // bien, es a la entrada!, obtengo el item al cual lo tengo que conectar
399                                 to_connect = find((char *)XML_GET_CONTENT(conector1->children));
400                                 // y lo conecto
401                                 if (!current_item->connect(to_connect, IConector::IN)) {
402                                         std::cout << s << " Error al conectar1 ENTRADA = " << (char *)XML_GET_CONTENT(conector1->children) << std::endl;
403                                 } else {
404                                         std::cout << s << " ENTRADA1 = " << (char *)XML_GET_CONTENT(conector1->children) << std::endl;
405                                 }
406                         } else if (xmlStrcmp(conector1->name, BAD_CAST"salida") == 0) {
407                                 // Era a salida, es casi lo mismo que arriba 
408                                 to_connect = find((char *)XML_GET_CONTENT(conector1->children));
409                                 if (!current_item->connect(to_connect, IConector::OUT)) {
410                                         std::cout << s << " Error al conectar2 SALIDA" << std::endl;
411                                 } else {
412                                         std::cout << s << " SALIDA1 = " << (char *)XML_GET_CONTENT(conector1->children) << std::endl;
413                                 }
414                         }
415                 }
416                 if (conector2 != NULL) {
417                         // si, aca hay un conector!, veamos cual es
418                         if (xmlStrcmp(conector2->name, BAD_CAST"entrada") == 0) {
419                                 // bien, es a la entrada!, obtengo el item al cual lo tengo que conectar
420                                 to_connect = find((char *)XML_GET_CONTENT(conector2->children));
421                                 // y lo conecto
422                                 if (!current_item->connect(to_connect, IConector::IN)) {
423                                         std::cout << s << " Error al conectar2 ENTRADA = " << (char *)XML_GET_CONTENT(conector2->children) << std::endl;
424                                 } else {
425                                         std::cout << s << " ENTRADA2 = " << (char *)XML_GET_CONTENT(conector2->children) << std::endl;
426                                 }
427                         } else if (xmlStrcmp(conector2->name, BAD_CAST"salida") == 0) {
428                                 // Era a salida, es casi lo mismo que arriba 
429                                 to_connect = find((char *)XML_GET_CONTENT(conector2->children));
430                                 if (!current_item->connect(to_connect, IConector::OUT)) {
431                                         std::cout << s << " Error al conectar " << std::endl;
432                                 }
433                         }
434                 }
435                 if (conector3 != NULL) {
436                         // si, aca hay un conector!, veamos cual es
437                         if (xmlStrcmp(conector3->name, BAD_CAST"entrada") == 0) {
438                                 // bien, es a la entrada!, obtengo el item al cual lo tengo que conectar
439                                 to_connect = find((char *)XML_GET_CONTENT(conector3->children));
440                                 // y lo conecto
441                                 if (!current_item->connect(to_connect, IConector::IN)) {
442                                         std::cout << s << " Error al conectar " << std::endl;
443                                 }
444                         } else if (xmlStrcmp(conector3->name, BAD_CAST"salida") == 0) {
445                                 // Era a salida, es casi lo mismo que arriba 
446                                 to_connect = find((char *)XML_GET_CONTENT(conector3->children));
447                                 if (!current_item->connect(to_connect, IConector::OUT)) {
448                                         std::cout << s << " Error al conectar " << std::endl;
449                                 }
450                         }
451                 }
452                 nodo = nodo->next;
453         }
454         // Fin de las conexiones
455 }
456
457 std::string Simulator::get_state_as_xml()
458 {
459         std::stringstream out;
460
461         // XML Header
462         out << "<?xml version=\"1.0\" ?>" << std::endl;
463
464         out << "<plantstatus frame=\"" << frame << "\">" << std::endl;
465         
466         std::list<PlantItem *>::iterator i2;
467         for(i2=items.begin(); i2!=items.end(); i2++)
468                 (*i2)->get_state_as_xml(out);
469
470         out << "</plantstatus>";
471         return out.str();;
472 }
473
474 RGB Simulator::loadRGB(xmlNodePtr nodo)
475 {
476         unsigned r,g,b;
477         while (nodo != NULL) {
478                 if (nodo->type == XML_ELEMENT_NODE) {
479                         if (xmlStrcmp(nodo->name, BAD_CAST"rojo")==0)
480                                 r = atoi( (char *)XML_GET_CONTENT(nodo->children) );
481                         if (xmlStrcmp(nodo->name, BAD_CAST"verde")==0)
482                                 g = atoi( (char *)XML_GET_CONTENT(nodo->children) );
483                         if (xmlStrcmp(nodo->name, BAD_CAST"azul")==0)
484                                 b = atoi( (char *)XML_GET_CONTENT(nodo->children) );
485                 }
486                 nodo = nodo->next;
487         }
488         return RGB(r,g,b);
489 }
490