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 << "@@@@@ Param value: " << 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 | const SString Production::getF1Genotype(const vector<double> params) { |
---|
417 | return SString(); //return empty |
---|
418 | } |
---|
419 | |
---|
420 | const list<ActionP> Production::getActionList(const vector<double> params) { |
---|
421 | list<ActionP> l; |
---|
422 | #if CONV_DEBUG > 1 |
---|
423 | cout << "params.size(): " << params.size() << ", this->parameters.size(): " << this->parameters.size() << endl; |
---|
424 | #endif |
---|
425 | for (int i = 0; i < params.size() && i < this->parameters.size(); i++) { |
---|
426 | this->parameters.setValue(i + 1, params[i]); |
---|
427 | } |
---|
428 | |
---|
429 | //iterate through subproductions |
---|
430 | for (vector<SubProduction>::iterator subProdIter = this->subproductions.begin(); |
---|
431 | subProdIter != this->subproductions.end(); subProdIter++) { |
---|
432 | #if CONV_DEBUG > 1 |
---|
433 | cout << "this->subproductions.size(): " << this->subproductions.size() << endl; |
---|
434 | #endif |
---|
435 | SubProduction &sp = *subProdIter; |
---|
436 | bool conditionsOK = true; |
---|
437 | //check conditions of subproduction |
---|
438 | for (vector<Condition>::iterator condIter = sp.conditions.begin(); |
---|
439 | condIter != sp.conditions.end(); condIter++) { |
---|
440 | if (conditionsOK == false) { |
---|
441 | break; //because it's no use checking further |
---|
442 | } |
---|
443 | Condition &c = *condIter; |
---|
444 | switch (c.relation) { |
---|
445 | case r_greater: |
---|
446 | if (this->parameters.getValue(c.parameter) <= c.value) { |
---|
447 | conditionsOK = false; |
---|
448 | } |
---|
449 | break; |
---|
450 | case r_greaterEqual: |
---|
451 | if (this->parameters.getValue(c.parameter) < c.value) { |
---|
452 | conditionsOK = false; |
---|
453 | } |
---|
454 | break; |
---|
455 | case r_less: |
---|
456 | if (this->parameters.getValue(c.parameter) >= c.value) { |
---|
457 | conditionsOK = false; |
---|
458 | } |
---|
459 | break; |
---|
460 | case r_lessEqual: |
---|
461 | if (this->parameters.getValue(c.parameter) > c.value) { |
---|
462 | conditionsOK = false; |
---|
463 | } |
---|
464 | break; |
---|
465 | case r_equal: |
---|
466 | if (this->parameters.getValue(c.parameter) != c.value) { |
---|
467 | conditionsOK = false; |
---|
468 | } |
---|
469 | break; |
---|
470 | case r_different: |
---|
471 | if (this->parameters.getValue(c.parameter) == c.value) { |
---|
472 | conditionsOK = false; |
---|
473 | } |
---|
474 | break; |
---|
475 | } |
---|
476 | } |
---|
477 | if (conditionsOK) { |
---|
478 | //iterate through each action in subproduction |
---|
479 | for (int i = 0; i < sp.actions.size(); i++) { |
---|
480 | #if CONV_DEBUG > 1 |
---|
481 | cout << "sp.actions.size(): " << sp.actions.size() << endl; |
---|
482 | #endif |
---|
483 | Action *action = sp.actions[i].action; |
---|
484 | vector<SString> strParams = sp.actions[i].params; |
---|
485 | vector<double> params; |
---|
486 | //replace parameter names with values |
---|
487 | for (vector<SString>::iterator paramIter = strParams.begin(); |
---|
488 | paramIter != strParams.end(); paramIter++) { |
---|
489 | SString parameter; |
---|
490 | SString element; |
---|
491 | int pos = 0; |
---|
492 | while ((*paramIter).getNextToken(pos, element, ';')) { |
---|
493 | if (element[0] == 'n') { |
---|
494 | double val = this->parameters.getValue(element); |
---|
495 | parameter += SString::valueOf(val) + ";"; |
---|
496 | } else { |
---|
497 | parameter += element + ";"; |
---|
498 | } |
---|
499 | } |
---|
500 | params.push_back(parseExpression(parameter)); |
---|
501 | } |
---|
502 | |
---|
503 | ActionP ap; |
---|
504 | ap.action = action; |
---|
505 | ap.params = params; |
---|
506 | l.push_back(ap); |
---|
507 | } |
---|
508 | } |
---|
509 | } |
---|
510 | |
---|
511 | return l; |
---|
512 | } |
---|
513 | |
---|
514 | const SString Production::getF8Representation() { |
---|
515 | return this->name; |
---|
516 | } |
---|
517 | |
---|
518 | vector<SString> GenoConv_F8ToF1::readProductionNames(const SString &in) { |
---|
519 | vector<SString> names; |
---|
520 | SString line; |
---|
521 | int pos = 0; |
---|
522 | bool afterFirstProd = false; |
---|
523 | //ParsingStatus status = firstLine; |
---|
524 | while (in.getNextToken(pos, line, '\n')) { |
---|
525 | #if CONV_DEBUG > 1 |
---|
526 | std::cout << "### Line: " << line << std::endl; |
---|
527 | #endif |
---|
528 | if (line.startsWith("P") && line.indexOf('(', 0) == -1) { |
---|
529 | afterFirstProd = true; |
---|
530 | continue; |
---|
531 | } |
---|
532 | if (!afterFirstProd) { |
---|
533 | continue; |
---|
534 | } |
---|
535 | int lParenIndex = line.indexOf('(', 0); |
---|
536 | if (line.startsWith("P") && lParenIndex != -1) { |
---|
537 | SString prodName = line.substr(0, lParenIndex); |
---|
538 | #if CONV_DEBUG > 1 |
---|
539 | std::cout << "###Production: " << prodName << std::endl; |
---|
540 | #endif |
---|
541 | names.push_back(prodName); |
---|
542 | } |
---|
543 | } |
---|
544 | return names; |
---|
545 | } |
---|
546 | |
---|
547 | bool GenoConv_F8ToF1::checkSyntax(const char *geno) { |
---|
548 | return this->parseInput(geno, NULL); |
---|
549 | } |
---|
550 | |
---|
551 | SString GenoConv_F8ToF1::convert(SString &in, MultiMap *mmap) { |
---|
552 | #if CONV_DEBUG > 0 |
---|
553 | cout << "convert() start" << endl; |
---|
554 | #endif |
---|
555 | SString dst = ""; |
---|
556 | const char* src = in; |
---|
557 | |
---|
558 | if (in.len() < 1 || !this->checkSyntax(src)) { |
---|
559 | return SString(); |
---|
560 | } |
---|
561 | |
---|
562 | Lsystem *lsystem = this->createLsystem(in); |
---|
563 | if (lsystem == NULL) { |
---|
564 | return SString(); |
---|
565 | } |
---|
566 | if (lsystem->firstProductionName.empty()) { |
---|
567 | delete lsystem; |
---|
568 | return SString(); |
---|
569 | } |
---|
570 | |
---|
571 | #if CONV_DEBUG > 0 |
---|
572 | for (map<string, Production*>::iterator i1 = lsystem->productions.begin(); i1 != lsystem->productions.end(); i1++) { |
---|
573 | Production *p = i1->second; |
---|
574 | cout << "Production: " << p->name << endl; |
---|
575 | for (vector<SubProduction>::iterator i2 = p->subproductions.begin(); i2 != p->subproductions.end(); i2++) { |
---|
576 | SubProduction sp = *i2; |
---|
577 | cout << "\tConditions" << endl; |
---|
578 | for (vector<Condition>::iterator i3 = sp.conditions.begin(); i3 != sp.conditions.end(); i3++) { |
---|
579 | cout << "\t\t" << *i3 << endl; |
---|
580 | } |
---|
581 | cout << "\tAction : params" << endl; |
---|
582 | for (int i = 0; i < sp.actions.size(); i++) { |
---|
583 | Action *a = sp.actions[i].action; |
---|
584 | vector<SString> strParams = sp.actions[i].params; |
---|
585 | cout << "\t\t" << a->name << " : "; |
---|
586 | for (vector<SString>::iterator i4 = strParams.begin(); i4 != strParams.end(); i4++) { |
---|
587 | cout << *i4 << ", "; |
---|
588 | } |
---|
589 | cout << endl; |
---|
590 | } |
---|
591 | } |
---|
592 | } |
---|
593 | #endif |
---|
594 | #if CONV_DEBUG > 1 |
---|
595 | cout << "&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&" << endl; |
---|
596 | #endif |
---|
597 | |
---|
598 | //cout << "convert() 1" << endl; |
---|
599 | //set parameters for start production |
---|
600 | Production *firstProduction = lsystem->productions[lsystem->firstProductionName]; |
---|
601 | if (firstProduction==NULL) { |
---|
602 | delete lsystem; |
---|
603 | return SString(""); //should never happen because of syntax validation |
---|
604 | } |
---|
605 | |
---|
606 | vector<double> params; |
---|
607 | params.assign(lsystem->startParams.size(), 0.0); |
---|
608 | //cout << "startParams->size: " << lsystem->startParams.size() << endl; |
---|
609 | for (map<string, double>::iterator iter = lsystem->startParams.begin(); |
---|
610 | iter != lsystem->startParams.end(); iter++) |
---|
611 | { |
---|
612 | int position = firstProduction->parameters.getParameterPosition(stringToSString(iter->first)); |
---|
613 | //cout << "position of " << iter->first << ": " << position << endl; |
---|
614 | //params.insert(params.begin() + (position - 1), iter->second); //no need because the vector has required length (assign above) |
---|
615 | if (position>params.size()) { |
---|
616 | delete lsystem; |
---|
617 | return SString(""); |
---|
618 | } |
---|
619 | params[position - 1] = iter->second; |
---|
620 | } |
---|
621 | |
---|
622 | //cout << "convert() 2" << endl; |
---|
623 | |
---|
624 | ActionP ap; |
---|
625 | ap.action = firstProduction; |
---|
626 | ap.params = params; |
---|
627 | |
---|
628 | list<ActionP> actionList; |
---|
629 | actionList.push_back(ap); |
---|
630 | |
---|
631 | //cout << "iterations: " << lsystem->iterations << endl; |
---|
632 | for (int i = 0; i < lsystem->iterations; i++) { |
---|
633 | //cout << "convert() 2.1" << endl; |
---|
634 | list<ActionP> newList; |
---|
635 | for (list<ActionP>::iterator iter = actionList.begin(); iter != actionList.end(); iter++) { |
---|
636 | //cout << "convert() 2.1.1" << endl; |
---|
637 | Action *a = (*iter).action; |
---|
638 | vector<double> p = (*iter).params; |
---|
639 | if (a != NULL) { |
---|
640 | list<ActionP> tmpList = a->getActionList(p); |
---|
641 | newList.insert(newList.end(), tmpList.begin(), tmpList.end()); |
---|
642 | } |
---|
643 | } |
---|
644 | actionList = newList; |
---|
645 | } |
---|
646 | |
---|
647 | #if CONV_DEBUG > 1 |
---|
648 | cout << "&&&&&&&&&&&&&&&&&&&&& ^ &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&" << endl; |
---|
649 | for (list<ActionP>::iterator it = actionList.begin(); it != actionList.end(); it++) { |
---|
650 | cout << (*it).action->name << "("; |
---|
651 | for (vector<double>::iterator it2 = (*it).params.begin(); it2 != (*it).params.end(); it2++) { |
---|
652 | cout << *it2 << ", "; |
---|
653 | } |
---|
654 | cout << ")" << endl; |
---|
655 | } |
---|
656 | cout << "&&&&&&&&&&&&&&&&&&&&& ^ &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&" << endl; |
---|
657 | #endif |
---|
658 | for (list<ActionP>::iterator iter = actionList.begin(); iter != actionList.end(); iter++) { |
---|
659 | Action *a = (*iter).action; |
---|
660 | vector<double> p = (*iter).params; |
---|
661 | if (a != NULL) { |
---|
662 | dst += a->getF1Genotype(p); |
---|
663 | if (dst.len() > maxF1Length) { |
---|
664 | delete lsystem; |
---|
665 | return SString(); //genotype becomes too long so we abort conversion |
---|
666 | } |
---|
667 | } |
---|
668 | } |
---|
669 | |
---|
670 | delete lsystem; |
---|
671 | |
---|
672 | #if CONV_DEBUG > 0 |
---|
673 | cout << "convert() end" << endl; |
---|
674 | #endif |
---|
675 | return dst; |
---|
676 | } |
---|
677 | |
---|
678 | //Lsystem* GenoConv_F8ToF1::createLsystem(const SString &in) { |
---|
679 | Lsystem* GenoConv_F8ToF1::createLsystem(SString in) { |
---|
680 | #if CONV_DEBUG > 0 |
---|
681 | cout << "createLsystem() start" << endl; |
---|
682 | #endif |
---|
683 | Lsystem *lsys = new Lsystem(); |
---|
684 | |
---|
685 | //read production names and create objects for them |
---|
686 | vector<SString> names = this->readProductionNames(in); |
---|
687 | for (vector<SString>::iterator nameIter = names.begin(); nameIter != names.end(); nameIter++) { |
---|
688 | Production *production = new Production(); |
---|
689 | production->name = *nameIter; |
---|
690 | //lsystem->productions.insert(make_pair(*nameIter, production)); |
---|
691 | lsys->productions[sstringToString(*nameIter)] = production; |
---|
692 | } |
---|
693 | |
---|
694 | #if CONV_DEBUG > 1 |
---|
695 | cout << "lsystemprodsize " << lsys->productions.size() << endl; |
---|
696 | for (map<string, Production*>::iterator iii = lsys->productions.begin(); iii != lsys->productions.end(); iii++) { |
---|
697 | cout << "PPP '" << iii->first << "' adr " << (long) &(iii->second) << endl; |
---|
698 | } |
---|
699 | #endif |
---|
700 | |
---|
701 | const char* src = in; |
---|
702 | bool result = this->parseInput(src, lsys); |
---|
703 | if (!result) { |
---|
704 | delete lsys; |
---|
705 | return NULL; |
---|
706 | } |
---|
707 | |
---|
708 | #if CONV_DEBUG > 1 |
---|
709 | |
---|
710 | cout << "@@@@@ Parsed" << endl; |
---|
711 | |
---|
712 | cout << "&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&" << endl; |
---|
713 | #endif |
---|
714 | //final check |
---|
715 | for (map<string, Production*>::iterator prodIter = lsys->productions.begin(); |
---|
716 | prodIter != lsys->productions.end(); prodIter++) { |
---|
717 | for (vector<SubProduction>::iterator subProdIter = prodIter->second->subproductions.begin(); |
---|
718 | subProdIter != prodIter->second->subproductions.end(); subProdIter++) { |
---|
719 | SubProduction subProduction = *subProdIter; |
---|
720 | for (vector<ActionStrP>::iterator i = subProduction.actions.begin(); |
---|
721 | i != subProduction.actions.end(); /*nothing*/) { |
---|
722 | if ((*i).action == NULL) { |
---|
723 | i = subProduction.actions.erase(i); |
---|
724 | } else { |
---|
725 | i++; |
---|
726 | } |
---|
727 | } |
---|
728 | } |
---|
729 | } |
---|
730 | #if CONV_DEBUG > 0 |
---|
731 | cout << "createLsystem() end" << endl; |
---|
732 | #endif |
---|
733 | return lsys; |
---|
734 | } |
---|
735 | |
---|
736 | bool GenoConv_F8ToF1::parseInput(const char* src, Lsystem* lsys) { |
---|
737 | //initialize parser |
---|
738 | int yv; |
---|
739 | istringstream input; |
---|
740 | input.str(string(src)); |
---|
741 | ostringstream output; |
---|
742 | yyFlexLexer scanner(&input, &output); |
---|
743 | bool syntaxOk = false; |
---|
744 | void* pParser = prs::ParseAlloc(malloc, lsys, (lsys == NULL), &syntaxOk); |
---|
745 | struct prs::Token t0; |
---|
746 | //t0.strValue = ""; |
---|
747 | memset(t0.strArrValue, 0, 30); |
---|
748 | extern YYSTYPE yylval; |
---|
749 | |
---|
750 | //parse input |
---|
751 | // on EOF yylex will return 0 |
---|
752 | while((yv = scanner.yylex()) != 0) { |
---|
753 | #if CONV_DEBUG > 1 |
---|
754 | cout << " yylex() " << yv << " yylval.strVal " << yylval.strVal << endl; |
---|
755 | #endif |
---|
756 | memset(t0.strArrValue, 0, 30); |
---|
757 | sprintf(t0.strArrValue, yylval.strVal); |
---|
758 | prs::Parse (pParser, yv, t0); |
---|
759 | } |
---|
760 | prs::Parse(pParser, 0, t0); |
---|
761 | prs::ParseFree(pParser, free); |
---|
762 | |
---|
763 | return syntaxOk; |
---|
764 | } |
---|
765 | #undef CONV_DEBUG |
---|