1 | /* |
---|
2 | * conv_f8tof1.h |
---|
3 | * L-systemToF1 |
---|
4 | * |
---|
5 | * Created by Maciej Wajcht on 08-03-21. |
---|
6 | * Copyright 2008 __MyCompanyName__. All rights reserved. |
---|
7 | * |
---|
8 | */ |
---|
9 | |
---|
10 | #ifndef _CONV_F8_TO_F1_H |
---|
11 | #define _CONV_F8_TO_F1_H |
---|
12 | |
---|
13 | #include <map> |
---|
14 | #include <vector> |
---|
15 | #include <string> |
---|
16 | #include <list> |
---|
17 | #include <iostream> |
---|
18 | #include "genoconv.h" |
---|
19 | #include "model.h" |
---|
20 | |
---|
21 | using std::ostream; |
---|
22 | using std::vector; |
---|
23 | using std::list; |
---|
24 | using std::string; |
---|
25 | using std::map; |
---|
26 | |
---|
27 | enum RelationType { |
---|
28 | r_greater, |
---|
29 | r_greaterEqual, |
---|
30 | r_less, |
---|
31 | r_lessEqual, |
---|
32 | r_equal, |
---|
33 | r_different |
---|
34 | }; |
---|
35 | |
---|
36 | class Condition { |
---|
37 | public: |
---|
38 | RelationType relation; |
---|
39 | SString parameter; |
---|
40 | double value; |
---|
41 | |
---|
42 | friend ostream& operator<<(ostream& os, const Condition& c); |
---|
43 | }; |
---|
44 | |
---|
45 | class ActionP; //only declaration |
---|
46 | |
---|
47 | /// superclass of all actions, e.g. Production, PrimitiveProduction etc. |
---|
48 | class Action { |
---|
49 | public: |
---|
50 | /// action's name |
---|
51 | SString name; |
---|
52 | /// if true action should ignore params passed to it |
---|
53 | bool ignoreParams; |
---|
54 | /// gets string representaion of this action in f1 format |
---|
55 | virtual const SString getF1Genotype(vector<double> params) = 0; |
---|
56 | /// gets list of actions with double parameters to each of them |
---|
57 | /// @param params parameters passed to this action |
---|
58 | virtual const list<ActionP> getActionList(const vector<double> params) = 0; |
---|
59 | /// gets string representaion of this action in f8 format |
---|
60 | virtual const SString getF8Representation() = 0; |
---|
61 | virtual ~Action() {} |
---|
62 | }; |
---|
63 | |
---|
64 | /// this class consists of pointer to some action and a list of parameters to that action; |
---|
65 | /// parameters are kept as strings |
---|
66 | class ActionStrP { |
---|
67 | public: |
---|
68 | Action *action; |
---|
69 | vector<SString> params; |
---|
70 | }; |
---|
71 | |
---|
72 | /// this class consists of pointer to some action and a list of parameters to that action; |
---|
73 | /// parameters are kept as doubles |
---|
74 | class ActionP { |
---|
75 | public: |
---|
76 | Action *action; |
---|
77 | vector<double> params; |
---|
78 | }; |
---|
79 | |
---|
80 | /// part of production which is used in translation only if all the conditions are met |
---|
81 | class SubProduction { |
---|
82 | public: |
---|
83 | /// list of conditions necessary to be met |
---|
84 | vector<Condition> conditions; |
---|
85 | /// actions with params (as strings) |
---|
86 | vector<ActionStrP> actions; |
---|
87 | }; |
---|
88 | |
---|
89 | /// primitive production; it's directly translated to its f1 equivalent |
---|
90 | /// e.g. X, r, R etc. |
---|
91 | class PrimitiveProduction : public Action { |
---|
92 | public: |
---|
93 | PrimitiveProduction(const SString command); |
---|
94 | const SString getF1Genotype(const vector<double> params); |
---|
95 | const list<ActionP> getActionList(const vector<double> params); |
---|
96 | const SString getF8Representation(); |
---|
97 | protected: |
---|
98 | SString f1command; |
---|
99 | SString f8command; |
---|
100 | }; |
---|
101 | |
---|
102 | /// param production; all param productions during translation are replaced with the current value of parameter |
---|
103 | /// pointed by this production |
---|
104 | class ParamProduction : public Action { |
---|
105 | public: |
---|
106 | ParamProduction(const SString paramName); |
---|
107 | const SString getF1Genotype(const vector<double> params); |
---|
108 | const list<ActionP> getActionList(const vector<double> params); |
---|
109 | const SString getF8Representation(); |
---|
110 | protected: |
---|
111 | SString paramName; |
---|
112 | }; |
---|
113 | |
---|
114 | /// neuron production; it is directly translated to f1 without any change |
---|
115 | class NeuronProduction : public Action { |
---|
116 | public: |
---|
117 | NeuronProduction(SString body); |
---|
118 | const SString getF1Genotype(const vector<double> params); |
---|
119 | const list<ActionP> getActionList(const vector<double> params); |
---|
120 | const SString getF8Representation(); |
---|
121 | protected: |
---|
122 | SString body; |
---|
123 | }; |
---|
124 | |
---|
125 | /// class which keeps all parameters of a production and enables to access them in convenient way |
---|
126 | /// IMPORTANT! All indices (positions) begins with 1 (not 0)! |
---|
127 | class ParameterCollection { |
---|
128 | public: |
---|
129 | /// returns parameter's value |
---|
130 | /// @param position parameter's position (first parameter has an index 1!) |
---|
131 | const double getValue(int position); |
---|
132 | /// returns parameter's value |
---|
133 | /// @param name parameter's name |
---|
134 | const double getValue(SString name); |
---|
135 | /// returns parameter's name |
---|
136 | /// @param position parameter's position (first parameter has an index 1!) |
---|
137 | const SString getParameterName(int position); |
---|
138 | /// returns parameter's position (first parameter has an index 1!) |
---|
139 | /// @param name parameter's name |
---|
140 | const int getParameterPosition(SString name); |
---|
141 | /// sets parameter's value |
---|
142 | /// @param position parameter's position |
---|
143 | /// @param value parameter's new value |
---|
144 | void setValue(int position, double value); |
---|
145 | /// sets parameter's value |
---|
146 | /// @param name parameter's name |
---|
147 | /// @param value parameter's new value |
---|
148 | void setValue(SString name, double value); |
---|
149 | /// adds parameter |
---|
150 | /// @param name parameter's name |
---|
151 | /// @param position desired parameter's position; defualts to -1 which means it will have first available position |
---|
152 | /// @param value parameter's value; defaults to 0.0 |
---|
153 | void addParameter(SString name, int position = -1, double value = 0.0); |
---|
154 | /// returns the number of parameters kept in this class |
---|
155 | const int size(); |
---|
156 | /// removes a parameter |
---|
157 | /// @param position position of parameter to be deleted |
---|
158 | void removeParameter(int position); |
---|
159 | /// removes a parameter |
---|
160 | /// @param name name of parameter to be deleted |
---|
161 | void removeParameter(SString name); |
---|
162 | /// returns true if parameter with given name exists |
---|
163 | /// @param name parameter's name |
---|
164 | bool paramExist(SString name); |
---|
165 | protected: |
---|
166 | vector<string> parameters; |
---|
167 | map<string, double> paramValues; |
---|
168 | }; |
---|
169 | |
---|
170 | /// represents a general rule in L-systems |
---|
171 | /// only "calls" to Production in genotype are replaced in translation procedure |
---|
172 | class Production : public Action { |
---|
173 | public: |
---|
174 | /// parameters of this production |
---|
175 | ParameterCollection parameters; |
---|
176 | /// list of subproductions |
---|
177 | vector<SubProduction> subproductions; |
---|
178 | |
---|
179 | Production(); |
---|
180 | ~Production() {}; |
---|
181 | const SString getF1Genotype(const vector<double> params); |
---|
182 | const list<ActionP> getActionList(const vector<double> params); |
---|
183 | const SString getF8Representation(); |
---|
184 | }; |
---|
185 | |
---|
186 | /// Main class that represents a genotype in f8 format |
---|
187 | class Lsystem { |
---|
188 | public: |
---|
189 | /// number of iterations in f8->f1 translation procedure |
---|
190 | int iterations; |
---|
191 | /// map of parameters of start production; key - parameter's name, value - parameter's value |
---|
192 | map<string, double> startParams; |
---|
193 | /// map of productions of L-system; key - productions's name, value - pointer to production |
---|
194 | map<string, Production*> productions; |
---|
195 | /// collection of neuron productions held in L-system |
---|
196 | vector<NeuronProduction*> neuronProductions; |
---|
197 | /// first production's name |
---|
198 | string firstProductionName; |
---|
199 | |
---|
200 | Lsystem(); |
---|
201 | ~Lsystem(); |
---|
202 | /// returns a primitive production of a given name |
---|
203 | /// @param name primitive production's name |
---|
204 | PrimitiveProduction* getPrimitiveProduction(SString name); |
---|
205 | /// returns a param production of a given name |
---|
206 | /// @param name param production's name |
---|
207 | ParamProduction* getParamProduction(SString name); |
---|
208 | /// gets string representation of this L-system (f8 genotype) |
---|
209 | SString toString(); |
---|
210 | /// returns all actions |
---|
211 | /// @param normal if true all normal actions will be included |
---|
212 | /// @param primitives if true all primitive actions will be included |
---|
213 | /// @param params if true all param actions will be included |
---|
214 | /// @param neurons if true all neuron actions will be included |
---|
215 | vector<Action*> getAllActions(bool normal, bool primitives, bool params, bool neurons); |
---|
216 | protected: |
---|
217 | map<string, PrimitiveProduction*> primitiveProductions; |
---|
218 | map<string, ParamProduction*> paramProductions; |
---|
219 | |
---|
220 | void removeEmptySubproductionsAndProductions(); |
---|
221 | }; |
---|
222 | |
---|
223 | /// Converter between f8 and f1 format |
---|
224 | class GenoConv_F8ToF1 : public GenoConverter { |
---|
225 | public: |
---|
226 | /// Default constructor |
---|
227 | GenoConv_F8ToF1() { |
---|
228 | name = "f8 to f1 converter"; |
---|
229 | in_format = '8'; |
---|
230 | out_format = '1'; |
---|
231 | mapsupport = 0; |
---|
232 | info = "ble"; |
---|
233 | maxF1Length = 500; |
---|
234 | } |
---|
235 | |
---|
236 | /// Constructor with parameter |
---|
237 | /// @param f1LengthLimit If resulting f1 genotype is longer than f1LengthLimit, converter aborts its job |
---|
238 | GenoConv_F8ToF1(int f1LengthLimit) { |
---|
239 | name = "f8 to f1 converter"; |
---|
240 | in_format = '8'; |
---|
241 | out_format = '1'; |
---|
242 | mapsupport = 0; |
---|
243 | info = "ble"; |
---|
244 | maxF1Length = f1LengthLimit; |
---|
245 | } |
---|
246 | |
---|
247 | ~GenoConv_F8ToF1() {} |
---|
248 | |
---|
249 | SString convert(SString &in, MultiMap *map); |
---|
250 | |
---|
251 | /// check syntax of given f8 genotype |
---|
252 | /// @param geno f8 genotype to be checked |
---|
253 | bool checkSyntax(const char *geno); |
---|
254 | |
---|
255 | /// returns names of productions in a given genotype |
---|
256 | /// @param in f8 genotype |
---|
257 | vector<SString> readProductionNames(const SString &in); |
---|
258 | //Lsystem* createLsystem(const SString &in); |
---|
259 | |
---|
260 | /// creates Lsystem object based on input genotype |
---|
261 | /// @param f8 genotype |
---|
262 | Lsystem* createLsystem(SString in); |
---|
263 | protected: |
---|
264 | bool parseInput(const char* src, Lsystem* lsys); |
---|
265 | int maxF1Length; |
---|
266 | }; |
---|
267 | |
---|
268 | #endif |
---|