Free cookie consent management tool by TermsFeed Policy Generator

Changeset 17394


Ignore:
Timestamp:
01/07/20 16:25:39 (5 years ago)
Author:
dpiringe
Message:

#3026:

  • deleted: ConvertableAttribute, DummyConverter, ObjectExtensions
  • renamed: CustomJsonWriter -> SingleLineArrayJsonWriter, JCInstantiator -> JsonTemplateInstantiator
  • added: JsonItemConverterFactory, UnsupportedJsonItem
  • IJsonItemConverter:
    • added two new properties: Priority and ConvertableType -> because converters are automatically collected by plugin infrastructure now
    • Extract, Inject references a root converter now -> typically an instance of JsonItemConverter -> to prevent cycles
  • JsonItemConverter:
    • now implements the interface IJsonItemConverter
    • is now a dynamic class
    • is only instantiable with an factory (JsonItemConverterFactory)
    • still has the old (but now public) static methods Extract and Inject (without ref param IJsonItemConverter root) -> creates instance with factory and calls methods of instance
    • removed register and unregister methods, because the factory collects all converters automatically now (on first call of Create)
    • has cycle detection for Extract and Inject
    • renamed method Get to GetConverter
Location:
branches/3026_IntegrationIntoSymSpace
Files:
2 added
3 deleted
23 edited
2 moved

Legend:

Unmodified
Added
Removed
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface.App/Runner.cs

    r17349 r17394  
    1111  internal static class Runner {
    1212    internal static void Run(string template, string config, string outputFile = @"C:\Workspace\test.txt") {
    13       IAlgorithm alg = JCInstantiator.Instantiate(template, config);
     13      IAlgorithm alg = JsonTemplateInstantiator.Instantiate(template, config);
    1414 
    1515      Task task = alg.StartAsync();
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface.OptimizerIntegration/FileManager.cs

    r17354 r17394  
    6363      if (openFileDialog.ShowDialog() == DialogResult.OK) {
    6464        try {
    65           var content = JCInstantiator.Instantiate(openFileDialog.FileName);
     65          var content = JsonTemplateInstantiator.Instantiate(openFileDialog.FileName);
    6666          IView view = MainFormManager.MainForm.ShowContent(content);
    6767          if (view == null)
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/BaseConverter.cs

    r17379 r17394  
    1111  public abstract class BaseConverter : IJsonItemConverter
    1212  {
    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);
    1519    }
    1620
    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);
    2024      return data;
    2125    }
    2226   
    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);
    2529
    2630    #region Helper
    2731    protected ValueType CastValue<ValueType>(object obj) {
    2832      if (obj is JToken)
    29         return obj.Cast<JToken>().ToObject<ValueType>();
     33        return ((JToken)obj).ToObject<ValueType>();
    3034      else if (obj is IConvertible)
    31         return Convert.ChangeType(obj, typeof(ValueType)).Cast<ValueType>();
     35        return (ValueType)Convert.ChangeType(obj, typeof(ValueType));
    3236      else return (ValueType)obj;
    3337    }
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/ConstrainedValueParameterConverter.cs

    r17379 r17394  
    99namespace HeuristicLab.JsonInterface {
    1010  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) {
    1215      foreach(var x in GetValidValues(parameter))
    1316        if(x.ToString() == CastValue<string>(data.Value))
     
    1720        foreach(var param in data.Children) {
    1821          if(param.Name == parameter.ActualValue.ItemName)
    19             JsonItemConverter.Inject(parameter.ActualValue, param);
     22            root.Inject(parameter.ActualValue, param, root);
    2023        }
    2124      }
    2225    }
    2326
    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    }     
    3133
    3234    #region Helper
    3335    private IItem[] GetValidValues(IParameter value) {
    3436      List<IItem> list = new List<IItem>();
    35       var values = value.Cast<dynamic>().ValidValues;
     37      var values = ((dynamic)value).ValidValues;
    3638      foreach (var x in values) list.Add((IItem)x);
    3739      return list.ToArray();
     
    4042    private IList<JsonItem> GetParameterizedChilds(IParameter value) {
    4143      List<JsonItem> list = new List<JsonItem>();
    42       var values = value.Cast<dynamic>().ValidValues;
     44      var values = ((dynamic)value).ValidValues;
    4345      foreach(var x in values) {
    4446        if (x is IParameterizedItem) {
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/EnumTypeConverter.cs

    r17342 r17394  
    99namespace HeuristicLab.JsonInterface {
    1010  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(
    1316        item.GetType().GenericTypeArguments.First(),
    1417        CastValue<string>(data.Value));
    1518
    16     public override JsonItem ExtractData(IItem value) {
    17       JsonItem data = new JsonItem();
     19    public override void Populate(IItem value, JsonItem item, IJsonItemConverter root) {
    1820      object val = ((dynamic)value).Value;
    1921      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);
    2324    }
    2425  }
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/LookupParameterConverter.cs

    r17342 r17394  
    88namespace HeuristicLab.JsonInterface {
    99  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);
    1512
    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;
    1820  }
    1921}
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/MultiCheckedOperatorConverter.cs

    r17379 r17394  
    99namespace HeuristicLab.JsonInterface {
    1010  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<>);
    1313
    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;
    1718      foreach (var op in val.Operators) {
    18         data.Children.Add(new JsonItem() {
     19        item.AddChilds(new JsonItem() {
    1920          Name = op.Name,
    2021          Value = val.Operators.ItemChecked(op),
     
    2223        });
    2324      }
    24       return data;
    2525    }
    2626
    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);
    2929
    30       dynamic val = item.Cast<dynamic>();
     30      dynamic val = item as dynamic;
    3131      foreach (var op in val.Operators)
    3232        val.Operators.SetItemCheckedState(op, GetOperatorState(op.Name, data));
     
    3535    private bool GetOperatorState(string name, JsonItem data) {
    3636      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;
    3838      }
    3939      return false;
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/ParameterBaseConverter.cs

    r17379 r17394  
    88namespace HeuristicLab.JsonInterface {
    99  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);
    1212
    13     public abstract JsonItem ExtractData(IParameter value);
     13    public abstract void Populate(IParameter value, JsonItem item, IJsonItemConverter root);
    1414
    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);
    1617
    17     public abstract void InjectData(IParameter parameter, JsonItem data);
     18    public abstract void InjectData(IParameter parameter, JsonItem data, IJsonItemConverter root);
    1819  }
    1920}
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/ParameterizedItemConverter.cs

    r17379 r17394  
    88namespace HeuristicLab.JsonInterface {
    99  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;
    1215
    1316      if(data.Children != null) {
    1417        foreach (var sp in data.Children)
    1518          if (pItem.Parameters.TryGetValue(sp.Name, out IParameter param))
    16             JsonItemConverter.Inject(param, sp);
     19            root.Inject(param, sp, root);
    1720      }
    1821    }
    1922
    20     public override JsonItem ExtractData(IItem value) {
    21       JsonItem item = new JsonItem();
     23    public override void Populate(IItem value, JsonItem item, IJsonItemConverter root) {
    2224      var parameterizedItem = value as IParameterizedItem;
    2325
    2426      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));
    3329      }
    34       return item;
    3530    }
    3631  }
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/RegressionProblemDataConverter.cs

    r17379 r17394  
    1111  public class RegressionProblemDataConverter : BaseConverter {
    1212    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) {     
    1917      dynamic val = (dynamic)value;
    2018      object dataset = (object)val.Dataset;
     
    2220      FieldInfo dataInfo = dataset.GetType().GetField("storableData", flags);
    2321      // TODO: aufteilen in trainings und test daten abschnitte
    24       item.Children.Add(new JsonItem() {
     22      item.AddChilds(new JsonItem() {
    2523        Name = "Dataset",
    26         Value = dataInfo.GetValue(dataset),
    27         Path = "Dataset"
     24        Value = dataInfo.GetValue(dataset)
    2825      });
    2926
    3027      IEnumerable<StringValue> variables = (IEnumerable<StringValue>)val.InputVariables;
    31       item.Children.Add(new JsonItem() {
     28      item.AddChilds(new JsonItem() {
    3229        Name = "TargetVariable",
    3330        Value = (object)targetVariable,
    34         Range = variables.Select(x => x.Value),
    35         Path = "TargetVariable"
     31        Range = variables.Select(x => x.Value)
    3632      });
    3733
    3834
    39       item.Children.Add(new JsonItem() {
     35      item.AddChilds(new JsonItem() {
    4036        Name = "AllowedInputVariables",
    4137        Value = (object)val.AllowedInputVariables,
    42         Range = variables.Select(x => x.Value),
    43         Path = "AllowedInputVariables"
     38        Range = variables.Select(x => x.Value)
    4439      });
    45 
    46       item.UpdatePath();
    47 
    48       return item;
    4940    }
    5041
    51     public override void InjectData(IItem item, JsonItem data) {
     42    public override void InjectData(IItem item, JsonItem data, IJsonItemConverter root) {
    5243      // TODO: inject data
    5344      throw new NotImplementedException();
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/StringValueConverter.cs

    r17342 r17394  
    99namespace HeuristicLab.JsonInterface {
    1010  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);
    1313
    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;
    1819  }
    1920}
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/ValueLookupParameterConverter.cs

    r17379 r17394  
    88namespace HeuristicLab.JsonInterface {
    99  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
    1219      object actualValue = null;
    1320      IEnumerable<object> actualRange = null;
    1421      if(param.Value != null) {
    15         JsonItem tmp = JsonItemConverter.Extract(param.Value);
     22        JsonItem tmp = root.Extract(param.Value, root);
     23        tmp.Parent = item;
    1624        actualValue = tmp.Value;
    1725        actualRange = tmp.Range;
     
    1927        actualRange = new object[] { GetMinValue(param.DataType), GetMaxValue(param.DataType) };
    2028      }
    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;
    2831    }
    2932
    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;
    3235      param.ActualName = CastValue<string>(data.ActualName);
    3336      if (param.Value != null)
    34         JsonItemConverter.Inject(param.Value, data);
     37        root.Inject(param.Value, data, root);
    3538    }
    3639  }
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/ValueParameterConverter.cs

    r17374 r17394  
    88namespace HeuristicLab.JsonInterface {
    99  public class ValueParameterConverter : ParameterBaseConverter {
     10    public override int Priority => 2;
     11    public override Type ConvertableType => typeof(IValueParameter);
    1012
    11     public override void InjectData(IParameter parameter, JsonItem data) {
     13    public override void InjectData(IParameter parameter, JsonItem data, IJsonItemConverter root) {
    1214      if (parameter.ActualValue == null && data.Value != null)
    1315        parameter.ActualValue = Instantiate(parameter.DataType);
    14       JsonItemConverter.Inject(parameter.ActualValue, data);
     16      root.Inject(parameter.ActualValue, data, root);
    1517    }
    1618
    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;
    1921      if (value.ActualValue != null) {
    20         JsonItem tmp = JsonItemConverter.Extract(value.ActualValue);
     22        JsonItem tmp = root.Extract(value.ActualValue, root);
    2123        if(tmp.Name == "[OverridableParamName]") {
    2224          tmp.Name = value.Name;
    23           data = tmp;
     25          JsonItem.Merge(item, tmp);
    2426        }
    2527        else
    26           data.AddParameter(tmp);
     28          item.AddChilds(tmp);
    2729      }
    28       return data;
    2930    }
    3031  }
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/ValueRangeConverter.cs

    r17379 r17394  
    1111namespace HeuristicLab.JsonInterface {
    1212
    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  }
    1521
    16   public class ValueRangeConverter<RangeType, T, TType> : BaseConverter
     22  public abstract class ValueRangeConverter<RangeType, T, TType> : BaseConverter
    1723    where RangeType : StringConvertibleValueTuple<T, T>
    1824    where T : ValueTypeValue<TType>, IDeepCloneable, IStringConvertibleValue
     
    2127    private const BindingFlags Flags = BindingFlags.NonPublic | BindingFlags.Instance;
    2228
    23     public override JsonItem ExtractData(IItem value) {
     29    public override void Populate(IItem value, JsonItem item, IJsonItemConverter root) {
    2430      var field = value.GetType().GetField("values", Flags);
    2531      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()) };
    3335    }
    3436
    35     public override void InjectData(IItem item, JsonItem data) {
     37    public override void InjectData(IItem item, JsonItem data, IJsonItemConverter root) {
    3638      object[] arr = (object[])data.Value;
    3739      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  
    99
    1010namespace 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
    1233    where ArrayType : ValueTypeArray<T>
    1334    where T : struct
    1435  {
    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));
    1738
    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    }
    2343
    2444    #region Helper
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/ValueTypeMatrixConverter.cs

    r17379 r17394  
    99
    1010namespace 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
    1232    where MatrixType : ValueTypeMatrix<T>
    1333    where T : struct
    1434  {
    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);
    1737
    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    }
    2342
    2443    #region Helper
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/ValueTypeValueConverter.cs

    r17374 r17394  
    88
    99namespace 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
    1137    where ValueType : ValueTypeValue<T>
    1238    where T : struct {
    1339
    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);
    1642
    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    }
    2348  }
    2449}
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/HeuristicLab.JsonInterface.csproj

    r17379 r17394  
    6161  </ItemGroup>
    6262  <ItemGroup>
    63     <Compile Include="Attributes\ConvertableAttribute.cs" />
    6463    <Compile Include="Constants.cs" />
    6564    <Compile Include="Converters\RegressionProblemDataConverter.cs" />
    6665    <Compile Include="Converters\ValueLookupParameterConverter.cs" />
    6766    <Compile Include="Converters\ValueRangeConverter.cs" />
    68     <Compile Include="CustomJsonWriter.cs" />
    69     <Compile Include="Extensions\ObjectExtensions.cs" />
     67    <Compile Include="SingleLineArrayJsonWriter.cs" />
    7068    <Compile Include="Extensions\TypeExtensions.cs" />
    7169    <Compile Include="JCGenerator.cs" />
    72     <Compile Include="JCInstantiator.cs" />
     70    <Compile Include="JsonTemplateInstantiator.cs" />
    7371    <Compile Include="Converters\ConstrainedValueParameterConverter.cs" />
    7472    <Compile Include="Converters\ParameterBaseConverter.cs" />
     
    7674    <Compile Include="Converters\ValueParameterConverter.cs" />
    7775    <Compile Include="Converters\EnumTypeConverter.cs" />
    78     <Compile Include="Converters\DummyConverter.cs" />
    7976    <Compile Include="Converters\LookupParameterConverter.cs" />
    8077    <Compile Include="Converters\MultiCheckedOperatorConverter.cs" />
     
    8481    <Compile Include="Converters\ValueTypeMatrixConverter.cs" />
    8582    <Compile Include="Converters\ValueTypeValueConverter.cs" />
     83    <Compile Include="JsonItemConverterFactory.cs" />
    8684    <Compile Include="Plugin.cs" />
    8785    <Compile Include="Properties\AssemblyInfo.cs" />
     
    8987    <Compile Include="Interfaces\IJsonItemConverter.cs" />
    9088    <Compile Include="JsonItemConverter.cs" />
     89    <Compile Include="UnsupportedJsonItem.cs" />
    9190  </ItemGroup>
    9291  <ItemGroup>
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Interfaces/IJsonItemConverter.cs

    r17353 r17394  
    1515    /// <param name="item">The IItem which get the data injected.</param>
    1616    /// <param name="data">The JsonItem with the saved values.</param>
    17     void Inject(IItem item, JsonItem data);
     17    void Inject(IItem item, JsonItem data, IJsonItemConverter root);
    1818
    1919    /// <summary>
     
    2323    /// <param name="value">The IItem to extract infos.</param>
    2424    /// <returns>JsonItem with infos to reinitialise the IItem.</returns>
    25     JsonItem Extract(IItem value);
     25    JsonItem Extract(IItem value, IJsonItemConverter root);
     26
     27    Type ConvertableType { get; }
     28    int Priority { get; }
    2629  }
    2730}
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/JCGenerator.cs

    r17379 r17394  
    3535      // which have parameters aswell
    3636      AddInstantiableIItem(Constants.Algorithm, algorithm, genData);
    37       //IsConvertable(algorithm, true);
    3837      if (algorithm.Problem != null) // only when an problem exists
    3938        AddInstantiableIItem(Constants.Problem, algorithm.Problem, genData);
     
    4241      genData.Template[Constants.Parameters] = genData.JsonItems;
    4342      // serialize template and return string
    44       return CustomJsonWriter.Serialize(genData.Template);
     43      return SingleLineArrayJsonWriter.Serialize(genData.Template);
    4544    }
    4645   
    4746    #region Helper
    48     private static bool IsConvertable(object obj, bool throwException = false) {
    49       bool tmp = ConvertableAttribute.IsConvertable(obj);
    50       if (throwException && tmp)
    51         throw new NotSupportedException($"Type {obj.GetType().GetPrettyName(false)} is not convertable!");
    52       return tmp;
    53     }
    5447
    5548    private static void AddInstantiableIItem(string metaDataTagName, IItem item, GenData genData) {
    5649      JsonItem jsonItem = JsonItemConverter.Extract(item);
     50     
    5751      genData.Template[Constants.Metadata][metaDataTagName] = item.ItemName;
    5852      PopulateJsonItems(jsonItem, genData);
     
    6256    private static void PopulateJsonItems(JsonItem item, GenData genData) {
    6357      IEnumerable<JsonItem> tmpParameter = item.Children;
    64       item.Children = null;
    6558
    6659      if (item.Value != null || item.Range != null) {
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/JsonItem.cs

    r17379 r17394  
    1515  /// </summary>
    1616  public class JsonItem {
     17    public class JsonItemValidator {
     18      private IDictionary<int, bool> Cache = new Dictionary<int, bool>();
     19      private JsonItem Root { get; set; }
     20      public JsonItemValidator(JsonItem root) {
     21        Root = root;
     22      }
    1723
    18     #region Private Fields
    19     private string name;
    20     private object value;
    21     private IEnumerable<object> range;
     24      public bool Validate(ref IList<JsonItem> faultyItems) {
     25        faultyItems = new List<JsonItem>();
     26        return ValidateHelper(Root, ref faultyItems);
     27      }
     28
     29      private bool ValidateHelper(JsonItem item, ref IList<JsonItem> faultyItems) {
     30        int hash = item.GetHashCode();
     31        if (Cache.TryGetValue(hash, out bool r))
     32          return r;
     33
     34        bool res = true;
     35        if (item.Value != null && item.Range != null)
     36          res = item.IsInRange();
     37        if (!res) faultyItems.Add(item);
     38        Cache.Add(hash, res);
     39        foreach (var child in item.Children)
     40          res = res && ValidateHelper(child, ref faultyItems);
     41        return res;
     42      }
     43    }
     44
     45    public virtual string Name { get; set; }
     46
     47    public virtual string Path {
     48      get {
     49        JsonItem tmp = Parent;
     50        StringBuilder builder = new StringBuilder(this.Name);
     51        while(tmp != null) {
     52          builder.Insert(0, tmp.Name + ".");
     53          tmp = tmp.Parent;
     54        }
     55        return builder.ToString();
     56      }
     57    }
     58
     59    [JsonIgnore]
     60    public virtual IList<JsonItem> Children { get; protected set; }
     61
     62    [JsonIgnore]
     63    public virtual JsonItem Parent { get; set; }
     64
     65    public virtual object Value { get; set; }
     66
     67    public virtual IEnumerable<object> Range { get; set; }
     68   
     69    public virtual string ActualName { get; set; }
     70
     71    #region Constructors
     72    public JsonItem() { }
     73
     74    public JsonItem(IEnumerable<JsonItem> childs) {
     75      AddChilds(childs);
     76    }
    2277    #endregion
    2378
    24     public string Name {
    25       get => name;
    26       set {
    27         if(Name != value) {
    28           string oldName = Name;
    29           name = value;
    30           // replace name in path if path != null
    31           if (Path != null) {
    32             var parts = Path.Split('.');
    33             parts[Array.IndexOf(parts, oldName)] = name;
    34             Path = string.Join(".", parts);
    35           } else
    36             Path = Name;
    37          
    38           UpdatePath();
    39         }
    40       }
    41     }
    42     public string Path { get; set; }
    43     public IList<JsonItem> Children { get; set; }
    44     public object Value {
    45       get => value;
    46       set {
    47         this.value = value;
    48         CheckConstraints();
    49       }
    50     }
    51     public IEnumerable<object> Range {
    52       get => range;
    53       set {
    54         range = value;
    55         CheckConstraints();
    56       }
    57     }
    58     public string ActualName { get; set; }
    5979
    6080    #region Public Static Methods
     
    6282      target.Name = from.Name ?? target.Name;
    6383      target.Range = from.Range ?? target.Range;
    64       target.Path = from.Path ?? target.Path;
    6584      target.Value = from.Value ?? target.Value;
    6685      target.ActualName = from.ActualName ?? target.ActualName;
     
    7594
    7695    #region Public Methods
    77     public void AddParameter(JsonItem item) {
     96    public void AddChilds(params JsonItem[] childs) =>
     97      AddChilds(childs as IEnumerable<JsonItem>);
     98
     99    public void AddChilds(IEnumerable<JsonItem> childs) {
    78100      if (Children == null)
    79101        Children = new List<JsonItem>();
    80       Children.Add(item);
    81       item.Path = $"{Path}.{item.Name}";
    82       item.UpdatePath();
     102      foreach (var child in childs) {
     103        Children.Add(child);
     104        child.Parent = this;
     105      }
    83106    }
    84107
    85     public void UpdatePath() {
    86       if (Children != null)
    87         UpdatePathHelper(Children);
    88     }
     108    public virtual JsonItemValidator GetValidator() => new JsonItemValidator(this);
    89109    #endregion
    90110
    91111    #region Helper
    92     private void UpdatePathHelper(IEnumerable<JsonItem> items) {
    93       foreach (var item in items) {
    94         item.Path = $"{Path}.{item.Name}";
    95         item.UpdatePath();
    96       }
    97     }
    98112
    99     private void CheckConstraints() {
    100       if (Range != null && Value != null && !IsInRange())
    101         throw new ArgumentOutOfRangeException(nameof(Value), $"{nameof(Value)} is not in range.");
    102     }
    103113
    104     private bool IsInRange() {
     114    protected bool IsInRange() {
    105115      bool b1 = true, b2 = true;
    106116      if (Value is IEnumerable && !(Value is string)) {
     
    117127    }
    118128
    119     private bool IsInRangeList(object value) {
     129    protected bool IsInRangeList(object value) {
    120130      foreach (var x in Range)
    121131        if (x.Equals(value)) return true;
     
    123133    }
    124134
    125     private bool IsInNumericRange(object value) =>
     135    protected bool IsInNumericRange(object value) =>
    126136      IsInNumericRange<ulong>(value)
    127137      || IsInNumericRange<uint>(value)
     
    136146      || (value is double && double.IsNaN((double)value));
    137147
    138     private bool IsInNumericRange<T>(object value) where T : IComparable {
     148    protected bool IsInNumericRange<T>(object value) where T : IComparable {
    139149      object min = Range.First(), max = Range.Last();
    140150      return
     
    153163      return new JsonItem() {
    154164        Name = obj[nameof(Name)]?.ToString(),
    155         Path = obj[nameof(Path)]?.ToString(),
    156165        Value = val,
    157166        Range = obj[nameof(Range)]?.ToObject<object[]>(),
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/JsonItemConverter.cs

    r17379 r17394  
    1212namespace HeuristicLab.JsonInterface {
    1313  /// <summary>
    14   /// Static class for handling json converters.
     14  /// Class for handling json converters.
    1515  /// </summary>
    16   public static class JsonItemConverter {
     16  public class JsonItemConverter : IJsonItemConverter {
     17   
     18    #region Properties
     19    private IDictionary<Type, IJsonItemConverter> Converters { get; set; }
     20      = new Dictionary<Type, IJsonItemConverter>();
    1721
    18     private struct ConverterPriorityContainer {
    19       public IJsonItemConverter Converter { get; set; }
    20       public int Priority { get; set; }
    21     }
     22    private IDictionary<int, JsonItem> Cache { get; set; }
     23      = new Dictionary<int, JsonItem>();
    2224
    23     private static IDictionary<Type, ConverterPriorityContainer> Converters { get; set; }
    24       = new Dictionary<Type, ConverterPriorityContainer>();
    25    
    26     /// <summary>
    27     /// Register a converter for a given type and priority.
    28     /// </summary>
    29     /// <param name="type">The type for which the converter will be selected.</param>
    30     /// <param name="converter">The implemented converter.</param>
    31     /// <param name="priority">The priority for the converter selection (when multiple converter match for a given type). Higher is better.</param>
    32     public static void Register(Type type, IJsonItemConverter converter, int priority) {
    33       if (!Converters.ContainsKey(type))
    34         Converters.Add(type, new ConverterPriorityContainer() { Converter = converter, Priority = priority });
    35     }
     25    public int Priority => throw new NotImplementedException();
    3626
    37     public static void Register(string atticGuid, IJsonItemConverter converter, int priority) =>
    38       Register(new Guid(atticGuid), converter, priority);
    39 
    40     public static void Register(Guid atticGuid, IJsonItemConverter converter, int priority) {
    41       if (Mapper.StaticCache.TryGetType(atticGuid, out Type type)) {
    42         Register(type, converter, priority);
    43       }
    44     }
    45 
    46     public static void Register<T>(IJsonItemConverter converter, int priority) =>
    47       Register(typeof(T), converter, priority);
     27    public Type ConvertableType => throw new NotImplementedException();
     28    #endregion
    4829
    4930    /// <summary>
    50     /// Deregister a converter (same object has to be already registered).
    51     /// </summary>
    52     /// <param name="converter">Converter to deregister.</param>
    53     public static void Deregister(IJsonItemConverter converter) {
    54       var types =
    55         Converters
    56         .Where(x => x.Value.Converter.GetHashCode() == converter.GetHashCode())
    57         .Select(x => x.Key);
    58       foreach (var x in types)
    59         Converters.Remove(x);
    60     }
    61 
    62     /// <summary>
    63     /// Get a converter for a specific type.
     31    /// GetConverter a converter for a specific type.
    6432    /// </summary>
    6533    /// <param name="type">The type for which the converter will be selected.</param>
    6634    /// <returns>An IJsonItemConverter object.</returns>
    67     public static IJsonItemConverter Get(Type type) {
    68       IList<ConverterPriorityContainer> possibleConverters = new List<ConverterPriorityContainer>();
     35    public IJsonItemConverter GetConverter(Type type) {
     36      IList<IJsonItemConverter> possibleConverters = new List<IJsonItemConverter>();
    6937     
    7038      foreach (var x in Converters)
     
    7341
    7442      if(possibleConverters.Count > 0) {
    75         ConverterPriorityContainer best = possibleConverters.First();
     43        IJsonItemConverter best = possibleConverters.First();
    7644        foreach (var x in possibleConverters) {
    7745          if (x.Priority > best.Priority)
    7846            best = x;
    7947        }
    80         return best.Converter;
     48        return best;
    8149      }
    82       return new DummyConverter();
     50      return null;
     51    }
     52   
     53    public void Inject(IItem item, JsonItem data, IJsonItemConverter root) {
     54      if(!Cache.ContainsKey(item.GetHashCode())) {
     55        IJsonItemConverter converter = GetConverter(item.GetType());
     56        if(converter != null) converter.Inject(item, data, root);
     57      }
    8358    }
    8459
    85     internal static void Inject(IItem item, JsonItem data) =>
    86       Get(item.GetType()).Inject(item, data);
     60    public JsonItem Extract(IItem item, IJsonItemConverter root) {
     61      int hash = item.GetHashCode();
     62      if (Cache.TryGetValue(hash, out JsonItem val))
     63        return val;
     64      else {
     65        IJsonItemConverter converter = GetConverter(item.GetType());
     66        if (converter == null) return new UnsupportedJsonItem();
     67        JsonItem tmp = GetConverter(item.GetType()).Extract(item, root);
     68        Cache.Add(hash, tmp);
     69        return tmp;
     70      }
     71    }
     72   
     73    public static void Inject(IItem item, JsonItem data) {
     74      IJsonItemConverter c = JsonItemConverterFactory.Create();
     75      c.Inject(item, data, c);
     76    }
    8777
    88     internal static JsonItem Extract(IItem item) =>
    89       Get(item.GetType()).Extract(item);
    90 
     78    public static JsonItem Extract(IItem item) {
     79      IJsonItemConverter c = JsonItemConverterFactory.Create();
     80      return c.Extract(item, c);
     81    }
    9182
    9283    /// <summary>
    9384    /// Static constructor for default converter configuration.
    9485    /// </summary>
    95     static JsonItemConverter() {
    96       Register<IntValue>(new ValueTypeValueConverter<IntValue, int>(), 1);
    97       Register<DoubleValue>(new ValueTypeValueConverter<DoubleValue, double>(), 1);
    98       Register<PercentValue>(new ValueTypeValueConverter<PercentValue, double>(), 2);
    99       Register<BoolValue>(new ValueTypeValueConverter<BoolValue, bool>(), 1);
    100       Register<DateTimeValue>(new ValueTypeValueConverter<DateTimeValue, DateTime>(), 1);
    101       Register<StringValue>(new StringValueConverter(), 1);
    102 
    103       Register<IntArray>(new ValueTypeArrayConverter<IntArray, int>(), 1);
    104       Register<DoubleArray>(new ValueTypeArrayConverter<DoubleArray, double>(), 1);
    105       Register<PercentArray>(new ValueTypeArrayConverter<PercentArray, double>(), 2);
    106       Register<BoolArray>(new ValueTypeArrayConverter<BoolArray, bool>(), 1);
    107 
    108       Register<IntMatrix>(new ValueTypeMatrixConverter<IntMatrix, int>(), 1);
    109       Register<DoubleMatrix>(new ValueTypeMatrixConverter<DoubleMatrix, double>(), 1);
    110       Register<PercentMatrix>(new ValueTypeMatrixConverter<PercentMatrix, double>(), 2);
    111       Register<BoolMatrix>(new ValueTypeMatrixConverter<BoolMatrix, bool>(), 1);
    112 
    113       Register<DoubleRange>(new DoubleRangeConverter(), 1);
    114       Register<IntRange>(new IntRangeConverter(), 1);
    115 
    116       Register(typeof(EnumValue<>), new EnumTypeConverter(), 1);
    117      
    118       Register<IValueParameter>(new ValueParameterConverter(), 2);
    119       Register<IParameterizedItem>(new ParameterizedItemConverter(), 2);
    120       Register<ILookupParameter>(new LookupParameterConverter(), 3);
    121       Register<IValueLookupParameter>(new ValueLookupParameterConverter(), 4);
    122 
    123       Register(typeof(IConstrainedValueParameter<>), new ConstrainedValueParameterConverter(), 3);
    124       Register(typeof(ICheckedMultiOperator<>), new MultiCheckedOperatorConverter(), 3);
    125      
    126       Register("EE612297-B1AF-42D2-BF21-AF9A2D42791C", new RegressionProblemDataConverter(), 20);
     86    internal JsonItemConverter(IDictionary<Type, IJsonItemConverter> converters) {
     87      Converters = converters;
    12788    }
    12889  }
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/JsonTemplateInstantiator.cs

    r17393 r17394  
    1111using HeuristicLab.Data;
    1212using HeuristicLab.Optimization;
    13 using HeuristicLab.SequentialEngine;
    1413using Newtonsoft.Json.Linq;
    1514
     
    1817  /// Static class to instantiate an IAlgorithm object with a json interface template and config.
    1918  /// </summary>
    20   public static class JCInstantiator {
     19  public static class JsonTemplateInstantiator {
    2120    private struct InstData {
    2221      public JToken Template { get; set; }
    2322      public JArray Config { get; set; }
    2423      public IDictionary<string, JsonItem> Objects { get; set; }
    25       public IDictionary<string, JsonItem> ResolvedItems { get; set; }
    2624    }
    2725
     
    3432    public static IAlgorithm Instantiate(string templateFile, string configFile = "") {
    3533      InstData instData = new InstData() {
    36         Objects = new Dictionary<string, JsonItem>(),
    37         ResolvedItems = new Dictionary<string, JsonItem>()
     34        Objects = new Dictionary<string, JsonItem>()
    3835      };
    3936
     
    5451      // collect all parameterizedItems from template
    5552      CollectParameterizedItems(instData);
    56 
    57       // rebuild tree with paths
    58       RebuildTree(instData);
    59 
     53     
    6054      // if config != null -> merge Template and Config
    6155      if (instData.Config != null)
     
    7973    private static void CollectParameterizedItems(InstData instData) {
    8074      foreach (JObject item in instData.Template[Constants.Parameters]) {
    81         JsonItem data = JsonItem.BuildJsonItem(item);
    82         instData.Objects.Add(data.Path, data);
    83       }
    84     }
    85    
    86     private static JsonItem RebuildTreeHelper(IList<JsonItem> col, string name) {
    87       JsonItem target = null;
    88       foreach (var val in col) {
    89         if (val.Name == name)
    90           target = val;
    91       }
    92       if (target == null) {
    93         target = new JsonItem() {
    94           Name = name,
    95           Path = name,
    96           Children = new List<JsonItem>()
    97         };
    98         col.Add(target);
    99       }
    100       return target;
    101     }
    102 
    103     // rebuilds item tree with splitting paths of each jsonitem
    104     private static void RebuildTree(InstData instData) {
    105       List<JsonItem> values = new List<JsonItem>();
    106       foreach (var x in instData.Objects) {
    107         string[] pathParts = x.Key.Split('.');
    108         JsonItem target = RebuildTreeHelper(values, pathParts[0]);
    109 
    110         for (int i = 1; i < pathParts.Length; ++i) {
    111           target = RebuildTreeHelper(target.Children, pathParts[i]);
     75        string[] pathParts = item.Property("Path").Value.ToString().Split('.');
     76       
     77        // rebuilds object tree
     78        JsonItem parent = null;
     79        StringBuilder partialPath = new StringBuilder();
     80        for(int i = 0; i < pathParts.Length-1; ++i) {
     81          partialPath.Append(pathParts[i]);
     82          JsonItem tmp = null;
     83          if (instData.Objects.TryGetValue(partialPath.ToString(), out JsonItem value)) {
     84            tmp = value;
     85          } else {
     86            tmp = new JsonItem() { Name = pathParts[i] };
     87            if (parent != null) parent.AddChilds(tmp);
     88            instData.Objects.Add(partialPath.ToString(), tmp);
     89          }
     90          partialPath.Append(".");
     91          parent = tmp;
    11292        }
    11393
    114         JsonItem.Merge(target, x.Value);
    115       }
    116       foreach(var val in values) {
    117         instData.ResolvedItems.Add(val.Name, val);
     94        JsonItem data = JsonItem.BuildJsonItem(item);
     95        parent.AddChilds(data);
     96        instData.Objects.Add(data.Path, data);
    11897      }
    11998    }
     
    135114    private static JsonItem GetData(string key, InstData instData)
    136115    {
    137       if (instData.ResolvedItems.TryGetValue(key, out JsonItem value))
     116      if (instData.Objects.TryGetValue(key, out JsonItem value))
    138117        return value;
    139118      else
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/SingleLineArrayJsonWriter.cs

    r17393 r17394  
    88  /// It collapses arrays into a single line.
    99  /// </summary>
    10   internal class CustomJsonWriter : JsonTextWriter {
     10  internal class SingleLineArrayJsonWriter : JsonTextWriter {
    1111    private bool isRangeArray = false;
    1212    public override void WriteStartArray() {
     
    3232    }
    3333
    34     public CustomJsonWriter(TextWriter writer) : base(writer) { }
     34    public SingleLineArrayJsonWriter(TextWriter writer) : base(writer) { }
    3535
    3636    public static string Serialize(JToken token) {
    3737      JsonSerializer serializer = new JsonSerializer();
    3838      StringWriter sw = new StringWriter();
    39       CustomJsonWriter writer = new CustomJsonWriter(sw);
     39      SingleLineArrayJsonWriter writer = new SingleLineArrayJsonWriter(sw);
    4040      writer.Formatting = Formatting.Indented;
    4141      serializer.Serialize(writer, token);
  • branches/3026_IntegrationIntoSymSpace/Heuristiclab.ConfigStarter/Program.cs

    r17379 r17394  
    4646
    4747      File.WriteAllText(@"C:\Workspace\Template.json", JCGenerator.GenerateTemplate(alg));
    48       JCInstantiator.Instantiate(@"C:\Workspace\Template.json");
     48      JsonTemplateInstantiator.Instantiate(@"C:\Workspace\Template.json");
    4949      /*
    5050      List<ICommandLineArgument> arguments = new List<ICommandLineArgument>();
Note: See TracChangeset for help on using the changeset viewer.