From bd2c5ebb465f2265d618feefb66479e5259dcfaa Mon Sep 17 00:00:00 2001 From: Ricardo Markiewicz Date: Sun, 30 Nov 2003 04:49:38 +0000 Subject: [PATCH] * Se agrega la logica del tanque para los flotantes * Se hace que compilen las logicas al ser incluidas al proyecto * Se cargan compuertas logicas desde el XML en el modelo y se utilizan para simular (creo que la AND era la que hice) MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Bueno, la Not y la Or es trivial agregarla y la voy a hacer mañana porque me mata el sueño. La AND esta andando bien, pero por alguna razon el modelo no lo refleja. Mañana reviso el parser, ya que el XML es correcto. --- Client/src/principal.cpp | 1 + Model/include/and.h | 10 +-- Model/include/logiccontrol.h | 6 ++ Model/include/not.h | 5 +- Model/include/or.h | 10 +-- Model/include/simulator.h | 10 +++ Model/include/tank.h | 1 + Model/src/main.cpp | 9 +- Model/src/pump.cpp | 1 + Model/src/simulator.cpp | 154 +++++++++++++++++++++++++++++++---- Model/src/tank.cpp | 7 ++ 11 files changed, 181 insertions(+), 33 deletions(-) diff --git a/Client/src/principal.cpp b/Client/src/principal.cpp index 845de81..f9cb07f 100644 --- a/Client/src/principal.cpp +++ b/Client/src/principal.cpp @@ -846,6 +846,7 @@ bool Principal::get_bool_from_xml(xmlNodePtr nodo) } nodo = nodo->next; } + std::cout << "AAACCCCAA : " << tmp << std::endl; return tmp == "true"; } diff --git a/Model/include/and.h b/Model/include/and.h index 7bd1a89..482576b 100644 --- a/Model/include/and.h +++ b/Model/include/and.h @@ -4,15 +4,15 @@ #include "logiccontrol.h" #include -#include +#include namespace PlaQui { namespace Model { /** Función AND Lógica */ -class And:LogicControl { +class And:public LogicControl { public: - And():LogicControl(MAX_INT,1) {} + And():LogicControl(INT_MAX,1) {} virtual ~And() {} virtual bool get_output() { @@ -21,8 +21,8 @@ public: /* Si tengo alguien conectado */ std::list::iterator it; for(it = in_list.begin(); it != in_list.end(); it++) { - mi_entrada = *it; - mi_salida &&= mi_entrada->get_output(); + mi_entrada = static_cast(*it); + mi_salida = mi_salida && mi_entrada->get_output(); } return mi_salida; } diff --git a/Model/include/logiccontrol.h b/Model/include/logiccontrol.h index 76f7174..3a56c65 100644 --- a/Model/include/logiccontrol.h +++ b/Model/include/logiccontrol.h @@ -3,6 +3,7 @@ #define _LOGIC_CONTROL_H_ #include "iconector.h" +#include namespace PlaQui { namespace Model { @@ -40,6 +41,11 @@ public: * \see Exclusa::update */ bool is_operational() { return (in_list.begin() != in_list.end()); } + + void set_name(const std::string &_name) { name = _name; } + std::string get_name() { return name; } +protected: + std::string name; }; } diff --git a/Model/include/not.h b/Model/include/not.h index 5e74940..0f888bb 100644 --- a/Model/include/not.h +++ b/Model/include/not.h @@ -8,7 +8,8 @@ namespace PlaQui { namespace Model { -class Not:LogicControl { +class Not:public LogicControl { +public: Not():LogicControl(1,1) {} virtual ~Not() {} @@ -24,7 +25,7 @@ class Not:LogicControl { std::cout << "NOT NO CONECTADO" << std::endl; return false; } -} +}; } } diff --git a/Model/include/or.h b/Model/include/or.h index 0bbc22c..b7d6d0c 100644 --- a/Model/include/or.h +++ b/Model/include/or.h @@ -4,15 +4,15 @@ #include "logiccontrol.h" #include -#include +#include namespace PlaQui { namespace Model { /** Función O Lógica */ -class Or:LogicControl { +class Or:public LogicControl { public: - Or():LogicControl(MAX_INT,1) {} + Or():LogicControl(INT_MAX,1) {} virtual ~Or() {} virtual bool get_output() { @@ -21,8 +21,8 @@ public: /* Si tengo alguien conectado */ std::list::iterator it; for(it = in_list.begin(); it != in_list.end(); it++) { - mi_entrada = *it; - mi_salida ||= mi_entrada->get_output(); + mi_entrada = static_cast(*it); + mi_salida = mi_entrada || mi_entrada->get_output(); } return mi_salida; } diff --git a/Model/include/simulator.h b/Model/include/simulator.h index 3802602..12f0ba0 100644 --- a/Model/include/simulator.h +++ b/Model/include/simulator.h @@ -15,6 +15,9 @@ #include "drainage.h" #include "iconector.h" #include "libxml/parser.h" +#include "not.h" +#include "or.h" +#include "and.h" namespace PlaQui { namespace Model { @@ -64,15 +67,19 @@ protected: std::list tank_lst; std::list drainage_lst; std::list exclusa_lst; + std::list control_lst; // Tambien tengo una lista generica! std::list items; // Utilidades IConector *find(const std::string &name); + LogicControl *find_logic(const std::string &name); // Conecta todos los items desde el XML void do_connections(xmlNodePtr nodo); + void do_logic_connetions(xmlNodePtr nodo); + void connect_logic(LogicControl *current, xmlNodePtr); // Carga del XML void loadBomba(xmlNodePtr nodo); @@ -81,6 +88,9 @@ protected: void loadTank(xmlNodePtr nodo); void loadUnion(xmlNodePtr nodo); void loadDrain(xmlNodePtr nodo); + void loadNot(xmlNodePtr nodo); + void loadOr(xmlNodePtr nodo); + void loadAnd(xmlNodePtr nodo); RGB loadRGB(xmlNodePtr nodo); diff --git a/Model/include/tank.h b/Model/include/tank.h index 652955b..9220241 100644 --- a/Model/include/tank.h +++ b/Model/include/tank.h @@ -32,6 +32,7 @@ public: virtual void simulate(); void set_litros(float l) { litros = l; } + float get_litros() { return litros; } void get_state_as_xml(std::stringstream &out); protected: float litros; ///< cantidad de líquido actual diff --git a/Model/src/main.cpp b/Model/src/main.cpp index e6e52b5..8671daa 100644 --- a/Model/src/main.cpp +++ b/Model/src/main.cpp @@ -19,11 +19,14 @@ int main(int argc, char *argv[]) Simulator *sim = new Simulator(argv[1]); int i=0; - while (i<2) { + while (i<5) { + if (i==1) { + sim->set_open("exclusa6", false); + std::cout << "apagando" << std::endl; + } + i++; sim->simulate(); - std::cout << sim->get_state_as_xml() << std::endl << std::endl; - i++; } delete sim; diff --git a/Model/src/pump.cpp b/Model/src/pump.cpp index d42bc58..93b8b72 100644 --- a/Model/src/pump.cpp +++ b/Model/src/pump.cpp @@ -27,6 +27,7 @@ void Pump::update(int dir) if (updated) return; // Me fijo si me tengo que apagar automaticamente open = input->get_output(); + std::cout << name << " open = " << (open?"true":"false") << std::endl; if (active && open) actual_flow = max_flow; else diff --git a/Model/src/simulator.cpp b/Model/src/simulator.cpp index b6a77d3..9e84e8a 100644 --- a/Model/src/simulator.cpp +++ b/Model/src/simulator.cpp @@ -37,12 +37,19 @@ Simulator::Simulator(const std::string &filename) 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); + do_logic_connetions(nodo->children); } xmlFreeDoc(document); } @@ -170,6 +177,16 @@ IConector *Simulator::find(const std::string &name) return NULL; } +LogicControl *Simulator::find_logic(const std::string &name) +{ + // Busco el item, aca no me importa de que tipo es! + std::list::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 :-( @@ -345,6 +362,74 @@ void Simulator::loadDrain(xmlNodePtr nodo) 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 :) @@ -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 - 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)); - 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) { @@ -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 - 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)); - 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) { @@ -491,3 +562,50 @@ RGB Simulator::loadRGB(xmlNodePtr nodo) 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(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(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; + } +} + diff --git a/Model/src/tank.cpp b/Model/src/tank.cpp index 3f2fe43..4abbc6b 100644 --- a/Model/src/tank.cpp +++ b/Model/src/tank.cpp @@ -1,16 +1,23 @@ #include "tank.h" #include +#include "condition.h" using namespace PlaQui::Model; Tank::Tank(const std::string &_name):Source(_name),Drain(_name),Control(_name) { litros = 0.0f; + + /* Utilizo en input como el flotante de arriba y el output como el de abajo */ + input = new Condition(Condition::GT, 0.9, this); + output = new Condition(Condition::LT, 0.1, this); } Tank::~Tank() { + delete input; + delete output; } bool Tank::get_output() -- 2.43.0