4 using namespace PlaQui::Model;
6 Simulator::Simulator(const std::string &filename)
9 /* Parseo de ejemplo de un XML desde archivo */
11 document = xmlParseFile(filename.c_str());
12 if (document == NULL) {
14 std::cout << "Error cargando XML" << std::endl;
20 /* bien, el archivo se parseo bien! */
21 xmlNodePtr nodo, items;
22 nodo = document->children;
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) {
30 } else if ((xmlStrcmp(items->name, BAD_CAST"tubo")==0)||(xmlStrcmp(items->name, BAD_CAST"codo")==0)) {
32 } else if (xmlStrcmp(items->name, BAD_CAST"exclusa")==0) {
34 } else if (xmlStrcmp(items->name, BAD_CAST"tanque")==0) {
36 } else if (xmlStrcmp(items->name, BAD_CAST"empalme")==0) {
38 } else if (xmlStrcmp(items->name, BAD_CAST"drenaje")==0) {
40 } else if (xmlStrcmp(items->name, BAD_CAST"and")==0) {
42 } else if (xmlStrcmp(items->name, BAD_CAST"or")==0) {
44 } else if (xmlStrcmp(items->name, BAD_CAST"not")==0) {
50 // Bien, la planta esta cargada, conectemos todo!!
51 do_connections(nodo->children);
52 do_logic_connetions(nodo->children);
57 Simulator::~Simulator()
59 std::list<PlantItem *>::iterator i = items.begin();
62 while (i != items.end()) {
70 void Simulator::add_pump(const std::string &name, float max_flow, RGB color)
72 Pump *p = new Pump(name);
73 p->set_max_flow(max_flow);
75 pump_lst.push_back(p);
79 void Simulator::add_union(const std::string &name, float max_flow)
81 Union *u = new Union(name);
82 u->set_max_flow(max_flow);
83 union_lst.push_back(u);
87 void Simulator::add_splitter(const std::string &name, float max_flow)
89 Splitter *p = new Splitter(name);
90 p->set_max_flow(max_flow);
91 split_lst.push_back(p);
95 void Simulator::add_conduct(const std::string &name, float flujo)
97 Conduct *p = new Conduct(name);
98 p->set_max_flow(flujo);
99 conduct_lst.push_back(p);
103 void Simulator::add_exclusa(const std::string &name, bool open)
105 Exclusa *p = new Exclusa(name);
108 exclusa_lst.push_back(p);
112 void Simulator::add_tank(const std::string &name, float capacity, float initial, RGB color)
114 Tank *p = new Tank(name);
115 p->set_capacity(capacity);
116 p->set_max_flow(initial);
117 p->set_litros(initial);
119 tank_lst.push_back(p);
123 void Simulator::add_drainage(const std::string &name)
125 Drainage *p = new Drainage(name);
126 drainage_lst.push_back(p);
130 bool Simulator::connect(const std::string &name1, const std::string &name2, int flag)
136 if ((o1 == NULL) || (o2 == NULL)) {
137 // NO SE PUDO CONECTAR!, FALTAN ITEMS!!
142 if (flag == IConector::OUT) {
143 b = o1->connect(o2, IConector::OUT);
144 b = b && o2->connect(o1, IConector::IN);
146 b = o1->connect(o2, IConector::IN);
147 b = b && o2->connect(o1, IConector::OUT);
153 void Simulator::simulate()
156 std::list<Pump *>::iterator i1;
157 for(i1=pump_lst.begin(); i1!=pump_lst.end(); i1++)
160 std::list<PlantItem *>::iterator i2;
161 for(i2=items.begin(); i2!=items.end(); i2++)
164 for(i2=items.begin(); i2!=items.end(); i2++)
170 IConector *Simulator::find(const std::string &name)
172 // Busco el item, aca no me importa de que tipo es!
173 std::list<PlantItem *>::iterator i;
174 for(i=items.begin(); i!=items.end(); i++)
175 if ((*i)->get_name() == name)
180 LogicControl *Simulator::find_logic(const std::string &name)
182 // Busco el item, aca no me importa de que tipo es!
183 std::list<LogicControl *>::iterator i;
184 for(i=control_lst.begin(); i!=control_lst.end(); i++)
185 if ((*i)->get_name() == name)
190 bool Simulator::set_open(const std::string &name, bool open)
192 // Busco el elemento, usando RTTI :-(
193 IConector *tmp = find(name);
196 if ((p = dynamic_cast<Pump*>(tmp))) {
202 } else if ((e = dynamic_cast<Exclusa*>(tmp))) {
213 void Simulator::loadBomba(xmlNodePtr nodo)
215 std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
216 int orientacion=0, x, y;
220 nodo = nodo->children;
221 while (nodo != NULL) {
222 if (nodo->type == XML_ELEMENT_NODE) {
223 if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
224 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
225 } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
226 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
227 } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
228 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
229 } else if (xmlStrcmp(nodo->name, BAD_CAST"entrega") == 0) {
230 flujo = atof( (char *)XML_GET_CONTENT(nodo->children) );
231 } else if (xmlStrcmp(nodo->name, BAD_CAST"color") == 0) {
232 color = loadRGB(nodo->children);
238 add_pump(name, flujo, color);
241 void Simulator::loadConduct(xmlNodePtr nodo)
243 std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
244 int orientacion=0, x, y;
247 nodo = nodo->children;
248 while (nodo != NULL) {
249 if (nodo->type == XML_ELEMENT_NODE) {
250 if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
251 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
252 } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
253 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
254 } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
255 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
256 } else if (xmlStrcmp(nodo->name, BAD_CAST"caudal") == 0) {
257 flujo = atof( (char *)XML_GET_CONTENT(nodo->children) );
265 // listo, ya recolecte todos los datos, ahora creo el objeto!
266 add_conduct(name, flujo);
269 void Simulator::loadExclusa(xmlNodePtr nodo)
271 std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
272 int orientacion=0, x, y;
275 nodo = nodo->children;
276 while (nodo != NULL) {
277 if (nodo->type == XML_ELEMENT_NODE) {
278 if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
279 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
280 } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
281 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
282 } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
283 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
284 } else if (xmlStrcmp(nodo->name, BAD_CAST"estado") == 0) {
285 open = (char *)XML_GET_CONTENT(nodo->children);
291 // listo, ya recolecte todos los datos, ahora creo el objeto!
292 add_exclusa(name, open == "1");
295 void Simulator::loadTank(xmlNodePtr nodo)
297 std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
298 int orientacion=0, x, y;
299 float capacidad, inicial;
302 nodo = nodo->children;
303 while (nodo != NULL) {
304 if (nodo->type == XML_ELEMENT_NODE) {
305 if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
306 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
307 } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
308 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
309 } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
310 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
311 } else if (xmlStrcmp(nodo->name, BAD_CAST"capacidad") == 0) {
312 capacidad = atoi( (char *)XML_GET_CONTENT(nodo->children) );
313 } else if (xmlStrcmp(nodo->name, BAD_CAST"inicial") == 0) {
314 inicial = atof( (char *)XML_GET_CONTENT(nodo->children) );
315 } else if (xmlStrcmp(nodo->name, BAD_CAST"") == 0) {
316 color = loadRGB(nodo->children);
322 // listo, ya recolecte todos los datos, ahora creo el objeto!
323 add_tank(name, capacidad, inicial, color);
326 void Simulator::loadUnion(xmlNodePtr nodo)
328 std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
329 int orientacion=0, x, y;
333 nodo = nodo->children;
334 while (nodo != NULL) {
335 if (nodo->type == XML_ELEMENT_NODE) {
336 if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
337 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
338 } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
339 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
340 } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
341 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
342 } else if (xmlStrcmp(nodo->name, BAD_CAST"caudal") == 0) {
343 flow = atof( (char *)XML_GET_CONTENT(nodo->children) );
344 } else if (xmlStrcmp(nodo->name, BAD_CAST"tipo") == 0) {
345 type = (char *)XML_GET_CONTENT(nodo->children);
351 // listo, ya recolecte todos los datos, ahora creo el objeto!
353 add_union(name, flow);
355 add_splitter(name, flow);
358 void Simulator::loadDrain(xmlNodePtr nodo)
360 std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
365 void Simulator::loadNot(xmlNodePtr nodo)
367 std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
368 std::string id = (char *)xmlGetProp(nodo, BAD_CAST"id");
369 int orientacion=0, x, y;
371 nodo = nodo->children;
372 while (nodo != NULL) {
373 if (nodo->type == XML_ELEMENT_NODE) {
374 if (xmlStrcmp(nodo->name, BAD_CAST"salida") == 0) {
375 //p->out_lines.push_back((char *)XML_GET_CONTENT(nodo->children));
376 } else if (xmlStrcmp(nodo->name, BAD_CAST"entrada") == 0) {
377 //p->in_lines.push_back((char *)XML_GET_CONTENT(nodo->children));
385 control_lst.push_back(n);
388 void Simulator::loadOr(xmlNodePtr nodo)
390 std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
391 std::string id = (char *)xmlGetProp(nodo, BAD_CAST"id");
393 nodo = nodo->children;
394 while (nodo != NULL) {
395 if (nodo->type == XML_ELEMENT_NODE) {
396 if (xmlStrcmp(nodo->name, BAD_CAST"salida") == 0) {
397 //p->out_lines.push_back((char *)XML_GET_CONTENT(nodo->children));
398 } else if (xmlStrcmp(nodo->name, BAD_CAST"entrada") == 0) {
399 //p->in_lines.push_back((char *)XML_GET_CONTENT(nodo->children));
407 control_lst.push_back(n);
410 void Simulator::loadAnd(xmlNodePtr nodo)
412 std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
413 std::string id = (char *)xmlGetProp(nodo, BAD_CAST"id");
414 int orientacion=0, x, y;
416 nodo = nodo->children;
417 while (nodo != NULL) {
418 if (nodo->type == XML_ELEMENT_NODE) {
419 if (xmlStrcmp(nodo->name, BAD_CAST"salida") == 0) {
420 // p->out_lines.push_back((char *)XML_GET_CONTENT(nodo->children));
421 } else if (xmlStrcmp(nodo->name, BAD_CAST"entrada") == 0) {
422 // p->in_lines.push_back((char *)XML_GET_CONTENT(nodo->children));
430 control_lst.push_back(n);
433 void Simulator::do_connections(xmlNodePtr nodo)
435 // Intengo conectar los elementos :)
436 IConector *current_item, *to_connect;
437 xmlNodePtr props; // propiedades del item actual
438 xmlNodePtr conector1, conector2, conector3;
440 while (nodo != NULL) {
441 if (nodo->type != XML_ELEMENT_NODE) {
445 // obtengo el items actual por su nombre
446 std::string s = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
447 current_item = find((char *)xmlGetProp(nodo, BAD_CAST"nombre"));
448 props = nodo->children;
449 conector3 = conector2 = conector1 = NULL;
450 while (props != NULL) {
451 if (nodo->type == XML_ELEMENT_NODE) {
452 if (xmlStrcmp(props->name, BAD_CAST"conector") == 0) {
453 xmlNodePtr temp = props->children;
454 while ((temp != NULL) && (conector1 == NULL))
455 if (temp->type == XML_ELEMENT_NODE) {
462 while ((temp != NULL) && (conector2 == NULL))
463 if (temp->type == XML_ELEMENT_NODE) {
470 while ((temp != NULL) && (conector3 == NULL))
471 if (temp->type == XML_ELEMENT_NODE) {
482 // Bien, conector1 y 2 deberian estar apuntando a <entrada> y/o <salida>
483 if (conector1 != NULL) {
484 // si, aca hay un conector!, veamos cual es
485 if (xmlStrcmp(conector1->name, BAD_CAST"entrada") == 0) {
486 // bien, es a la entrada!, obtengo el item al cual lo tengo que conectar
487 to_connect = find((char *)XML_GET_CONTENT(conector1->children));
489 current_item->connect(to_connect, IConector::IN);
490 } else if (xmlStrcmp(conector1->name, BAD_CAST"salida") == 0) {
491 // Era a salida, es casi lo mismo que arriba
492 to_connect = find((char *)XML_GET_CONTENT(conector1->children));
493 current_item->connect(to_connect, IConector::OUT);
496 if (conector2 != NULL) {
497 // si, aca hay un conector!, veamos cual es
498 if (xmlStrcmp(conector2->name, BAD_CAST"entrada") == 0) {
499 // bien, es a la entrada!, obtengo el item al cual lo tengo que conectar
500 to_connect = find((char *)XML_GET_CONTENT(conector2->children));
502 current_item->connect(to_connect, IConector::IN);
503 } else if (xmlStrcmp(conector2->name, BAD_CAST"salida") == 0) {
504 // Era a salida, es casi lo mismo que arriba
505 to_connect = find((char *)XML_GET_CONTENT(conector2->children));
506 current_item->connect(to_connect, IConector::OUT);
509 if (conector3 != NULL) {
510 // si, aca hay un conector!, veamos cual es
511 if (xmlStrcmp(conector3->name, BAD_CAST"entrada") == 0) {
512 // bien, es a la entrada!, obtengo el item al cual lo tengo que conectar
513 to_connect = find((char *)XML_GET_CONTENT(conector3->children));
515 if (!current_item->connect(to_connect, IConector::IN)) {
516 std::cout << s << " Error al conectar " << std::endl;
518 } else if (xmlStrcmp(conector3->name, BAD_CAST"salida") == 0) {
519 // Era a salida, es casi lo mismo que arriba
520 to_connect = find((char *)XML_GET_CONTENT(conector3->children));
521 if (!current_item->connect(to_connect, IConector::OUT)) {
522 std::cout << s << " Error al conectar " << std::endl;
528 // Fin de las conexiones
531 std::string Simulator::get_state_as_xml()
533 std::stringstream out;
536 out << "<?xml version=\"1.0\" ?>" << std::endl;
538 out << "<plantstatus frame=\"" << frame << "\">" << std::endl;
540 std::list<PlantItem *>::iterator i2;
541 for(i2=items.begin(); i2!=items.end(); i2++)
542 (*i2)->get_state_as_xml(out);
544 out << "</plantstatus>";
548 RGB Simulator::loadRGB(xmlNodePtr nodo)
551 while (nodo != NULL) {
552 if (nodo->type == XML_ELEMENT_NODE) {
553 if (xmlStrcmp(nodo->name, BAD_CAST"rojo")==0)
554 r = atoi( (char *)XML_GET_CONTENT(nodo->children) );
555 if (xmlStrcmp(nodo->name, BAD_CAST"verde")==0)
556 g = atoi( (char *)XML_GET_CONTENT(nodo->children) );
557 if (xmlStrcmp(nodo->name, BAD_CAST"azul")==0)
558 b = atoi( (char *)XML_GET_CONTENT(nodo->children) );
565 void Simulator::do_logic_connetions(xmlNodePtr nodo)
567 LogicControl *current;
569 while (nodo != NULL) {
570 if (nodo->type != XML_ELEMENT_NODE) {
574 // obtengo el items actual por su nombre
575 if (xmlStrcmp(nodo->name, BAD_CAST"and") == 0) {
576 std::string s = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
577 current = find_logic((char *)xmlGetProp(nodo, BAD_CAST"nombre"));
578 connect_logic(current, nodo->children);
584 void Simulator::connect_logic(LogicControl *current, xmlNodePtr nodo)
587 while (nodo != NULL) {
588 if (nodo->type != XML_ELEMENT_NODE) {
592 if (xmlStrcmp(nodo->name, BAD_CAST"entrada") == 0) {
593 item = dynamic_cast<Control *>(find((char *)XML_GET_CONTENT(nodo->children)));
595 current->connect( item->get_logic_output(), IConector::IN );
597 std::cout << "ERROR : Item no es tipo Control!!" << std::endl;
599 } else if (xmlStrcmp(nodo->name, BAD_CAST"salida") == 0) {
600 item = dynamic_cast<Control *>(find((char *)XML_GET_CONTENT(nodo->children)));
602 item->get_logic_input()->connect( current, IConector::IN );
604 std::cout << "ERROR : Item no es tipo Control!!" << std::endl;