[286] | 1 | // This file is a part of Framsticks SDK. http://www.framsticks.com/ |
---|
[534] | 2 | // Copyright (C) 1999-2016 Maciej Komosinski and Szymon Ulatowski. |
---|
[286] | 3 | // See LICENSE.txt for details. |
---|
[109] | 4 | |
---|
| 5 | #ifndef _GENO_H_ |
---|
| 6 | #define _GENO_H_ |
---|
| 7 | |
---|
| 8 | #include <frams/util/sstring.h> |
---|
| 9 | #include <frams/util/extvalue.h> |
---|
| 10 | |
---|
| 11 | class MultiMap; |
---|
| 12 | class Geno; |
---|
[145] | 13 | class GenoConvManager; |
---|
[109] | 14 | |
---|
| 15 | class GenoValidator |
---|
| 16 | { |
---|
[150] | 17 | public: |
---|
| 18 | virtual int testGenoValidity(Geno& g) = 0;/// -1=no information 0=invalid 1=valid |
---|
[492] | 19 | virtual ~GenoValidator() { }; |
---|
[109] | 20 | }; |
---|
| 21 | |
---|
[150] | 22 | /// Basic GenoValidator that works by building a Model from any Geno (by converting to f0). |
---|
| 23 | /// Validation fails when the model can't be built or the genotype can't be converted. |
---|
| 24 | class ModelGenoValidator : public GenoValidator |
---|
[145] | 25 | { |
---|
| 26 | public: |
---|
| 27 | int testGenoValidity(Geno& g); |
---|
| 28 | }; |
---|
| 29 | |
---|
[109] | 30 | /// basic information about a single genotype. |
---|
[150] | 31 | class Geno : public DestrBase |
---|
[109] | 32 | { |
---|
[150] | 33 | friend class Simulator;//needs to access validators directly |
---|
| 34 | SString gen; |
---|
| 35 | SString name; |
---|
| 36 | char format; |
---|
| 37 | SString txt; |
---|
| 38 | int isvalid; ///< <0 -> unknown >=0 -> value for "isValid" |
---|
[109] | 39 | |
---|
[150] | 40 | Geno *f0gen; |
---|
[109] | 41 | |
---|
[150] | 42 | int mapinshift; /// number of characters in the initial comment |
---|
| 43 | int mapoutshift; /// number of characters in the output comment |
---|
| 44 | int multiline; |
---|
[109] | 45 | |
---|
[150] | 46 | void init(const SString& genstring, char genformat, const SString& genname, const SString& comment); |
---|
| 47 | void validate(void); |
---|
[109] | 48 | |
---|
[150] | 49 | void freeF0(); |
---|
[109] | 50 | |
---|
[150] | 51 | bool isInvalid() { return isvalid == 0; } |
---|
[109] | 52 | |
---|
[150] | 53 | friend class Model; |
---|
| 54 | friend class GenoConvManager; |
---|
| 55 | |
---|
[109] | 56 | public: |
---|
[530] | 57 | static const char INVALID_FORMAT = '!'; |
---|
[346] | 58 | typedef SListTempl<GenoValidator*> Validators; |
---|
| 59 | |
---|
[150] | 60 | /// create a genotype object from primitives |
---|
| 61 | /// @param genstring pure genotype, without any comments |
---|
| 62 | /// @param genformat genotype format |
---|
| 63 | /// @param comment information about genotype (for genetic operators and "history") |
---|
| 64 | Geno(const char *genstring = 0, char genformat = -1, const char *genname = 0, const char *comment = 0); |
---|
[109] | 65 | |
---|
[150] | 66 | /// create a genotype object from primitives |
---|
| 67 | /// @param genstring pure genotype, wihtout any comments |
---|
| 68 | /// @param genformat genotype format |
---|
| 69 | /// @param name genotype name, new name will generated if needed |
---|
| 70 | /// @param comment information about genotype (for genetic operators and "history") |
---|
| 71 | Geno(const SString& genstring, char genformat, const SString& genname, const SString& comment); |
---|
[109] | 72 | |
---|
[150] | 73 | /// create object from full string, containing optional format and comment information |
---|
| 74 | Geno(const SString & fullstring); |
---|
[109] | 75 | |
---|
[150] | 76 | /// clone |
---|
| 77 | Geno(const Geno& src); |
---|
[109] | 78 | |
---|
[150] | 79 | void operator=(const Geno& src); |
---|
[109] | 80 | |
---|
[150] | 81 | ~Geno(); |
---|
[109] | 82 | |
---|
[150] | 83 | void setValid(int v) { isvalid = v; } |
---|
| 84 | int getValid() { return isvalid; } |
---|
[109] | 85 | |
---|
[150] | 86 | /// return string representation, with format comment at the beginning |
---|
[534] | 87 | SString getGenesAndFormat(void) const; |
---|
[109] | 88 | |
---|
[150] | 89 | void setString(const SString& genewithcomments); |
---|
[109] | 90 | |
---|
[534] | 91 | /** @param genformat=-1 -> detect genotype format from genstring comment (like the constructor does), else specify the valid format in genformat and pure genes in genstring. */ |
---|
| 92 | void setGenesAndFormat(const SString& genstring, char genformat=-1); |
---|
| 93 | /** g must be pure genes, without format. For the standard behavior use setGenesAndFormat() */ |
---|
| 94 | void setGenesAssumingSameFormat(const SString& g); |
---|
| 95 | SString getGenes(void) const; |
---|
[109] | 96 | |
---|
[150] | 97 | SString getName(void) const; |
---|
| 98 | void setName(const SString&); |
---|
| 99 | char getFormat(void) const; |
---|
[109] | 100 | |
---|
[150] | 101 | SString getComment(void) const; |
---|
| 102 | void setComment(const SString&); |
---|
[109] | 103 | |
---|
[150] | 104 | /// invalid genotype cannot be used to build a creature |
---|
| 105 | bool isValid(void); |
---|
[109] | 106 | |
---|
[150] | 107 | /// make converted version of the genotype. |
---|
| 108 | /// @param converter_missing optional output parameter (ignored when NULL). Receives true if the conversion fails because of the lack of appropriate converter(s) (the returned Geno is always invalid in this case). Receives false if the genotype was converted by a converter or a converter chain (the returned Geno can be valid or invalid, depending on the converter's decision). |
---|
| 109 | Geno getConverted(char otherformat, MultiMap *m = 0, bool *converter_missing = NULL); |
---|
[109] | 110 | |
---|
[150] | 111 | /// @return -1 = before first char in the string |
---|
| 112 | /// @return -2 = after last char in the string |
---|
| 113 | int mapGenToString(int genpos) const; |
---|
| 114 | /// @return -1 = before first char in the genotype |
---|
| 115 | /// @return -2 = after last char in the genotype |
---|
| 116 | int mapStringToGen(int stringpos) const; |
---|
[109] | 117 | |
---|
[150] | 118 | int operator==(const Geno &g) { return (format == g.format) && (gen == g.gen); } |
---|
[109] | 119 | |
---|
[150] | 120 | void* owner; |
---|
[145] | 121 | |
---|
[150] | 122 | // managing global Geno-related objects (used for validation and conversion) |
---|
[348] | 123 | static Validators* useValidators(Validators* val); |
---|
| 124 | static Validators* getValidators(); |
---|
| 125 | |
---|
| 126 | static GenoConvManager* useConverters(GenoConvManager* gcm); |
---|
| 127 | static GenoConvManager* getConverters(); |
---|
[109] | 128 | }; |
---|
| 129 | |
---|
| 130 | #ifndef NO_GENOCONVMANAGER |
---|
| 131 | #include "genoconv.h" |
---|
| 132 | #endif |
---|
| 133 | |
---|
| 134 | #endif |
---|