source: cpp/frams/_demos/conv_test.cpp @ 113

Last change on this file since 113 was 109, checked in by sz, 10 years ago

source reorganization (see README)
new feature added: part/joint shapes (see frams/_demos/part_shapes.cpp)

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