Changeset 727 for cpp/frams/_demos
- Timestamp:
- 01/25/18 01:31:28 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
cpp/frams/_demos/genoconv_test.cpp
r534 r727 1 1 // This file is a part of Framsticks SDK. http://www.framsticks.com/ 2 // Copyright (C) 1999-201 5Maciej Komosinski and Szymon Ulatowski.2 // Copyright (C) 1999-2018 Maciej Komosinski and Szymon Ulatowski. 3 3 // See LICENSE.txt for details. 4 4 … … 14 14 @file 15 15 Sample code: Genotype converter class 16 */16 */ 17 17 18 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 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. 21 class GenoConv_Test : public GenoConverter 23 22 { 24 23 public: 25 GenoConv_Test()24 GenoConv_Test() 26 25 { 27 name="Test Converter";28 in_format='x';26 name = "Test Converter"; 27 in_format = 'x'; 29 28 } 30 SString convert(SString &i,MultiMap *map) {return SString("after conversion...");}31 ~GenoConv_Test() {}29 SString convert(SString &i, MultiMap *map) { return SString("after conversion..."); } 30 ~GenoConv_Test() {} 32 31 }; 33 32 34 33 /// Sample Geno converter using Model class. 35 /// ( this converter generates the same output for each input).36 class GenoConv_Test2 : public GenoConverter34 /// (This converter generates the same output for each input). 35 class GenoConv_Test2 : public GenoConverter 37 36 { 38 37 public: 39 GenoConv_Test2()38 GenoConv_Test2() 40 39 { 41 name="Test Converter #2";42 in_format='y';40 name = "Test Converter #2"; 41 in_format = 'y'; 43 42 } 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().getGenes(); 54 } 55 ~GenoConv_Test2() {} 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() {} 56 57 }; 57 58 58 59 /// 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 GenoConverter60 /// The conversion is very simple: any sequence of <digit><character> 61 /// (but not inside neurons) is replaced by the repeated sequence of the character. 62 class GenoConv_Test3 : public GenoConverter 62 63 { 63 64 public: 64 GenoConv_Test3()65 GenoConv_Test3() 65 66 { 66 name="Test Converter #3";67 in_format='z';68 out_format='1';69 mapsupport=1;67 name = "Test Converter #3"; 68 in_format = 'z'; 69 out_format = '1'; 70 mapsupport = 1; 70 71 } 71 SString convert(SString &in,MultiMap *map);72 ~GenoConv_Test3() {}72 SString convert(SString &in, MultiMap *map); 73 ~GenoConv_Test3() {} 73 74 }; 74 75 75 76 /** main converting routine - most important: direct conversion map example */ 76 SString GenoConv_Test3::convert(SString &in, MultiMap *map)77 SString GenoConv_Test3::convert(SString &in, MultiMap *map) 77 78 { 78 SString dst;79 const char* src=in.c_str();80 const char* t;81 int insideneuron=0;82 int n;83 for(t=src;*t;t++)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++) 84 85 { 85 if (insideneuron&&*t==']') insideneuron=0;86 if (*t=='[') insideneuron=1;87 if ((!insideneuron)&&isdigit(*t)&&t[1])86 if (insideneuron&&*t == ']') insideneuron = 0; 87 if (*t == '[') insideneuron = 1; 88 if ((!insideneuron) && isdigit(*t) && t[1]) 88 89 { // special sequence detected! 89 n=*t-'0';90 t++; // *t will be repeated 'n' times91 for (int i=0;i<n;i++)92 dst+=*t;93 if (map) // fill in the map only if requested94 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)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) 96 97 } 97 else98 else 98 99 { 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)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) 103 104 } 104 105 } 105 return dst;106 return dst; 106 107 } 107 108 … … 111 112 void printGen(Geno &g) 112 113 { 113 printf("Genotype:\n%s\nFormat: %c\nValid: %s\nComment: %s\n",114 g.getGenes().c_str(),g.getFormat(),g.isValid()?"yes":"no",g.getComment().c_str());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()); 115 116 } 116 117 117 int main(int argc, char *argv[])118 int main(int argc, char *argv[]) 118 119 { 119 LoggerToStdout messages_to_stdout(LoggerBase::Enable);120 LoggerToStdout messages_to_stdout(LoggerBase::Enable); 120 121 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);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); 127 128 128 Geno::Validators validators;129 ModelGenoValidator model_validator;130 validators+=&model_validator;131 Geno::useValidators(&validators);129 Geno::Validators validators; 130 ModelGenoValidator model_validator; 131 validators += &model_validator; 132 Geno::useValidators(&validators); 132 133 133 SString src;134 if (argc>1)134 SString src; 135 if (argc > 1) 135 136 { 136 src=argv[1];137 if (src=="-")137 src = argv[1]; 138 if (src == "-") 138 139 { 139 StdioFILEDontClose in(stdin);140 src="";141 loadSString(&in,src,false);140 StdioFILEDontClose in(stdin); 141 src = ""; 142 loadSString(&in, src, false); 142 143 } 143 144 } 144 else145 src="X";146 char dst=(argc>2)?*argv[2]:'0';145 else 146 src = "X"; 147 char dst = (argc > 2) ? *argv[2] : '0'; 147 148 148 printf("*** source genotype:\n");149 Geno g1(src);150 printGen(g1);151 MultiMap m;152 Geno g2=g1.getConverted(dst,&m);153 printf("*** converted to f%c:\n",dst);154 printGen(g2);155 if (m.isEmpty())156 printf("(conversion map not available)\n");157 else149 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 158 159 { 159 printf("conversion map:\n");160 m.print();161 printConvMap(g1.getGenes(),g2.getGenes(),m);162 printf("reverse conversion map:\n");163 MultiMap rm;164 rm.addReversed(m);165 rm.print();166 printConvMap(g2.getGenes(),g1.getGenes(),rm);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); 167 168 } 168 169 169 Model mod1(g1,1);170 printf("\nmodel map for f%c genotype:\n",g1.getFormat());171 printModelMap(g1.getGenes(),mod1.getMap());172 mod1.getMap().print();173 Model mod2(g2,1);174 printf("\nmodel map for f%c genotype:\n",g2.getFormat());175 printModelMap(g2.getGenes(),mod2.getMap());176 mod2.getMap().print();177 return 0;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; 178 179 }
Note: See TracChangeset
for help on using the changeset viewer.