1 | // This file is a part of Framsticks SDK. http://www.framsticks.com/ |
---|
2 | // Copyright (C) 1999-2015 Maciej Komosinski and Szymon Ulatowski. |
---|
3 | // See LICENSE.txt for details. |
---|
4 | |
---|
5 | #include "geno.h" |
---|
6 | #include "genoconv.h" |
---|
7 | #include <common/loggers/loggers.h> |
---|
8 | #include <common/stl-util.h> |
---|
9 | #include <frams/model/model.h> |
---|
10 | |
---|
11 | THREAD_LOCAL_DEF_PTR(Geno::Validators, geno_validators); |
---|
12 | THREAD_LOCAL_DEF_PTR(GenoConvManager, geno_converters); |
---|
13 | |
---|
14 | Geno::Validators* Geno::getValidators() {return tlsGetPtr(geno_validators);} |
---|
15 | GenoConvManager* Geno::getConverters() {return tlsGetPtr(geno_converters);} |
---|
16 | |
---|
17 | Geno::Validators* Geno::useValidators(Validators* val) |
---|
18 | {return tlsSetPtr(geno_validators,val);} |
---|
19 | GenoConvManager* Geno::useConverters(GenoConvManager* gcm) |
---|
20 | {return tlsSetPtr(geno_converters,gcm);} |
---|
21 | |
---|
22 | void Geno::init(const SString& genstring, char genformat, const SString& genname, const SString& comment) |
---|
23 | { |
---|
24 | refcount = 1; |
---|
25 | owner = 0; |
---|
26 | f0gen = 0; |
---|
27 | mapinshift = 0; |
---|
28 | mapoutshift = 0; |
---|
29 | isvalid = -1; |
---|
30 | SString gencopy(genstring); |
---|
31 | if (genformat == -1) |
---|
32 | { // unknown format |
---|
33 | genformat = '1'; |
---|
34 | if (genstring.charAt(0) == '/') |
---|
35 | { |
---|
36 | int end; |
---|
37 | SString newcomment; |
---|
38 | switch (genstring.charAt(1)) |
---|
39 | { |
---|
40 | case '/': |
---|
41 | genformat = genstring.charAt(2); |
---|
42 | if ((end = genstring.indexOf('\n')) >= 0) |
---|
43 | { |
---|
44 | newcomment = genstring.substr(2, end - 2); |
---|
45 | gencopy = genstring.substr(end + 1); |
---|
46 | mapinshift = end + 1; |
---|
47 | } |
---|
48 | else |
---|
49 | { |
---|
50 | gencopy = 0; |
---|
51 | mapinshift = genstring.len(); |
---|
52 | } |
---|
53 | break; |
---|
54 | case '*': |
---|
55 | genformat = genstring.charAt(2); |
---|
56 | if ((end = genstring.indexOf("*/")) >= 0) |
---|
57 | { |
---|
58 | newcomment = genstring.substr(2, end - 2); |
---|
59 | gencopy = genstring.substr(end + 2); |
---|
60 | mapinshift = end + 2; |
---|
61 | } |
---|
62 | else |
---|
63 | { |
---|
64 | gencopy = 0; |
---|
65 | mapinshift = genstring.len(); |
---|
66 | } |
---|
67 | break; |
---|
68 | } |
---|
69 | if (newcomment.len() > 0) |
---|
70 | { |
---|
71 | SString token; int pos = 0; |
---|
72 | if (newcomment.getNextToken(pos, token, ';')) |
---|
73 | if (newcomment.getNextToken(pos, token, ';')) |
---|
74 | { |
---|
75 | if (token.len()) txt = token; |
---|
76 | if (newcomment.getNextToken(pos, token, ';')) |
---|
77 | if (token.len()) name = token; |
---|
78 | } |
---|
79 | } |
---|
80 | } |
---|
81 | } |
---|
82 | |
---|
83 | gen = gencopy; |
---|
84 | format = genformat; |
---|
85 | if (!name.len()) name = genname; |
---|
86 | if (!txt.len()) txt = comment; |
---|
87 | multiline = (strchr(gen.c_str(), '\n') != 0); |
---|
88 | // mapoutshift...? |
---|
89 | } |
---|
90 | |
---|
91 | void Geno::freeF0() |
---|
92 | { |
---|
93 | if (f0gen) { delete f0gen; f0gen = 0; } |
---|
94 | } |
---|
95 | |
---|
96 | Geno::Geno(const char *genstring, char genformat, const char *genname, const char *comment) |
---|
97 | { |
---|
98 | init(SString(genstring), genformat, SString(genname), SString(comment)); |
---|
99 | } |
---|
100 | |
---|
101 | Geno::Geno(const SString& genstring, char genformat, const SString& genname, const SString& comment) |
---|
102 | { |
---|
103 | init(genstring, genformat, genname, comment); |
---|
104 | } |
---|
105 | |
---|
106 | Geno::Geno(const Geno& src) |
---|
107 | :gen(src.gen), name(src.name), format(src.format), txt(src.txt), isvalid(src.isvalid), |
---|
108 | f0gen(0), mapinshift(src.mapinshift), mapoutshift(src.mapinshift), |
---|
109 | multiline(src.multiline), owner(0) |
---|
110 | { |
---|
111 | f0gen = src.f0gen ? new Geno(*src.f0gen) : 0; refcount = 1; |
---|
112 | } |
---|
113 | |
---|
114 | void Geno::operator=(const Geno& src) |
---|
115 | { |
---|
116 | freeF0(); |
---|
117 | gen = src.gen; |
---|
118 | name = src.name; |
---|
119 | format = src.format; |
---|
120 | txt = src.txt; |
---|
121 | isvalid = src.isvalid; |
---|
122 | mapinshift = src.mapinshift; |
---|
123 | mapoutshift = src.mapinshift; |
---|
124 | multiline = src.multiline; |
---|
125 | f0gen = src.f0gen ? new Geno(*src.f0gen) : 0; |
---|
126 | owner = 0; |
---|
127 | } |
---|
128 | |
---|
129 | Geno::Geno(const SString& src) |
---|
130 | { |
---|
131 | init(src, -1, SString::empty(), SString::empty()); |
---|
132 | } |
---|
133 | |
---|
134 | void Geno::setGene(const SString& g, char newformat) |
---|
135 | { |
---|
136 | gen = g; |
---|
137 | isvalid = -1; |
---|
138 | freeF0(); |
---|
139 | if (newformat >= 0) format = newformat; |
---|
140 | } |
---|
141 | |
---|
142 | void Geno::setString(const SString& g) |
---|
143 | { |
---|
144 | freeF0(); |
---|
145 | init(g, -1, SString::empty(), SString::empty()); |
---|
146 | } |
---|
147 | |
---|
148 | void Geno::setName(const SString& n) |
---|
149 | { |
---|
150 | name = n; |
---|
151 | } |
---|
152 | |
---|
153 | void Geno::setComment(const SString& c) |
---|
154 | { |
---|
155 | txt = c; |
---|
156 | } |
---|
157 | |
---|
158 | SString Geno::toString(void) const |
---|
159 | { |
---|
160 | SString out; |
---|
161 | int comment = 0; |
---|
162 | if ((format != '1') || (comment = (txt.len() || name.len()))) |
---|
163 | { |
---|
164 | if (multiline) |
---|
165 | out += "//"; |
---|
166 | else |
---|
167 | out += "/*"; |
---|
168 | out += format; |
---|
169 | if (comment) |
---|
170 | { |
---|
171 | if (txt.len()) { out += ";"; out += txt; } |
---|
172 | if (name.len()){ out += ";"; out += name; } |
---|
173 | } |
---|
174 | if (multiline) |
---|
175 | out += "\n"; |
---|
176 | else |
---|
177 | out += "*/"; |
---|
178 | } |
---|
179 | out += gen; |
---|
180 | return out; |
---|
181 | } |
---|
182 | |
---|
183 | SString Geno::shortString(void) const |
---|
184 | { |
---|
185 | SString out; |
---|
186 | if (format != '1') |
---|
187 | { |
---|
188 | if (multiline) |
---|
189 | out += "//"; |
---|
190 | else |
---|
191 | out += "/*"; |
---|
192 | if (format == 0) |
---|
193 | out += "invalid"; |
---|
194 | else |
---|
195 | out += format; |
---|
196 | if (multiline) |
---|
197 | out += "\n"; |
---|
198 | else |
---|
199 | out += "*/"; |
---|
200 | } |
---|
201 | out += gen; |
---|
202 | return out; |
---|
203 | } |
---|
204 | |
---|
205 | int Geno::mapGenToString(int genpos) const |
---|
206 | { |
---|
207 | if (genpos > gen.len()) return -2; |
---|
208 | if (genpos<0) return -1; |
---|
209 | return mapinshift + genpos; |
---|
210 | } |
---|
211 | |
---|
212 | int Geno::mapStringToGen(int stringpos) const |
---|
213 | { |
---|
214 | stringpos -= mapinshift; |
---|
215 | if (stringpos>gen.len()) return -2; |
---|
216 | if (stringpos < 0) return -1; |
---|
217 | return stringpos; |
---|
218 | } |
---|
219 | |
---|
220 | SString Geno::getGene(void) const { return gen; } |
---|
221 | SString Geno::getName(void) const { return name; } |
---|
222 | char Geno::getFormat(void) const { return format; } |
---|
223 | SString Geno::getComment(void) const { return txt; } |
---|
224 | |
---|
225 | int ModelGenoValidator::testGenoValidity(Geno& g) |
---|
226 | { |
---|
227 | if (g.getFormat() == '0') |
---|
228 | { |
---|
229 | Model mod(g); |
---|
230 | return mod.isValid(); |
---|
231 | } |
---|
232 | else |
---|
233 | { |
---|
234 | bool converter_missing; |
---|
235 | Geno f0geno = g.getConverted('0', NULL, &converter_missing); |
---|
236 | if (converter_missing) |
---|
237 | return -1;//no result |
---|
238 | return f0geno.isValid(); |
---|
239 | } |
---|
240 | } |
---|
241 | |
---|
242 | void Geno::validate() |
---|
243 | { |
---|
244 | if (isvalid >= 0) return; |
---|
245 | if (gen.len() == 0) { isvalid = 0; return; } |
---|
246 | Validators* vals=getValidators(); |
---|
247 | if (vals!=NULL) |
---|
248 | { |
---|
249 | FOREACH(GenoValidator*, v, (*vals)) |
---|
250 | if ((isvalid = v->testGenoValidity(*this)) >= 0) |
---|
251 | return; |
---|
252 | } |
---|
253 | isvalid = 0; |
---|
254 | logPrintf("Geno", "validate", LOG_WARN, "Wrong configuration? No genotype validators defined for genetic format f%c.", format); |
---|
255 | } |
---|
256 | |
---|
257 | bool Geno::isValid(void) |
---|
258 | { |
---|
259 | if (isvalid<0) |
---|
260 | { |
---|
261 | LoggerToMemory err(LoggerBase::Enable | LoggerToMemory::StoreAllMessages,LOG_INFO); |
---|
262 | validate(); |
---|
263 | err.disable(); |
---|
264 | string msg=err.getCountSummary(); |
---|
265 | if (msg.size()>0) |
---|
266 | { |
---|
267 | msg+=ssprintf(" while checking validity of '%s'",getName().c_str()); |
---|
268 | msg+="\n"; |
---|
269 | msg+=err.getMessages(); |
---|
270 | logMessage("Geno","isValid",err.getErrorLevel(),msg.c_str()); |
---|
271 | } |
---|
272 | } |
---|
273 | return isvalid>0; |
---|
274 | } |
---|
275 | |
---|
276 | Geno Geno::getConverted(char otherformat, MultiMap *m, bool *converter_missing) |
---|
277 | { |
---|
278 | if (otherformat == getFormat()) { if (converter_missing) *converter_missing = false; return *this; } |
---|
279 | #ifndef NO_GENOCONVMANAGER |
---|
280 | GenoConvManager *converters=getConverters(); |
---|
281 | if (converters) |
---|
282 | { |
---|
283 | if ((otherformat == '0') && (!m)) |
---|
284 | { |
---|
285 | if (!f0gen) |
---|
286 | f0gen = new Geno(converters->convert(*this, otherformat, NULL, converter_missing)); |
---|
287 | return *f0gen; |
---|
288 | } |
---|
289 | else |
---|
290 | return converters->convert(*this, otherformat, m, converter_missing); |
---|
291 | } |
---|
292 | #endif |
---|
293 | if (converter_missing) *converter_missing = true; |
---|
294 | return (otherformat == getFormat()) ? *this : Geno(0, 0, 0, "GenConvManager not available"); |
---|
295 | } |
---|
296 | |
---|
297 | Geno::~Geno() |
---|
298 | { |
---|
299 | if (f0gen) delete f0gen; |
---|
300 | } |
---|