- Timestamp:
- 01/28/20 14:53:45 (5 years ago)
- Location:
- branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface
- Files:
-
- 2 added
- 14 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/BaseConverter.cs
r17407 r17410 34 34 TypeCode typeCode = Type.GetTypeCode(t); 35 35 36 36 37 if (t.IsEqualTo(typeof(PercentValue))) 37 38 return 1.0d; 39 40 if(t == typeof(IntValue)) { 41 return int.MaxValue; 42 } 38 43 39 44 switch (typeCode) { … … 59 64 return 0.0d; 60 65 66 if (t == typeof(IntValue)) { 67 return int.MinValue; 68 } 69 61 70 switch (typeCode) { 62 71 case TypeCode.Int16: return short.MinValue; -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/ConstrainedValueParameterConverter.cs
r17407 r17410 13 13 14 14 public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) { 15 StringJsonItem cdata = data as StringJsonItem; 15 16 IParameter parameter = item as IParameter; 16 17 foreach (var x in GetValidValues(parameter)) 17 if(x.ToString() == CastValue<string>( data.Value))18 if(x.ToString() == CastValue<string>(cdata.Value)) 18 19 parameter.ActualValue = x; 19 20 20 if (parameter.ActualValue is IParameterizedItem && data.Children != null) {21 foreach(var param in data.Children) {21 if (parameter.ActualValue is IParameterizedItem && cdata.Children != null) { 22 foreach(var param in cdata.Children) { 22 23 if(param.Name == parameter.ActualValue.ItemName) 23 24 root.Inject(parameter.ActualValue, param, root); … … 29 30 IParameter parameter = value as IParameter; 30 31 31 IJsonItem item = new JsonItem() {32 IJsonItem item = new StringJsonItem() { 32 33 Name = parameter.Name, 33 34 Value = parameter.ActualValue?.ToString(), -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/EnumTypeConverter.cs
r17407 r17410 15 15 ((dynamic)item).Value = Enum.Parse( 16 16 item.GetType().GenericTypeArguments.First(), 17 CastValue<string>(data.Value));17 ((StringJsonItem)data).Value); 18 18 19 19 public override IJsonItem Extract(IItem value, IJsonItemConverter root) { 20 20 object val = ((dynamic)value).Value; 21 21 Type enumType = val.GetType(); 22 return new JsonItem() {22 return new StringJsonItem() { 23 23 Name = value.ItemName, 24 24 Value = Enum.GetName(enumType, val), -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/MultiCheckedOperatorConverter.cs
r17407 r17410 24 24 dynamic val = value as dynamic; 25 25 foreach (var op in val.Operators) { 26 item.AddChilds(new JsonItem() {26 item.AddChilds(new BoolJsonItem() { 27 27 Name = op.Name, 28 28 Value = val.Operators.ItemChecked(op), 29 Range = new object[] { false, true }29 Range = new bool[] { false, true } 30 30 }); 31 31 } -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/ParameterizedItemConverter.cs
r17407 r17410 16 16 if(data.Children != null) { 17 17 foreach (var sp in data.Children) 18 if (pItem.Parameters.TryGetValue(sp.Name, out IParameter param) )18 if (pItem.Parameters.TryGetValue(sp.Name, out IParameter param) && param != null) 19 19 root.Inject(param, sp, root); 20 20 } -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/StringValueConverter.cs
r17407 r17410 13 13 14 14 public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) => 15 ((StringValue)item).Value = CastValue<string>(data.Value);15 ((StringValue)item).Value = ((StringJsonItem)data).Value; 16 16 17 17 public override IJsonItem Extract(IItem value, IJsonItemConverter root) => 18 new JsonItem() {18 new StringJsonItem() { 19 19 Name = value.ItemName, 20 20 Value = ((StringValue)value).Value -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/ValueParameterConverter.cs
r17407 r17410 31 31 if (tmp.Name == "[OverridableParamName]") { 32 32 tmp.Name = parameter.Name; 33 JsonItem.Merge(item as JsonItem, tmp as JsonItem); 33 item = tmp; 34 //JsonItem.Merge(item as JsonItem, tmp as JsonItem); 34 35 } else 35 36 item.AddChilds(tmp); -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/ValueRangeConverter.cs
r17407 r17410 11 11 namespace HeuristicLab.JsonInterface { 12 12 13 public class IntRangeConverter : ValueRangeConverter<IntRange, IntValue, int>{13 public class IntRangeConverter : BaseConverter { 14 14 public override int Priority => 1; 15 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 }21 22 public abstract class ValueRangeConverter<RangeType, T, TType> : BaseConverter23 where RangeType : StringConvertibleValueTuple<T, T>24 where T : ValueTypeValue<TType>, IDeepCloneable, IStringConvertibleValue25 where TType : struct {26 27 private const BindingFlags Flags = BindingFlags.NonPublic | BindingFlags.Instance;28 16 29 17 public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) { 30 object[] arr = (object[])data.Value;31 Tuple<T,T> tuple = new Tuple<T,T>(Instantiate<T>(arr[0]), Instantiate<T>(arr[1]));32 var field = item.GetType().GetField("values", Flags);33 field.SetValue(tuple, item);18 IntRange range = item as IntRange; 19 IntArrayJsonItem cdata = data as IntArrayJsonItem; 20 range.Start = cdata.Value[0]; 21 range.End = cdata.Value[1]; 34 22 } 35 23 36 24 public override IJsonItem Extract(IItem value, IJsonItemConverter root) { 37 var field = value.GetType().GetField("values", Flags); 38 Tuple<T, T> tuple = (Tuple<T, T>)field.GetValue(value); 39 return new JsonItem() { 25 IntRange range = value as IntRange; 26 return new IntArrayJsonItem() { 40 27 Name = "[OverridableParamName]", 41 Value = new object[] { tuple.Item1.Value, tuple.Item2.Value},42 Range = new object[] { GetMinValue(typeof(TType)), GetMaxValue(typeof(TType))}28 Value = new int[] { range.Start, range.End }, 29 Range = new int[] { int.MinValue, int.MaxValue } 43 30 }; 44 31 } 45 32 } 33 34 public class DoubleRangeConverter : BaseConverter { 35 public override int Priority => 1; 36 public override Type ConvertableType => typeof(DoubleRange); 37 38 public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) { 39 DoubleRange range = item as DoubleRange; 40 DoubleArrayJsonItem cdata = data as DoubleArrayJsonItem; 41 range.Start = cdata.Value[0]; 42 range.End = cdata.Value[1]; 43 } 44 45 public override IJsonItem Extract(IItem value, IJsonItemConverter root) { 46 DoubleRange range = value as DoubleRange; 47 return new DoubleArrayJsonItem() { 48 Name = "[OverridableParamName]", 49 Value = new double[] { range.Start, range.End }, 50 Range = new double[] { double.MinValue, double.MaxValue } 51 }; 52 } 46 53 } 47 54 } -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/ValueTypeValueConverter.cs
r17407 r17410 9 9 namespace HeuristicLab.JsonInterface { 10 10 11 public class IntValueConverter : ValueTypeValueConverter<IntValue, int>{11 public class IntValueConverter : BaseConverter { 12 12 public override int Priority => 1; 13 13 public override Type ConvertableType => typeof(IntValue); 14 15 public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) => 16 ((IntValue)item).Value = ((IntJsonItem)data).Value; 17 18 public override IJsonItem Extract(IItem value, IJsonItemConverter root) => 19 new IntJsonItem() { 20 Name = "[OverridableParamName]", 21 Value = ((IntValue)value).Value, 22 Range = new int[] { int.MinValue, int.MaxValue } 23 }; 14 24 } 15 25 16 public class DoubleValueConverter : ValueTypeValueConverter<DoubleValue, double>{26 public class DoubleValueConverter : BaseConverter { 17 27 public override int Priority => 1; 18 28 public override Type ConvertableType => typeof(DoubleValue); 29 30 public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) => 31 ((DoubleValue)item).Value = ((DoubleJsonItem)data).Value; 32 33 public override IJsonItem Extract(IItem value, IJsonItemConverter root) => 34 new DoubleJsonItem() { 35 Name = "[OverridableParamName]", 36 Value = ((DoubleValue)value).Value, 37 Range = new double[] { double.MinValue, double.MaxValue } 38 }; 19 39 } 20 40 21 public class PercentValueConverter : ValueTypeValueConverter<PercentValue, double>{41 public class PercentValueConverter : BaseConverter { 22 42 public override int Priority => 2; 23 43 public override Type ConvertableType => typeof(PercentValue); 44 45 public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) => 46 ((PercentValue)item).Value = ((DoubleJsonItem)data).Value; 47 48 public override IJsonItem Extract(IItem value, IJsonItemConverter root) => 49 new DoubleJsonItem() { 50 Name = "[OverridableParamName]", 51 Value = ((PercentValue)value).Value, 52 Range = new double[] { double.MinValue, double.MaxValue } 53 }; 24 54 } 25 55 26 public class BoolValueConverter : ValueTypeValueConverter<BoolValue, bool>{56 public class BoolValueConverter : BaseConverter { 27 57 public override int Priority => 1; 28 58 public override Type ConvertableType => typeof(BoolValue); 59 60 public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) => 61 ((BoolValue)item).Value = ((BoolJsonItem)data).Value; 62 63 public override IJsonItem Extract(IItem value, IJsonItemConverter root) => 64 new BoolJsonItem() { 65 Name = "[OverridableParamName]", 66 Value = ((BoolValue)value).Value, 67 Range = new bool[] { false, true } 68 }; 29 69 } 30 70 31 public class DateTimeValueConverter : ValueTypeValueConverter<DateTimeValue, DateTime>{71 public class DateTimeValueConverter : BaseConverter { 32 72 public override int Priority => 1; 33 73 public override Type ConvertableType => typeof(DateTimeValue); 34 }35 36 public abstract class ValueTypeValueConverter<ValueType, T> : BaseConverter37 where ValueType : ValueTypeValue<T>38 where T : struct {39 74 40 75 public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) => 41 (( ValueType)item).Value = CastValue<T>(data.Value);76 ((DateTimeValue)item).Value = ((DateTimeJsonItem)data).Value; 42 77 43 78 public override IJsonItem Extract(IItem value, IJsonItemConverter root) => 44 new JsonItem() {79 new DateTimeJsonItem() { 45 80 Name = "[OverridableParamName]", 46 Value = (( ValueType)value).Value,47 Range = new object[] { GetMinValue(typeof(T)), GetMaxValue(typeof(T))}81 Value = ((DateTimeValue)value).Value, 82 Range = new DateTime[] { DateTime.MinValue, DateTime.MaxValue } 48 83 }; 49 84 } -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/HeuristicLab.JsonInterface.csproj
r17408 r17410 68 68 <Compile Include="Interfaces\IJsonItem.cs" /> 69 69 <Compile Include="Interfaces\IJsonItemValidator.cs" /> 70 <Compile Include="Models\GenericJsonItem.cs" /> 70 71 <Compile Include="Models\JsonItem.cs" /> 72 <Compile Include="Models\JsonItems.cs" /> 71 73 <Compile Include="Models\ResultItem.cs" /> 72 74 <Compile Include="Models\UnsupportedJsonItem.cs" /> -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/JCGenerator.cs
r17406 r17410 15 15 /// </summary> 16 16 public class JCGenerator { 17 private JObject Template { get; set; } 17 private JObject Template { get; set; } = JObject.Parse(Constants.Template); 18 18 private JArray JArrayItems { get; set; } 19 19 private IList<IJsonItem> JsonItems { get; set; } … … 24 24 // data container 25 25 JArrayItems = new JArray(); 26 JArray ResultItems = new JArray(); 26 27 JsonItems = new List<IJsonItem>(); 27 28 … … 35 36 AddIItem(optimizer); 36 37 // save the JArray with JsonItems (= IParameterizedItems) 38 39 JArrayItems = new JArray(); 40 foreach (var item in JsonItems) { 41 if (item is ResultItem) 42 ResultItems.Add(Serialize(item)); 43 else 44 JArrayItems.Add(Serialize(item)); 45 } 37 46 Template[Constants.Parameters] = JArrayItems; 47 Template[Constants.ActivatedResults] = ResultItems; 38 48 // serialize template and return string 39 49 return SingleLineArrayJsonWriter.Serialize(Template); -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/JsonItemConverter.cs
r17406 r17410 52 52 53 53 public void Inject(IItem item, IJsonItem data, IJsonItemConverter root) { 54 if( !Cache.ContainsKey(item.GetHashCode())) {54 if(item != null && !Cache.ContainsKey(item.GetHashCode())) { 55 55 IJsonItemConverter converter = GetConverter(item.GetType()); 56 56 if(converter != null) converter.Inject(item, data, root); -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/JsonTemplateInstantiator.cs
r17406 r17410 22 22 public JArray Config { get; set; } 23 23 public IDictionary<string, IJsonItem> Objects { get; set; } 24 public IOptimizer Optimizer { get; set; } 24 25 } 25 26 … … 47 48 ProtoBufSerializer serializer = new ProtoBufSerializer(); 48 49 IOptimizer optimizer = (IOptimizer)serializer.Deserialize(hLFileLocation); 50 instData.Optimizer = optimizer; 49 51 50 52 // collect all parameterizedItems from template … … 65 67 66 68 #region Helper 69 70 private static object GetValueFromJObject(JObject obj) { 71 object val = obj[nameof(IJsonItem.Value)]?.ToObject<object>(); 72 if (val is JContainer jContainer) // for resolving array values 73 val = jContainer.ToObject<object[]>(); 74 75 return val; 76 } 77 67 78 private static void CollectParameterizedItems(InstData instData) { 79 //JCGenerator generator = new JCGenerator(); 80 //IEnumerable<IJsonItem> items = generator.FetchJsonItems(instData.Optimizer); 81 IJsonItem root = JsonItemConverter.Extract(instData.Optimizer); 82 instData.Objects.Add(root.Path, root); 83 84 foreach (JObject obj in instData.Template[Constants.Parameters]) { 85 string[] pathParts = obj.Property("Path").Value.ToString().Split('.'); 86 IJsonItem tmp = root; 87 IJsonItem old = null; 88 for(int i = 1; i < pathParts.Length; ++i) { 89 foreach(var c in tmp.Children) { 90 if (c.Name == pathParts[i]) 91 tmp = c; 92 } 93 if (old == tmp) 94 throw new Exception($"Invalid path '{string.Join(".", pathParts)}'"); 95 else old = tmp; 96 } 97 tmp.Value = GetValueFromJObject(obj); 98 tmp.Range = obj[nameof(IJsonItem.Range)]?.ToObject<object[]>(); 99 tmp.ActualName = obj[nameof(IJsonItem.ActualName)]?.ToString(); 100 instData.Objects.Add(tmp.Path, tmp); 101 } 102 103 104 /* 68 105 foreach (JObject item in instData.Template[Constants.Parameters]) { 69 106 string[] pathParts = item.Property("Path").Value.ToString().Split('.'); … … 89 126 parent.AddChilds(data); 90 127 instData.Objects.Add(data.Path, data); 91 } 128 }*/ 92 129 } 93 130 … … 95 132 foreach (JObject obj in instData.Config) { 96 133 // build item from config object 97 IJsonItem item = JsonItem.BuildJsonItem(obj); 134 //IJsonItem item = JsonItem.BuildJsonItem(obj); 135 string path = obj.Property("Path").Value.ToString(); 98 136 // override default value 99 if (instData.Objects.TryGetValue( item.Path, out IJsonItem param)) {100 param.Value = item.Value;137 if (instData.Objects.TryGetValue(path, out IJsonItem param)) { 138 param.Value = GetValueFromJObject(obj); 101 139 // override ActualName (for LookupParameters) 102 140 if (param.ActualName != null) 103 param.ActualName = item.ActualName;104 } else throw new InvalidDataException($"No parameter with path='{ item.Path}' defined!");141 param.ActualName = obj[nameof(IJsonItem.ActualName)]?.ToString(); 142 } else throw new InvalidDataException($"No parameter with path='{path}' defined!"); 105 143 } 106 144 } -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Models/JsonItem.cs
r17408 r17410 152 152 } 153 153 #endregion 154 155 #region BuildJsonItemMethods156 public static IJsonItem BuildJsonItem(JObject obj) {157 object val = obj[nameof(Value)]?.ToObject<object>();158 if (val is JContainer jContainer) // for resolving array values159 val = jContainer.ToObject<object[]>();160 161 return new JsonItem() {162 Name = obj[nameof(Name)]?.ToString(),163 Value = val,164 Range = obj[nameof(Range)]?.ToObject<object[]>(),165 ActualName = obj[nameof(ActualName)]?.ToString()166 };167 }168 #endregion169 154 } 170 155 }
Note: See TracChangeset
for help on using the changeset viewer.