]> git.llucax.com Git - z.facultad/75.42/plaqui.git/blobdiff - Model/src/simulator.cpp
se mejora la conexion entre compuertas, se salvan en el XML, hay un par de cosas...
[z.facultad/75.42/plaqui.git] / Model / src / simulator.cpp
index b6a77d3bc68922ba1729f686091b39eaa001dd2e..9e84e8a257559f093b83e56fbd43000bcd2ae77f 100644 (file)
@@ -37,12 +37,19 @@ 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);
 }
        }
        xmlFreeDoc(document);
 }
@@ -170,6 +177,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 :-(
@@ -345,6 +362,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 :)
@@ -401,19 +486,11 @@ 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
                                // 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
-                               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;
-                               }
+                               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));
                        } 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));
-                               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;
-                               }
+                               current_item->connect(to_connect, IConector::OUT);
                        }
                }
                if (conector2 != NULL) {
                        }
                }
                if (conector2 != NULL) {
@@ -422,17 +499,11 @@ 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
                                // 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
-                               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;
-                               }
+                               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));
                        } 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));
-                               if (!current_item->connect(to_connect, IConector::OUT)) {
-                                       std::cout << s << " Error al conectar " << std::endl;
-                               }
+                               current_item->connect(to_connect, IConector::OUT);
                        }
                }
                if (conector3 != NULL) {
                        }
                }
                if (conector3 != NULL) {
@@ -491,3 +562,50 @@ RGB Simulator::loadRGB(xmlNodePtr nodo)
        return RGB(r,g,b);
 }
 
        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) {
+                       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) {
+                               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 {
+                               std::cout << "ERROR : Item no es tipo Control!!" << std::endl;
+                       }
+               }
+
+               nodo = nodo->next;
+       }
+}
+