source: cpp/frams/_demos/genoconv_test.cpp @ 136

Last change on this file since 136 was 136, checked in by Maciej Komosinski, 6 years ago

renamed conv_test => genoconv_test

  • Property svn:eol-style set to native
File size: 3.8 KB
Line 
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
13StdoutErrorHandler err;
14
15/**
16 @file
17 Sample code: Genotype converter class
18*/
19
20/// Sample Geno converter not using Model class.
21/// (this converter generates the same output for each input).
22/// such a converter is responsible for doing valid
23/// f0 (or other format) output and storing temporary data
24class GenoConv_Test: public GenoConverter
25{
26public:
27GenoConv_Test()
28        {
29        name="Test Converter";
30        in_format='x';
31        }
32SString convert(SString &i,MultiMap *map) {return SString("after conversion...");}
33~GenoConv_Test() {}
34};
35
36/// Sample Geno converter using Model class.
37/// (this converter generates the same output for each input).
38class GenoConv_Test2: public GenoConverter
39{
40public:
41GenoConv_Test2()
42        {
43        name="Test Converter #2";
44        in_format='y';
45        }
46SString convert(SString &i,MultiMap *map)
47{
48Model mod;
49mod.open();
50mod.singleStepBuild("p:");
51mod.singleStepBuild("p:");
52mod.singleStepBuild("j:0,1");
53mod.getPart(1)->p=Pt3D(0,0.2,-1);
54mod.close();
55return mod.getGeno().getGene();
56}
57~GenoConv_Test2() {}
58};
59
60/// Sample Geno converter supporting conversion mapping.
61/// the conversion is very simple: any sequence of <digit><character>
62/// (but not inside neurons) is replaced by the repeated sequence of the character
63class GenoConv_Test3: public GenoConverter
64{
65public:
66GenoConv_Test3()
67        {
68        name="Test Converter #3";
69        in_format='z';
70        out_format='1';
71        mapsupport=1;
72        }
73SString convert(SString &in,MultiMap *map);
74~GenoConv_Test3() {}
75};
76
77/** main converting routine - most important: direct conversion map example */
78SString GenoConv_Test3::convert(SString &in,MultiMap *map)
79{
80SString dst;
81const char* src=in;
82const char* t;
83int insideneuron=0;
84int n;
85for(t=src;*t;t++)
86        {
87        if (insideneuron&&*t==']') insideneuron=0;
88        if (*t=='[') insideneuron=1;
89        if ((!insideneuron)&&isdigit(*t)&&t[1])
90                { // special sequence detected!
91                n=*t-'0';
92                t++; // *t will be repeated 'n' times
93                for (int i=0;i<n;i++)
94                        dst+=*t;
95                if (map) // fill in the map only if requested
96                        map->add(t-src, t-src, dst.len()-n, dst.len()-1);
97                // meaning: source character (t-src) becomes (dst.len()-n ... dst.len()-1)
98                }
99        else
100                {
101                dst+=*t;
102                if (map)
103                        map->add(t-src, t-src, dst.len()-1, dst.len()-1);
104                // meaning: map single to single character: (t-src) into (dst.len()-1)
105                }
106        }
107return dst;
108}
109
110
111///////////////////////////////////////////////
112
113void printGen(Geno &g)
114{
115printf("Genotype:\n%s\nFormat: %c\nValid: %s\nComment: %s\n",
116        (const char*)g.getGene(),g.getFormat(),g.isValid()?"yes":"no",(const char*)g.getComment());
117}
118
119int main(int argc,char *argv[])
120{
121DefaultGenoConvManager gcm;
122gcm.addConverter(new GenoConv_Test());
123gcm.addConverter(new GenoConv_Test2());
124gcm.addConverter(new GenoConv_Test3());
125
126const char* src=(argc>1)?argv[1]:"X";
127char dst=(argc>2)?*argv[2]:'0';
128
129printf("*** source genotype:\n");
130Geno g1(src);
131printGen(g1);
132MultiMap m;
133Geno g2=g1.getConverted(dst,&m);
134printf("*** converted to f%c:\n",dst);
135printGen(g2);
136if (m.isEmpty())
137        printf("(conversion map not available)\n");
138else
139        {
140        printf("conversion map:\n");
141        m.print();
142        printConvMap(g1.getGene(),g2.getGene(),m);
143        printf("reverse conversion map:\n");
144        MultiMap rm;
145        rm.addReversed(m);
146        rm.print();
147        printConvMap(g2.getGene(),g1.getGene(),rm);
148        }
149
150Model mod1(g1,1);
151printf("\nmodel map for f%c genotype:\n",g1.getFormat());
152printModelMap(g1.getGene(),mod1.getMap());
153Model mod2(g2,1);
154printf("\nmodel map for f%c genotype:\n",g2.getFormat());
155printModelMap(g2.getGene(),mod2.getMap());
156return 0;
157}
Note: See TracBrowser for help on using the repository browser.