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

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

The sample 'y' format converter returns f0 genotype now instead of empty genotype; code formatting

  • Property svn:eol-style set to native
File size: 4.5 KB
Line 
1// This file is a part of Framsticks SDK.  http://www.framsticks.com/
2// Copyright (C) 1999-2018  Maciej Komosinski and Szymon Ulatowski.
3// See LICENSE.txt for details.
4
5#include <ctype.h>
6#include <frams/genetics/defgenoconv.h>
7#include <frams/model/model.h>
8#include <frams/util/multimap.h>
9#include <common/virtfile/stdiofile.h>
10#include "printconvmap.h"
11#include <common/loggers/loggertostdout.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 f0 (or other format) output and storing temporary data.
21class GenoConv_Test : public GenoConverter
22{
23public:
24        GenoConv_Test()
25        {
26                name = "Test Converter";
27                in_format = 'x';
28        }
29        SString convert(SString &i, MultiMap *map) { return SString("after conversion..."); }
30        ~GenoConv_Test() {}
31};
32
33/// Sample Geno converter using Model class.
34/// (This converter generates the same output for each input).
35class GenoConv_Test2 : public GenoConverter
36{
37public:
38        GenoConv_Test2()
39        {
40                name = "Test Converter #2";
41                in_format = 'y';
42        }
43
44        SString convert(SString &i, MultiMap *map)
45        {
46                Model mod;
47                mod.open();
48                mod.addFromString(Model::PartType, "0,0,0");
49                mod.addFromString(Model::PartType, "0,0,-1");
50                mod.addFromString(Model::JointType, "0,1");
51                mod.getPart(1)->p.y += 0.2; //as an example, directly modify position of part #1
52                mod.close();
53                return mod.getF0Geno().getGenes();
54        }
55       
56        ~GenoConv_Test2() {}
57};
58
59/// Sample Geno converter supporting conversion mapping.
60/// The conversion is very simple: any sequence of <digit><character>
61/// (but not inside neurons) is replaced by the repeated sequence of the character.
62class GenoConv_Test3 : public GenoConverter
63{
64public:
65        GenoConv_Test3()
66        {
67                name = "Test Converter #3";
68                in_format = 'z';
69                out_format = '1';
70                mapsupport = 1;
71        }
72        SString convert(SString &in, MultiMap *map);
73        ~GenoConv_Test3() {}
74};
75
76/** main converting routine - most important: direct conversion map example */
77SString GenoConv_Test3::convert(SString &in, MultiMap *map)
78{
79        SString dst;
80        const char* src = in.c_str();
81        const char* t;
82        int insideneuron = 0;
83        int n;
84        for (t = src; *t; t++)
85        {
86                if (insideneuron&&*t == ']') insideneuron = 0;
87                if (*t == '[') insideneuron = 1;
88                if ((!insideneuron) && isdigit(*t) && t[1])
89                { // special sequence detected!
90                        n = *t - '0';
91                        t++; // *t will be repeated 'n' times
92                        for (int i = 0; i < n; i++)
93                                dst += *t;
94                        if (map) // fill in the map only if requested
95                                map->add(t - src, t - src, dst.len() - n, dst.len() - 1);
96                        // meaning: source character (t-src) becomes (dst.len()-n ... dst.len()-1)
97                }
98                else
99                {
100                        dst += *t;
101                        if (map)
102                                map->add(t - src, t - src, dst.len() - 1, dst.len() - 1);
103                        // meaning: map single to single character: (t-src) into (dst.len()-1)
104                }
105        }
106        return dst;
107}
108
109
110///////////////////////////////////////////////
111
112void printGen(Geno &g)
113{
114        printf("Genotype:\n%s\nFormat: %c\nValid: %s\nComment: %s\n",
115                g.getGenes().c_str(), g.getFormat(), g.isValid() ? "yes" : "no", g.getComment().c_str());
116}
117
118int main(int argc, char *argv[])
119{
120        LoggerToStdout messages_to_stdout(LoggerBase::Enable);
121
122        DefaultGenoConvManager gcm;
123        gcm.addDefaultConverters();
124        gcm.addConverter(new GenoConv_Test());
125        gcm.addConverter(new GenoConv_Test2());
126        gcm.addConverter(new GenoConv_Test3());
127        Geno::useConverters(&gcm);
128
129        Geno::Validators validators;
130        ModelGenoValidator model_validator;
131        validators += &model_validator;
132        Geno::useValidators(&validators);
133
134        SString src;
135        if (argc > 1)
136        {
137                src = argv[1];
138                if (src == "-")
139                {
140                        StdioFILEDontClose in(stdin);
141                        src = "";
142                        loadSString(&in, src, false);
143                }
144        }
145        else
146                src = "X";
147        char dst = (argc > 2) ? *argv[2] : '0';
148
149        printf("*** Source genotype:\n");
150        Geno g1(src);
151        printGen(g1);
152        MultiMap m;
153        Geno g2 = g1.getConverted(dst, &m);
154        printf("*** Converted to f%c:\n", dst);
155        printGen(g2);
156        if (m.isEmpty())
157                printf("(conversion map not available)\n");
158        else
159        {
160                printf("Conversion map:\n");
161                m.print();
162                printConvMap(g1.getGenes(), g2.getGenes(), m);
163                printf("Reverse conversion map:\n");
164                MultiMap rm;
165                rm.addReversed(m);
166                rm.print();
167                printConvMap(g2.getGenes(), g1.getGenes(), rm);
168        }
169
170        Model mod1(g1, 1);
171        printf("\nModel map for f%c genotype:\n", g1.getFormat());
172        printModelMap(g1.getGenes(), mod1.getMap());
173        mod1.getMap().print();
174        Model mod2(g2, 1);
175        printf("\nModel map for f%c genotype:\n", g2.getFormat());
176        printModelMap(g2.getGenes(), mod2.getMap());
177        mod2.getMap().print();
178        return 0;
179}
Note: See TracBrowser for help on using the repository browser.