1 | /* |
---|
2 | * conv_f8tof1.cpp |
---|
3 | * L-systemToF1 |
---|
4 | * |
---|
5 | * Created by Maciej Wajcht on 08-03-21. |
---|
6 | * Copyright 2008 __MyCompanyName__. All rights reserved. |
---|
7 | * |
---|
8 | */ |
---|
9 | |
---|
10 | #include "conv_f8tof1.h" |
---|
11 | #include "conv_f8_utils.h" |
---|
12 | #include <sstream> |
---|
13 | #include <string> |
---|
14 | #include <algorithm> |
---|
15 | #include "lexglobal.h" |
---|
16 | #include "conv_f8tof1_scanner.h" |
---|
17 | namespace prs { |
---|
18 | #include "conv_f8tof1_grammar.c" |
---|
19 | } |
---|
20 | |
---|
21 | #if CONV_DEBUG > 0 |
---|
22 | #include <iostream> |
---|
23 | #endif |
---|
24 | |
---|
25 | #define CONV_DEBUG 0 //0 - off, 1 - minimal, 2 - full |
---|
26 | |
---|
27 | using namespace std; |
---|
28 | |
---|
29 | Lsystem::Lsystem() { |
---|
30 | PrimitiveProduction *pp1 = new PrimitiveProduction(SString("X")); |
---|
31 | this->primitiveProductions.insert(make_pair("X", pp1)); |
---|
32 | PrimitiveProduction *pp2 = new PrimitiveProduction(SString("R")); |
---|
33 | this->primitiveProductions.insert(make_pair("R", pp2)); |
---|
34 | PrimitiveProduction *pp3 = new PrimitiveProduction(SString("r")); |
---|
35 | this->primitiveProductions.insert(make_pair("r", pp3)); |
---|
36 | PrimitiveProduction *pp4 = new PrimitiveProduction(SString("C")); |
---|
37 | this->primitiveProductions.insert(make_pair("C", pp4)); |
---|
38 | PrimitiveProduction *pp5 = new PrimitiveProduction(SString("c")); |
---|
39 | this->primitiveProductions.insert(make_pair("c", pp5)); |
---|
40 | PrimitiveProduction *pp6 = new PrimitiveProduction(SString("Q")); |
---|
41 | this->primitiveProductions.insert(make_pair("Q", pp6)); |
---|
42 | PrimitiveProduction *pp7 = new PrimitiveProduction(SString("q")); |
---|
43 | this->primitiveProductions.insert(make_pair("q", pp7)); |
---|
44 | PrimitiveProduction *pp8 = new PrimitiveProduction(SString("[")); |
---|
45 | this->primitiveProductions.insert(make_pair("[", pp8)); |
---|
46 | PrimitiveProduction *pp9 = new PrimitiveProduction(SString("]")); |
---|
47 | this->primitiveProductions.insert(make_pair("]", pp9)); |
---|
48 | PrimitiveProduction *pp10 = new PrimitiveProduction(SString("^")); |
---|
49 | this->primitiveProductions.insert(make_pair("^", pp10)); |
---|
50 | |
---|
51 | } |
---|
52 | |
---|
53 | Lsystem::~Lsystem() { |
---|
54 | for (map<string, Production*>::iterator iter = this->productions.begin(); |
---|
55 | iter != this->productions.end(); iter++) { |
---|
56 | delete iter->second; |
---|
57 | } |
---|
58 | for (map<string, PrimitiveProduction*>::iterator iter = this->primitiveProductions.begin(); |
---|
59 | iter != this->primitiveProductions.end(); iter++) { |
---|
60 | delete iter->second; |
---|
61 | } |
---|
62 | for (map<string, ParamProduction*>::iterator iter = this->paramProductions.begin(); |
---|
63 | iter != this->paramProductions.end(); iter++) { |
---|
64 | delete iter->second; |
---|
65 | } |
---|
66 | for (vector<NeuronProduction*>::iterator iter = this->neuronProductions.begin(); |
---|
67 | iter != this->neuronProductions.end(); iter++) { |
---|
68 | delete *iter; |
---|
69 | } |
---|
70 | } |
---|
71 | |
---|
72 | PrimitiveProduction* Lsystem::getPrimitiveProduction(SString name) { |
---|
73 | string sname = sstringToString(name); |
---|
74 | if (this->primitiveProductions.find(sname) == this->primitiveProductions.end()) { |
---|
75 | PrimitiveProduction *pp = new PrimitiveProduction(name); |
---|
76 | this->primitiveProductions.insert(make_pair(sname, pp)); |
---|
77 | } |
---|
78 | return this->primitiveProductions[sname]; |
---|
79 | } |
---|
80 | |
---|
81 | ParamProduction* Lsystem::getParamProduction(SString name) { |
---|
82 | string sname = sstringToString(name); |
---|
83 | if (this->paramProductions.find(sname) == this->paramProductions.end()) { |
---|
84 | ParamProduction *pp = new ParamProduction(name); |
---|
85 | this->paramProductions.insert(make_pair(sname, pp)); |
---|
86 | } |
---|
87 | return this->paramProductions[sname]; |
---|
88 | } |
---|
89 | |
---|
90 | SString Lsystem::toString() { |
---|
91 | this->removeEmptySubproductionsAndProductions(); |
---|
92 | SString result = ""; |
---|
93 | result += SString::valueOf(this->iterations) + "\n"; |
---|
94 | for (map<string, double>::iterator it = this->startParams.begin(); |
---|
95 | it != this->startParams.end(); it++) { |
---|
96 | result += stringToSString(it->first) + "=" + SString::valueOf(it->second) + "\n"; |
---|
97 | } |
---|
98 | result += SString("---") + "\n"; |
---|
99 | result += stringToSString(this->firstProductionName) + "\n"; |
---|
100 | for (map<string, Production*>::iterator prodIter = this->productions.begin(); |
---|
101 | prodIter != this->productions.end(); prodIter++) { |
---|
102 | Production *p = prodIter->second; |
---|
103 | result += p->name + "("; |
---|
104 | for (int i = 1; i <= p->parameters.size(); i++) { |
---|
105 | result += p->parameters.getParameterName(i); |
---|
106 | if (i < p->parameters.size()) { |
---|
107 | result += ","; |
---|
108 | } |
---|
109 | } |
---|
110 | result += "):"; |
---|
111 | for (int subprodIter = 0; subprodIter < p->subproductions.size(); subprodIter++) { |
---|
112 | SubProduction *sp = &(p->subproductions[subprodIter]); |
---|
113 | for (int condIter = 0; condIter < sp->conditions.size(); condIter++) { |
---|
114 | Condition *c = &(sp->conditions[condIter]); |
---|
115 | RelationType r = c->relation; |
---|
116 | SString op = (r == r_greater) ? ">" : (r == r_greaterEqual) ? ">=" : (r == r_less) ? "<" : |
---|
117 | (r == r_lessEqual) ? "<=" : (r == r_equal) ? "==" : (r == r_different) ? "!=" : ""; |
---|
118 | result += c->parameter + op + SString::valueOf(c->value); |
---|
119 | if (condIter != sp->conditions.size() - 1) { |
---|
120 | result += ","; |
---|
121 | } |
---|
122 | } |
---|
123 | if (sp->conditions.size() > 0) { |
---|
124 | result += SString("|"); |
---|
125 | } |
---|
126 | for (vector<ActionStrP>::iterator actionIter = sp->actions.begin(); |
---|
127 | actionIter != sp->actions.end(); actionIter++) { |
---|
128 | ActionStrP *a = &(*actionIter); |
---|
129 | if (a->action == NULL) { |
---|
130 | continue; |
---|
131 | } |
---|
132 | result += a->action->getF8Representation(); |
---|
133 | if (!a->action->ignoreParams) { |
---|
134 | result += SString("("); |
---|
135 | for (int paramIter = 0; paramIter < a->params.size(); paramIter++) { |
---|
136 | result += convertReversePolishNotationToNatural(a->params[paramIter]); |
---|
137 | if (paramIter != a->params.size() - 1) { |
---|
138 | result += ","; |
---|
139 | } |
---|
140 | } |
---|
141 | result += SString(")"); |
---|
142 | } |
---|
143 | } |
---|
144 | if (subprodIter != p->subproductions.size() - 1) { |
---|
145 | result += SString(":"); |
---|
146 | } |
---|
147 | } |
---|
148 | result += SString("\n"); |
---|
149 | } |
---|
150 | return result; |
---|
151 | } |
---|
152 | |
---|
153 | vector<Action*> Lsystem::getAllActions(bool normal, bool primitives, bool params, bool neurons) { |
---|
154 | vector<Action*> actions; |
---|
155 | if (normal) { |
---|
156 | for (map<string, Production*>::iterator iter = this->productions.begin(); |
---|
157 | iter != this->productions.end(); iter++) { |
---|
158 | actions.push_back(iter->second); |
---|
159 | } |
---|
160 | } |
---|
161 | if (primitives) { |
---|
162 | for (map<string, PrimitiveProduction*>::iterator iter = this->primitiveProductions.begin(); |
---|
163 | iter != this->primitiveProductions.end(); iter++) { |
---|
164 | actions.push_back(iter->second); |
---|
165 | } |
---|
166 | } |
---|
167 | if (params) { |
---|
168 | for (map<string, ParamProduction*>::iterator iter = this->paramProductions.begin(); |
---|
169 | iter != this->paramProductions.end(); iter++) { |
---|
170 | actions.push_back(iter->second); |
---|
171 | } |
---|
172 | } |
---|
173 | if (neurons) { |
---|
174 | for (vector<NeuronProduction*>::iterator iter = this->neuronProductions.begin(); |
---|
175 | iter != this->neuronProductions.end(); iter++) { |
---|
176 | actions.push_back(*iter); |
---|
177 | } |
---|
178 | } |
---|
179 | return actions; |
---|
180 | } |
---|
181 | |
---|
182 | void Lsystem::removeEmptySubproductionsAndProductions() { |
---|
183 | /* po wykonaniu moga pozostac puste subprodukcje (usuwanie wywolan do usunietych wczesniej produkcji |
---|
184 | * ale i tak znikna one przy kolejnym wywolaniu, tak wiec ilosc smieci nie powinna byc razaca |
---|
185 | */ |
---|
186 | |
---|
187 | //delete empty subproductions |
---|
188 | for (map<string, Production*>::iterator prodIter = this->productions.begin(); |
---|
189 | prodIter != this->productions.end(); prodIter++) { |
---|
190 | vector<SubProduction> *subproductions = &(prodIter->second->subproductions); |
---|
191 | for (vector<SubProduction>::iterator i = subproductions->begin(); i != subproductions->end(); /*nothing*/) { |
---|
192 | if ((*i).actions.size() == 0) { |
---|
193 | i = subproductions->erase(i); |
---|
194 | } else { |
---|
195 | i++; |
---|
196 | } |
---|
197 | } |
---|
198 | } |
---|
199 | |
---|
200 | //find empty productions |
---|
201 | vector<SString> emptyProductionNames; |
---|
202 | for (map<string, Production*>::iterator prodIter = this->productions.begin(); |
---|
203 | prodIter != this->productions.end(); prodIter++) { |
---|
204 | if (prodIter->second->subproductions.size() == 0 && |
---|
205 | sstringToString(prodIter->second->name) != this->firstProductionName) { |
---|
206 | emptyProductionNames.push_back(prodIter->second->name); |
---|
207 | } |
---|
208 | } |
---|
209 | |
---|
210 | //delete calls to empty productions |
---|
211 | for (map<string, Production*>::iterator prodIter = this->productions.begin(); |
---|
212 | prodIter != this->productions.end(); prodIter++) { |
---|
213 | vector<SubProduction> *subproductions = &(prodIter->second->subproductions); |
---|
214 | for (vector<SubProduction>::iterator subProdIter = subproductions->begin(); |
---|
215 | subProdIter != subproductions->end(); subProdIter++) { |
---|
216 | SubProduction *sp = &(*subProdIter); |
---|
217 | for (vector<ActionStrP>::iterator actionIter = sp->actions.begin(); |
---|
218 | actionIter != sp->actions.end(); /*nothing*/) { |
---|
219 | bool deleted = false; |
---|
220 | if ((*actionIter).action != NULL) { |
---|
221 | vector<SString>::iterator result = find(emptyProductionNames.begin(), emptyProductionNames.end(), |
---|
222 | (*actionIter).action->name); |
---|
223 | if (result != emptyProductionNames.end()) { //emptyProductionNames contains the action name |
---|
224 | actionIter = sp->actions.erase(actionIter); |
---|
225 | deleted = true; |
---|
226 | } |
---|
227 | } |
---|
228 | if (!deleted) { |
---|
229 | actionIter++; |
---|
230 | } |
---|
231 | } |
---|
232 | } |
---|
233 | } |
---|
234 | |
---|
235 | //delete empty productions |
---|
236 | for(int i = 0; i < emptyProductionNames.size(); i++) { |
---|
237 | this->productions.erase(sstringToString(emptyProductionNames[i])); |
---|
238 | } |
---|
239 | } |
---|
240 | |
---|
241 | ostream& operator<<(ostream& os, const Condition& c) { |
---|
242 | RelationType r = c.relation; |
---|
243 | SString op = (r == r_greater) ? ">" : (r == r_greaterEqual) ? ">=" : (r == r_less) ? "<" : |
---|
244 | (r == r_lessEqual) ? "<=" : (r == r_equal) ? "==" : (r == r_different) ? "!=" : ""; |
---|
245 | os << c.parameter << " " << op << " " << c.value; |
---|
246 | return os; |
---|
247 | } |
---|
248 | |
---|
249 | const double ParameterCollection::getValue(int position) { |
---|
250 | string name = this->parameters[position - 1]; |
---|
251 | return this->paramValues[name]; |
---|
252 | } |
---|
253 | |
---|
254 | const double ParameterCollection::getValue(SString name) { |
---|
255 | return this->paramValues[sstringToString(name)]; |
---|
256 | } |
---|
257 | |
---|
258 | const SString ParameterCollection::getParameterName(int position) { |
---|
259 | return stringToSString(this->parameters[position - 1]); |
---|
260 | } |
---|
261 | |
---|
262 | const int ParameterCollection::getParameterPosition(SString name) { |
---|
263 | for (int i = 0; i < this->parameters.size(); i++) { |
---|
264 | string &s = this->parameters[i]; |
---|
265 | if (s == sstringToString(name)) { |
---|
266 | return i + 1; |
---|
267 | } |
---|
268 | } |
---|
269 | return -1; |
---|
270 | } |
---|
271 | |
---|
272 | void ParameterCollection::setValue(int position, double value) { |
---|
273 | string name = this->parameters[position - 1]; |
---|
274 | this->paramValues[name] = value; |
---|
275 | } |
---|
276 | |
---|
277 | void ParameterCollection::setValue(SString name, double value) { |
---|
278 | this->paramValues[sstringToString(name)] = value; |
---|
279 | } |
---|
280 | |
---|
281 | void ParameterCollection::addParameter(SString name, int position, double value) { |
---|
282 | //TODO sprawdzenie czy position > 0 |
---|
283 | string sname = sstringToString(name); |
---|
284 | if (position == -1) { |
---|
285 | this->parameters.push_back(sname); |
---|
286 | this->paramValues[sname] = value; |
---|
287 | } else { |
---|
288 | this->parameters.reserve(position); |
---|
289 | this->parameters.insert(this->parameters.begin() + (position -1), sname); |
---|
290 | this->paramValues[sname] = value; |
---|
291 | } |
---|
292 | //this->paramValues[name] = value; |
---|
293 | } |
---|
294 | |
---|
295 | const int ParameterCollection::size() { |
---|
296 | return this->parameters.size(); |
---|
297 | } |
---|
298 | |
---|
299 | void ParameterCollection::removeParameter(int position) { |
---|
300 | string name = this->parameters[position - 1]; |
---|
301 | vector<string>::iterator it = this->parameters.begin() + (position - 1); |
---|
302 | this->parameters.erase(it); |
---|
303 | this->paramValues.erase(name); |
---|
304 | } |
---|
305 | |
---|
306 | void ParameterCollection::removeParameter(SString name) { |
---|
307 | int idx = getParameterPosition(name); |
---|
308 | this->removeParameter(idx); |
---|
309 | } |
---|
310 | |
---|
311 | bool ParameterCollection::paramExist(SString name) { |
---|
312 | string sname = sstringToString(name); |
---|
313 | for (vector<string>::iterator it = this->parameters.begin(); it != this->parameters.end(); it++) { |
---|
314 | if ((*it) == sname) { |
---|
315 | return true; |
---|
316 | } |
---|
317 | } |
---|
318 | return false; |
---|
319 | } |
---|
320 | |
---|
321 | PrimitiveProduction::PrimitiveProduction(const SString command) { |
---|
322 | this->f8command = command; |
---|
323 | this->ignoreParams = true; |
---|
324 | if (command == SString("[")) { |
---|
325 | this->f1command = SString("("); |
---|
326 | this->name = SString("_primitive production '[' => '('"); |
---|
327 | } else if (command == SString("]")) { |
---|
328 | this->f1command = SString(")"); |
---|
329 | this->name = SString("_primitive production ']' => ')'"); |
---|
330 | } else if (command == SString("^")) { |
---|
331 | this->f1command = SString(","); |
---|
332 | this->name = SString("_primitive production '^' => ','"); |
---|
333 | } else { |
---|
334 | this->f1command = command; |
---|
335 | this->name = SString("_primitive production '") + command + "'"; |
---|
336 | } |
---|
337 | } |
---|
338 | |
---|
339 | const SString PrimitiveProduction::getF1Genotype(const vector<double> params) { |
---|
340 | #if CONV_DEBUG > 1 |
---|
341 | cout << "@@@@@ Prymityw: " << this->f1command << endl; |
---|
342 | #endif |
---|
343 | return SString(this->f1command); |
---|
344 | } |
---|
345 | |
---|
346 | const list<ActionP> PrimitiveProduction::getActionList(const vector<double> param) { |
---|
347 | list<ActionP> l; |
---|
348 | ActionP ap; |
---|
349 | ap.action = this; |
---|
350 | ap.params = param; |
---|
351 | l.push_back(ap); |
---|
352 | return l; |
---|
353 | } |
---|
354 | |
---|
355 | const SString PrimitiveProduction::getF8Representation() { |
---|
356 | return this->f8command; |
---|
357 | } |
---|
358 | |
---|
359 | ParamProduction::ParamProduction(const SString paramName) { |
---|
360 | this->paramName = paramName; |
---|
361 | this->name = SString("_param production") + paramName; |
---|
362 | this->ignoreParams = true; |
---|
363 | } |
---|
364 | |
---|
365 | const SString ParamProduction::getF1Genotype(const vector<double> params) { |
---|
366 | SString s = SString::valueOf(params[0]); |
---|
367 | #if CONV_DEBUG > 1 |
---|
368 | cout << "@@@@@ Wartosc parametru: " << this->paramName << ": " << params[0] << endl; |
---|
369 | #endif |
---|
370 | return s; |
---|
371 | |
---|
372 | } |
---|
373 | |
---|
374 | const list<ActionP> ParamProduction::getActionList(const vector<double> param) { |
---|
375 | list<ActionP> l; |
---|
376 | ActionP ap; |
---|
377 | ap.action = this; |
---|
378 | ap.params = param; |
---|
379 | l.push_back(ap); |
---|
380 | return l; |
---|
381 | |
---|
382 | } |
---|
383 | |
---|
384 | const SString ParamProduction::getF8Representation() { |
---|
385 | return this->paramName; |
---|
386 | } |
---|
387 | |
---|
388 | NeuronProduction::NeuronProduction(const SString body) { |
---|
389 | this->body = body; |
---|
390 | this->name = SString("_neuron production") + body; |
---|
391 | this->ignoreParams = true; |
---|
392 | } |
---|
393 | |
---|
394 | const SString NeuronProduction::getF1Genotype(const vector<double> params) { |
---|
395 | return this->body; |
---|
396 | } |
---|
397 | |
---|
398 | const list<ActionP> NeuronProduction::getActionList(const vector<double> param) { |
---|
399 | list<ActionP> l; |
---|
400 | ActionP ap; |
---|
401 | ap.action = this; |
---|
402 | ap.params = param; |
---|
403 | l.push_back(ap); |
---|
404 | return l; |
---|
405 | |
---|
406 | } |
---|
407 | |
---|
408 | const SString NeuronProduction::getF8Representation() { |
---|
409 | return this->body; |
---|
410 | } |
---|
411 | |
---|
412 | Production::Production() { |
---|
413 | this->ignoreParams = false; |
---|
414 | } |
---|
415 | |
---|
416 | /** |
---|
417 | * Zwracamy puste, bo co innego mamy zrobić? |
---|
418 | */ |
---|
419 | const SString Production::getF1Genotype(const vector<double> params) { |
---|
420 | return SString(""); |
---|
421 | } |
---|
422 | |
---|
423 | const list<ActionP> Production::getActionList(const vector<double> params) { |
---|
424 | list<ActionP> l; |
---|
425 | #if CONV_DEBUG > 1 |
---|
426 | cout << "params.size(): " << params.size() << ", this->parameters.size(): " << this->parameters.size() << endl; |
---|
427 | #endif |
---|
428 | for (int i = 0; i < params.size() && i < this->parameters.size(); i++) { |
---|
429 | this->parameters.setValue(i + 1, params[i]); |
---|
430 | } |
---|
431 | |
---|
432 | //jedziemy po podprodukcjach |
---|
433 | for (vector<SubProduction>::iterator subProdIter = this->subproductions.begin(); |
---|
434 | subProdIter != this->subproductions.end(); subProdIter++) { |
---|
435 | #if CONV_DEBUG > 1 |
---|
436 | cout << "this->subproductions.size(): " << this->subproductions.size() << endl; |
---|
437 | #endif |
---|
438 | SubProduction &sp = *subProdIter; |
---|
439 | bool conditionsOK = true; |
---|
440 | //sprawdzenie warunków dla danej podprodukcji |
---|
441 | for (vector<Condition>::iterator condIter = sp.conditions.begin(); |
---|
442 | condIter != sp.conditions.end(); condIter++) { |
---|
443 | if (conditionsOK == false) { |
---|
444 | break; //bo dalsze sprawdzanie i tak nie ma sensu |
---|
445 | } |
---|
446 | Condition &c = *condIter; |
---|
447 | switch (c.relation) { |
---|
448 | case r_greater: |
---|
449 | if (this->parameters.getValue(c.parameter) <= c.value) { |
---|
450 | conditionsOK = false; |
---|
451 | } |
---|
452 | break; |
---|
453 | case r_greaterEqual: |
---|
454 | if (this->parameters.getValue(c.parameter) < c.value) { |
---|
455 | conditionsOK = false; |
---|
456 | } |
---|
457 | break; |
---|
458 | case r_less: |
---|
459 | if (this->parameters.getValue(c.parameter) >= c.value) { |
---|
460 | conditionsOK = false; |
---|
461 | } |
---|
462 | break; |
---|
463 | case r_lessEqual: |
---|
464 | if (this->parameters.getValue(c.parameter) > c.value) { |
---|
465 | conditionsOK = false; |
---|
466 | } |
---|
467 | break; |
---|
468 | case r_equal: |
---|
469 | if (this->parameters.getValue(c.parameter) != c.value) { |
---|
470 | conditionsOK = false; |
---|
471 | } |
---|
472 | break; |
---|
473 | case r_different: |
---|
474 | if (this->parameters.getValue(c.parameter) == c.value) { |
---|
475 | conditionsOK = false; |
---|
476 | } |
---|
477 | break; |
---|
478 | } |
---|
479 | } |
---|
480 | if (conditionsOK) { |
---|
481 | //jedziemy po każdej akcji w danej podprodukcji |
---|
482 | for (int i = 0; i < sp.actions.size(); i++) { |
---|
483 | #if CONV_DEBUG > 1 |
---|
484 | cout << "sp.actions.size(): " << sp.actions.size() << endl; |
---|
485 | #endif |
---|
486 | Action *action = sp.actions[i].action; |
---|
487 | vector<SString> strParams = sp.actions[i].params; |
---|
488 | vector<double> params; |
---|
489 | //podmieniamy nazwy parametrów na ich wartości |
---|
490 | for (vector<SString>::iterator paramIter = strParams.begin(); |
---|
491 | paramIter != strParams.end(); paramIter++) { |
---|
492 | SString parameter; |
---|
493 | SString element; |
---|
494 | int pos = 0; |
---|
495 | while ((*paramIter).getNextToken(pos, element, ';')) { |
---|
496 | if (element[0] == 'n') { |
---|
497 | double val = this->parameters.getValue(element); |
---|
498 | parameter += SString::valueOf(val) + ";"; |
---|
499 | } else { |
---|
500 | parameter += element + ";"; |
---|
501 | } |
---|
502 | } |
---|
503 | params.push_back(parseExpression(parameter)); |
---|
504 | } |
---|
505 | |
---|
506 | ActionP ap; |
---|
507 | ap.action = action; |
---|
508 | ap.params = params; |
---|
509 | l.push_back(ap); |
---|
510 | } |
---|
511 | } |
---|
512 | } |
---|
513 | |
---|
514 | return l; |
---|
515 | } |
---|
516 | |
---|
517 | const SString Production::getF8Representation() { |
---|
518 | return this->name; |
---|
519 | } |
---|
520 | |
---|
521 | vector<SString> GenoConv_F8ToF1::readProductionNames(const SString &in) { |
---|
522 | vector<SString> names; |
---|
523 | SString line; |
---|
524 | int pos = 0; |
---|
525 | bool afterFirstProd = false; |
---|
526 | //ParsingStatus status = firstLine; |
---|
527 | while (in.getNextToken(pos, line, '\n')) { |
---|
528 | #if CONV_DEBUG > 1 |
---|
529 | std::cout << "### Linia: " << line << std::endl; |
---|
530 | #endif |
---|
531 | if (line.startsWith("P") && line.indexOf('(', 0) == -1) { |
---|
532 | afterFirstProd = true; |
---|
533 | continue; |
---|
534 | } |
---|
535 | if (!afterFirstProd) { |
---|
536 | continue; |
---|
537 | } |
---|
538 | int lParenIndex = line.indexOf('(', 0); |
---|
539 | if (line.startsWith("P") && lParenIndex != -1) { |
---|
540 | SString prodName = line.substr(0, lParenIndex); |
---|
541 | #if CONV_DEBUG > 1 |
---|
542 | std::cout << "###Produkcja: " << prodName << std::endl; |
---|
543 | #endif |
---|
544 | names.push_back(prodName); |
---|
545 | } |
---|
546 | } |
---|
547 | return names; |
---|
548 | } |
---|
549 | |
---|
550 | bool GenoConv_F8ToF1::checkSyntax(const char *geno) { |
---|
551 | return this->parseInput(geno, NULL); |
---|
552 | } |
---|
553 | |
---|
554 | SString GenoConv_F8ToF1::convert(SString &in, MultiMap *mmap) { |
---|
555 | #if CONV_DEBUG > 0 |
---|
556 | cout << "convert() start" << endl; |
---|
557 | #endif |
---|
558 | SString dst = ""; |
---|
559 | const char* src = in; |
---|
560 | |
---|
561 | if (in.len() < 1 && !this->checkSyntax(src)) { |
---|
562 | return SString(); |
---|
563 | } |
---|
564 | |
---|
565 | Lsystem *lsystem = this->createLsystem(in); |
---|
566 | if (lsystem == NULL) { |
---|
567 | return SString(); |
---|
568 | } |
---|
569 | if (lsystem->firstProductionName.empty()) { |
---|
570 | return SString(); |
---|
571 | } |
---|
572 | |
---|
573 | #if CONV_DEBUG > 0 |
---|
574 | for (map<string, Production*>::iterator i1 = lsystem->productions.begin(); i1 != lsystem->productions.end(); i1++) { |
---|
575 | Production *p = i1->second; |
---|
576 | cout << "Production: " << p->name << endl; |
---|
577 | for (vector<SubProduction>::iterator i2 = p->subproductions.begin(); i2 != p->subproductions.end(); i2++) { |
---|
578 | SubProduction sp = *i2; |
---|
579 | cout << "\tConditions" << endl; |
---|
580 | for (vector<Condition>::iterator i3 = sp.conditions.begin(); i3 != sp.conditions.end(); i3++) { |
---|
581 | cout << "\t\t" << *i3 << endl; |
---|
582 | } |
---|
583 | cout << "\tAction : params" << endl; |
---|
584 | for (int i = 0; i < sp.actions.size(); i++) { |
---|
585 | Action *a = sp.actions[i].action; |
---|
586 | vector<SString> strParams = sp.actions[i].params; |
---|
587 | cout << "\t\t" << a->name << " : "; |
---|
588 | for (vector<SString>::iterator i4 = strParams.begin(); i4 != strParams.end(); i4++) { |
---|
589 | cout << *i4 << ", "; |
---|
590 | } |
---|
591 | cout << endl; |
---|
592 | } |
---|
593 | } |
---|
594 | } |
---|
595 | #endif |
---|
596 | #if CONV_DEBUG > 1 |
---|
597 | cout << "&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&" << endl; |
---|
598 | #endif |
---|
599 | |
---|
600 | //cout << "convert() 1" << endl; |
---|
601 | //ustawienie zmiennych dla początkowej produkcji |
---|
602 | Production *firstProduction = lsystem->productions[lsystem->firstProductionName]; |
---|
603 | vector<double> params; |
---|
604 | params.assign(lsystem->startParams.size(), 0.0); |
---|
605 | //cout << "startParams->size: " << lsystem->startParams.size() << endl; |
---|
606 | for (map<string, double>::iterator iter = lsystem->startParams.begin(); |
---|
607 | iter != lsystem->startParams.end(); iter++) { |
---|
608 | int position = firstProduction->parameters.getParameterPosition(stringToSString(iter->first)); |
---|
609 | //cout << "position of " << iter->first << ": " << position << endl; |
---|
610 | params.insert(params.begin() + (position - 1), iter->second); |
---|
611 | //params[position - 1] = iter->second; |
---|
612 | } |
---|
613 | |
---|
614 | //cout << "convert() 2" << endl; |
---|
615 | |
---|
616 | ActionP ap; |
---|
617 | ap.action = firstProduction; |
---|
618 | ap.params = params; |
---|
619 | |
---|
620 | list<ActionP> actionList; |
---|
621 | actionList.push_back(ap); |
---|
622 | |
---|
623 | //cout << "iterations: " << lsystem->iterations << endl; |
---|
624 | for (int i = 0; i < lsystem->iterations; i++) { |
---|
625 | //cout << "convert() 2.1" << endl; |
---|
626 | list<ActionP> newList; |
---|
627 | for (list<ActionP>::iterator iter = actionList.begin(); iter != actionList.end(); iter++) { |
---|
628 | //cout << "convert() 2.1.1" << endl; |
---|
629 | Action *a = (*iter).action; |
---|
630 | vector<double> p = (*iter).params; |
---|
631 | if (a != NULL) { |
---|
632 | list<ActionP> tmpList = a->getActionList(p); |
---|
633 | newList.insert(newList.end(), tmpList.begin(), tmpList.end()); |
---|
634 | } |
---|
635 | } |
---|
636 | actionList = newList; |
---|
637 | } |
---|
638 | |
---|
639 | #if CONV_DEBUG > 1 |
---|
640 | cout << "&&&&&&&&&&&&&&&&&&&&& ^ &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&" << endl; |
---|
641 | for (list<ActionP>::iterator it = actionList.begin(); it != actionList.end(); it++) { |
---|
642 | cout << (*it).action->name << "("; |
---|
643 | for (vector<double>::iterator it2 = (*it).params.begin(); it2 != (*it).params.end(); it2++) { |
---|
644 | cout << *it2 << ", "; |
---|
645 | } |
---|
646 | cout << ")" << endl; |
---|
647 | } |
---|
648 | cout << "&&&&&&&&&&&&&&&&&&&&& ^ &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&" << endl; |
---|
649 | #endif |
---|
650 | for (list<ActionP>::iterator iter = actionList.begin(); iter != actionList.end(); iter++) { |
---|
651 | Action *a = (*iter).action; |
---|
652 | vector<double> p = (*iter).params; |
---|
653 | if (a != NULL) { |
---|
654 | dst += a->getF1Genotype(p); |
---|
655 | if (dst.len() > maxF1Length) { |
---|
656 | return SString(); //genotype becomes too long so we abort conversion |
---|
657 | } |
---|
658 | } |
---|
659 | } |
---|
660 | |
---|
661 | delete lsystem; |
---|
662 | |
---|
663 | #if CONV_DEBUG > 0 |
---|
664 | cout << "convert() end" << endl; |
---|
665 | #endif |
---|
666 | return dst; |
---|
667 | } |
---|
668 | |
---|
669 | //Lsystem* GenoConv_F8ToF1::createLsystem(const SString &in) { |
---|
670 | Lsystem* GenoConv_F8ToF1::createLsystem(SString in) { |
---|
671 | #if CONV_DEBUG > 0 |
---|
672 | cout << "createLsystem() start" << endl; |
---|
673 | #endif |
---|
674 | Lsystem *lsys = new Lsystem(); |
---|
675 | |
---|
676 | //wczytujemy nazwy produkcji i tworzymy wstępnie dla nich obiekty |
---|
677 | vector<SString> names = this->readProductionNames(in); |
---|
678 | for (vector<SString>::iterator nameIter = names.begin(); nameIter != names.end(); nameIter++) { |
---|
679 | Production *production = new Production(); |
---|
680 | production->name = *nameIter; |
---|
681 | //lsystem->productions.insert(make_pair(*nameIter, production)); |
---|
682 | lsys->productions[sstringToString(*nameIter)] = production; |
---|
683 | } |
---|
684 | |
---|
685 | #if CONV_DEBUG > 1 |
---|
686 | cout << "lsystemprodsize " << lsys->productions.size() << endl; |
---|
687 | for (map<string, Production*>::iterator iii = lsys->productions.begin(); iii != lsys->productions.end(); iii++) { |
---|
688 | cout << "PPP '" << iii->first << "' adr " << (long) &(iii->second) << endl; |
---|
689 | } |
---|
690 | #endif |
---|
691 | |
---|
692 | const char* src = in; |
---|
693 | bool result = this->parseInput(src, lsys); |
---|
694 | if (!result) { |
---|
695 | delete lsys; |
---|
696 | return NULL; |
---|
697 | } |
---|
698 | |
---|
699 | #if CONV_DEBUG > 1 |
---|
700 | |
---|
701 | cout << "@@@@@ Przeparsowane" << endl; |
---|
702 | |
---|
703 | cout << "&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&" << endl; |
---|
704 | #endif |
---|
705 | //final check |
---|
706 | for (map<string, Production*>::iterator prodIter = lsys->productions.begin(); |
---|
707 | prodIter != lsys->productions.end(); prodIter++) { |
---|
708 | for (vector<SubProduction>::iterator subProdIter = prodIter->second->subproductions.begin(); |
---|
709 | subProdIter != prodIter->second->subproductions.end(); subProdIter++) { |
---|
710 | SubProduction subProduction = *subProdIter; |
---|
711 | for (vector<ActionStrP>::iterator i = subProduction.actions.begin(); |
---|
712 | i != subProduction.actions.end(); /*nothing*/) { |
---|
713 | if ((*i).action == NULL) { |
---|
714 | i = subProduction.actions.erase(i); |
---|
715 | } else { |
---|
716 | i++; |
---|
717 | } |
---|
718 | } |
---|
719 | } |
---|
720 | } |
---|
721 | #if CONV_DEBUG > 0 |
---|
722 | cout << "createLsystem() end" << endl; |
---|
723 | #endif |
---|
724 | return lsys; |
---|
725 | } |
---|
726 | |
---|
727 | bool GenoConv_F8ToF1::parseInput(const char* src, Lsystem* lsys) { |
---|
728 | //inicjalizacja zmiennych parsera |
---|
729 | int yv; |
---|
730 | istringstream input; |
---|
731 | input.str(string(src)); |
---|
732 | ostringstream output; |
---|
733 | yyFlexLexer scanner(&input, &output); |
---|
734 | bool syntaxOk = false; |
---|
735 | void* pParser = prs::ParseAlloc(malloc, lsys, (lsys == NULL), &syntaxOk); |
---|
736 | struct prs::Token t0; |
---|
737 | //t0.strValue = ""; |
---|
738 | memset(t0.strArrValue, 0, 30); |
---|
739 | extern YYSTYPE yylval; |
---|
740 | |
---|
741 | //parsowanie wejścia |
---|
742 | // on EOF yylex will return 0 |
---|
743 | while((yv = scanner.yylex()) != 0) { |
---|
744 | #if CONV_DEBUG > 1 |
---|
745 | cout << " yylex() " << yv << " yylval.strVal " << yylval.strVal << endl; |
---|
746 | #endif |
---|
747 | memset(t0.strArrValue, 0, 30); |
---|
748 | sprintf(t0.strArrValue, yylval.strVal); |
---|
749 | prs::Parse (pParser, yv, t0); |
---|
750 | } |
---|
751 | prs::Parse(pParser, 0, t0); |
---|
752 | prs::ParseFree(pParser, free); |
---|
753 | |
---|
754 | return syntaxOk; |
---|
755 | } |
---|
756 | #undef CONV_DEBUG |
---|