[286] | 1 | // This file is a part of Framsticks SDK. http://www.framsticks.com/ |
---|
[972] | 2 | // Copyright (C) 1999-2020 Maciej Komosinski and Szymon Ulatowski. |
---|
[286] | 3 | // See LICENSE.txt for details. |
---|
[138] | 4 | |
---|
| 5 | #include "genoobj.h" |
---|
| 6 | #include <frams/util/extvalue.h> |
---|
| 7 | #include <frams/model/autoname.h> |
---|
[203] | 8 | #include "collectionobj.h" |
---|
[138] | 9 | |
---|
| 10 | #define FIELDSTRUCT GenoObj |
---|
[420] | 11 | ParamEntry geno_paramtab[] = |
---|
[138] | 12 | { |
---|
[732] | 13 | { "Geno", 1, 16, "Geno", "All information about a single genotype.\nThis is a genetics-only object which does not contain any performance data. See also: Genotype class" }, |
---|
[420] | 14 | { "name", 0, PARAM_NOSTATIC, "Name", "s 0 40", GETSET(name), }, |
---|
| 15 | { "rawgenotype", 0, PARAM_NOSTATIC | PARAM_READONLY, "Raw genotype", "s 1", GETONLY(genotype), "Genotype, excluding the format specifier" }, |
---|
| 16 | { "info", 0, PARAM_NOSTATIC, "Info", "s 1", GETSET(info), "Additional information or comments", }, |
---|
[516] | 17 | { "format", 0, PARAM_NOSTATIC | PARAM_READONLY, "Format", "s", GETONLY(format), "Genotype format", }, |
---|
[420] | 18 | { "genotype", 0, PARAM_NOSTATIC | PARAM_READONLY, "Genotype", "s 1", GETONLY(string), "Genes as a string of characters", }, |
---|
| 19 | { "isValid", 0, PARAM_NOSTATIC | PARAM_READONLY | PARAM_DEPRECATED, "Valid", "d 0 1", GETONLY(isvalid), "Use 'is_valid' instead of 'isValid'." }, |
---|
| 20 | { "is_valid", 0, PARAM_NOSTATIC, "Validity", "d -1 1 -1", GETSET(isvalid), |
---|
| 21 | "0 = invalid genotype\n" |
---|
| 22 | "1 = valid genotype\n" |
---|
| 23 | "-1 = validity is not known. This is a transient state. The value of \"is_valid\" will never be -1 when read. It is safe to treat is_valid as boolean in statements like \"if (g.is_valid) ...\". Setting \"is_valid=-1\" will make it 0 or 1 again. This third state (-1) is only needed for loading Genotype objects from files where the \"is_valid\" field might not be present." |
---|
| 24 | }, |
---|
[1158] | 25 | { "getConverted", 0, PARAM_READONLY | PARAM_NOSTATIC, "Get converted genotype", "p oGeno(s format)", PROCEDURE(p_getconvert), }, |
---|
| 26 | { "getConvertedWithCheckpoints", 0, PARAM_READONLY | PARAM_NOSTATIC, "Get converted genotype", "p oGeno(s format)", PROCEDURE(p_getconvert_ch), "See also Model.newWithCheckpoints()" }, |
---|
[420] | 27 | { "f0genotype", 0, PARAM_NOSTATIC | PARAM_READONLY, "f0 genotype", "s 1", GETONLY(f0genotype), "converted to f0 genotype", }, |
---|
| 28 | { "new", 0, 0, "create new empty object", "p oGeno()", PROCEDURE(p_new), }, |
---|
| 29 | { "newFromString", 0, 0, "create new object from supplied string argument", "p oGeno(s genotype)", PROCEDURE(p_newfromstring), }, |
---|
[516] | 30 | { "newFrom", 0, 0, "create new object", "p oGeno(s genotype,s format,s name,s description)", PROCEDURE(p_newfrom), }, |
---|
[420] | 31 | { "autoname", 0, PARAM_NOSTATIC | PARAM_READONLY, "Autogenerated name", "s", GETONLY(autoname), }, |
---|
| 32 | { "toVector", 0, PARAM_READONLY | PARAM_NOSTATIC, "serialization support", "oVector", GETONLY(toVector), }, |
---|
| 33 | { "newFromVector", 0, 0, "serialization support", "p oGeno(oVector)", PROCEDURE(p_newfromvector), }, |
---|
| 34 | { 0, 0, 0, }, |
---|
[138] | 35 | }; |
---|
| 36 | #undef FIELDSTRUCT |
---|
| 37 | |
---|
| 38 | void GenoObj::get_isvalid(ExtValue *ret) |
---|
[420] | 39 | { |
---|
| 40 | ret->setInt(isValid()); |
---|
| 41 | } |
---|
[138] | 42 | |
---|
[415] | 43 | int GenoObj::set_isvalid(const ExtValue *v) |
---|
| 44 | { |
---|
[420] | 45 | paInt n = v->getInt(); |
---|
| 46 | if (getValid() != n) |
---|
[415] | 47 | { |
---|
[420] | 48 | setValid(n); |
---|
| 49 | return PSET_CHANGED; |
---|
[415] | 50 | } |
---|
[420] | 51 | return 0; |
---|
[415] | 52 | } |
---|
| 53 | |
---|
[138] | 54 | void GenoObj::get_genotype(ExtValue *ret) |
---|
[420] | 55 | { |
---|
[534] | 56 | ret->setString(getGenes()); |
---|
[420] | 57 | } |
---|
[138] | 58 | |
---|
| 59 | void GenoObj::get_name(ExtValue *ret) |
---|
[420] | 60 | { |
---|
| 61 | ret->setString(getName()); |
---|
| 62 | } |
---|
[138] | 63 | |
---|
| 64 | void GenoObj::get_autoname(ExtValue *ret) |
---|
| 65 | { |
---|
[999] | 66 | Model m(*this, Model::SHAPETYPE_UNKNOWN); |
---|
[420] | 67 | ret->setString(AutoName::makeName(m)); |
---|
[138] | 68 | } |
---|
| 69 | |
---|
| 70 | int GenoObj::set_name(const ExtValue *v) |
---|
[420] | 71 | { |
---|
| 72 | setName(v->getString()); |
---|
| 73 | return PSET_CHANGED; |
---|
| 74 | } |
---|
[138] | 75 | |
---|
| 76 | void GenoObj::get_info(ExtValue *ret) |
---|
[420] | 77 | { |
---|
| 78 | ret->setString(getComment()); |
---|
| 79 | } |
---|
[138] | 80 | |
---|
| 81 | void GenoObj::get_string(ExtValue *ret) |
---|
[420] | 82 | { |
---|
[534] | 83 | ret->setString(getGenesAndFormat()); |
---|
[420] | 84 | } |
---|
[138] | 85 | |
---|
| 86 | void GenoObj::get_format(ExtValue *ret) |
---|
[420] | 87 | { |
---|
[955] | 88 | ret->setString(getFormat()); |
---|
[420] | 89 | } |
---|
[138] | 90 | |
---|
| 91 | int GenoObj::set_info(const ExtValue *v) |
---|
[420] | 92 | { |
---|
| 93 | setComment(v->getString()); |
---|
| 94 | return PSET_CHANGED; |
---|
| 95 | } |
---|
[138] | 96 | |
---|
| 97 | void GenoObj::get_f0genotype(ExtValue *ret) |
---|
[420] | 98 | { |
---|
[972] | 99 | ret->setString(getConverted(Geno::F0_FORMAT_LIST).getGenes()); |
---|
[420] | 100 | } |
---|
[138] | 101 | |
---|
[955] | 102 | SString GenoObj::formatFromExtValue(ExtValue& v) |
---|
[516] | 103 | { |
---|
[732] | 104 | if (v.getType() == TInt) |
---|
[955] | 105 | { |
---|
| 106 | char ch = v.getInt(); |
---|
[972] | 107 | return SString(&ch, 1); |
---|
[955] | 108 | } |
---|
[732] | 109 | if (v.getType() == TString) |
---|
[516] | 110 | { |
---|
[955] | 111 | return v.getString(); |
---|
[516] | 112 | } |
---|
[988] | 113 | return Geno::FORMAT_INVALID; |
---|
[516] | 114 | } |
---|
| 115 | |
---|
[420] | 116 | void GenoObj::p_getconvert(ExtValue *args, ExtValue *ret) |
---|
| 117 | { |
---|
[516] | 118 | *ret = makeDynamicObjectAndDecRef(new Geno(getConverted(formatFromExtValue(args[0])))); |
---|
[420] | 119 | } |
---|
[138] | 120 | |
---|
[732] | 121 | void GenoObj::p_getconvert_ch(ExtValue *args, ExtValue *ret) |
---|
| 122 | { |
---|
| 123 | *ret = makeDynamicObjectAndDecRef(new Geno(getConverted(formatFromExtValue(args[0]), NULL, true))); |
---|
| 124 | } |
---|
| 125 | |
---|
[420] | 126 | void GenoObj::p_new(ExtValue *args, ExtValue *ret) |
---|
| 127 | { |
---|
| 128 | *ret = makeDynamicObjectAndDecRef(new Geno()); |
---|
| 129 | } |
---|
[138] | 130 | |
---|
[420] | 131 | void GenoObj::p_newfromstring(ExtValue *args, ExtValue *ret) |
---|
| 132 | { |
---|
| 133 | *ret = makeDynamicObjectAndDecRef(new Geno(args[0].getString())); |
---|
| 134 | } |
---|
[138] | 135 | |
---|
[420] | 136 | void GenoObj::p_newfrom(ExtValue *args, ExtValue *ret) |
---|
| 137 | { |
---|
[516] | 138 | *ret = makeDynamicObjectAndDecRef(new Geno(args[3].getString(), formatFromExtValue(args[2]), |
---|
[420] | 139 | args[1].getString(), args[0].getString())); |
---|
| 140 | } |
---|
[138] | 141 | |
---|
| 142 | Param& GenoObj::getStaticParam() |
---|
| 143 | { |
---|
| 144 | #ifdef __CODEGUARD__ |
---|
[420] | 145 | static GenoObj static_genoobj; |
---|
[972] | 146 | static Param static_genoparam(geno_paramtab, &static_genoobj); |
---|
[138] | 147 | #else |
---|
[420] | 148 | static Param static_genoparam(geno_paramtab); |
---|
[138] | 149 | #endif |
---|
[420] | 150 | return static_genoparam; |
---|
[138] | 151 | } |
---|
| 152 | |
---|
| 153 | Param& GenoObj::getDynamicParam() |
---|
| 154 | { |
---|
[420] | 155 | static Param dynamic_genoparam(geno_paramtab); |
---|
| 156 | return dynamic_genoparam; |
---|
[138] | 157 | } |
---|
| 158 | |
---|
[420] | 159 | ParamInterface* GenoObj::getInterface() { return &getStaticParam(); } |
---|
[138] | 160 | |
---|
| 161 | ExtObject GenoObj::makeStaticObject(Geno* g) |
---|
[420] | 162 | { |
---|
| 163 | return ExtObject(&getStaticParam(), (void*)g); |
---|
| 164 | } |
---|
[138] | 165 | |
---|
| 166 | ExtObject GenoObj::makeDynamicObject(Geno* g) |
---|
[420] | 167 | { |
---|
| 168 | return ExtObject(&getDynamicParam(), (DestrBase*)g); |
---|
| 169 | } |
---|
[138] | 170 | |
---|
| 171 | ExtObject GenoObj::makeDynamicObjectAndDecRef(Geno* g) |
---|
| 172 | { |
---|
[420] | 173 | const ExtObject& o = makeDynamicObject(g); |
---|
| 174 | g->decref(); |
---|
| 175 | return o; |
---|
[138] | 176 | } |
---|
| 177 | |
---|
[171] | 178 | Geno* GenoObj::fromObject(const ExtValue& v, bool warn) |
---|
[138] | 179 | { |
---|
[420] | 180 | return (Geno*)v.getObjectTarget(getStaticParam().getName(), warn); |
---|
[138] | 181 | } |
---|
[203] | 182 | |
---|
| 183 | void GenoObj::get_toVector(ExtValue *ret) |
---|
| 184 | { |
---|
[420] | 185 | VectorObject *vec = new VectorObject; |
---|
[534] | 186 | vec->data += new ExtValue(getGenesAndFormat()); |
---|
[420] | 187 | vec->data += new ExtValue(getName()); |
---|
| 188 | vec->data += new ExtValue(getComment()); |
---|
| 189 | ret->setObject(ExtObject(&VectorObject::par, vec)); |
---|
[203] | 190 | } |
---|
| 191 | |
---|
[420] | 192 | void GenoObj::p_newfromvector(ExtValue *args, ExtValue *ret) |
---|
[203] | 193 | { |
---|
[420] | 194 | VectorObject *vec = VectorObject::fromObject(args->getObject()); |
---|
| 195 | if (vec && (vec->data.size() >= 3)) |
---|
[203] | 196 | { |
---|
[420] | 197 | SString g = vec->get(0) ? vec->get(0)->getString() : SString::empty(); |
---|
| 198 | SString n = vec->get(1) ? vec->get(1)->getString() : SString::empty(); |
---|
| 199 | SString c = vec->get(2) ? vec->get(2)->getString() : SString::empty(); |
---|
[988] | 200 | *ret = makeDynamicObjectAndDecRef(new Geno(g, Geno::FORMAT_UNKNOWN, n, c)); |
---|
[203] | 201 | } |
---|
[420] | 202 | else |
---|
| 203 | ret->setEmpty(); |
---|
[203] | 204 | } |
---|
[222] | 205 | |
---|
| 206 | ///////////// |
---|
| 207 | |
---|
| 208 | REGISTER_DESERIALIZABLE(GenoObj) |
---|