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++) {
163 (*i2)->update_color();
166 for(i2=items.begin(); i2!=items.end(); i2++)
172 IConector *Simulator::find(const std::string &name)
174 // Busco el item, aca no me importa de que tipo es!
175 std::list<PlantItem *>::iterator i;
176 for(i=items.begin(); i!=items.end(); i++)
177 if ((*i)->get_name() == name)
182 LogicControl *Simulator::find_logic(const std::string &name)
184 // Busco el item, aca no me importa de que tipo es!
185 std::list<LogicControl *>::iterator i;
186 for(i=control_lst.begin(); i!=control_lst.end(); i++)
187 if ((*i)->get_name() == name)
192 bool Simulator::set_open(const std::string &name, bool open)
194 // Busco el elemento, usando RTTI :-(
195 IConector *tmp = find(name);
198 if ((p = dynamic_cast<Pump*>(tmp))) {
204 } else if ((e = dynamic_cast<Exclusa*>(tmp))) {
215 void Simulator::loadBomba(xmlNodePtr nodo)
217 std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
218 int orientacion=0, x, y;
222 nodo = nodo->children;
223 while (nodo != NULL) {
224 if (nodo->type == XML_ELEMENT_NODE) {
225 if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
226 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
227 } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
228 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
229 } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
230 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
231 } else if (xmlStrcmp(nodo->name, BAD_CAST"entrega") == 0) {
232 flujo = atof( (char *)XML_GET_CONTENT(nodo->children) );
233 } else if (xmlStrcmp(nodo->name, BAD_CAST"color") == 0) {
234 color = loadRGB(nodo->children);
240 add_pump(name, flujo, color);
243 void Simulator::loadConduct(xmlNodePtr nodo)
245 std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
246 int orientacion=0, x, y;
249 nodo = nodo->children;
250 while (nodo != NULL) {
251 if (nodo->type == XML_ELEMENT_NODE) {
252 if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
253 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
254 } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
255 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
256 } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
257 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
258 } else if (xmlStrcmp(nodo->name, BAD_CAST"caudal") == 0) {
259 flujo = atof( (char *)XML_GET_CONTENT(nodo->children) );
267 // listo, ya recolecte todos los datos, ahora creo el objeto!
268 add_conduct(name, flujo);
271 void Simulator::loadExclusa(xmlNodePtr nodo)
273 std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
274 int orientacion=0, x, y;
277 nodo = nodo->children;
278 while (nodo != NULL) {
279 if (nodo->type == XML_ELEMENT_NODE) {
280 if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
281 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
282 } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
283 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
284 } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
285 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
286 } else if (xmlStrcmp(nodo->name, BAD_CAST"estado") == 0) {
287 open = (char *)XML_GET_CONTENT(nodo->children);
293 // listo, ya recolecte todos los datos, ahora creo el objeto!
294 add_exclusa(name, open == "1");
297 void Simulator::loadTank(xmlNodePtr nodo)
299 std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
300 int orientacion=0, x, y;
301 float capacidad, inicial;
304 nodo = nodo->children;
305 while (nodo != NULL) {
306 if (nodo->type == XML_ELEMENT_NODE) {
307 if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
308 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
309 } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
310 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
311 } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
312 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
313 } else if (xmlStrcmp(nodo->name, BAD_CAST"capacidad") == 0) {
314 capacidad = atoi( (char *)XML_GET_CONTENT(nodo->children) );
315 } else if (xmlStrcmp(nodo->name, BAD_CAST"inicial") == 0) {
316 inicial = atof( (char *)XML_GET_CONTENT(nodo->children) );
317 } else if (xmlStrcmp(nodo->name, BAD_CAST"color") == 0) {
318 color = loadRGB(nodo->children);
324 // listo, ya recolecte todos los datos, ahora creo el objeto!
325 add_tank(name, capacidad, inicial, color);
328 void Simulator::loadUnion(xmlNodePtr nodo)
330 std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
331 int orientacion=0, x, y;
335 nodo = nodo->children;
336 while (nodo != NULL) {
337 if (nodo->type == XML_ELEMENT_NODE) {
338 if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
339 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
340 } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
341 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
342 } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
343 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
344 } else if (xmlStrcmp(nodo->name, BAD_CAST"caudal") == 0) {
345 flow = atof( (char *)XML_GET_CONTENT(nodo->children) );
346 } else if (xmlStrcmp(nodo->name, BAD_CAST"tipo") == 0) {
347 type = (char *)XML_GET_CONTENT(nodo->children);
353 // listo, ya recolecte todos los datos, ahora creo el objeto!
355 add_union(name, flow);
357 add_splitter(name, flow);
360 void Simulator::loadDrain(xmlNodePtr nodo)
362 std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
367 void Simulator::loadNot(xmlNodePtr nodo)
369 std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
370 std::string id = (char *)xmlGetProp(nodo, BAD_CAST"id");
371 int orientacion=0, x, y;
373 nodo = nodo->children;
374 while (nodo != NULL) {
375 if (nodo->type == XML_ELEMENT_NODE) {
376 if (xmlStrcmp(nodo->name, BAD_CAST"salida") == 0) {
377 //p->out_lines.push_back((char *)XML_GET_CONTENT(nodo->children));
378 } else if (xmlStrcmp(nodo->name, BAD_CAST"entrada") == 0) {
379 //p->in_lines.push_back((char *)XML_GET_CONTENT(nodo->children));
387 control_lst.push_back(n);
390 void Simulator::loadOr(xmlNodePtr nodo)
392 std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
393 std::string id = (char *)xmlGetProp(nodo, BAD_CAST"id");
395 nodo = nodo->children;
396 while (nodo != NULL) {
397 if (nodo->type == XML_ELEMENT_NODE) {
398 if (xmlStrcmp(nodo->name, BAD_CAST"salida") == 0) {
399 //p->out_lines.push_back((char *)XML_GET_CONTENT(nodo->children));
400 } else if (xmlStrcmp(nodo->name, BAD_CAST"entrada") == 0) {
401 //p->in_lines.push_back((char *)XML_GET_CONTENT(nodo->children));
409 control_lst.push_back(n);
412 void Simulator::loadAnd(xmlNodePtr nodo)
414 std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
415 std::string id = (char *)xmlGetProp(nodo, BAD_CAST"id");
416 int orientacion=0, x, y;
418 nodo = nodo->children;
419 while (nodo != NULL) {
420 if (nodo->type == XML_ELEMENT_NODE) {
421 if (xmlStrcmp(nodo->name, BAD_CAST"salida") == 0) {
422 // p->out_lines.push_back((char *)XML_GET_CONTENT(nodo->children));
423 } else if (xmlStrcmp(nodo->name, BAD_CAST"entrada") == 0) {
424 // p->in_lines.push_back((char *)XML_GET_CONTENT(nodo->children));
432 control_lst.push_back(n);
435 void Simulator::do_connections(xmlNodePtr nodo)
437 // Intengo conectar los elementos :)
438 IConector *current_item, *to_connect;
439 xmlNodePtr props; // propiedades del item actual
440 xmlNodePtr conector1, conector2, conector3;
442 while (nodo != NULL) {
443 if (nodo->type != XML_ELEMENT_NODE) {
447 // obtengo el items actual por su nombre
448 std::string s = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
449 current_item = find((char *)xmlGetProp(nodo, BAD_CAST"nombre"));
450 props = nodo->children;
451 conector3 = conector2 = conector1 = NULL;
452 while (props != NULL) {
453 if (nodo->type == XML_ELEMENT_NODE) {
454 if (xmlStrcmp(props->name, BAD_CAST"conector") == 0) {
455 xmlNodePtr temp = props->children;
456 while ((temp != NULL) && (conector1 == NULL))
457 if (temp->type == XML_ELEMENT_NODE) {
464 while ((temp != NULL) && (conector2 == NULL))
465 if (temp->type == XML_ELEMENT_NODE) {
472 while ((temp != NULL) && (conector3 == NULL))
473 if (temp->type == XML_ELEMENT_NODE) {
484 // Bien, conector1 y 2 deberian estar apuntando a <entrada> y/o <salida>
485 if (conector1 != NULL) {
486 // si, aca hay un conector!, veamos cual es
487 if (xmlStrcmp(conector1->name, BAD_CAST"entrada") == 0) {
488 // bien, es a la entrada!, obtengo el item al cual lo tengo que conectar
489 to_connect = find((char *)XML_GET_CONTENT(conector1->children));
491 current_item->connect(to_connect, IConector::IN);
492 } else if (xmlStrcmp(conector1->name, BAD_CAST"salida") == 0) {
493 // Era a salida, es casi lo mismo que arriba
494 to_connect = find((char *)XML_GET_CONTENT(conector1->children));
495 current_item->connect(to_connect, IConector::OUT);
498 if (conector2 != NULL) {
499 // si, aca hay un conector!, veamos cual es
500 if (xmlStrcmp(conector2->name, BAD_CAST"entrada") == 0) {
501 // bien, es a la entrada!, obtengo el item al cual lo tengo que conectar
502 to_connect = find((char *)XML_GET_CONTENT(conector2->children));
504 current_item->connect(to_connect, IConector::IN);
505 } else if (xmlStrcmp(conector2->name, BAD_CAST"salida") == 0) {
506 // Era a salida, es casi lo mismo que arriba
507 to_connect = find((char *)XML_GET_CONTENT(conector2->children));
508 current_item->connect(to_connect, IConector::OUT);
511 if (conector3 != NULL) {
512 // si, aca hay un conector!, veamos cual es
513 if (xmlStrcmp(conector3->name, BAD_CAST"entrada") == 0) {
514 // bien, es a la entrada!, obtengo el item al cual lo tengo que conectar
515 to_connect = find((char *)XML_GET_CONTENT(conector3->children));
517 current_item->connect(to_connect, IConector::IN);
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 current_item->connect(to_connect, IConector::OUT);
526 // Fin de las conexiones
529 std::string Simulator::get_state_as_xml()
531 std::stringstream out;
534 out << "<?xml version=\"1.0\" ?>" << std::endl;
536 out << "<plantstatus frame=\"" << frame << "\">" << std::endl;
538 std::list<PlantItem *>::iterator i2;
539 for(i2=items.begin(); i2!=items.end(); i2++)
540 (*i2)->get_state_as_xml(out);
542 std::list<LogicControl *>::iterator i3;
543 for(i3=control_lst.begin(); i3!=control_lst.end(); i3++)
544 (*i3)->get_state_as_xml(out);
545 out << "</plantstatus>";
549 RGB Simulator::loadRGB(xmlNodePtr nodo)
552 while (nodo != NULL) {
553 if (nodo->type == XML_ELEMENT_NODE) {
554 if (xmlStrcmp(nodo->name, BAD_CAST"rojo")==0)
555 r = atoi( (char *)XML_GET_CONTENT(nodo->children) );
556 if (xmlStrcmp(nodo->name, BAD_CAST"verde")==0)
557 g = atoi( (char *)XML_GET_CONTENT(nodo->children) );
558 if (xmlStrcmp(nodo->name, BAD_CAST"azul")==0)
559 b = atoi( (char *)XML_GET_CONTENT(nodo->children) );
563 r = static_cast<unsigned long>(255 * (r / 65535.0f));
564 g = static_cast<unsigned long>(255 * (g / 65535.0f));
565 b = static_cast<unsigned long>(255 * (b / 65535.0f));
570 void Simulator::do_logic_connetions(xmlNodePtr nodo)
572 LogicControl *current;
574 while (nodo != NULL) {
575 if (nodo->type != XML_ELEMENT_NODE) {
579 // obtengo el items actual por su nombre
580 if ((xmlStrcmp(nodo->name, BAD_CAST"and") == 0) || (xmlStrcmp(nodo->name, BAD_CAST"not") == 0) || (xmlStrcmp(nodo->name, BAD_CAST"or") == 0)) {
581 std::string s = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
582 current = find_logic((char *)xmlGetProp(nodo, BAD_CAST"nombre"));
583 connect_logic(current, nodo->children);
589 void Simulator::connect_logic(LogicControl *current, xmlNodePtr nodo)
592 while (nodo != NULL) {
593 if (nodo->type != XML_ELEMENT_NODE) {
597 if (xmlStrcmp(nodo->name, BAD_CAST"entrada") == 0) {
598 item = dynamic_cast<Control *>(find((char *)XML_GET_CONTENT(nodo->children)));
600 if (dynamic_cast<Tank *>(item) != NULL) {
601 std::string donde = (char *)xmlGetProp(nodo, BAD_CAST"id");
602 if (donde == "inferior") {
603 current->connect( item->get_logic_output(), IConector::IN );
605 current->connect( item->get_logic_input(), IConector::IN );
608 current->connect( item->get_logic_output(), IConector::IN );
611 LogicControl *logic = dynamic_cast<LogicControl *>(find_logic((char *)XML_GET_CONTENT(nodo->children)));
612 /* Bien, si no era un control, debe ser una compuerta */
614 current->connect(logic, IConector::IN);
616 std::cout << "ERROR : Item no es tipo Control ni tipo LogicControl ... esto si que esta todo malllll!!" << std::endl;
619 } else if (xmlStrcmp(nodo->name, BAD_CAST"salida") == 0) {
620 item = dynamic_cast<Control *>(find((char *)XML_GET_CONTENT(nodo->children)));
622 item->get_logic_input()->connect( current, IConector::IN );
624 LogicControl *logic = dynamic_cast<LogicControl *>(find_logic((char *)XML_GET_CONTENT(nodo->children)));
625 /* Bien, si no era un control, debe ser una compuerta */
627 logic->connect(current, IConector::IN);
629 std::cout << "ERROR : Item no es tipo Control ni tipo LogicControl ... esto si que esta todo malllll!!" << std::endl;