Esempio n. 1
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            Debug.WriteLine("Reading KWArgs");

            var jObject = JObject.Load(reader);

            KWArgsModel retVal = new KWArgsModel();

            foreach (JToken arrItem in jObject.Values())
            {
                if (arrItem.Type == JTokenType.Integer)
                {
                    retVal.Add(arrItem.Path, arrItem.Value <int>());
                }
                if (arrItem.Type == JTokenType.Object)
                {
                    JObject obj = (JObject)arrItem;

                    var children = arrItem.Children <JProperty>();
                    if (children.Any(c => c.Name == "Type"))
                    {
                        string type     = obj.Value <string>("Type");
                        var    childObj = Activator.CreateInstance(Type.GetType(type));

                        serializer.Populate(obj.CreateReader(), childObj);
                        retVal.Add(arrItem.Path, childObj);
                    }
                }
            }

            //JsonReader reader2 = new JTokenReader(jObj);
            //serializer.Populate(reader2, retVal);

            return(retVal);

            throw new NotImplementedException();
        }
Esempio n. 2
0
        /// <summary>
        /// Return a dict that can be used to construct this encoder. This dict
        /// can be passed directly to the addMultipleEncoders() method of the
        /// multi encoder.
        /// </summary>
        /// <param name="encoderName">name of the encoder</param>
        /// <param name="flattenedChosenValues">
        /// dict of the flattened permutation variables. Any
        /// variables within this dict whose key starts
        /// with encoderName will be substituted for
        /// encoder constructor args which are being
        /// permuted over.
        /// </param>
        /// <returns></returns>
        public PermuteEncoder getEncoderFlattened(string encoderName, Dictionary <string, object> flattenedChosenValues)
        {
            Dictionary <string, object> encoder = new Dictionary <string, object>();

            //encoder.Add("fieldname", this.fieldName);
            //encoder.Add("name", this.name);
            //encoder = dict(fieldname = this.fieldName,name = this.name);

            // Get the position of each encoder argument
            //for (encoderArg, value in this.kwArgs.iteritems())
            foreach (var pair in this.kwArgs)
            {
                var encoderArg = pair.Key;
                var value      = pair.Value;
                // If a permuted variable, get its chosen value.
                if (value is PermuteVariable)
                {
                    value = flattenedChosenValues[string.Format("{0}:{1}", encoderName, encoderArg)];
                }

                encoder[encoderArg] = value;
            }

            // Special treatment for DateEncoder timeOfDay and dayOfWeek stuff. In the
            //  permutations file, the class can be one of:
            //    DateEncoder.timeOfDay
            //    DateEncoder.dayOfWeek
            //    DateEncoder.season
            // If one of these, we need to intelligently set the constructor args.
            if (this.encoderClass.Contains("."))
            {
                // (encoder['type'], argName) = this.encoderClass.Split('.');
                string[] splitted = this.encoderClass.Split('.');
                encoder["type"] = splitted[0];
                string argName = splitted[1];
                Tuple <object, object> argValue = new Tuple <object, object>(encoder["w"], encoder["radius"]);
                encoder[argName] = argValue;
                encoder.Remove("w");
                encoder.Remove("radius");
            }
            else
            {
                encoder["type"] = this.encoderClass;
            }

            var args = new KWArgsModel();

            foreach (var pair in encoder)
            {
                if (pair.Key == "type")
                {
                    continue;
                }
                if (pair.Key == "maxval")
                {
                    continue;
                }
                if (pair.Key == "minval")
                {
                    continue;
                }
                if (pair.Key == "n")
                {
                    continue;
                }
                if (pair.Key == "w")
                {
                    continue;
                }
                args.Add(pair.Key, pair.Value);
            }

            PermuteEncoder pe = new PermuteEncoder(fieldName, (string)encoder["type"], name, args);

            pe.maxval = encoder["maxval"];
            pe.minval = encoder["minval"];
            pe.n      = encoder["n"];
            pe.w      = encoder["w"];
            return(pe);
        }
Esempio n. 3
0
        public PermuteEncoder(string fieldName, string encoderClass, string name = null, KWArgsModel kwArgs = null)
        {
            this.fieldName = fieldName;
            if (name == null)
            {
                name = fieldName;
            }
            this.name         = name;
            this.encoderClass = encoderClass;

            // Possible values in kwArgs include: w, n, minval, maxval, etc.
            this.kwArgs = kwArgs;
        }