]> git.llucax.com Git - z.facultad/75.42/plaqui.git/blobdiff - Model/src/simulator.cpp
Ahora genera libplaquimodel.a.
[z.facultad/75.42/plaqui.git] / Model / src / simulator.cpp
index 3f432b930a519544607f652cec36f12265de5ca5..3aeacd667c25c3af77b8fce1853f51e7c6dbe673 100644 (file)
@@ -23,9 +23,7 @@ Simulator::Simulator(const std::string &filename)
                        if (items->type == XML_ELEMENT_NODE) {
                                if (xmlStrcmp(items->name, BAD_CAST"bomba")==0) {
                                        loadBomba(items);
                        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) {
+                               } else if ((xmlStrcmp(items->name, BAD_CAST"tubo")==0)||(xmlStrcmp(items->name, BAD_CAST"codo")==0)) {
                                        loadConduct(items);
                                } else if (xmlStrcmp(items->name, BAD_CAST"exclusa")==0) {
                                        loadExclusa(items);
                                        loadConduct(items);
                                } else if (xmlStrcmp(items->name, BAD_CAST"exclusa")==0) {
                                        loadExclusa(items);
@@ -33,10 +31,14 @@ Simulator::Simulator(const std::string &filename)
                                        loadTank(items);
                                } else if (xmlStrcmp(items->name, BAD_CAST"empalme")==0) {
                                        loadUnion(items);
                                        loadTank(items);
                                } else if (xmlStrcmp(items->name, BAD_CAST"empalme")==0) {
                                        loadUnion(items);
+                               } else if (xmlStrcmp(items->name, BAD_CAST"drenaje")==0) {
+                                       loadDrain(items);
                                }
                        }
                        items = items->next;
                }
                                }
                        }
                        items = items->next;
                }
+               // Bien, la planta esta cargada, conectemos todo!!
+               do_connections(nodo->children);
        }
 }
 
        }
 }
 
@@ -45,56 +47,58 @@ Simulator::~Simulator()
        // FIXME REMOVER TODOOOOOO
 }
 
        // FIXME REMOVER TODOOOOOO
 }
 
-void Simulator::add_pump(const std::string &name)
+void Simulator::add_pump(const std::string &name, float max_flow, RGB color)
 {
        Pump *p = new Pump(name);
        // FIXME no va!!
 {
        Pump *p = new Pump(name);
        // FIXME no va!!
-       p->set_max_flow(5);
+       p->set_max_flow(max_flow);
+       p->set_color(color);
        pump_lst.push_back(p);
        items.push_back(p);
 }
 
        pump_lst.push_back(p);
        items.push_back(p);
 }
 
-void Simulator::add_union(const std::string &name)
+void Simulator::add_union(const std::string &name, float max_flow)
 {
        Union *u = new Union(name);
        // FIXME no va!!
 {
        Union *u = new Union(name);
        // FIXME no va!!
-       u->set_max_flow(5);
+       u->set_max_flow(max_flow);
        union_lst.push_back(u);
        items.push_back(u);
 }
 
        union_lst.push_back(u);
        items.push_back(u);
 }
 
-void Simulator::add_splitter(const std::string &name)
+void Simulator::add_splitter(const std::string &name, float max_flow)
 {
        Splitter *p = new Splitter(name);
 {
        Splitter *p = new Splitter(name);
-       // FIXME no va!!
-       p->set_max_flow(5);
+       p->set_max_flow(max_flow);
        split_lst.push_back(p);
        items.push_back(p);
 }
 
        split_lst.push_back(p);
        items.push_back(p);
 }
 
-void Simulator::add_conduct(const std::string &name)
+void Simulator::add_conduct(const std::string &name, float flujo)
 {
        Conduct *p = new Conduct(name);
        // FIXME no va!!
 {
        Conduct *p = new Conduct(name);
        // FIXME no va!!
-       p->set_max_flow(5);
+       p->set_max_flow(flujo);
        conduct_lst.push_back(p);
        items.push_back(p);
 }
 
        conduct_lst.push_back(p);
        items.push_back(p);
 }
 
-void Simulator::add_exclusa(const std::string &name)
+void Simulator::add_exclusa(const std::string &name, bool open)
 {
        Exclusa *p = new Exclusa(name);
 {
        Exclusa *p = new Exclusa(name);
-       // FIXME no va!!
+       if (!open)
+               p->close();
        exclusa_lst.push_back(p);
        items.push_back(p);
 }
 
        exclusa_lst.push_back(p);
        items.push_back(p);
 }
 
-void Simulator::add_tank(const std::string &name)
+void Simulator::add_tank(const std::string &name, float capacity, float initial, RGB color)
 {
        Tank *p = new Tank(name);
 {
        Tank *p = new Tank(name);
-       p->set_capacity(100);
-       p->set_max_flow(10);
-       p->set_litros(10);
+       p->set_capacity(capacity);
+       p->set_max_flow(initial);
+       p->set_litros(initial);
+       p->set_color(color);
        tank_lst.push_back(p);
        items.push_back(p);
 }
        tank_lst.push_back(p);
        items.push_back(p);
 }
@@ -102,7 +106,6 @@ void Simulator::add_tank(const std::string &name)
 void Simulator::add_drainage(const std::string &name)
 {
        Drainage *p = new Drainage(name);
 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);
 }
        drainage_lst.push_back(p);
        items.push_back(p);
 }
@@ -155,23 +158,35 @@ IConector *Simulator::find(const std::string &name)
        return NULL;
 }
 
        return NULL;
 }
 
-bool Simulator::pump_deactivate(const std::string &name)
+bool Simulator::set_open(const std::string &name, bool open)
 {
        // Busco el elemento, usando RTTI :-(
 {
        // Busco el elemento, usando RTTI :-(
-       Pump *pump = dynamic_cast<Pump *>(find(name));
-
-       if (!pump) {
-               // Ups!, "name" no era un Pump!!!
+       IConector *tmp = find(name);
+       Pump *p;
+       Exclusa *e;
+       if ((p = dynamic_cast<Pump*>(tmp))) {
+               if (open) {
+                       p->activate();
+               } else {
+                       p->deactivate();
+               }
+       } else if ((e = dynamic_cast<Exclusa*>(tmp))) {
+               if (open) {
+                       e->open();
+               } else {
+                       e->close();
+               }
+       } else {
                return false;
        }
                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;
 }
 
 void Simulator::loadBomba(xmlNodePtr nodo)
 {
        std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
        int orientacion=0, x, y;
+       RGB color;
+       float flujo;
 
        nodo = nodo->children;
        while (nodo != NULL) {
 
        nodo = nodo->children;
        while (nodo != NULL) {
@@ -182,18 +197,24 @@ void Simulator::loadBomba(xmlNodePtr nodo)
                                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) );
                                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) );
+                       } else if (xmlStrcmp(nodo->name, BAD_CAST"entrega") == 0) {
+                               flujo = atof( (char *)XML_GET_CONTENT(nodo->children) );
+                       } else if (xmlStrcmp(nodo->name, BAD_CAST"color") == 0) {
+                               // FIXME !
+                               color = RGB();
                        }
                }
                nodo = nodo->next;
        }
 
                        }
                }
                nodo = nodo->next;
        }
 
-       add_pump(name);
+       add_pump(name, flujo, color);
 }
 
 }
 
-void Simulator::loadCodo(xmlNodePtr nodo)
+void Simulator::loadConduct(xmlNodePtr nodo)
 {
        std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
        int orientacion=0, x, y;
 {
        std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
        int orientacion=0, x, y;
+       float flujo;
 
        nodo = nodo->children;
        while (nodo != NULL) {
 
        nodo = nodo->children;
        while (nodo != NULL) {
@@ -204,18 +225,24 @@ void Simulator::loadCodo(xmlNodePtr nodo)
                                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) );
                                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) );
+                       } else if (xmlStrcmp(nodo->name, BAD_CAST"caudal") == 0) {
+                               flujo = atof( (char *)XML_GET_CONTENT(nodo->children) );
                        }
                        }
+                       
+
                }
                nodo = nodo->next;
        }
 
                }
                nodo = nodo->next;
        }
 
-       add_conduct(name);
+       // listo, ya recolecte todos los datos, ahora creo el objeto!
+       add_conduct(name, flujo);
 }
 
 }
 
-void Simulator::loadConduct(xmlNodePtr nodo)
+void Simulator::loadExclusa(xmlNodePtr nodo)
 {
        std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
        int orientacion=0, x, y;
 {
        std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
        int orientacion=0, x, y;
+       std::string open;
 
        nodo = nodo->children;
        while (nodo != NULL) {
 
        nodo = nodo->children;
        while (nodo != NULL) {
@@ -226,19 +253,23 @@ void Simulator::loadConduct(xmlNodePtr nodo)
                                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) );
                                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) );
+                       } else if (xmlStrcmp(nodo->name, BAD_CAST"estado") == 0) {
+                               open = (char *)XML_GET_CONTENT(nodo->children);
                        }
                }
                nodo = nodo->next;
        }
 
        // listo, ya recolecte todos los datos, ahora creo el objeto!
                        }
                }
                nodo = nodo->next;
        }
 
        // listo, ya recolecte todos los datos, ahora creo el objeto!
-       add_conduct(name);
+       add_exclusa(name, open == "1");
 }
 
 }
 
-void Simulator::loadExclusa(xmlNodePtr nodo)
+void Simulator::loadTank(xmlNodePtr nodo)
 {
        std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
        int orientacion=0, x, y;
 {
        std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
        int orientacion=0, x, y;
+       float capacidad, inicial;
+       RGB color; // TODO
 
        nodo = nodo->children;
        while (nodo != NULL) {
 
        nodo = nodo->children;
        while (nodo != NULL) {
@@ -249,19 +280,25 @@ void Simulator::loadExclusa(xmlNodePtr nodo)
                                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) );
                                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) );
+                       } else if (xmlStrcmp(nodo->name, BAD_CAST"capacidad") == 0) {
+                               capacidad = atoi( (char *)XML_GET_CONTENT(nodo->children) );
+                       } else if (xmlStrcmp(nodo->name, BAD_CAST"inicial") == 0) {
+                               inicial = atof( (char *)XML_GET_CONTENT(nodo->children) );
                        }
                }
                nodo = nodo->next;
        }
 
        // listo, ya recolecte todos los datos, ahora creo el objeto!
                        }
                }
                nodo = nodo->next;
        }
 
        // listo, ya recolecte todos los datos, ahora creo el objeto!
-       add_exclusa(name);
+       add_tank(name, capacidad, inicial, color);
 }
 
 }
 
-void Simulator::loadTank(xmlNodePtr nodo)
+void Simulator::loadUnion(xmlNodePtr nodo)
 {
        std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
        int orientacion=0, x, y;
 {
        std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
        int orientacion=0, x, y;
+       float flow;
+       std::string type;
 
        nodo = nodo->children;
        while (nodo != NULL) {
 
        nodo = nodo->children;
        while (nodo != NULL) {
@@ -272,39 +309,122 @@ void Simulator::loadTank(xmlNodePtr nodo)
                                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) );
                                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) );
+                       } else if (xmlStrcmp(nodo->name, BAD_CAST"caudal") == 0) {
+                               flow = atof( (char *)XML_GET_CONTENT(nodo->children) );
+                       } else if (xmlStrcmp(nodo->name, BAD_CAST"tipo") == 0) {
+                               type = (char *)XML_GET_CONTENT(nodo->children);
                        }
                }
                nodo = nodo->next;
        }
 
        // listo, ya recolecte todos los datos, ahora creo el objeto!
                        }
                }
                nodo = nodo->next;
        }
 
        // listo, ya recolecte todos los datos, ahora creo el objeto!
-       add_tank(name);
+       if (type == "union")
+               add_union(name, flow);
+       else
+               add_splitter(name, flow);
 }
 
 }
 
-void Simulator::loadUnion(xmlNodePtr nodo)
+void Simulator::loadDrain(xmlNodePtr nodo)
 {
        std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
 {
        std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
-       int orientacion=0, x, y;
 
 
-       nodo = nodo->children;
+       add_drainage(name);
+}
+
+void Simulator::do_connections(xmlNodePtr nodo)
+{
+       // Intengo conectar los elementos :)
+       IConector *current_item, *to_connect;
+       xmlNodePtr props; // propiedades del item actual
+       xmlNodePtr conector1, conector2, conector3;
+
        while (nodo != NULL) {
        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) );
+               if (nodo->type != XML_ELEMENT_NODE) {
+                       nodo = nodo->next;
+                       continue;
+               }
+               // obtengo el items actual por su nombre
+               current_item = find((char *)xmlGetProp(nodo, BAD_CAST"nombre"));
+               props = nodo->children;
+               conector3 = conector2 = conector1 = NULL;
+               while (props != NULL) {
+                       if (nodo->type == XML_ELEMENT_NODE) {
+                               if (xmlStrcmp(props->name, BAD_CAST"conector") == 0) {
+                                       xmlNodePtr temp = props->children;
+                                       while ((temp != NULL) && (conector1 == NULL))
+                                               if (temp->type == XML_ELEMENT_NODE) {
+                                                       conector1 = temp;
+                                                       temp = temp->next;
+                                                       break;
+                                               } else {
+                                                       temp = temp->next;
+                                               }
+                                       while ((temp != NULL) && (conector2 == NULL))
+                                               if (temp->type == XML_ELEMENT_NODE) {
+                                                       conector2 = temp;
+                                                       temp = temp->next;
+                                                       break;
+                                               } else {
+                                                       temp = temp->next;
+                                               }
+                                       while ((temp != NULL) && (conector3 == NULL))
+                                               if (temp->type == XML_ELEMENT_NODE) {
+                                                       conector3 = temp;
+                                                       temp = temp->next;
+                                                       break;
+                                               } else {
+                                                       temp = temp->next;
+                                               }
+                               }
+                       }
+                       props = props->next;
+               }
+               // Bien, conector1 y 2 deberian estar apuntando a <entrada> y/o <salida>
+               if (conector1 != NULL) {
+                       // si, aca hay un conector!, veamos cual es
+                       if (xmlStrcmp(conector1->name, BAD_CAST"entrada") == 0) {
+                               // bien, es a la entrada!, obtengo el item al cual lo tengo que conectar
+                               to_connect = find((char *)XML_GET_CONTENT(conector1->children));
+                               // y lo conecto
+                               current_item->connect(to_connect, IConector::IN);
+                       } else if (xmlStrcmp(conector1->name, BAD_CAST"salida") == 0) {
+                               // Era a salida, es casi lo mismo que arriba 
+                               to_connect = find((char *)XML_GET_CONTENT(conector1->children));
+                               current_item->connect(to_connect, IConector::OUT);
+                       }
+               }
+               if (conector2 != NULL) {
+                       // si, aca hay un conector!, veamos cual es
+                       if (xmlStrcmp(conector2->name, BAD_CAST"entrada") == 0) {
+                               // bien, es a la entrada!, obtengo el item al cual lo tengo que conectar
+                               to_connect = find((char *)XML_GET_CONTENT(conector2->children));
+                               // y lo conecto
+                               current_item->connect(to_connect, IConector::IN);
+                       } else if (xmlStrcmp(conector2->name, BAD_CAST"salida") == 0) {
+                               // Era a salida, es casi lo mismo que arriba 
+                               to_connect = find((char *)XML_GET_CONTENT(conector2->children));
+                               current_item->connect(to_connect, IConector::OUT);
+                       }
+               }
+               if (conector3 != NULL) {
+                       // si, aca hay un conector!, veamos cual es
+                       if (xmlStrcmp(conector3->name, BAD_CAST"entrada") == 0) {
+                               // bien, es a la entrada!, obtengo el item al cual lo tengo que conectar
+                               to_connect = find((char *)XML_GET_CONTENT(conector3->children));
+                               // y lo conecto
+                               current_item->connect(to_connect, IConector::IN);
+                       } else if (xmlStrcmp(conector3->name, BAD_CAST"salida") == 0) {
+                               // Era a salida, es casi lo mismo que arriba 
+                               to_connect = find((char *)XML_GET_CONTENT(conector3->children));
+                               current_item->connect(to_connect, IConector::OUT);
                        }
                }
                nodo = nodo->next;
        }
                        }
                }
                nodo = nodo->next;
        }
-
-       // listo, ya recolecte todos los datos, ahora creo el objeto!
-       add_union(name);
+       // Fin de las conexiones
 }
 
 }
 
-
 std::string Simulator::get_state_as_xml()
 {
        std::stringstream out;
 std::string Simulator::get_state_as_xml()
 {
        std::stringstream out;