+
+#include "simulator.h"
+
+using namespace PlaQui::Model;
+
+Simulator::Simulator(const std::string &filename)
+{
+ /* Parseo de ejemplo de un XML desde archivo */
+ xmlDocPtr document;
+ document = xmlParseFile(filename.c_str());
+ if (document == NULL) {
+ return;
+ }
+
+ /* bien, el archivo se parseo bien! */
+ xmlNodePtr nodo, items;
+ nodo = document->children;
+
+ if (strcmp((char *)nodo->name, "planta") == 0) {
+ items = nodo->children;
+ while (items != NULL) {
+ if (items->type == XML_ELEMENT_NODE) {
+ if (xmlStrcmp(items->name, BAD_CAST"bomba")==0) {
+ loadBomba(items);
+ } else if (xmlStrcmp(items->name, BAD_CAST"codo")==0) {
+ loadCodo(items);
+ } else if (xmlStrcmp(items->name, BAD_CAST"tubo")==0) {
+ loadConduct(items);
+ } else if (xmlStrcmp(items->name, BAD_CAST"exclusa")==0) {
+ loadExclusa(items);
+ } else if (xmlStrcmp(items->name, BAD_CAST"tanque")==0) {
+ loadTank(items);
+ } else if (xmlStrcmp(items->name, BAD_CAST"empalme")==0) {
+ loadUnion(items);
+ }
+ }
+ items = items->next;
+ }
+ }
+}
+
+Simulator::~Simulator()
+{
+ // FIXME REMOVER TODOOOOOO
+}
+
+void Simulator::add_pump(const std::string &name)
+{
+ Pump *p = new Pump(name);
+ // FIXME no va!!
+ p->set_max_flow(5);
+ pump_lst.push_back(p);
+ items.push_back(p);
+}
+
+void Simulator::add_union(const std::string &name)
+{
+ Union *u = new Union(name);
+ // FIXME no va!!
+ u->set_max_flow(5);
+ union_lst.push_back(u);
+ items.push_back(u);
+}
+
+void Simulator::add_splitter(const std::string &name)
+{
+ Splitter *p = new Splitter(name);
+ // FIXME no va!!
+ p->set_max_flow(5);
+ split_lst.push_back(p);
+ items.push_back(p);
+}
+
+void Simulator::add_conduct(const std::string &name)
+{
+ Conduct *p = new Conduct(name);
+ // FIXME no va!!
+ p->set_max_flow(5);
+ conduct_lst.push_back(p);
+ items.push_back(p);
+}
+
+void Simulator::add_exclusa(const std::string &name)
+{
+ Exclusa *p = new Exclusa(name);
+ // FIXME no va!!
+ exclusa_lst.push_back(p);
+ items.push_back(p);
+}
+
+void Simulator::add_tank(const std::string &name)
+{
+ Tank *p = new Tank(name);
+ p->set_capacity(100);
+ p->set_max_flow(10);
+ p->set_litros(10);
+ tank_lst.push_back(p);
+ items.push_back(p);
+}
+
+void Simulator::add_drainage(const std::string &name)
+{
+ Drainage *p = new Drainage(name);
+ // FIXME no va!!
+ drainage_lst.push_back(p);
+ items.push_back(p);
+}
+
+bool Simulator::connect(const std::string &name1, const std::string &name2, int flag)
+{
+ IConector *o1, *o2;
+ o1 = find(name1);
+ o2 = find(name2);
+
+ if ((o1 == NULL) || (o2 == NULL)) {
+ // NO SE PUDO CONECTAR!, FALTAN ITEMS!!
+ return false;
+ }
+
+ bool b;
+ if (flag == IConector::OUT) {
+ b = o1->connect(o2, IConector::OUT);
+ b = b && o2->connect(o1, IConector::IN);
+ } else {
+ b = o1->connect(o2, IConector::IN);
+ b = b && o2->connect(o1, IConector::OUT);
+ }
+
+ return b;
+}
+
+void Simulator::simulate()
+{
+ // Actualizo
+ std::list<Pump *>::iterator i1;
+ for(i1=pump_lst.begin(); i1!=pump_lst.end(); i1++)
+ (*i1)->update();
+
+ // Simulo!
+ std::list<PlantItem *>::iterator i2;
+ for(i2=items.begin(); i2!=items.end(); i2++)
+ (*i2)->simulate();
+}
+
+IConector *Simulator::find(const std::string &name)
+{
+ // Busco el item, aca no me importa de que tipo es!
+ std::list<PlantItem *>::iterator i;
+ for(i=items.begin(); i!=items.end(); i++)
+ if ((*i)->get_name() == name)
+ return *i;
+ return NULL;
+}
+
+bool Simulator::pump_deactivate(const std::string &name)
+{
+ // Busco el elemento, usando RTTI :-(
+ Pump *pump = dynamic_cast<Pump *>(find(name));
+
+ if (!pump) {
+ // Ups!, "name" no era un Pump!!!
+ return false;
+ }
+ pump->deactivate();
+ return true;
+}
+
+void Simulator::loadBomba(xmlNodePtr nodo)
+{
+ std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
+ int orientacion=0, x, y;
+
+ nodo = nodo->children;
+ while (nodo != NULL) {
+ if (nodo->type == XML_ELEMENT_NODE) {
+ if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
+ orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
+ } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
+ x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
+ } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
+ y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
+ }
+ }
+ nodo = nodo->next;
+ }
+
+ add_pump(name);
+}
+
+void Simulator::loadCodo(xmlNodePtr nodo)
+{
+ std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
+ int orientacion=0, x, y;
+
+ nodo = nodo->children;
+ while (nodo != NULL) {
+ if (nodo->type == XML_ELEMENT_NODE) {
+ if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
+ orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
+ } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
+ x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
+ } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
+ y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
+ }
+ }
+ nodo = nodo->next;
+ }
+
+ add_conduct(name);
+}
+
+void Simulator::loadConduct(xmlNodePtr nodo)
+{
+ std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
+ int orientacion=0, x, y;
+
+ nodo = nodo->children;
+ while (nodo != NULL) {
+ if (nodo->type == XML_ELEMENT_NODE) {
+ if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
+ orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
+ } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
+ x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
+ } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
+ y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
+ }
+ }
+ nodo = nodo->next;
+ }
+
+ // listo, ya recolecte todos los datos, ahora creo el objeto!
+ add_conduct(name);
+}
+
+void Simulator::loadExclusa(xmlNodePtr nodo)
+{
+ std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
+ int orientacion=0, x, y;
+
+ nodo = nodo->children;
+ while (nodo != NULL) {
+ if (nodo->type == XML_ELEMENT_NODE) {
+ if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
+ orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
+ } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
+ x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
+ } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
+ y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
+ }
+ }
+ nodo = nodo->next;
+ }
+
+ // listo, ya recolecte todos los datos, ahora creo el objeto!
+ add_exclusa(name);
+}
+
+void Simulator::loadTank(xmlNodePtr nodo)
+{
+ std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
+ int orientacion=0, x, y;
+
+ nodo = nodo->children;
+ while (nodo != NULL) {
+ if (nodo->type == XML_ELEMENT_NODE) {
+ if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
+ orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
+ } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
+ x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
+ } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
+ y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
+ }
+ }
+ nodo = nodo->next;
+ }
+
+ // listo, ya recolecte todos los datos, ahora creo el objeto!
+ add_tank(name);
+}
+
+void Simulator::loadUnion(xmlNodePtr nodo)
+{
+ std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
+ int orientacion=0, x, y;
+
+ nodo = nodo->children;
+ while (nodo != NULL) {
+ if (nodo->type == XML_ELEMENT_NODE) {
+ if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
+ orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
+ } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
+ x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
+ } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
+ y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
+ }
+ }
+ nodo = nodo->next;
+ }
+
+ // listo, ya recolecte todos los datos, ahora creo el objeto!
+ add_union(name);
+}
+