1 | // This file is a part of the Framsticks GDK. |
---|
2 | // Copyright (C) 2002-2014 Maciej Komosinski and Szymon Ulatowski. See LICENSE.txt for details. |
---|
3 | // Refer to http://www.framsticks.com/ for further information. |
---|
4 | |
---|
5 | #include <ctype.h> |
---|
6 | #include <frams/genetics/defgenoconv.h> |
---|
7 | #include <frams/model/model.h> |
---|
8 | #include <frams/util/multimap.h> |
---|
9 | |
---|
10 | #include "printconvmap.h" |
---|
11 | #include <frams/errmgr/stdouterr.h> |
---|
12 | |
---|
13 | /** |
---|
14 | @file |
---|
15 | Sample code: Genotype converter class |
---|
16 | */ |
---|
17 | |
---|
18 | /// Sample Geno converter not using Model class. |
---|
19 | /// (this converter generates the same output for each input). |
---|
20 | /// such a converter is responsible for doing valid |
---|
21 | /// f0 (or other format) output and storing temporary data |
---|
22 | class GenoConv_Test: public GenoConverter |
---|
23 | { |
---|
24 | public: |
---|
25 | GenoConv_Test() |
---|
26 | { |
---|
27 | name="Test Converter"; |
---|
28 | in_format='x'; |
---|
29 | } |
---|
30 | SString convert(SString &i,MultiMap *map) {return SString("after conversion...");} |
---|
31 | ~GenoConv_Test() {} |
---|
32 | }; |
---|
33 | |
---|
34 | /// Sample Geno converter using Model class. |
---|
35 | /// (this converter generates the same output for each input). |
---|
36 | class GenoConv_Test2: public GenoConverter |
---|
37 | { |
---|
38 | public: |
---|
39 | GenoConv_Test2() |
---|
40 | { |
---|
41 | name="Test Converter #2"; |
---|
42 | in_format='y'; |
---|
43 | } |
---|
44 | SString convert(SString &i,MultiMap *map) |
---|
45 | { |
---|
46 | Model mod; |
---|
47 | mod.open(); |
---|
48 | mod.singleStepBuild("p:"); |
---|
49 | mod.singleStepBuild("p:"); |
---|
50 | mod.singleStepBuild("j:0,1"); |
---|
51 | mod.getPart(1)->p=Pt3D(0,0.2,-1); |
---|
52 | mod.close(); |
---|
53 | return mod.getGeno().getGene(); |
---|
54 | } |
---|
55 | ~GenoConv_Test2() {} |
---|
56 | }; |
---|
57 | |
---|
58 | /// Sample Geno converter supporting conversion mapping. |
---|
59 | /// the conversion is very simple: any sequence of <digit><character> |
---|
60 | /// (but not inside neurons) is replaced by the repeated sequence of the character |
---|
61 | class GenoConv_Test3: public GenoConverter |
---|
62 | { |
---|
63 | public: |
---|
64 | GenoConv_Test3() |
---|
65 | { |
---|
66 | name="Test Converter #3"; |
---|
67 | in_format='z'; |
---|
68 | out_format='1'; |
---|
69 | mapsupport=1; |
---|
70 | } |
---|
71 | SString convert(SString &in,MultiMap *map); |
---|
72 | ~GenoConv_Test3() {} |
---|
73 | }; |
---|
74 | |
---|
75 | /** main converting routine - most important: direct conversion map example */ |
---|
76 | SString GenoConv_Test3::convert(SString &in,MultiMap *map) |
---|
77 | { |
---|
78 | SString dst; |
---|
79 | const char* src=in; |
---|
80 | const char* t; |
---|
81 | int insideneuron=0; |
---|
82 | int n; |
---|
83 | for(t=src;*t;t++) |
---|
84 | { |
---|
85 | if (insideneuron&&*t==']') insideneuron=0; |
---|
86 | if (*t=='[') insideneuron=1; |
---|
87 | if ((!insideneuron)&&isdigit(*t)&&t[1]) |
---|
88 | { // special sequence detected! |
---|
89 | n=*t-'0'; |
---|
90 | t++; // *t will be repeated 'n' times |
---|
91 | for (int i=0;i<n;i++) |
---|
92 | dst+=*t; |
---|
93 | if (map) // fill in the map only if requested |
---|
94 | map->add(t-src, t-src, dst.len()-n, dst.len()-1); |
---|
95 | // meaning: source character (t-src) becomes (dst.len()-n ... dst.len()-1) |
---|
96 | } |
---|
97 | else |
---|
98 | { |
---|
99 | dst+=*t; |
---|
100 | if (map) |
---|
101 | map->add(t-src, t-src, dst.len()-1, dst.len()-1); |
---|
102 | // meaning: map single to single character: (t-src) into (dst.len()-1) |
---|
103 | } |
---|
104 | } |
---|
105 | return dst; |
---|
106 | } |
---|
107 | |
---|
108 | |
---|
109 | /////////////////////////////////////////////// |
---|
110 | |
---|
111 | void printGen(Geno &g) |
---|
112 | { |
---|
113 | printf("Genotype:\n%s\nFormat: %c\nValid: %s\nComment: %s\n", |
---|
114 | (const char*)g.getGene(),g.getFormat(),g.isValid()?"yes":"no",(const char*)g.getComment()); |
---|
115 | } |
---|
116 | |
---|
117 | int main(int argc,char *argv[]) |
---|
118 | { |
---|
119 | StdoutErrorHandler err;//the default ErrorHandler constructor automatically registers this object to receive framsg messages (and in this case, redirect them to standard output) |
---|
120 | |
---|
121 | DefaultGenoConvManager gcm; |
---|
122 | gcm.addDefaultConverters(); |
---|
123 | gcm.addConverter(new GenoConv_Test()); |
---|
124 | gcm.addConverter(new GenoConv_Test2()); |
---|
125 | gcm.addConverter(new GenoConv_Test3()); |
---|
126 | Geno::useConverters(gcm); |
---|
127 | |
---|
128 | ModelGenoValidator model_validator; |
---|
129 | Geno::addValidator(&model_validator); |
---|
130 | |
---|
131 | const char* src=(argc>1)?argv[1]:"X"; |
---|
132 | char dst=(argc>2)?*argv[2]:'0'; |
---|
133 | |
---|
134 | printf("*** source genotype:\n"); |
---|
135 | Geno g1(src); |
---|
136 | printGen(g1); |
---|
137 | MultiMap m; |
---|
138 | Geno g2=g1.getConverted(dst,&m); |
---|
139 | printf("*** converted to f%c:\n",dst); |
---|
140 | printGen(g2); |
---|
141 | if (m.isEmpty()) |
---|
142 | printf("(conversion map not available)\n"); |
---|
143 | else |
---|
144 | { |
---|
145 | printf("conversion map:\n"); |
---|
146 | m.print(); |
---|
147 | printConvMap(g1.getGene(),g2.getGene(),m); |
---|
148 | printf("reverse conversion map:\n"); |
---|
149 | MultiMap rm; |
---|
150 | rm.addReversed(m); |
---|
151 | rm.print(); |
---|
152 | printConvMap(g2.getGene(),g1.getGene(),rm); |
---|
153 | } |
---|
154 | |
---|
155 | Model mod1(g1,1); |
---|
156 | printf("\nmodel map for f%c genotype:\n",g1.getFormat()); |
---|
157 | printModelMap(g1.getGene(),mod1.getMap()); |
---|
158 | Model mod2(g2,1); |
---|
159 | printf("\nmodel map for f%c genotype:\n",g2.getFormat()); |
---|
160 | printModelMap(g2.getGene(),mod2.getMap()); |
---|
161 | return 0; |
---|
162 | } |
---|