- Timestamp:
- 01/07/20 16:25:39 (5 years ago)
- Location:
- branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters
- Files:
-
- 1 deleted
- 15 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/BaseConverter.cs
r17379 r17394 11 11 public abstract class BaseConverter : IJsonItemConverter 12 12 { 13 public void Inject(IItem item, JsonItem data) { 14 InjectData(item, data); 13 public abstract int Priority { get; } 14 public abstract Type ConvertableType { get; } 15 16 public void Inject(IItem item, JsonItem data, IJsonItemConverter root) { 17 18 InjectData(item, data, root); 15 19 } 16 20 17 public JsonItem Extract(IItem value ) {18 JsonItem data = ExtractData(value);19 data.Name = string.IsNullOrEmpty(data.Name) ? value.ItemName : data.Name;21 public JsonItem Extract(IItem value, IJsonItemConverter root) { 22 JsonItem data = new JsonItem() { Name = value.ItemName }; 23 Populate(value, data, root); 20 24 return data; 21 25 } 22 26 23 public abstract void InjectData(IItem item, JsonItem data );24 public abstract JsonItem ExtractData(IItem value);27 public abstract void InjectData(IItem item, JsonItem data, IJsonItemConverter root); 28 public abstract void Populate(IItem value, JsonItem item, IJsonItemConverter root); 25 29 26 30 #region Helper 27 31 protected ValueType CastValue<ValueType>(object obj) { 28 32 if (obj is JToken) 29 return obj.Cast<JToken>().ToObject<ValueType>();33 return ((JToken)obj).ToObject<ValueType>(); 30 34 else if (obj is IConvertible) 31 return Convert.ChangeType(obj, typeof(ValueType)).Cast<ValueType>();35 return (ValueType)Convert.ChangeType(obj, typeof(ValueType)); 32 36 else return (ValueType)obj; 33 37 } -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/ConstrainedValueParameterConverter.cs
r17379 r17394 9 9 namespace HeuristicLab.JsonInterface { 10 10 public class ConstrainedValueParameterConverter : ParameterBaseConverter { 11 public override void InjectData(IParameter parameter, JsonItem data) { 11 public override int Priority => 3; 12 public override Type ConvertableType => typeof(IConstrainedValueParameter<>); 13 14 public override void InjectData(IParameter parameter, JsonItem data, IJsonItemConverter root) { 12 15 foreach(var x in GetValidValues(parameter)) 13 16 if(x.ToString() == CastValue<string>(data.Value)) … … 17 20 foreach(var param in data.Children) { 18 21 if(param.Name == parameter.ActualValue.ItemName) 19 JsonItemConverter.Inject(parameter.ActualValue, param);22 root.Inject(parameter.ActualValue, param, root); 20 23 } 21 24 } 22 25 } 23 26 24 public override JsonItem ExtractData(IParameter value) => 25 new JsonItem() { 26 Name = value.Name, 27 Value = value.ActualValue?.ToString(), 28 Range = GetValidValues(value).Select(x => x.ToString()), 29 Children = GetParameterizedChilds(value) 30 }; 27 public override void Populate(IParameter value, JsonItem item, IJsonItemConverter root) { 28 item.AddChilds(GetParameterizedChilds(value)); 29 item.Name = value.Name; 30 item.Value = value.ActualValue?.ToString(); 31 item.Range = GetValidValues(value).Select(x => x.ToString()); 32 } 31 33 32 34 #region Helper 33 35 private IItem[] GetValidValues(IParameter value) { 34 36 List<IItem> list = new List<IItem>(); 35 var values = value.Cast<dynamic>().ValidValues;37 var values = ((dynamic)value).ValidValues; 36 38 foreach (var x in values) list.Add((IItem)x); 37 39 return list.ToArray(); … … 40 42 private IList<JsonItem> GetParameterizedChilds(IParameter value) { 41 43 List<JsonItem> list = new List<JsonItem>(); 42 var values = value.Cast<dynamic>().ValidValues;44 var values = ((dynamic)value).ValidValues; 43 45 foreach(var x in values) { 44 46 if (x is IParameterizedItem) { -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/EnumTypeConverter.cs
r17342 r17394 9 9 namespace HeuristicLab.JsonInterface { 10 10 public class EnumTypeConverter : BaseConverter { 11 public override void InjectData(IItem item, JsonItem data) => 12 item.Cast<dynamic>().Value = Enum.Parse( 11 public override int Priority => 1; 12 public override Type ConvertableType => typeof(EnumValue<>); 13 14 public override void InjectData(IItem item, JsonItem data, IJsonItemConverter root) => 15 ((dynamic)item).Value = Enum.Parse( 13 16 item.GetType().GenericTypeArguments.First(), 14 17 CastValue<string>(data.Value)); 15 18 16 public override JsonItem ExtractData(IItem value) { 17 JsonItem data = new JsonItem(); 19 public override void Populate(IItem value, JsonItem item, IJsonItemConverter root) { 18 20 object val = ((dynamic)value).Value; 19 21 Type enumType = val.GetType(); 20 data.Value = Enum.GetName(enumType, val); 21 data.Range = Enum.GetNames(enumType); 22 return data; 22 item.Value = Enum.GetName(enumType, val); 23 item.Range = Enum.GetNames(enumType); 23 24 } 24 25 } -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/LookupParameterConverter.cs
r17342 r17394 8 8 namespace HeuristicLab.JsonInterface { 9 9 public class LookupParameterConverter : ParameterBaseConverter { 10 public override JsonItem ExtractData(IParameter value) => 11 new JsonItem() { 12 Name = value.Name, 13 ActualName = value.Cast<ILookupParameter>().ActualName 14 }; 10 public override int Priority => 3; 11 public override Type ConvertableType => typeof(ILookupParameter); 15 12 16 public override void InjectData(IParameter parameter, JsonItem data) => 17 parameter.Cast<ILookupParameter>().ActualName = data.ActualName.Cast<string>(); 13 public override void Populate(IParameter value, JsonItem item, IJsonItemConverter root) { 14 item.Name = value.Name; 15 item.ActualName = ((ILookupParameter)value).ActualName; 16 } 17 18 public override void InjectData(IParameter parameter, JsonItem data, IJsonItemConverter root) => 19 ((ILookupParameter)parameter).ActualName = data.ActualName as string; 18 20 } 19 21 } -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/MultiCheckedOperatorConverter.cs
r17379 r17394 9 9 namespace HeuristicLab.JsonInterface { 10 10 public class MultiCheckedOperatorConverter : ParameterizedItemConverter { 11 public override JsonItem ExtractData(IItem value) {12 JsonItem data = base.ExtractData(value);11 public override int Priority => 3; 12 public override Type ConvertableType => typeof(ICheckedMultiOperator<>); 13 13 14 if(data.Children == null) 15 data.Children = new List<JsonItem>(); 16 dynamic val = value.Cast<dynamic>(); 14 public override void Populate(IItem value, JsonItem item, IJsonItemConverter root) { 15 base.Populate(value, item, root); 16 17 dynamic val = value as dynamic; 17 18 foreach (var op in val.Operators) { 18 data.Children.Add(new JsonItem() {19 item.AddChilds(new JsonItem() { 19 20 Name = op.Name, 20 21 Value = val.Operators.ItemChecked(op), … … 22 23 }); 23 24 } 24 return data;25 25 } 26 26 27 public override void InjectData(IItem item, JsonItem data ) {28 base.InjectData(item, data );27 public override void InjectData(IItem item, JsonItem data, IJsonItemConverter root) { 28 base.InjectData(item, data, root); 29 29 30 dynamic val = item .Cast<dynamic>();30 dynamic val = item as dynamic; 31 31 foreach (var op in val.Operators) 32 32 val.Operators.SetItemCheckedState(op, GetOperatorState(op.Name, data)); … … 35 35 private bool GetOperatorState(string name, JsonItem data) { 36 36 foreach(var op in data.Children) { 37 if (op.Name == name && op.Value is bool) return op.Value.Cast<bool>();37 if (op.Name == name && op.Value is bool) return (bool)op.Value; 38 38 } 39 39 return false; -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/ParameterBaseConverter.cs
r17379 r17394 8 8 namespace HeuristicLab.JsonInterface { 9 9 public abstract class ParameterBaseConverter : BaseConverter { 10 public override JsonItem ExtractData(IItem value) =>11 ExtractData(value.Cast<IParameter>());10 public override void Populate(IItem value, JsonItem item, IJsonItemConverter root) => 11 Populate(value as IParameter, item, root); 12 12 13 public abstract JsonItem ExtractData(IParameter value);13 public abstract void Populate(IParameter value, JsonItem item, IJsonItemConverter root); 14 14 15 public override void InjectData(IItem item, JsonItem data) => InjectData(item.Cast<IParameter>(), data); 15 public override void InjectData(IItem item, JsonItem data, IJsonItemConverter root) => 16 InjectData(item as IParameter, data, root); 16 17 17 public abstract void InjectData(IParameter parameter, JsonItem data );18 public abstract void InjectData(IParameter parameter, JsonItem data, IJsonItemConverter root); 18 19 } 19 20 } -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/ParameterizedItemConverter.cs
r17379 r17394 8 8 namespace HeuristicLab.JsonInterface { 9 9 public class ParameterizedItemConverter : BaseConverter { 10 public override void InjectData(IItem item, JsonItem data) { 11 IParameterizedItem pItem = item.Cast<IParameterizedItem>(); 10 public override int Priority => 2; 11 public override Type ConvertableType => typeof(IParameterizedItem); 12 13 public override void InjectData(IItem item, JsonItem data, IJsonItemConverter root) { 14 IParameterizedItem pItem = item as IParameterizedItem; 12 15 13 16 if(data.Children != null) { 14 17 foreach (var sp in data.Children) 15 18 if (pItem.Parameters.TryGetValue(sp.Name, out IParameter param)) 16 JsonItemConverter.Inject(param, sp);19 root.Inject(param, sp, root); 17 20 } 18 21 } 19 22 20 public override JsonItem ExtractData(IItem value) { 21 JsonItem item = new JsonItem(); 23 public override void Populate(IItem value, JsonItem item, IJsonItemConverter root) { 22 24 var parameterizedItem = value as IParameterizedItem; 23 25 24 26 foreach (var param in parameterizedItem.Parameters) { 25 if(!param.Hidden) { 26 JsonItem data = JsonItemConverter.Extract(param); 27 //data.Name = param.Name; 28 29 if (item.Children == null) 30 item.Children = new List<JsonItem>(); 31 item.Children.Add(data); 32 } 27 if(!param.Hidden) 28 item.AddChilds(root.Extract(param, root)); 33 29 } 34 return item;35 30 } 36 31 } -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/RegressionProblemDataConverter.cs
r17379 r17394 11 11 public class RegressionProblemDataConverter : BaseConverter { 12 12 private const BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance; 13 public override JsonItem ExtractData(IItem value) { 14 JsonItem item = new JsonItem() { 15 Path = value.ItemName, 16 Children = new List<JsonItem>() 17 }; 18 13 public override int Priority => 20; 14 public override Type ConvertableType => HEAL.Attic.Mapper.StaticCache.GetType(new Guid("EE612297-B1AF-42D2-BF21-AF9A2D42791C")); 15 16 public override void Populate(IItem value, JsonItem item, IJsonItemConverter root) { 19 17 dynamic val = (dynamic)value; 20 18 object dataset = (object)val.Dataset; … … 22 20 FieldInfo dataInfo = dataset.GetType().GetField("storableData", flags); 23 21 // TODO: aufteilen in trainings und test daten abschnitte 24 item. Children.Add(new JsonItem() {22 item.AddChilds(new JsonItem() { 25 23 Name = "Dataset", 26 Value = dataInfo.GetValue(dataset), 27 Path = "Dataset" 24 Value = dataInfo.GetValue(dataset) 28 25 }); 29 26 30 27 IEnumerable<StringValue> variables = (IEnumerable<StringValue>)val.InputVariables; 31 item. Children.Add(new JsonItem() {28 item.AddChilds(new JsonItem() { 32 29 Name = "TargetVariable", 33 30 Value = (object)targetVariable, 34 Range = variables.Select(x => x.Value), 35 Path = "TargetVariable" 31 Range = variables.Select(x => x.Value) 36 32 }); 37 33 38 34 39 item. Children.Add(new JsonItem() {35 item.AddChilds(new JsonItem() { 40 36 Name = "AllowedInputVariables", 41 37 Value = (object)val.AllowedInputVariables, 42 Range = variables.Select(x => x.Value), 43 Path = "AllowedInputVariables" 38 Range = variables.Select(x => x.Value) 44 39 }); 45 46 item.UpdatePath();47 48 return item;49 40 } 50 41 51 public override void InjectData(IItem item, JsonItem data ) {42 public override void InjectData(IItem item, JsonItem data, IJsonItemConverter root) { 52 43 // TODO: inject data 53 44 throw new NotImplementedException(); -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/StringValueConverter.cs
r17342 r17394 9 9 namespace HeuristicLab.JsonInterface { 10 10 public class StringValueConverter : BaseConverter { 11 public override void InjectData(IItem item, JsonItem data) =>12 item.Cast<StringValue>().Value = CastValue<string>(data.Value);11 public override int Priority => 1; 12 public override Type ConvertableType => typeof(StringValue); 13 13 14 public override JsonItem ExtractData(IItem value) => 15 new JsonItem() { 16 Value = value.Cast<StringValue>().Value 17 }; 14 public override void InjectData(IItem item, JsonItem data, IJsonItemConverter root) => 15 ((StringValue)item).Value = CastValue<string>(data.Value); 16 17 public override void Populate(IItem value, JsonItem item, IJsonItemConverter root) => 18 item.Value = ((StringValue)value).Value; 18 19 } 19 20 } -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/ValueLookupParameterConverter.cs
r17379 r17394 8 8 namespace HeuristicLab.JsonInterface { 9 9 public class ValueLookupParameterConverter : ParameterBaseConverter { 10 public override JsonItem ExtractData(IParameter value) { 11 IValueLookupParameter param = value.Cast<IValueLookupParameter>(); 10 public override int Priority => 4; 11 public override Type ConvertableType => typeof(IValueLookupParameter); 12 13 public override void Populate(IParameter value, JsonItem item, IJsonItemConverter root) { 14 IValueLookupParameter param = value as IValueLookupParameter; 15 16 item.Name = value.Name; 17 item.ActualName = param.ActualName; 18 12 19 object actualValue = null; 13 20 IEnumerable<object> actualRange = null; 14 21 if(param.Value != null) { 15 JsonItem tmp = JsonItemConverter.Extract(param.Value); 22 JsonItem tmp = root.Extract(param.Value, root); 23 tmp.Parent = item; 16 24 actualValue = tmp.Value; 17 25 actualRange = tmp.Range; … … 19 27 actualRange = new object[] { GetMinValue(param.DataType), GetMaxValue(param.DataType) }; 20 28 } 21 22 return new JsonItem() { 23 Name = value.Name, 24 ActualName = param.ActualName, 25 Value = actualValue, 26 Range = actualRange 27 }; 29 item.Value = actualValue; 30 item.Range = actualRange; 28 31 } 29 32 30 public override void InjectData(IParameter parameter, JsonItem data ) {31 IValueLookupParameter param = parameter .Cast<IValueLookupParameter>();33 public override void InjectData(IParameter parameter, JsonItem data, IJsonItemConverter root) { 34 IValueLookupParameter param = parameter as IValueLookupParameter; 32 35 param.ActualName = CastValue<string>(data.ActualName); 33 36 if (param.Value != null) 34 JsonItemConverter.Inject(param.Value, data);37 root.Inject(param.Value, data, root); 35 38 } 36 39 } -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/ValueParameterConverter.cs
r17374 r17394 8 8 namespace HeuristicLab.JsonInterface { 9 9 public class ValueParameterConverter : ParameterBaseConverter { 10 public override int Priority => 2; 11 public override Type ConvertableType => typeof(IValueParameter); 10 12 11 public override void InjectData(IParameter parameter, JsonItem data ) {13 public override void InjectData(IParameter parameter, JsonItem data, IJsonItemConverter root) { 12 14 if (parameter.ActualValue == null && data.Value != null) 13 15 parameter.ActualValue = Instantiate(parameter.DataType); 14 JsonItemConverter.Inject(parameter.ActualValue, data);16 root.Inject(parameter.ActualValue, data, root); 15 17 } 16 18 17 public override JsonItem ExtractData(IParameter value) {18 JsonItem data = new JsonItem() { Name = value.Name };19 public override void Populate(IParameter value, JsonItem item, IJsonItemConverter root) { 20 item.Name = value.Name; 19 21 if (value.ActualValue != null) { 20 JsonItem tmp = JsonItemConverter.Extract(value.ActualValue);22 JsonItem tmp = root.Extract(value.ActualValue, root); 21 23 if(tmp.Name == "[OverridableParamName]") { 22 24 tmp.Name = value.Name; 23 data = tmp;25 JsonItem.Merge(item, tmp); 24 26 } 25 27 else 26 data.AddParameter(tmp);28 item.AddChilds(tmp); 27 29 } 28 return data;29 30 } 30 31 } -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/ValueRangeConverter.cs
r17379 r17394 11 11 namespace HeuristicLab.JsonInterface { 12 12 13 public class IntRangeConverter : ValueRangeConverter<IntRange, IntValue, int> { } 14 public class DoubleRangeConverter : ValueRangeConverter<DoubleRange, DoubleValue, double> {} 13 public class IntRangeConverter : ValueRangeConverter<IntRange, IntValue, int> { 14 public override int Priority => 1; 15 public override Type ConvertableType => typeof(IntRange); 16 } 17 public class DoubleRangeConverter : ValueRangeConverter<DoubleRange, DoubleValue, double> { 18 public override int Priority => 1; 19 public override Type ConvertableType => typeof(DoubleRange); 20 } 15 21 16 public class ValueRangeConverter<RangeType, T, TType> : BaseConverter22 public abstract class ValueRangeConverter<RangeType, T, TType> : BaseConverter 17 23 where RangeType : StringConvertibleValueTuple<T, T> 18 24 where T : ValueTypeValue<TType>, IDeepCloneable, IStringConvertibleValue … … 21 27 private const BindingFlags Flags = BindingFlags.NonPublic | BindingFlags.Instance; 22 28 23 public override JsonItem ExtractData(IItem value) {29 public override void Populate(IItem value, JsonItem item, IJsonItemConverter root) { 24 30 var field = value.GetType().GetField("values", Flags); 25 31 Tuple<T,T> tuple = (Tuple<T,T>)field.GetValue(value); 26 27 return new JsonItem() { 28 Name = "[OverridableParamName]", 29 Value = new object[] { tuple.Item1.Value, tuple.Item2.Value }, 30 Range = new object[] { GetMinValue(tuple.Item1.Value.GetType()), 31 GetMaxValue(tuple.Item2.Value.GetType()) } 32 }; 32 item.Name = "[OverridableParamName]"; 33 item.Value = new object[] { tuple.Item1.Value, tuple.Item2.Value }; 34 item.Range = new object[] { GetMinValue(tuple.Item1.Value.GetType()), GetMaxValue(tuple.Item2.Value.GetType()) }; 33 35 } 34 36 35 public override void InjectData(IItem item, JsonItem data ) {37 public override void InjectData(IItem item, JsonItem data, IJsonItemConverter root) { 36 38 object[] arr = (object[])data.Value; 37 39 Tuple<T,T> tuple = new Tuple<T,T>(Instantiate<T>(arr[0]), Instantiate<T>(arr[1])); -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/ValueTypeArrayConverter.cs
r17374 r17394 9 9 10 10 namespace HeuristicLab.JsonInterface { 11 public class ValueTypeArrayConverter<ArrayType, T> : BaseConverter 11 12 public class IntArrayConverter : ValueTypeArrayConverter<IntArray, int> { 13 public override int Priority => 1; 14 public override Type ConvertableType => typeof(IntArray); 15 } 16 17 public class DoubleArrayConverter : ValueTypeArrayConverter<DoubleArray, double> { 18 public override int Priority => 1; 19 public override Type ConvertableType => typeof(DoubleArray); 20 } 21 22 public class PercentArrayConverter : ValueTypeArrayConverter<PercentArray, double> { 23 public override int Priority => 2; 24 public override Type ConvertableType => typeof(PercentArray); 25 } 26 27 public class BoolArrayConverter : ValueTypeArrayConverter<BoolArray, bool> { 28 public override int Priority => 1; 29 public override Type ConvertableType => typeof(BoolArray); 30 } 31 32 public abstract class ValueTypeArrayConverter<ArrayType, T> : BaseConverter 12 33 where ArrayType : ValueTypeArray<T> 13 34 where T : struct 14 35 { 15 public override void InjectData(IItem item, JsonItem data ) =>16 CopyArrayData( item.Cast<ArrayType>(), CastValue<T[]>(data.Value));36 public override void InjectData(IItem item, JsonItem data, IJsonItemConverter root) => 37 CopyArrayData(((ArrayType)item), CastValue<T[]>(data.Value)); 17 38 18 public override JsonItem ExtractData(IItem value) => 19 new JsonItem() { 20 Name = "[OverridableParamName]", 21 Value = value.Cast<ArrayType>().CloneAsArray() 22 }; 39 public override void Populate(IItem value, JsonItem item, IJsonItemConverter root) { 40 item.Name = "[OverridableParamName]"; 41 item.Value = ((ArrayType)value).CloneAsArray(); 42 } 23 43 24 44 #region Helper -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/ValueTypeMatrixConverter.cs
r17379 r17394 9 9 10 10 namespace HeuristicLab.JsonInterface { 11 public class ValueTypeMatrixConverter<MatrixType, T> : BaseConverter 11 public class IntMatrixConverter : ValueTypeMatrixConverter<IntMatrix, int> { 12 public override int Priority => 1; 13 public override Type ConvertableType => typeof(IntMatrix); 14 } 15 16 public class DoubleMatrixConverter : ValueTypeMatrixConverter<DoubleMatrix, double> { 17 public override int Priority => 1; 18 public override Type ConvertableType => typeof(DoubleMatrix); 19 } 20 21 public class PercentMatrixConverter : ValueTypeMatrixConverter<PercentMatrix, double> { 22 public override int Priority => 2; 23 public override Type ConvertableType => typeof(PercentMatrix); 24 } 25 26 public class BoolMatrixConverter : ValueTypeMatrixConverter<BoolMatrix, bool> { 27 public override int Priority => 1; 28 public override Type ConvertableType => typeof(BoolMatrix); 29 } 30 31 public abstract class ValueTypeMatrixConverter<MatrixType, T> : BaseConverter 12 32 where MatrixType : ValueTypeMatrix<T> 13 33 where T : struct 14 34 { 15 public override void InjectData(IItem item, JsonItem data ) =>16 CopyMatrixData(item .Cast<MatrixType>(), data.Value);35 public override void InjectData(IItem item, JsonItem data, IJsonItemConverter root) => 36 CopyMatrixData(item as MatrixType, data.Value); 17 37 18 public override JsonItem ExtractData(IItem value) => 19 new JsonItem() { 20 Name = "[OverridableParamName]", 21 Value = value.Cast<MatrixType>().CloneAsMatrix() 22 }; 38 public override void Populate(IItem value, JsonItem item, IJsonItemConverter root) { 39 item.Name = "[OverridableParamName]"; 40 item.Value = ((MatrixType)value).CloneAsMatrix(); 41 } 23 42 24 43 #region Helper -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/ValueTypeValueConverter.cs
r17374 r17394 8 8 9 9 namespace HeuristicLab.JsonInterface { 10 public class ValueTypeValueConverter<ValueType, T> : BaseConverter 10 11 public class IntValueConverter : ValueTypeValueConverter<IntValue, int> { 12 public override int Priority => 1; 13 public override Type ConvertableType => typeof(IntValue); 14 } 15 16 public class DoubleValueConverter : ValueTypeValueConverter<DoubleValue, double> { 17 public override int Priority => 1; 18 public override Type ConvertableType => typeof(DoubleValue); 19 } 20 21 public class PercentValueConverter : ValueTypeValueConverter<PercentValue, double> { 22 public override int Priority => 2; 23 public override Type ConvertableType => typeof(PercentValue); 24 } 25 26 public class BoolValueConverter : ValueTypeValueConverter<BoolValue, bool> { 27 public override int Priority => 1; 28 public override Type ConvertableType => typeof(BoolValue); 29 } 30 31 public class DateTimeValueConverter : ValueTypeValueConverter<DateTimeValue, DateTime> { 32 public override int Priority => 1; 33 public override Type ConvertableType => typeof(DateTimeValue); 34 } 35 36 public abstract class ValueTypeValueConverter<ValueType, T> : BaseConverter 11 37 where ValueType : ValueTypeValue<T> 12 38 where T : struct { 13 39 14 public override void InjectData(IItem item, JsonItem data ) =>15 item.Cast<ValueType>().Value = CastValue<T>(data.Value);40 public override void InjectData(IItem item, JsonItem data, IJsonItemConverter root) => 41 ((ValueType)item).Value = CastValue<T>(data.Value); 16 42 17 public override JsonItem ExtractData(IItem value) => 18 new JsonItem() { 19 Name = "[OverridableParamName]", 20 Value = value.Cast<ValueType>().Value, 21 Range = new object[] { GetMinValue(typeof(T)), GetMaxValue(typeof(T)) } 22 }; 43 public override void Populate(IItem value, JsonItem item, IJsonItemConverter root) { 44 item.Name = "[OverridableParamName]"; 45 item.Value = ((ValueType)value).Value; 46 item.Range = new object[] { GetMinValue(typeof(T)), GetMaxValue(typeof(T)) }; 47 } 23 48 } 24 49 }
Note: See TracChangeset
for help on using the changeset viewer.