]> git.llucax.com Git - z.facultad/75.42/plaqui.git/blobdiff - Model/src/simulator.cpp
Se agregan Makefiles simples para genera los PDF de los manuales :)
[z.facultad/75.42/plaqui.git] / Model / src / simulator.cpp
index 106f6e39ef69fc2f51508fb7f6c08745a738575d..7811d4eef0694d3520ac04452c42421ad986b8cb 100644 (file)
@@ -10,9 +10,13 @@ Simulator::Simulator(const std::string &filename)
        xmlDocPtr document;
        document = xmlParseFile(filename.c_str());
        if (document == NULL) {
        xmlDocPtr document;
        document = xmlParseFile(filename.c_str());
        if (document == NULL) {
+               is_load_ok = false;
+               std::cout << "Error cargando XML" << std::endl;
                return;
        }
 
                return;
        }
 
+       is_load_ok = true;
+
        /* bien, el archivo se parseo bien! */
        xmlNodePtr nodo, items;
        nodo = document->children;
        /* bien, el archivo se parseo bien! */
        xmlNodePtr nodo, items;
        nodo = document->children;
@@ -33,24 +37,39 @@ Simulator::Simulator(const std::string &filename)
                                        loadUnion(items);
                                } else if (xmlStrcmp(items->name, BAD_CAST"drenaje")==0) {
                                        loadDrain(items);
                                        loadUnion(items);
                                } else if (xmlStrcmp(items->name, BAD_CAST"drenaje")==0) {
                                        loadDrain(items);
+                               } else if (xmlStrcmp(items->name, BAD_CAST"and")==0) {
+                                       loadAnd(items);
+                               } else if (xmlStrcmp(items->name, BAD_CAST"or")==0) {
+                                       loadOr(items);
+                               } else if (xmlStrcmp(items->name, BAD_CAST"not")==0) {
+                                       loadNot(items);
                                }
                        }
                        items = items->next;
                }
                // Bien, la planta esta cargada, conectemos todo!!
                do_connections(nodo->children);
                                }
                        }
                        items = items->next;
                }
                // Bien, la planta esta cargada, conectemos todo!!
                do_connections(nodo->children);
+               do_logic_connetions(nodo->children);
        }
        }
+       xmlFreeDoc(document);
 }
 
 Simulator::~Simulator()
 {
 }
 
 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);
 }
 
 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);
        p->set_max_flow(max_flow);
        p->set_color(color);
        pump_lst.push_back(p);
@@ -60,7 +79,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);
 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);
        u->set_max_flow(max_flow);
        union_lst.push_back(u);
        items.push_back(u);
@@ -77,8 +95,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);
 void Simulator::add_conduct(const std::string &name, float flujo)
 {
        Conduct *p = new Conduct(name);
-       // FIXME no va!!
-       std::cout << flujo << std::endl;
        p->set_max_flow(flujo);
        conduct_lst.push_back(p);
        items.push_back(p);
        p->set_max_flow(flujo);
        conduct_lst.push_back(p);
        items.push_back(p);
@@ -141,9 +157,13 @@ void Simulator::simulate()
        for(i1=pump_lst.begin(); i1!=pump_lst.end(); i1++)
                (*i1)->update();
 
        for(i1=pump_lst.begin(); i1!=pump_lst.end(); i1++)
                (*i1)->update();
 
-       // Simulo!
        std::list<PlantItem *>::iterator i2;
        std::list<PlantItem *>::iterator i2;
-       for(i2=items.begin(); i2!=items.end(); i2++)
+       for(i2=items.begin(); i2!=items.end(); i2++) {
+               (*i2)->update();
+               (*i2)->update_color();
+       }
+       
+       for(i2=items.begin(); i2!=items.end(); i2++) 
                (*i2)->simulate();
 
        frame++;
                (*i2)->simulate();
 
        frame++;
@@ -159,6 +179,16 @@ IConector *Simulator::find(const std::string &name)
        return NULL;
 }
 
        return NULL;
 }
 
+LogicControl *Simulator::find_logic(const std::string &name)
+{
+       // Busco el item, aca no me importa de que tipo es!
+       std::list<LogicControl *>::iterator i;
+       for(i=control_lst.begin(); i!=control_lst.end(); i++)
+               if ((*i)->get_name() == name)
+                       return *i;
+       return NULL;
+}
+
 bool Simulator::set_open(const std::string &name, bool open)
 {
        // Busco el elemento, usando RTTI :-(
 bool Simulator::set_open(const std::string &name, bool open)
 {
        // Busco el elemento, usando RTTI :-(
@@ -201,8 +231,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) {
                        } 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;
                        }
                }
                nodo = nodo->next;
@@ -270,7 +299,7 @@ void Simulator::loadTank(xmlNodePtr nodo)
        std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
        int orientacion=0, x, y;
        float capacidad, inicial;
        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) {
 
        nodo = nodo->children;
        while (nodo != NULL) {
@@ -285,6 +314,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) );
                                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"color") == 0) {
+                               color = loadRGB(nodo->children);
                        }
                }
                nodo = nodo->next;
                        }
                }
                nodo = nodo->next;
@@ -333,6 +364,74 @@ void Simulator::loadDrain(xmlNodePtr nodo)
        add_drainage(name);
 }
 
        add_drainage(name);
 }
 
+void Simulator::loadNot(xmlNodePtr nodo)
+{
+       std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
+       std::string id = (char *)xmlGetProp(nodo, BAD_CAST"id");
+       int orientacion=0, x, y;
+
+       nodo = nodo->children;
+       while (nodo != NULL) {
+               if (nodo->type == XML_ELEMENT_NODE) {
+                       if (xmlStrcmp(nodo->name, BAD_CAST"salida") == 0) {
+                               //p->out_lines.push_back((char *)XML_GET_CONTENT(nodo->children));
+                       } else if (xmlStrcmp(nodo->name, BAD_CAST"entrada") == 0) {
+                               //p->in_lines.push_back((char *)XML_GET_CONTENT(nodo->children));
+                       }
+               }
+               nodo = nodo->next;
+       }
+
+       Not *n = new Not();
+       n->set_name(name);
+       control_lst.push_back(n);
+}
+
+void Simulator::loadOr(xmlNodePtr nodo)
+{
+       std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
+       std::string id = (char *)xmlGetProp(nodo, BAD_CAST"id");
+
+       nodo = nodo->children;
+       while (nodo != NULL) {
+               if (nodo->type == XML_ELEMENT_NODE) {
+                       if (xmlStrcmp(nodo->name, BAD_CAST"salida") == 0) {
+                               //p->out_lines.push_back((char *)XML_GET_CONTENT(nodo->children));
+                       } else if (xmlStrcmp(nodo->name, BAD_CAST"entrada") == 0) {
+                               //p->in_lines.push_back((char *)XML_GET_CONTENT(nodo->children));
+                       }
+               }
+               nodo = nodo->next;
+       }
+
+       Or *n = new Or();
+       n->set_name(name);
+       control_lst.push_back(n);
+}
+
+void Simulator::loadAnd(xmlNodePtr nodo)
+{
+       std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
+       std::string id = (char *)xmlGetProp(nodo, BAD_CAST"id");
+       int orientacion=0, x, y;
+       
+       nodo = nodo->children;
+       while (nodo != NULL) {
+               if (nodo->type == XML_ELEMENT_NODE) {
+                       if (xmlStrcmp(nodo->name, BAD_CAST"salida") == 0) {
+                       //      p->out_lines.push_back((char *)XML_GET_CONTENT(nodo->children));
+                       } else if (xmlStrcmp(nodo->name, BAD_CAST"entrada") == 0) {
+                       //      p->in_lines.push_back((char *)XML_GET_CONTENT(nodo->children));
+                       }
+               }
+               nodo = nodo->next;
+       }
+
+       And *n = new And();
+       n->set_name(name);
+       control_lst.push_back(n);
+}
+
 void Simulator::do_connections(xmlNodePtr nodo)
 {
        // Intengo conectar los elementos :)
 void Simulator::do_connections(xmlNodePtr nodo)
 {
        // Intengo conectar los elementos :)
@@ -346,6 +445,7 @@ void Simulator::do_connections(xmlNodePtr nodo)
                        continue;
                }
                // obtengo el items actual por su nombre
                        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;
                conector3 = conector2 = conector1 = NULL;
                current_item = find((char *)xmlGetProp(nodo, BAD_CAST"nombre"));
                props = nodo->children;
                conector3 = conector2 = conector1 = NULL;
@@ -439,7 +539,93 @@ std::string Simulator::get_state_as_xml()
        for(i2=items.begin(); i2!=items.end(); i2++)
                (*i2)->get_state_as_xml(out);
 
        for(i2=items.begin(); i2!=items.end(); i2++)
                (*i2)->get_state_as_xml(out);
 
+       std::list<LogicControl *>::iterator i3;
+       for(i3=control_lst.begin(); i3!=control_lst.end(); i3++)
+               (*i3)->get_state_as_xml(out);
        out << "</plantstatus>";
        return out.str();;
 }
 
        out << "</plantstatus>";
        return out.str();;
 }
 
+RGB Simulator::loadRGB(xmlNodePtr nodo)
+{
+       unsigned long 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;
+       }
+       r = static_cast<unsigned long>(255 * (r / 65535.0f));
+       g = static_cast<unsigned long>(255 * (g / 65535.0f));
+       b = static_cast<unsigned long>(255 * (b / 65535.0f));
+
+       return RGB(r,g,b);
+}
+
+void Simulator::do_logic_connetions(xmlNodePtr nodo)
+{
+       LogicControl *current;
+
+       while (nodo != NULL) {
+               if (nodo->type != XML_ELEMENT_NODE) {
+                       nodo = nodo->next;
+                       continue;
+               }
+               // obtengo el items actual por su nombre
+               if ((xmlStrcmp(nodo->name, BAD_CAST"and") == 0) ||  (xmlStrcmp(nodo->name, BAD_CAST"not") == 0) || (xmlStrcmp(nodo->name, BAD_CAST"or") == 0)) {
+                       std::string s = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
+                       current = find_logic((char *)xmlGetProp(nodo, BAD_CAST"nombre"));
+                       connect_logic(current, nodo->children);
+               }
+               nodo = nodo->next;
+       }
+}
+
+void Simulator::connect_logic(LogicControl *current, xmlNodePtr nodo)
+{
+       Control *item;
+       while (nodo != NULL) {
+               if (nodo->type != XML_ELEMENT_NODE) {
+                       nodo = nodo->next;
+                       continue;
+               }
+               if (xmlStrcmp(nodo->name, BAD_CAST"entrada") == 0) {
+                       item = dynamic_cast<Control *>(find((char *)XML_GET_CONTENT(nodo->children)));
+                       if (item != NULL) {
+                               if (dynamic_cast<Tank *>(item)) {
+                                       std::string donde = (char *)xmlGetProp(nodo, BAD_CAST"id");
+                                       if (donde == "inferior") {
+                                               current->connect( item->get_logic_output(), IConector::IN );
+                                       } else {
+                                               current->connect( item->get_logic_input(), IConector::IN );
+                                       }
+                               } else {
+                                       current->connect( item->get_logic_output(), IConector::IN );
+                               }
+                       } else {
+                               std::cout << "ERROR : Item no es tipo Control!!" << std::endl;
+                       }
+               } else if (xmlStrcmp(nodo->name, BAD_CAST"salida") == 0) {
+                       item = dynamic_cast<Control *>(find((char *)XML_GET_CONTENT(nodo->children)));
+                       if (item != NULL) {
+                               item->get_logic_input()->connect( current, IConector::IN );
+                       } else {
+                               LogicControl *logic = dynamic_cast<LogicControl *>(find_logic((char *)XML_GET_CONTENT(nodo->children)));
+                               /* Bien, si no era un control, debe ser una compuerta */
+                               if (logic != NULL) {
+                                       logic->connect(current, IConector::IN);
+                               } else {
+                                       std::cout << "ERROR : Item no es tipo Control ni tipo LogicControl ... esto si que esta todo malllll!!" << std::endl;
+                               }
+                       }
+               }
+
+               nodo = nodo->next;
+       }
+}
+