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

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

maximal f1 genotype length published as the f8->f1 converter parameter

File size: 24.2 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        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                 
53Lsystem::~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
72PrimitiveProduction* 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
81ParamProduction* 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
90SString 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
153vector<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
182void 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
241ostream& 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
249const double ParameterCollection::getValue(int position) {
250        string name = this->parameters[position - 1];
251        return this->paramValues[name];
252}
253
254const double ParameterCollection::getValue(SString name) {
255        return this->paramValues[sstringToString(name)];
256}
257
258const SString ParameterCollection::getParameterName(int position) {
259        return stringToSString(this->parameters[position - 1]);
260}
261
262const 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
272void ParameterCollection::setValue(int position, double value) {
273        string name = this->parameters[position - 1];
274        this->paramValues[name] = value;
275}
276
277void ParameterCollection::setValue(SString name, double value) {
278        this->paramValues[sstringToString(name)] = value;
279}
280
281void 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
295const int ParameterCollection::size() {
296        return this->parameters.size();
297}
298
299void 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
306void ParameterCollection::removeParameter(SString name) {
307        int idx = getParameterPosition(name);
308        this->removeParameter(idx);
309}
310
311bool 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
321PrimitiveProduction::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
339const 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
346const 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
355const SString PrimitiveProduction::getF8Representation() {
356        return this->f8command;
357}
358
359ParamProduction::ParamProduction(const SString paramName) {
360        this->paramName = paramName;
361        this->name = SString("_param production") + paramName;
362        this->ignoreParams = true;
363}
364
365const 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
374const 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
384const SString ParamProduction::getF8Representation() {
385        return this->paramName;
386}
387
388NeuronProduction::NeuronProduction(const SString body) {
389        this->body = body;
390        this->name = SString("_neuron production") + body;
391        this->ignoreParams = true;
392}
393
394const SString NeuronProduction::getF1Genotype(const vector<double> params) {
395        return this->body;     
396}
397
398const 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
408const SString NeuronProduction::getF8Representation() {
409        return this->body;
410}
411
412Production::Production() {
413        this->ignoreParams = false;
414}
415
416const SString Production::getF1Genotype(const vector<double> params) {
417        return SString(); //return empty
418}
419
420const 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
514const SString Production::getF8Representation() {
515        return this->name;
516}
517
518
519
520
521
522
523
524
525
526#define FIELDSTRUCT GenoConv_F8ToF1
527
528static ParamEntry GENOCONVf8param_tab[]=
529{
530{"Genetics: f8: limits",1,1,},
531{"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.", },
532{0,},
533};
534
535#undef FIELDSTRUCT
536
537GenoConv_F8ToF1::GenoConv_F8ToF1()
538{
539        name = "f8 converter";
540        in_format = '8';
541        out_format = '1';
542        mapsupport = 0;
543        info = "Generative encoding";
544        maxF1Length = 500;
545        par.setParamTab(GENOCONVf8param_tab);
546        par.select(this);
547        par.setDefault();
548}
549
550
551vector<SString> GenoConv_F8ToF1::readProductionNames(const SString &in) {
552        vector<SString> names;
553        SString line;
554        int pos = 0;
555        bool afterFirstProd = false;
556        //ParsingStatus status = firstLine;
557        while (in.getNextToken(pos, line, '\n')) {
558#if CONV_DEBUG > 1
559                std::cout << "### Line: " << line << std::endl;
560#endif
561                if (line.startsWith("P") && line.indexOf('(', 0) == -1) {
562                        afterFirstProd = true;
563                        continue;
564                }
565                if (!afterFirstProd) {
566                        continue;
567                }
568                int lParenIndex = line.indexOf('(', 0);
569                if (line.startsWith("P") && lParenIndex != -1) {
570                        SString prodName = line.substr(0, lParenIndex);
571#if CONV_DEBUG > 1
572                        std::cout << "###Production: " << prodName << std::endl;
573#endif
574                        names.push_back(prodName);
575                }
576        }
577        return names;
578}
579
580bool GenoConv_F8ToF1::checkSyntax(const char *geno) {
581        return this->parseInput(geno, NULL);
582}
583
584SString GenoConv_F8ToF1::convert(SString &in, MultiMap *mmap) {
585#if CONV_DEBUG > 0
586        cout << "convert() start" << endl;
587#endif
588        SString dst = "";
589        const char* src = in;
590       
591        if (in.len() < 1 || !this->checkSyntax(src)) {
592                return SString();
593        }
594       
595        Lsystem *lsystem = this->createLsystem(in);
596        if (lsystem == NULL) {
597                return SString();
598        }
599        if (lsystem->firstProductionName.empty()) {
600                delete lsystem;
601                return SString();
602        }
603       
604#if CONV_DEBUG > 0
605        for (map<string, Production*>::iterator i1 = lsystem->productions.begin(); i1 != lsystem->productions.end(); i1++) {
606                Production *p = i1->second;
607                cout << "Production: " << p->name << endl;
608                for (vector<SubProduction>::iterator i2 = p->subproductions.begin(); i2 != p->subproductions.end(); i2++) {
609                        SubProduction sp = *i2;
610                        cout << "\tConditions" << endl;
611                        for (vector<Condition>::iterator i3 = sp.conditions.begin(); i3 != sp.conditions.end(); i3++) {
612                                cout << "\t\t" << *i3 << endl;
613                        }
614                        cout << "\tAction : params" << endl;
615                        for (int i = 0; i < sp.actions.size(); i++) {
616                                Action *a = sp.actions[i].action;
617                                vector<SString> strParams = sp.actions[i].params;
618                                cout << "\t\t" << a->name << " : ";
619                                for (vector<SString>::iterator i4 = strParams.begin(); i4 != strParams.end(); i4++) {
620                                        cout << *i4 << ", ";
621                                }
622                                cout << endl;
623                        }
624                }
625        }
626#endif
627#if CONV_DEBUG > 1
628        cout << "&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&" << endl;
629#endif
630       
631        //cout << "convert() 1" << endl;
632        //set parameters for start production
633        Production *firstProduction = lsystem->productions[lsystem->firstProductionName];
634        if (firstProduction==NULL) {
635                delete lsystem;
636                return SString(""); //should never happen because of syntax validation
637        }
638
639        vector<double> params;
640        params.assign(lsystem->startParams.size(), 0.0);
641        //cout << "startParams->size: " << lsystem->startParams.size() << endl;
642        for (map<string, double>::iterator iter = lsystem->startParams.begin();
643                 iter != lsystem->startParams.end(); iter++)
644        {
645                int position = firstProduction->parameters.getParameterPosition(stringToSString(iter->first));
646                //cout << "position of " << iter->first << ": " << position << endl;
647                //params.insert(params.begin() + (position - 1), iter->second); //no need because the vector has required length (assign above)
648                if (position>params.size()) {
649                        delete lsystem;
650                        return SString("");
651                }
652                params[position - 1] = iter->second;
653        }
654       
655        //cout << "convert() 2" << endl;
656       
657        ActionP ap;
658        ap.action = firstProduction;
659        ap.params = params;
660       
661        list<ActionP> actionList;
662        actionList.push_back(ap);
663       
664        //cout << "iterations: " << lsystem->iterations << endl;
665        for (int i = 0; i < lsystem->iterations; i++) {
666                //cout << "convert() 2.1" << endl;
667                list<ActionP> newList;
668                for (list<ActionP>::iterator iter = actionList.begin(); iter != actionList.end(); iter++) {
669                        //cout << "convert() 2.1.1" << endl;
670                        Action *a = (*iter).action;
671                        vector<double> p = (*iter).params;
672                        if (a != NULL) {
673                                list<ActionP> tmpList = a->getActionList(p);
674                                newList.insert(newList.end(), tmpList.begin(), tmpList.end());
675                        }
676                }
677                actionList = newList;
678        }
679       
680#if CONV_DEBUG > 1
681        cout << "&&&&&&&&&&&&&&&&&&&&& ^ &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&" << endl;
682        for (list<ActionP>::iterator it = actionList.begin(); it != actionList.end(); it++) {
683                cout << (*it).action->name << "(";
684                for (vector<double>::iterator it2 = (*it).params.begin(); it2 != (*it).params.end(); it2++) {
685                        cout << *it2 << ", ";
686                }
687                cout << ")" << endl;
688        }
689        cout << "&&&&&&&&&&&&&&&&&&&&& ^ &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&" << endl;
690#endif 
691        for (list<ActionP>::iterator iter = actionList.begin(); iter != actionList.end(); iter++) {
692                Action *a = (*iter).action;
693                vector<double> p = (*iter).params;
694                if (a != NULL) {
695                        dst += a->getF1Genotype(p);
696                        if (dst.len() > maxF1Length) {
697                                delete lsystem;
698                                return SString(); //genotype becomes too long so we abort conversion
699                        }
700                }
701        }
702       
703        delete lsystem;
704       
705#if CONV_DEBUG > 0
706        cout << "convert() end" << endl;
707#endif
708        return dst;
709}
710
711//Lsystem* GenoConv_F8ToF1::createLsystem(const SString &in) {
712Lsystem* GenoConv_F8ToF1::createLsystem(SString in) {
713#if CONV_DEBUG > 0
714        cout << "createLsystem() start" << endl;
715#endif
716        Lsystem *lsys = new Lsystem();
717
718        //read production names and create objects for them
719        vector<SString> names = this->readProductionNames(in);
720        for (vector<SString>::iterator nameIter = names.begin(); nameIter != names.end(); nameIter++) {
721                Production *production = new Production();
722                production->name = *nameIter;
723                //lsystem->productions.insert(make_pair(*nameIter, production));
724                lsys->productions[sstringToString(*nameIter)] = production;
725        }
726       
727#if CONV_DEBUG > 1
728        cout << "lsystemprodsize " << lsys->productions.size() << endl;
729        for (map<string, Production*>::iterator iii = lsys->productions.begin(); iii != lsys->productions.end(); iii++) {
730                cout << "PPP '" << iii->first << "' adr " << (long) &(iii->second) << endl;
731        }
732#endif
733       
734        const char* src = in;
735        bool result = this->parseInput(src, lsys);
736        if (!result) {
737                delete lsys;
738                return NULL;
739        }
740       
741#if CONV_DEBUG > 1
742       
743        cout << "@@@@@ Parsed" << endl;
744       
745        cout << "&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&" << endl;
746#endif
747        //final check
748        for (map<string, Production*>::iterator prodIter = lsys->productions.begin();
749                 prodIter != lsys->productions.end(); prodIter++) {
750                for (vector<SubProduction>::iterator subProdIter = prodIter->second->subproductions.begin();
751                         subProdIter != prodIter->second->subproductions.end(); subProdIter++) {
752                        SubProduction subProduction = *subProdIter;
753                        for (vector<ActionStrP>::iterator i = subProduction.actions.begin();
754                                 i != subProduction.actions.end(); /*nothing*/) {
755                                if ((*i).action == NULL) {
756                                        i = subProduction.actions.erase(i);
757                                } else {
758                                        i++;
759                                }
760                        }
761                }
762        }
763#if CONV_DEBUG > 0
764        cout << "createLsystem() end" << endl;
765#endif
766        return lsys;
767}
768
769bool GenoConv_F8ToF1::parseInput(const char* src, Lsystem* lsys) {
770        //initialize parser
771        int yv;
772        istringstream input;
773        input.str(string(src));
774        ostringstream output;
775        yyFlexLexer scanner(&input, &output);
776        bool syntaxOk = false;
777        void* pParser = prs::ParseAlloc(malloc, lsys, (lsys == NULL), &syntaxOk);
778        struct prs::Token t0;
779        //t0.strValue = "";
780        memset(t0.strArrValue, 0, 30);
781        extern YYSTYPE yylval;
782       
783        //parse input
784        // on EOF yylex will return 0
785        while((yv = scanner.yylex()) != 0) {
786#if CONV_DEBUG > 1
787                cout << " yylex() " << yv << " yylval.strVal " << yylval.strVal << endl;
788#endif
789                memset(t0.strArrValue, 0, 30);
790                sprintf(t0.strArrValue, yylval.strVal);
791                prs::Parse (pParser, yv, t0);
792        }
793        prs::Parse(pParser, 0, t0);
794        prs::ParseFree(pParser, free);
795       
796        return syntaxOk;       
797}
798#undef CONV_DEBUG
799
Note: See TracBrowser for help on using the repository browser.