4 using namespace PlaQui::Model;
6 Simulator::Simulator(const std::string &filename)
9 /* Parseo de ejemplo de un XML desde archivo */
11 document = xmlParseFile(filename.c_str());
12 if (document == NULL) {
16 /* bien, el archivo se parseo bien! */
17 xmlNodePtr nodo, items;
18 nodo = document->children;
20 if (strcmp((char *)nodo->name, "planta") == 0) {
21 items = nodo->children;
22 while (items != NULL) {
23 if (items->type == XML_ELEMENT_NODE) {
24 if (xmlStrcmp(items->name, BAD_CAST"bomba")==0) {
26 } else if (xmlStrcmp(items->name, BAD_CAST"codo")==0) {
28 } else if (xmlStrcmp(items->name, BAD_CAST"tubo")==0) {
30 } else if (xmlStrcmp(items->name, BAD_CAST"exclusa")==0) {
32 } else if (xmlStrcmp(items->name, BAD_CAST"tanque")==0) {
34 } else if (xmlStrcmp(items->name, BAD_CAST"empalme")==0) {
43 Simulator::~Simulator()
45 // FIXME REMOVER TODOOOOOO
48 void Simulator::add_pump(const std::string &name)
50 Pump *p = new Pump(name);
53 pump_lst.push_back(p);
57 void Simulator::add_union(const std::string &name)
59 Union *u = new Union(name);
62 union_lst.push_back(u);
66 void Simulator::add_splitter(const std::string &name)
68 Splitter *p = new Splitter(name);
71 split_lst.push_back(p);
75 void Simulator::add_conduct(const std::string &name)
77 Conduct *p = new Conduct(name);
80 conduct_lst.push_back(p);
84 void Simulator::add_exclusa(const std::string &name)
86 Exclusa *p = new Exclusa(name);
88 exclusa_lst.push_back(p);
92 void Simulator::add_tank(const std::string &name)
94 Tank *p = new Tank(name);
98 tank_lst.push_back(p);
102 void Simulator::add_drainage(const std::string &name)
104 Drainage *p = new Drainage(name);
106 drainage_lst.push_back(p);
110 bool Simulator::connect(const std::string &name1, const std::string &name2, int flag)
116 if ((o1 == NULL) || (o2 == NULL)) {
117 // NO SE PUDO CONECTAR!, FALTAN ITEMS!!
122 if (flag == IConector::OUT) {
123 b = o1->connect(o2, IConector::OUT);
124 b = b && o2->connect(o1, IConector::IN);
126 b = o1->connect(o2, IConector::IN);
127 b = b && o2->connect(o1, IConector::OUT);
133 void Simulator::simulate()
136 std::list<Pump *>::iterator i1;
137 for(i1=pump_lst.begin(); i1!=pump_lst.end(); i1++)
141 std::list<PlantItem *>::iterator i2;
142 for(i2=items.begin(); i2!=items.end(); i2++)
148 IConector *Simulator::find(const std::string &name)
150 // Busco el item, aca no me importa de que tipo es!
151 std::list<PlantItem *>::iterator i;
152 for(i=items.begin(); i!=items.end(); i++)
153 if ((*i)->get_name() == name)
158 bool Simulator::pump_deactivate(const std::string &name)
160 // Busco el elemento, usando RTTI :-(
161 Pump *pump = dynamic_cast<Pump *>(find(name));
164 // Ups!, "name" no era un Pump!!!
171 void Simulator::loadBomba(xmlNodePtr nodo)
173 std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
174 int orientacion=0, x, y;
176 nodo = nodo->children;
177 while (nodo != NULL) {
178 if (nodo->type == XML_ELEMENT_NODE) {
179 if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
180 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
181 } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
182 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
183 } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
184 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
193 void Simulator::loadCodo(xmlNodePtr nodo)
195 std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
196 int orientacion=0, x, y;
198 nodo = nodo->children;
199 while (nodo != NULL) {
200 if (nodo->type == XML_ELEMENT_NODE) {
201 if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
202 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
203 } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
204 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
205 } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
206 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
215 void Simulator::loadConduct(xmlNodePtr nodo)
217 std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
218 int orientacion=0, x, y;
220 nodo = nodo->children;
221 while (nodo != NULL) {
222 if (nodo->type == XML_ELEMENT_NODE) {
223 if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
224 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
225 } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
226 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
227 } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
228 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
234 // listo, ya recolecte todos los datos, ahora creo el objeto!
238 void Simulator::loadExclusa(xmlNodePtr nodo)
240 std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
241 int orientacion=0, x, y;
243 nodo = nodo->children;
244 while (nodo != NULL) {
245 if (nodo->type == XML_ELEMENT_NODE) {
246 if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
247 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
248 } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
249 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
250 } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
251 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
257 // listo, ya recolecte todos los datos, ahora creo el objeto!
261 void Simulator::loadTank(xmlNodePtr nodo)
263 std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
264 int orientacion=0, x, y;
266 nodo = nodo->children;
267 while (nodo != NULL) {
268 if (nodo->type == XML_ELEMENT_NODE) {
269 if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
270 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
271 } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
272 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
273 } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
274 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
280 // listo, ya recolecte todos los datos, ahora creo el objeto!
284 void Simulator::loadUnion(xmlNodePtr nodo)
286 std::string name = (char *)xmlGetProp(nodo, BAD_CAST"nombre");
287 int orientacion=0, x, y;
289 nodo = nodo->children;
290 while (nodo != NULL) {
291 if (nodo->type == XML_ELEMENT_NODE) {
292 if (xmlStrcmp(nodo->name, BAD_CAST"orientacion") == 0) {
293 orientacion = atoi( (char *)XML_GET_CONTENT(nodo->children) );
294 } else if (xmlStrcmp(nodo->name, BAD_CAST"x") == 0) {
295 x = atoi( (char *)XML_GET_CONTENT(nodo->children) );
296 } else if (xmlStrcmp(nodo->name, BAD_CAST"y") == 0) {
297 y = atoi( (char *)XML_GET_CONTENT(nodo->children) );
303 // listo, ya recolecte todos los datos, ahora creo el objeto!
308 std::string Simulator::get_state_as_xml()
310 std::stringstream out;
313 out << "<?xml version=\"1.0\" ?>" << std::endl;
315 out << "<plantstatus frame=\"" << frame << "\">" << std::endl;
317 std::list<PlantItem *>::iterator i2;
318 for(i2=items.begin(); i2!=items.end(); i2++)
319 (*i2)->get_state_as_xml(out);
321 out << "</plantstatus>";