]> git.llucax.com Git - z.facultad/75.42/plaqui.git/blobdiff - Model/src/simulator.cpp
* Se mejora la deteccion de conexiones en el constructor
[z.facultad/75.42/plaqui.git] / Model / src / simulator.cpp
index 3c81d341b708f82eb1d2b4d93d4f7eecd26408b7..b6a77d3bc68922ba1729f686091b39eaa001dd2e 100644 (file)
@@ -10,9 +10,13 @@ Simulator::Simulator(const std::string &filename)
        xmlDocPtr document;
        document = xmlParseFile(filename.c_str());
        if (document == NULL) {
+               is_load_ok = false;
+               std::cout << "Error cargando XML" << std::endl;
                return;
        }
 
+       is_load_ok = true;
+
        /* bien, el archivo se parseo bien! */
        xmlNodePtr nodo, items;
        nodo = document->children;
@@ -40,17 +44,25 @@ Simulator::Simulator(const std::string &filename)
                // Bien, la planta esta cargada, conectemos todo!!
                do_connections(nodo->children);
        }
+       xmlFreeDoc(document);
 }
 
 Simulator::~Simulator()
 {
-       // FIXME REMOVER TODOOOOOO
+       std::list<PlantItem *>::iterator i = items.begin();
+       PlantItem *o;
+
+       while (i != items.end()) {
+               o = (*i);
+               items.remove(o);
+               delete o;
+               i = items.begin();
+       }
 }
 
 void Simulator::add_pump(const std::string &name, float max_flow, RGB color)
 {
        Pump *p = new Pump(name);
-       // FIXME no va!!
        p->set_max_flow(max_flow);
        p->set_color(color);
        pump_lst.push_back(p);
@@ -60,7 +72,6 @@ void Simulator::add_pump(const std::string &name, float max_flow, RGB color)
 void Simulator::add_union(const std::string &name, float max_flow)
 {
        Union *u = new Union(name);
-       // FIXME no va!!
        u->set_max_flow(max_flow);
        union_lst.push_back(u);
        items.push_back(u);
@@ -77,7 +88,6 @@ void Simulator::add_splitter(const std::string &name, float max_flow)
 void Simulator::add_conduct(const std::string &name, float flujo)
 {
        Conduct *p = new Conduct(name);
-       // FIXME no va!!
        p->set_max_flow(flujo);
        conduct_lst.push_back(p);
        items.push_back(p);
@@ -140,9 +150,11 @@ void Simulator::simulate()
        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++)
+       for(i2=items.begin(); i2!=items.end(); i2++) 
+               (*i2)->update();
+       
+       for(i2=items.begin(); i2!=items.end(); i2++) 
                (*i2)->simulate();
 
        frame++;
@@ -158,17 +170,27 @@ IConector *Simulator::find(const std::string &name)
        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 :-(
-       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;
        }
-       pump->deactivate();
-       return true;
 }
 
 void Simulator::loadBomba(xmlNodePtr nodo)
@@ -190,8 +212,7 @@ void Simulator::loadBomba(xmlNodePtr nodo)
                        } 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();
+                               color = loadRGB(nodo->children);
                        }
                }
                nodo = nodo->next;
@@ -259,7 +280,7 @@ void Simulator::loadTank(xmlNodePtr nodo)
        std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
        int orientacion=0, x, y;
        float capacidad, inicial;
-       RGB color; // TODO
+       RGB color;
 
        nodo = nodo->children;
        while (nodo != NULL) {
@@ -274,6 +295,8 @@ void Simulator::loadTank(xmlNodePtr nodo)
                                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) );
+                       } else if (xmlStrcmp(nodo->name, BAD_CAST"") == 0) {
+                               color = loadRGB(nodo->children);
                        }
                }
                nodo = nodo->next;
@@ -327,7 +350,7 @@ void Simulator::do_connections(xmlNodePtr nodo)
        // Intengo conectar los elementos :)
        IConector *current_item, *to_connect;
        xmlNodePtr props; // propiedades del item actual
-       xmlNodePtr conector1, conector2;
+       xmlNodePtr conector1, conector2, conector3;
 
        while (nodo != NULL) {
                if (nodo->type != XML_ELEMENT_NODE) {
@@ -335,9 +358,10 @@ void Simulator::do_connections(xmlNodePtr nodo)
                        continue;
                }
                // obtengo el items actual por su nombre
+               std::string s = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
                current_item = find((char *)xmlGetProp(nodo, BAD_CAST"nombre"));
                props = nodo->children;
-               conector2 = conector1 = NULL;
+               conector3 = conector2 = conector1 = NULL;
                while (props != NULL) {
                        if (nodo->type == XML_ELEMENT_NODE) {
                                if (xmlStrcmp(props->name, BAD_CAST"conector") == 0) {
@@ -358,6 +382,14 @@ void Simulator::do_connections(xmlNodePtr nodo)
                                                } 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;
@@ -369,11 +401,19 @@ void Simulator::do_connections(xmlNodePtr nodo)
                                // 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);
+                               if (!current_item->connect(to_connect, IConector::IN)) {
+                                       std::cout << s << " Error al conectar1 ENTRADA = " << (char *)XML_GET_CONTENT(conector1->children) << std::endl;
+                               } else {
+                                       std::cout << s << " ENTRADA1 = " << (char *)XML_GET_CONTENT(conector1->children) << std::endl;
+                               }
                        } 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 (!current_item->connect(to_connect, IConector::OUT)) {
+                                       std::cout << s << " Error al conectar2 SALIDA" << std::endl;
+                               } else {
+                                       std::cout << s << " SALIDA1 = " << (char *)XML_GET_CONTENT(conector1->children) << std::endl;
+                               }
                        }
                }
                if (conector2 != NULL) {
@@ -382,11 +422,34 @@ void Simulator::do_connections(xmlNodePtr nodo)
                                // 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);
+                               if (!current_item->connect(to_connect, IConector::IN)) {
+                                       std::cout << s << " Error al conectar2 ENTRADA = " << (char *)XML_GET_CONTENT(conector2->children) << std::endl;
+                               } else {
+                                       std::cout << s << " ENTRADA2 = " << (char *)XML_GET_CONTENT(conector2->children) << std::endl;
+                               }
                        } 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 (!current_item->connect(to_connect, IConector::OUT)) {
+                                       std::cout << s << " Error al conectar " << std::endl;
+                               }
+                       }
+               }
+               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
+                               if (!current_item->connect(to_connect, IConector::IN)) {
+                                       std::cout << s << " Error al conectar " << std::endl;
+                               }
+                       } 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));
+                               if (!current_item->connect(to_connect, IConector::OUT)) {
+                                       std::cout << s << " Error al conectar " << std::endl;
+                               }
                        }
                }
                nodo = nodo->next;
@@ -411,3 +474,20 @@ std::string Simulator::get_state_as_xml()
        return out.str();;
 }
 
+RGB Simulator::loadRGB(xmlNodePtr nodo)
+{
+       unsigned r,g,b;
+       while (nodo != NULL) {
+               if (nodo->type == XML_ELEMENT_NODE) {
+                       if (xmlStrcmp(nodo->name, BAD_CAST"rojo")==0)
+                               r = atoi( (char *)XML_GET_CONTENT(nodo->children) );
+                       if (xmlStrcmp(nodo->name, BAD_CAST"verde")==0)
+                               g = atoi( (char *)XML_GET_CONTENT(nodo->children) );
+                       if (xmlStrcmp(nodo->name, BAD_CAST"azul")==0)
+                               b = atoi( (char *)XML_GET_CONTENT(nodo->children) );
+               }
+               nodo = nodo->next;
+       }
+       return RGB(r,g,b);
+}
+