source: cpp/f8-to-f1/conv_f8tof1.cpp @ 40

Last change on this file since 40 was 36, checked in by Maciej Komosinski, 14 years ago

adding the only f8 converter parameter to the list of simulator parameters

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