source: cpp/frams/vm/classes/genoobj.cpp @ 875

Last change on this file since 875 was 732, checked in by Maciej Komosinski, 7 years ago

Added support for "checkpoints" (intermediate phases of development of the Model when converting between genetic encodings). See Model.checkpoint() and conv_f1.cpp for an example.

  • Property svn:eol-style set to native
File size: 6.4 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 "genoobj.h"
6#include <frams/util/extvalue.h>
7#include <frams/model/autoname.h>
8#include "collectionobj.h"
9
10#define FIELDSTRUCT GenoObj
11ParamEntry geno_paramtab[] =
12{
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" },
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", },
17        { "format", 0, PARAM_NOSTATIC | PARAM_READONLY, "Format", "s", GETONLY(format), "Genotype format", },
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        },
25        { "getConverted", 0, PARAM_NOSTATIC, "Get converted genotype", "p oGeno(s format)", PROCEDURE(p_getconvert), },
26        { "getConvertedWithCheckpoints", 0, PARAM_NOSTATIC, "Get converted genotype", "p oGeno(s format)", PROCEDURE(p_getconvert_ch), "See also Model.newWithCheckpoints()" },
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), },
30        { "newFrom", 0, 0, "create new object", "p oGeno(s genotype,s format,s name,s description)", PROCEDURE(p_newfrom), },
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, },
35};
36#undef FIELDSTRUCT
37
38void GenoObj::get_isvalid(ExtValue *ret)
39{
40        ret->setInt(isValid());
41}
42
43int GenoObj::set_isvalid(const ExtValue *v)
44{
45        paInt n = v->getInt();
46        if (getValid() != n)
47        {
48                setValid(n);
49                return PSET_CHANGED;
50        }
51        return 0;
52}
53
54void GenoObj::get_genotype(ExtValue *ret)
55{
56        ret->setString(getGenes());
57}
58
59void GenoObj::get_name(ExtValue *ret)
60{
61        ret->setString(getName());
62}
63
64void GenoObj::get_autoname(ExtValue *ret)
65{
66        Model m(*this);
67        ret->setString(AutoName::makeName(m));
68}
69
70int GenoObj::set_name(const ExtValue *v)
71{
72        setName(v->getString());
73        return PSET_CHANGED;
74}
75
76void GenoObj::get_info(ExtValue *ret)
77{
78        ret->setString(getComment());
79}
80
81void GenoObj::get_string(ExtValue *ret)
82{
83        ret->setString(getGenesAndFormat());
84}
85
86void GenoObj::get_format(ExtValue *ret)
87{
88        char format_as_string[2] = { getFormat(), 0 };
89        ret->setString(format_as_string);
90}
91
92int GenoObj::set_info(const ExtValue *v)
93{
94        setComment(v->getString());
95        return PSET_CHANGED;
96}
97
98void GenoObj::get_f0genotype(ExtValue *ret)
99{
100        ret->setString(getConverted('0').getGenes());
101}
102
103char GenoObj::formatFromExtValue(ExtValue& v)
104{
105        if (v.getType() == TInt)
106                return v.getInt();
107        if (v.getType() == TString)
108        {
109                SString s = v.getString();
110                if (s.len() == 1)
111                        return s.charAt(0);
112        }
113        return Geno::INVALID_FORMAT;
114}
115
116void GenoObj::p_getconvert(ExtValue *args, ExtValue *ret)
117{
118        *ret = makeDynamicObjectAndDecRef(new Geno(getConverted(formatFromExtValue(args[0]))));
119}
120
121void GenoObj::p_getconvert_ch(ExtValue *args, ExtValue *ret)
122{
123        *ret = makeDynamicObjectAndDecRef(new Geno(getConverted(formatFromExtValue(args[0]), NULL, true)));
124}
125
126void GenoObj::p_new(ExtValue *args, ExtValue *ret)
127{
128        *ret = makeDynamicObjectAndDecRef(new Geno());
129}
130
131void GenoObj::p_newfromstring(ExtValue *args, ExtValue *ret)
132{
133        *ret = makeDynamicObjectAndDecRef(new Geno(args[0].getString()));
134}
135
136void GenoObj::p_newfrom(ExtValue *args, ExtValue *ret)
137{
138        *ret = makeDynamicObjectAndDecRef(new Geno(args[3].getString(), formatFromExtValue(args[2]),
139                args[1].getString(), args[0].getString()));
140}
141
142Param& GenoObj::getStaticParam()
143{
144#ifdef __CODEGUARD__
145        static GenoObj static_genoobj;
146        static Param static_genoparam(geno_paramtab,&static_genoobj);
147#else
148        static Param static_genoparam(geno_paramtab);
149#endif
150        return static_genoparam;
151}
152
153Param& GenoObj::getDynamicParam()
154{
155        static Param dynamic_genoparam(geno_paramtab);
156        return dynamic_genoparam;
157}
158
159ParamInterface* GenoObj::getInterface() { return &getStaticParam(); }
160
161ExtObject GenoObj::makeStaticObject(Geno* g)
162{
163        return ExtObject(&getStaticParam(), (void*)g);
164}
165
166ExtObject GenoObj::makeDynamicObject(Geno* g)
167{
168        return ExtObject(&getDynamicParam(), (DestrBase*)g);
169}
170
171ExtObject GenoObj::makeDynamicObjectAndDecRef(Geno* g)
172{
173        const ExtObject& o = makeDynamicObject(g);
174        g->decref();
175        return o;
176}
177
178Geno* GenoObj::fromObject(const ExtValue& v, bool warn)
179{
180        return (Geno*)v.getObjectTarget(getStaticParam().getName(), warn);
181}
182
183void GenoObj::get_toVector(ExtValue *ret)
184{
185        VectorObject *vec = new VectorObject;
186        vec->data += new ExtValue(getGenesAndFormat());
187        vec->data += new ExtValue(getName());
188        vec->data += new ExtValue(getComment());
189        ret->setObject(ExtObject(&VectorObject::par, vec));
190}
191
192void GenoObj::p_newfromvector(ExtValue *args, ExtValue *ret)
193{
194        VectorObject *vec = VectorObject::fromObject(args->getObject());
195        if (vec && (vec->data.size() >= 3))
196        {
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();
200                *ret = makeDynamicObjectAndDecRef(new Geno(g, -1, n, c));
201        }
202        else
203                ret->setEmpty();
204}
205
206/////////////
207
208REGISTER_DESERIALIZABLE(GenoObj)
Note: See TracBrowser for help on using the repository browser.