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

Last change on this file since 26 was 26, checked in by mwajcht, 15 years ago

Added some comments to converter classes

File size: 23.1 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
518vector<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
547bool GenoConv_F8ToF1::checkSyntax(const char *geno) {
548        return this->parseInput(geno, NULL);
549}
550
551SString 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                return SString();
568        }
569       
570#if CONV_DEBUG > 0
571        for (map<string, Production*>::iterator i1 = lsystem->productions.begin(); i1 != lsystem->productions.end(); i1++) {
572                Production *p = i1->second;
573                cout << "Production: " << p->name << endl;
574                for (vector<SubProduction>::iterator i2 = p->subproductions.begin(); i2 != p->subproductions.end(); i2++) {
575                        SubProduction sp = *i2;
576                        cout << "\tConditions" << endl;
577                        for (vector<Condition>::iterator i3 = sp.conditions.begin(); i3 != sp.conditions.end(); i3++) {
578                                cout << "\t\t" << *i3 << endl;
579                        }
580                        cout << "\tAction : params" << endl;
581                        for (int i = 0; i < sp.actions.size(); i++) {
582                                Action *a = sp.actions[i].action;
583                                vector<SString> strParams = sp.actions[i].params;
584                                cout << "\t\t" << a->name << " : ";
585                                for (vector<SString>::iterator i4 = strParams.begin(); i4 != strParams.end(); i4++) {
586                                        cout << *i4 << ", ";
587                                }
588                                cout << endl;
589                        }
590                }
591        }
592#endif
593#if CONV_DEBUG > 1
594        cout << "&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&" << endl;
595#endif
596       
597        //cout << "convert() 1" << endl;
598        //set parameters for start production
599        Production *firstProduction = lsystem->productions[lsystem->firstProductionName];
600        vector<double> params;
601        params.assign(lsystem->startParams.size(), 0.0);
602        //cout << "startParams->size: " << lsystem->startParams.size() << endl;
603        for (map<string, double>::iterator iter = lsystem->startParams.begin();
604                 iter != lsystem->startParams.end(); iter++) {
605                int position = firstProduction->parameters.getParameterPosition(stringToSString(iter->first));
606                //cout << "position of " << iter->first << ": " << position << endl;
607                params.insert(params.begin() + (position - 1), iter->second);
608                //params[position - 1] = iter->second;
609        }
610       
611        //cout << "convert() 2" << endl;
612       
613        ActionP ap;
614        ap.action = firstProduction;
615        ap.params = params;
616       
617        list<ActionP> actionList;
618        actionList.push_back(ap);
619       
620        //cout << "iterations: " << lsystem->iterations << endl;
621        for (int i = 0; i < lsystem->iterations; i++) {
622                //cout << "convert() 2.1" << endl;
623                list<ActionP> newList;
624                for (list<ActionP>::iterator iter = actionList.begin(); iter != actionList.end(); iter++) {
625                        //cout << "convert() 2.1.1" << endl;
626                        Action *a = (*iter).action;
627                        vector<double> p = (*iter).params;
628                        if (a != NULL) {
629                                list<ActionP> tmpList = a->getActionList(p);
630                                newList.insert(newList.end(), tmpList.begin(), tmpList.end());
631                        }
632                }
633                actionList = newList;
634        }
635       
636#if CONV_DEBUG > 1
637        cout << "&&&&&&&&&&&&&&&&&&&&& ^ &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&" << endl;
638        for (list<ActionP>::iterator it = actionList.begin(); it != actionList.end(); it++) {
639                cout << (*it).action->name << "(";
640                for (vector<double>::iterator it2 = (*it).params.begin(); it2 != (*it).params.end(); it2++) {
641                        cout << *it2 << ", ";
642                }
643                cout << ")" << endl;
644        }
645        cout << "&&&&&&&&&&&&&&&&&&&&& ^ &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&" << endl;
646#endif 
647        for (list<ActionP>::iterator iter = actionList.begin(); iter != actionList.end(); iter++) {
648                Action *a = (*iter).action;
649                vector<double> p = (*iter).params;
650                if (a != NULL) {
651                        dst += a->getF1Genotype(p);
652                        if (dst.len() > maxF1Length) {
653                                return SString(); //genotype becomes too long so we abort conversion
654                        }
655                }
656        }
657       
658        delete lsystem;
659       
660#if CONV_DEBUG > 0
661        cout << "convert() end" << endl;
662#endif
663        return dst;
664}
665
666//Lsystem* GenoConv_F8ToF1::createLsystem(const SString &in) {
667Lsystem* GenoConv_F8ToF1::createLsystem(SString in) {
668#if CONV_DEBUG > 0
669        cout << "createLsystem() start" << endl;
670#endif
671        Lsystem *lsys = new Lsystem();
672       
673        //read production names and create objects for them
674        vector<SString> names = this->readProductionNames(in);
675        for (vector<SString>::iterator nameIter = names.begin(); nameIter != names.end(); nameIter++) {
676                Production *production = new Production();
677                production->name = *nameIter;
678                //lsystem->productions.insert(make_pair(*nameIter, production));
679                lsys->productions[sstringToString(*nameIter)] = production;
680        }
681       
682#if CONV_DEBUG > 1
683        cout << "lsystemprodsize " << lsys->productions.size() << endl;
684        for (map<string, Production*>::iterator iii = lsys->productions.begin(); iii != lsys->productions.end(); iii++) {
685                cout << "PPP '" << iii->first << "' adr " << (long) &(iii->second) << endl;
686        }
687#endif
688       
689        const char* src = in;
690        bool result = this->parseInput(src, lsys);
691        if (!result) {
692                delete lsys;
693                return NULL;
694        }
695       
696#if CONV_DEBUG > 1
697       
698        cout << "@@@@@ Parsed" << endl;
699       
700        cout << "&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&" << endl;
701#endif
702        //final check
703        for (map<string, Production*>::iterator prodIter = lsys->productions.begin();
704                 prodIter != lsys->productions.end(); prodIter++) {
705                for (vector<SubProduction>::iterator subProdIter = prodIter->second->subproductions.begin();
706                         subProdIter != prodIter->second->subproductions.end(); subProdIter++) {
707                        SubProduction subProduction = *subProdIter;
708                        for (vector<ActionStrP>::iterator i = subProduction.actions.begin();
709                                 i != subProduction.actions.end(); /*nothing*/) {
710                                if ((*i).action == NULL) {
711                                        i = subProduction.actions.erase(i);
712                                } else {
713                                        i++;
714                                }
715                        }
716                }
717        }
718#if CONV_DEBUG > 0
719        cout << "createLsystem() end" << endl;
720#endif
721        return lsys;
722}
723
724bool GenoConv_F8ToF1::parseInput(const char* src, Lsystem* lsys) {
725        //initialize parser
726        int yv;
727        istringstream input;
728        input.str(string(src));
729        ostringstream output;
730        yyFlexLexer scanner(&input, &output);
731        bool syntaxOk = false;
732        void* pParser = prs::ParseAlloc(malloc, lsys, (lsys == NULL), &syntaxOk);
733        struct prs::Token t0;
734        //t0.strValue = "";
735        memset(t0.strArrValue, 0, 30);
736        extern YYSTYPE yylval;
737       
738        //parse input
739        // on EOF yylex will return 0
740        while((yv = scanner.yylex()) != 0) {
741#if CONV_DEBUG > 1
742                cout << " yylex() " << yv << " yylval.strVal " << yylval.strVal << endl;
743#endif
744                memset(t0.strArrValue, 0, 30);
745                sprintf(t0.strArrValue, yylval.strVal);
746                prs::Parse (pParser, yv, t0);
747        }
748        prs::Parse(pParser, 0, t0);
749        prs::ParseFree(pParser, free);
750       
751        return syntaxOk;       
752}
753#undef CONV_DEBUG
Note: See TracBrowser for help on using the repository browser.