X-Git-Url: https://git.llucax.com/z.facultad/75.42/plaqui.git/blobdiff_plain/1977f2e514c49ae6bdf411be9b08bc59be554a60..4341c033f13578b5b3aeb3e08e20d3889cb2d7c7:/Model/src/simulator.cpp diff --git a/Model/src/simulator.cpp b/Model/src/simulator.cpp index c5eb381..7811d4e 100644 --- a/Model/src/simulator.cpp +++ b/Model/src/simulator.cpp @@ -5,13 +5,18 @@ using namespace PlaQui::Model; Simulator::Simulator(const std::string &filename) { + frame = 0; /* Parseo de ejemplo de un XML desde archivo */ 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; @@ -22,9 +27,7 @@ Simulator::Simulator(const std::string &filename) 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); @@ -32,68 +35,87 @@ Simulator::Simulator(const std::string &filename) loadTank(items); } else if (xmlStrcmp(items->name, BAD_CAST"empalme")==0) { 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); } Simulator::~Simulator() { - // FIXME REMOVER TODOOOOOO + std::list::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) +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(5); + p->set_max_flow(max_flow); + p->set_color(color); 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!! - u->set_max_flow(5); + u->set_max_flow(max_flow); 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); - // FIXME no va!! - p->set_max_flow(5); + p->set_max_flow(max_flow); 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!! - p->set_max_flow(5); + p->set_max_flow(flujo); 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); - // FIXME no va!! + if (!open) + p->close(); 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); - 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); } @@ -101,7 +123,6 @@ void Simulator::add_tank(const std::string &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); } @@ -136,10 +157,16 @@ void Simulator::simulate() for(i1=pump_lst.begin(); i1!=pump_lst.end(); i1++) (*i1)->update(); - // Simulo! std::list::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++; } IConector *Simulator::find(const std::string &name) @@ -152,23 +179,45 @@ IConector *Simulator::find(const std::string &name) return NULL; } -bool Simulator::pump_deactivate(const std::string &name) +LogicControl *Simulator::find_logic(const std::string &name) { - // Busco el elemento, usando RTTI :-( - Pump *pump = dynamic_cast(find(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; +} - if (!pump) { - // Ups!, "name" no era un Pump!!! +bool Simulator::set_open(const std::string &name, bool open) +{ + // Busco el elemento, usando RTTI :-( + IConector *tmp = find(name); + Pump *p; + Exclusa *e; + if ((p = dynamic_cast(tmp))) { + if (open) { + p->activate(); + } else { + p->deactivate(); + } + } else if ((e = dynamic_cast(tmp))) { + if (open) { + e->open(); + } else { + e->close(); + } + } else { 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; + RGB color; + float flujo; nodo = nodo->children; while (nodo != NULL) { @@ -179,18 +228,23 @@ 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) ); + } 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) { + color = loadRGB(nodo->children); } } 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; + float flujo; nodo = nodo->children; while (nodo != NULL) { @@ -201,18 +255,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) ); + } else if (xmlStrcmp(nodo->name, BAD_CAST"caudal") == 0) { + flujo = atof( (char *)XML_GET_CONTENT(nodo->children) ); } + + } 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 open; nodo = nodo->children; while (nodo != NULL) { @@ -223,19 +283,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) ); + } 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! - 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; + float capacidad, inicial; + RGB color; nodo = nodo->children; while (nodo != NULL) { @@ -246,19 +310,27 @@ 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) ); + } 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) ); + } else if (xmlStrcmp(nodo->name, BAD_CAST"color") == 0) { + color = loadRGB(nodo->children); } } 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; + float flow; + std::string type; nodo = nodo->children; while (nodo != NULL) { @@ -269,35 +341,291 @@ 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) ); + } 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! - 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"); + + 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"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 (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; } - // listo, ya recolecte todos los datos, ahora creo el objeto! - add_union(name); + 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 :) + IConector *current_item, *to_connect; + xmlNodePtr props; // propiedades del item actual + xmlNodePtr conector1, conector2, conector3; + + while (nodo != NULL) { + if (nodo->type != XML_ELEMENT_NODE) { + nodo = nodo->next; + 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; + 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 y/o + 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; + } + // Fin de las conexiones +} + +std::string Simulator::get_state_as_xml() +{ + std::stringstream out; + + // XML Header + out << "" << std::endl; + + out << "" << std::endl; + + std::list::iterator i2; + for(i2=items.begin(); i2!=items.end(); i2++) + (*i2)->get_state_as_xml(out); + + std::list::iterator i3; + for(i3=control_lst.begin(); i3!=control_lst.end(); i3++) + (*i3)->get_state_as_xml(out); + out << ""; + 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(255 * (r / 65535.0f)); + g = static_cast(255 * (g / 65535.0f)); + b = static_cast(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(find((char *)XML_GET_CONTENT(nodo->children))); + if (item != NULL) { + if (dynamic_cast(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(find((char *)XML_GET_CONTENT(nodo->children))); + if (item != NULL) { + item->get_logic_input()->connect( current, IConector::IN ); + } else { + LogicControl *logic = dynamic_cast(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; + } }