source: cpp/frams/genetics/geno.cpp @ 372

Last change on this file since 372 was 372, checked in by sz, 9 years ago

Renamed some classes and functions to make their purpose more obvious:

All MessageHandlers? must now be given the explicit "Enable" argument if you want them to automatically become active. This makes side effects clearly visible.

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