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

Last change on this file since 438 was 391, checked in by sz, 10 years ago

Moved frams/loggers to common/loggers

  • Property svn:eol-style set to native
File size: 3.9 KB
RevLine 
[286]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.
[109]4
5#include <ctype.h>
[129]6#include <frams/genetics/defgenoconv.h>
[109]7#include <frams/model/model.h>
8#include <frams/util/multimap.h>
9
10#include "printconvmap.h"
[391]11#include <common/loggers/loggertostdout.h>
[109]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
22class GenoConv_Test: public GenoConverter
23{
24public:
25GenoConv_Test()
26        {
27        name="Test Converter";
28        in_format='x';
29        }
30SString 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).
36class GenoConv_Test2: public GenoConverter
37{
38public:
39GenoConv_Test2()
40        {
41        name="Test Converter #2";
42        in_format='y';
43        }
44SString convert(SString &i,MultiMap *map)
45{
46Model mod;
47mod.open();
48mod.singleStepBuild("p:");
49mod.singleStepBuild("p:");
50mod.singleStepBuild("j:0,1");
51mod.getPart(1)->p=Pt3D(0,0.2,-1);
52mod.close();
53return 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
61class GenoConv_Test3: public GenoConverter
62{
63public:
64GenoConv_Test3()
65        {
66        name="Test Converter #3";
67        in_format='z';
68        out_format='1';
69        mapsupport=1;
70        }
71SString convert(SString &in,MultiMap *map);
72~GenoConv_Test3() {}
73};
74
75/** main converting routine - most important: direct conversion map example */
76SString GenoConv_Test3::convert(SString &in,MultiMap *map)
77{
78SString dst;
[348]79const char* src=in.c_str();
[109]80const char* t;
81int insideneuron=0;
82int n;
83for(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        }
105return dst;
106}
107
108
109///////////////////////////////////////////////
110
111void printGen(Geno &g)
112{
113printf("Genotype:\n%s\nFormat: %c\nValid: %s\nComment: %s\n",
[348]114        g.getGene().c_str(),g.getFormat(),g.isValid()?"yes":"no",g.getComment().c_str());
[109]115}
116
117int main(int argc,char *argv[])
118{
[375]119LoggerToStdout messages_to_stdout(LoggerBase::Enable);
[145]120
[129]121DefaultGenoConvManager gcm;
[145]122gcm.addDefaultConverters();
[109]123gcm.addConverter(new GenoConv_Test());
124gcm.addConverter(new GenoConv_Test2());
125gcm.addConverter(new GenoConv_Test3());
[348]126Geno::useConverters(&gcm);
[109]127
[348]128Geno::Validators validators;
[145]129ModelGenoValidator model_validator;
[348]130validators+=&model_validator;
131Geno::useValidators(&validators);
[145]132
[109]133const char* src=(argc>1)?argv[1]:"X";
134char dst=(argc>2)?*argv[2]:'0';
135
136printf("*** source genotype:\n");
137Geno g1(src);
138printGen(g1);
139MultiMap m;
140Geno g2=g1.getConverted(dst,&m);
141printf("*** converted to f%c:\n",dst);
142printGen(g2);
143if (m.isEmpty())
144        printf("(conversion map not available)\n");
145else
146        {
147        printf("conversion map:\n");
148        m.print();
149        printConvMap(g1.getGene(),g2.getGene(),m);
150        printf("reverse conversion map:\n");
151        MultiMap rm;
152        rm.addReversed(m);
153        rm.print();
154        printConvMap(g2.getGene(),g1.getGene(),rm);
155        }
156
157Model mod1(g1,1);
158printf("\nmodel map for f%c genotype:\n",g1.getFormat());
159printModelMap(g1.getGene(),mod1.getMap());
160Model mod2(g2,1);
161printf("\nmodel map for f%c genotype:\n",g2.getFormat());
162printModelMap(g2.getGene(),mod2.getMap());
163return 0;
164}
Note: See TracBrowser for help on using the repository browser.