Beispiel #1
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            JToken t     = JObject.ReadFrom(reader);
            string type  = ((FunctionType)t["Type"].Value <int>()).ToString();
            string value = t["Val"].Value <string>();

            return(ValueUtilities.CreateValue(type, value));
        }
Beispiel #2
0
        public void WriteInput(OperatorPart opPart)
        {
            Writer.WriteStartObject();

            Writer.WriteValue("Name", opPart.Name);
            Writer.WriteValue("ID", opPart.ID);
            Writer.WriteValue("Type", opPart.Type);
            Writer.WriteValue("Value", ValueUtilities.GetValueForTypeFromContext(opPart.Type, opPart.Eval(new OperatorPartContext())));

            Writer.WriteEndObject();
        }
Beispiel #3
0
        public void ReadAndSetInputValues(OperatorPart opPart, JToken jsonOpPart)
        {
            opPart.Name = jsonOpPart["Name"].Value <string>();
            if (opPart.ID != Guid.Parse(jsonOpPart["ID"].Value <string>()))
            {
                throw new System.Exception("Wrong op part id in file");
            }

            string type  = jsonOpPart["Type"].Value <string>();
            string value = jsonOpPart["Value"].Value <string>();

            opPart.Func = Utilities.CreateValueFunction(ValueUtilities.CreateValue(type, value));
        }
Beispiel #4
0
        private MetaInput BuildMetaInput(MetaManager metaManager, JToken jsonInput)
        {
            var metaInstanceID = Guid.Parse(jsonInput["MetaInstanceID"].Value <string>());
            var name           = jsonInput["Name"].Value <string>();
            var metaIDOfOpPart = Guid.Parse(jsonInput["MetaID"].Value <string>());
            var opPart         = metaManager.GetMetaOperatorPart(metaIDOfOpPart);

            // read default value - can be multi line value
            var defaultValueString = string.Empty;
            var defaultValueEntry  = jsonInput["DefaultValue"]["Value"];

            if (defaultValueEntry is JValue)
            {
                defaultValueString = defaultValueEntry.Value <string>();
            }
            else
            {
                var lines = new List <String>();
                foreach (var valueLine in defaultValueEntry)
                {
                    lines.Add(valueLine.Value <string>());
                }
                defaultValueString = string.Join("\n", lines);
            }
            var defaultValue = ValueUtilities.CreateValue(jsonInput["DefaultValue"]["Type"].Value <string>(), defaultValueString);

            var isMultiInput = jsonInput["IsMultiInput"].Value <bool>();
            var relevance    = (MetaInput.RelevanceType)Enum.Parse(typeof(MetaInput.RelevanceType), jsonInput["Relevance"].Value <string>());
            var description  = jsonInput["Description"].Value <string>();

            var min       = jsonInput["Min"].Value <float>();
            var max       = jsonInput["Max"].Value <float>();
            var scale     = jsonInput["Scale"].Value <float>();
            var scaleType = (MetaInput.Scaling)Enum.Parse(typeof(MetaInput.Scaling), jsonInput["ScaleType"].Value <string>());

            var enumEntries = (from enumEntry in jsonInput["EnumValues"]
                               let metaOpPart = BuildEnumEntry(enumEntry)
                                                select metaOpPart).ToList();

            return(new MetaInput(metaInstanceID, name, opPart, defaultValue, isMultiInput)
            {
                Relevance = relevance,
                Description = description,
                Min = min,
                Max = max,
                Scale = scale,
                ScaleType = scaleType,
                EnumValues = enumEntries
            });
        }
Beispiel #5
0
 public OperatorPart CreateInstance()
 {
     return(OpPart.CreateFunc(ID, Utilities.CreateValueFunction(ValueUtilities.CreateValue(OpPart.Type.ToString(), String.Empty)), false, Name));
 }
Beispiel #6
0
        private MetaOperator.InstanceProperties BuildOperatorInstanceProperties(MetaManager metaManager, MetaOperator metaOp, JToken jsonOp)
        {
            var opProperties = new MetaOperator.InstanceProperties();

            opProperties.Name     = jsonOp["Name"].Value <string>();
            opProperties.Position = new Point(jsonOp["PosX"].Value <double>(), jsonOp["PosY"].Value <double>());
            opProperties.Width    = jsonOp["Width"].Value <double>();
            opProperties.Visible  = jsonOp["Visible"].Value <bool>();
            if (jsonOp["Disabled"] != null)
            {
                opProperties.Disabled = jsonOp["Disabled"].Value <bool>();
            }

            var foundMetaInputsList = new List <MetaInput>();

            foreach (var jsonInput in jsonOp["Inputs"])
            {
                var id        = Guid.Parse(jsonInput["ID"].Value <string>());
                var metaInput = (from input in metaOp.Inputs
                                 where input.ID == id
                                 select input).SingleOrDefault();
                if (metaInput == null)
                {
                    Logger.Debug("Skipped obsolete parameter value {0} in meta operator {1}, because it no longer exists.", id.ToString(), metaOp.Name);
                    continue;
                }

                var valueToken = jsonInput["Type"];
                if (valueToken != null)
                {
                    var type  = valueToken.Value <string>();
                    var lines = new List <String>();
                    foreach (var valueLine in jsonInput["Value"])
                    {
                        lines.Add(valueLine.Value <string>());
                    }
                    var value = string.Join("\n", lines);
                    opProperties.InputValues[id] = Utilities.CreateValueFunction(ValueUtilities.CreateValue(type, value));
                }
                else
                {
                    opProperties.InputValues[id] = metaInput.DefaultFunc;
                }

                foundMetaInputsList.Add(metaInput);
            }

            // extract inputs for which no values have been set, e.g. because the input was added later after creating the parent op
            // for these inputs the default func is set
            var inputsWithoutStoredInstanceValues = foundMetaInputsList.Except(metaOp.Inputs);

            foreach (var input in inputsWithoutStoredInstanceValues)
            {
                opProperties.InputValues[input.ID] = input.DefaultFunc;
                Logger.Debug("Added default value for new parameter {0} in Operator {1}", input.Name, input.ID.ToString());
            }

            foreach (var jsonState in jsonOp["States"])
            {
                var opPartId        = Guid.Parse(jsonState.Value <string>("ID"));
                var metaOpPart      = metaOp.OperatorParts.First(opPartEntry => opPartEntry.Item1 == opPartId).Item2;
                var stateType       = metaOpPart.StateType;
                var serializedState = jsonState.Value <JObject>("State");

                var serializer = new JsonSerializer();
                using (var reader = new StringReader(serializedState.ToString()))
                {
                    var state = serializer.Deserialize(reader, stateType) as IOperatorPartState;
                    if (state != null)
                    {
                        opProperties.OperatorPartStates.Add(opPartId, state);
                    }
                }
            }

            return(opProperties);
        }
Beispiel #7
0
        private void WriteOperatorInstanceProperties(MetaOperator parentOp, Guid metaOpInstanceID, MetaOperator metaOp, MetaOperator.InstanceProperties opProperties)
        {
            Writer.WriteStartObject();

            Writer.WriteValue("Name", opProperties.Name);
            Writer.WriteValue("PosX", opProperties.Position.X);
            Writer.WriteValue("PosY", opProperties.Position.Y);
            Writer.WriteValue("Width", opProperties.Width);
            Writer.WriteValue("Visible", opProperties.Visible);
            Writer.WriteValue("Disabled", opProperties.Disabled);

            Writer.WritePropertyName("Inputs");
            Writer.WriteStartArray();
            foreach (var inputValue in opProperties.InputValues)
            {
                var inputID        = inputValue.Key;
                var inputValueFunc = inputValue.Value;

                var metaInput = (from input in metaOp.Inputs
                                 where input.ID == inputValue.Key
                                 select input).SingleOrDefault();

                bool isDefault = ((metaInput != null) && (inputValueFunc == metaInput.DefaultFunc)) ||
                                 parentOp.ExistsConnectionToInput(metaOpInstanceID, metaInput.ID);

                if (!isDefault)
                {
                    Writer.WriteStartObject();
                    Writer.WriteValue("ID", inputID);
                    var inputValueValue = (inputValueFunc as Utilities.ValueFunction).Value;
                    var inputValueType  = inputValueValue.Type;

                    Writer.WriteValue("Type", inputValueType);

                    //write value multi lined
                    var value         = ValueUtilities.GetValueForTypeFromContext(inputValueType, inputValueValue.SetValueInContext(new OperatorPartContext()));
                    var splittedValue = value.Split(new string[] { "\n" }, StringSplitOptions.None);
                    Writer.WritePropertyName("Value");
                    Writer.WriteStartArray();
                    foreach (var valueLine in splittedValue)
                    {
                        Writer.WriteValue(valueLine);
                    }
                    Writer.WriteEndArray();
                    Writer.WriteEndObject();
                }
            }
            Writer.WriteEndArray();

            Writer.WritePropertyName("States");
            Writer.WriteStartArray();
            foreach (var stateEntry in opProperties.OperatorPartStates)
            {
                Writer.WriteStartObject();
                Writer.WriteValue("ID", stateEntry.Key);
                Writer.WritePropertyName("State");
                var s = new JsonSerializer();
                s.Serialize(Writer, stateEntry.Value);
                Writer.WriteEndObject();
            }
            Writer.WriteEndArray();

            Writer.WriteEndObject();
        }