public static Gene Generate(GeneConfiguration config, bool isActual)
        {
            if (config.Fibonacci && RandomizationProvider.Current.GetType() != typeof(FibonacciRandomization))
            {
                RandomizationProvider.Current = _fibonacci;
            }
            else
            {
                RandomizationProvider.Current = _basic;
            }

            if (isActual && config.ActualInt.HasValue)
            {
                return(new Gene(new KeyValuePair <string, object>(config.Key, config.ActualInt)));
            }
            else if (isActual && config.ActualDecimal.HasValue)
            {
                return(new Gene(new KeyValuePair <string, object>(config.Key, config.ActualDecimal)));
            }
            else if (config.MinDecimal.HasValue && config.MaxDecimal.HasValue)
            {
                return(new Gene(new KeyValuePair <string, object>(config.Key, GeneFactory.RandomBetween(config.MinDecimal.Value, config.MaxDecimal.Value, config.Precision))));
            }

            return(new Gene(new KeyValuePair <string, object>(config.Key, GeneFactory.RandomBetween(config.MinInt.Value, config.MaxInt.Value))));
        }
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            if (reader.TokenType == JsonToken.Null)
            {
                return(null);
            }

            var json = JObject.Load(reader);

            var precision = json["precision"]?.Value <int?>();

            GeneConfiguration gene = new GeneConfiguration
            {
                Key        = json["key"].Value <string>(),
                MinDecimal = precision > 0 ? json["min"].Value <decimal?>() : null,
                MaxDecimal = precision > 0 ? json["max"].Value <decimal?>() : null,
                MinInt     = precision > 0 ? null : json["min"].Value <int?>(),
                MaxInt     = precision > 0 ? null : json["max"].Value <int?>(),
                Precision  = precision,
                Fibonacci  = json["fibonacci"] != null ? json["fibonacci"].Value <bool>() : false
            };

            if (json["actual"] != null)
            {
                int    parsed;
                string raw = json["actual"].Value <string>();
                if (int.TryParse(raw, out parsed))
                {
                    gene.ActualInt = parsed;
                }

                if (!gene.ActualInt.HasValue)
                {
                    decimal decimalParsed;
                    if (decimal.TryParse(raw, out decimalParsed))
                    {
                        gene.ActualDecimal = decimalParsed;
                    }
                    if (decimal.TryParse(raw, NumberStyles.Float, CultureInfo.InvariantCulture, out decimalParsed))
                    {
                        gene.ActualDecimal = decimalParsed;
                    }
                }
            }

            return(gene);
        }
Beispiel #3
0
        public static Gene Generate(GeneConfiguration config, bool isActual)
        {
            if (isActual && config.ActualInt.HasValue)
            {
                return(new Gene(new KeyValuePair <string, object>(config.Key, config.ActualInt)));
            }
            else if (isActual && config.ActualDecimal.HasValue)
            {
                return(new Gene(new KeyValuePair <string, object>(config.Key, config.ActualDecimal)));
            }
            else if (config.MinDecimal.HasValue && config.MaxDecimal.HasValue)
            {
                return(new Gene(new KeyValuePair <string, object>(config.Key, GeneFactory.RandomBetween(config.MinDecimal.Value, config.MaxDecimal.Value, config.Precision))));
            }

            return(new Gene(new KeyValuePair <string, object>(config.Key, GeneFactory.RandomBetween(config.MinInt.Value, config.MaxInt.Value))));
        }