Ignore:
Timestamp:
06/06/18 01:45:18 (6 years ago)
Author:
Maciej Komosinski
Message:

A more complete implementation of fB, fH, fL

File:
1 edited

Legend:

Unmodified
Added
Removed
  • cpp/frams/genetics/fB/fB_conv.cpp

    r780 r797  
     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
    15#include "fB_conv.h"
    26
     
    8993                        continue;
    9094                }
    91                 fH_Handle *handle = convertCharacterToHandle(gene[2], dims, start, end, ranges);
     95                int hclasspos = 2;
     96                if (gene[2] == '"')
     97                {
     98                        hclasspos--;
     99                        if (!getNextCharId(gene, hclasspos))
     100                        {
     101                                return "";
     102                        }
     103                }
     104                fH_Handle *handle = convertCharacterToHandle(gene[hclasspos], dims, start, end, ranges);
    92105                ParamEntry *tab = creature.getParamTab(handle->type);
    93106                void *obj = ParamObject::makeObject(tab);
     
    97110
    98111                int propindex = 0;
    99                 int z = 3;
     112                int z = hclasspos;
     113                if (gene[z] == '"')
     114                {
     115                        z--;
     116                        if (!getNextCharId(gene, z))
     117                        {
     118                                delete handle;
     119                                ParamObject::freeObject(obj);
     120                                return "";
     121                        }
     122                }
    100123                endoffset = 0;
    101124                if (gene.indexOf("zz", 0) != -1) endoffset = 2;
     125                int nclassdefcount = 1;
    102126                while (z < gene.len() - endoffset)
    103127                {
    104                         if (processNextLetter(creature, handle, par, gene, propindex, z, ranges) == -1)
    105                         {
    106                                 logMessage("GenoConv_fBH", "convert", LOG_WARN, "Property of fH could not be parsed");
    107                         }
     128                        if (processNextLetter(creature, handle, par, gene, propindex, z, ranges, nclassdefcount) == -1)
     129                        {
     130                                logMessage("GenoConv_fBH", "convert", LOG_ERROR, "Property of fH could not be parsed");
     131                                delete handle;
     132                                ParamObject::freeObject(obj);
     133                                return "";
     134                        }
     135                }
     136                if (handle->type == fHBodyType::NEURON && propindex < par.getPropCount())
     137                {
     138                        SString nclass;
     139                        if (!getNeuroClass(gene, nclass, nclassdefcount))
     140                        {
     141                                delete handle;
     142                                ParamObject::freeObject(obj);
     143                                return "";
     144                        }
     145                        par.setStringById(FH_PE_NEURO_DET, nclass);
    108146                }
    109147                handle->loadProperties(par);
     
    132170}
    133171
    134 int GenoConv_fBH::processNextLetter(fH_Builder &creature, fH_Handle *&currhandle, Param &par, SString gene, int &propindex, int &i, std::vector<IRange> ranges[3])
     172bool GenoConv_fBH::getNextCharId(SString genotype, int &i)
     173{
     174        i++;
     175        if (genotype[i] == '"')
     176        {
     177                int nextid = i + 1;
     178                do
     179                {
     180                        nextid = genotype.indexOf('"', nextid);
     181                        if (nextid == -1)
     182                        {
     183                                return false;
     184                        }
     185                        nextid++;
     186                }
     187                while (genotype[nextid] == '"');
     188                i = nextid;
     189        }
     190        return true;
     191}
     192
     193bool GenoConv_fBH::getNeuroClass(SString gene, SString &def, int nclassdefcount)
     194{
     195        SString lastdef = "N";
     196        int nclass = 0;
     197        int pos = 0;
     198        while (nclass < nclassdefcount)
     199        {
     200                pos = gene.indexOf('\"', pos);
     201                if (pos == -1)
     202                {
     203                        def = lastdef;
     204                        return true;
     205                }
     206                pos++;
     207                SString currdef;
     208                if (gene.indexOf('\"', pos) == -1 || !gene.getNextToken(pos, currdef, '\"'))
     209                {
     210                        def = lastdef;
     211                        return false;
     212                }
     213                lastdef = currdef;
     214                nclass++;
     215        }
     216        def = lastdef;
     217        return true;
     218}
     219
     220int GenoConv_fBH::processNextLetter(fH_Builder &creature, fH_Handle *&currhandle, Param &par, SString gene, int &propindex, int &i, std::vector<IRange> ranges[3], int &nclassdefcount)
    135221{
    136222        if (propindex >= par.getPropCount())
     
    147233                par.setDefault();
    148234                propindex = 0;
    149                 i++;
     235                if (!getNextCharId(gene, i))
     236                        return -1;
    150237                return 0;
    151238        }
     
    166253                        }
    167254                        propindex++;
    168                         i++;
     255                        if (!getNextCharId(gene, i))
     256                                return -1;
    169257                        return 0;
    170258                }
    171259                else if (currhandle->type == fHBodyType::NEURON && *par.id(propindex) == 'd')
    172260                {
    173                         //TODO: handle neuron classes and properties
     261                        //When 'd' property appears for i-th element in gene, the method
     262                        //looks for i-th neuron definition
     263                        SString nclass;
     264                        if (!getNeuroClass(gene, nclass, nclassdefcount)) return -1;
     265                        par.setString(propindex, nclass);
    174266                        propindex++;
    175                         i++;
     267                        nclassdefcount++;
     268                        if (!getNextCharId(gene, i))
     269                                return -1;
    176270                        return 0;
    177271                }
Note: See TracChangeset for help on using the changeset viewer.