Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/17/20 12:33:35 (5 years ago)
Author:
dpiringe
Message:

#3026:

  • refactored JsonTemplateInstantiator -> now returns a InstantiatorResult which contains the optimizer and an IEnumerable of IResultJsonItem
  • code cleanup in JCGenerator
  • relocated the serialization of json items into IJsonItem with method GenerateJObject (virtual base implementation in JsonItem)
    • this allows custom serialization for json items (example: ValueLookupJsonItem)
    • items of interface IIntervalRestrictedJsonItem have a custom implementation of GenerateJObject -> hides Minimum and Maximum if the values are the physically min/max of their type
  • code cleanup in BaseConverter
Location:
branches/3026_IntegrationIntoSymSpace
Files:
30 edited

Legend:

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

    r17464 r17477  
    1616  internal static class Runner {
    1717    internal static void Run(string template, string config, string outputFile) {
    18       IOptimizer optimizer = JsonTemplateInstantiator.Instantiate(template, config, out IEnumerable<string> allowedResultNames);
     18      InstantiatorResult instantiatorResult = JsonTemplateInstantiator.Instantiate(template, config);
     19      IOptimizer optimizer = instantiatorResult.Optimizer;
     20      IEnumerable<IResultJsonItem> configuredResultItem = instantiatorResult.ConfiguredResultItems;
     21
    1922      optimizer.Runs.Clear();
    2023      if(optimizer is EngineAlgorithm e)
     
    2326      Task task = optimizer.StartAsync();
    2427      while(!task.IsCompleted) {
    25         WriteResultsToFile(outputFile, optimizer, allowedResultNames);
     28        WriteResultsToFile(outputFile, optimizer, configuredResultItem);
    2629        Thread.Sleep(100);
    2730      }
    2831     
    29       WriteResultsToFile(outputFile, optimizer, allowedResultNames);
     32      WriteResultsToFile(outputFile, optimizer, configuredResultItem);
    3033    }
    3134
    32     private static void WriteResultsToFile(string file, IOptimizer optimizer, IEnumerable<string> allowedResultNames) =>
    33       File.WriteAllText(file, FetchResults(optimizer, allowedResultNames));
     35    private static void WriteResultsToFile(string file, IOptimizer optimizer, IEnumerable<IResultJsonItem> configuredResultItem) =>
     36      File.WriteAllText(file, FetchResults(optimizer, configuredResultItem));
    3437
    35     private static string FetchResults(IOptimizer optimizer, IEnumerable<string> allowedResultNames) {
     38    private static string FetchResults(IOptimizer optimizer, IEnumerable<IResultJsonItem> configuredResultItem) {
    3639      JArray arr = new JArray();
    37      
     40      IEnumerable<string> configuredResults = configuredResultItem.Select(x => x.Name);
     41
    3842      foreach (var run in optimizer.Runs) {
    3943        JObject obj = new JObject();
     
    4145        obj.Add("Run", JToken.FromObject(run.ToString()));
    4246        foreach (var res in run.Results) {
    43           if (allowedResultNames.Contains(res.Key)) {
     47          if (configuredResults.Contains(res.Key)) {
    4448            if (res.Value is ISymbolicRegressionSolution solution) {
    4549              var formatter = new SymbolicDataAnalysisExpressionMATLABFormatter();
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface.OptimizerIntegration/FileManager.cs

    r17436 r17477  
    2424    }
    2525    public static void ExportJsonTemplate(IContentView view) {
    26       // TODO: view to select free params, warning if no results are generated
    2726      IStorableContent content = view.Content as IStorableContent;
    2827      if (!view.Locked && content != null) {
     
    4645        try {
    4746          var content = JsonTemplateInstantiator.Instantiate(openFileDialog.FileName);
    48           IView view = MainFormManager.MainForm.ShowContent(content);
     47          IView view = MainFormManager.MainForm.ShowContent(content.Optimizer);
    4948          if (view == null)
    5049            ErrorHandling.ShowErrorDialog("There is no view for the loaded item. It cannot be displayed.", new InvalidOperationException("No View Available"));
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface.OptimizerIntegration/ViewModels/JsonItemVMBase.cs

    r17473 r17477  
    1313  {
    1414    IJsonItem IJsonItemVM.Item {
    15       get => item;
    16       set => item = (JsonItemType)value;
     15      get => Item;
     16      set => Item = (JsonItemType)value;
    1717    }
    1818
     
    3939        if (TreeView != null)
    4040          TreeView.Refresh();
     41        SelectedChanged?.Invoke();
    4142        OnPropertyChange(this, nameof(Selected));
    4243      }
     
    6263    public event PropertyChangedEventHandler PropertyChanged;
    6364    public event Action ItemChanged;
     65    public event Action SelectedChanged;
    6466
    6567
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface.OptimizerIntegration/ViewModels/ValueLookupJsonItemVM.cs

    r17473 r17477  
    1010    public override Type TargetedJsonItemType => typeof(ValueLookupJsonItem);
    1111    public override UserControl Control => new ValueLookupJsonItemControl(this);
    12     public IJsonItem JsonItemReference => ((IValueLookupJsonItem)Item).JsonItemReference;
     12    public IJsonItem JsonItemReference => ((IValueLookupJsonItem)Item).ActualValue;
     13
     14    public ValueLookupJsonItemVM() {
     15      base.SelectedChanged += () => {
     16        if (JsonItemReference != null)
     17          JsonItemReference.Active = base.Selected;
     18      };
     19    }
    1320  }
    1421}
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface.OptimizerIntegration/Views/ExportJsonDialog.cs

    r17473 r17477  
    1616namespace HeuristicLab.JsonInterface.OptimizerIntegration {
    1717  public partial class ExportJsonDialog : Form {
     18
     19    #region Private Properties
    1820    private static FolderBrowserDialog FolderBrowserDialog { get; set; }
    1921    private IDictionary<TreeNode, UserControl> Node2Control { get; set; } = new Dictionary<TreeNode, UserControl>();
     
    2325    private IOptimizer Optimizer { get; set; }
    2426    private IList<IJsonItemVM> VMs { get; set; }
    25     private JCGenerator Generator { get; set; } = new JCGenerator();
     27    #endregion
    2628
    2729    private IContent content;
     
    3133        content = value;
    3234
     35        #region Clear
    3336        VMs = new List<IJsonItemVM>();
    3437        treeView.Nodes.Clear();
    3538        treeViewResults.Nodes.Clear();
     39        #endregion
    3640
    3741        Optimizer = content as IOptimizer;
     
    4549      }
    4650    }
    47 
    48    
    49 
    5051
    5152    private void InitCache() {
     
    8586
    8687        if (FolderBrowserDialog.ShowDialog() == DialogResult.OK) {
    87           Generator.GenerateTemplate(FolderBrowserDialog.SelectedPath, textBoxTemplateName.Text, Optimizer, Root);
     88          JCGenerator.GenerateTemplate(FolderBrowserDialog.SelectedPath, textBoxTemplateName.Text, Optimizer, Root);
    8889          Close();
    8990        }
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface.OptimizerIntegration/Views/JsonItemBoolControl.Designer.cs

    r17471 r17477  
    3737      this.checkBoxValue.Margin = new System.Windows.Forms.Padding(0);
    3838      this.checkBoxValue.Name = "checkBoxValue";
     39      this.checkBoxValue.Padding = new System.Windows.Forms.Padding(0, 0, 0, 1);
    3940      this.checkBoxValue.Size = new System.Drawing.Size(400, 22);
    4041      this.checkBoxValue.TabIndex = 19;
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface.OptimizerIntegration/Views/LookupJsonItemControl.Designer.cs

    r17471 r17477  
    6464      this.tableLayoutPanel1.RowCount = 1;
    6565      this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
    66       this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20F));
     66      this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 22F));
    6767      this.tableLayoutPanel1.Size = new System.Drawing.Size(416, 22);
    6868      this.tableLayoutPanel1.TabIndex = 13;
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/BaseConverter.cs

    r17433 r17477  
    1818
    1919    #region Helper
    20     protected ValueType CastValue<ValueType>(object obj) {
    21       if (obj is JToken)
    22         return ((JToken)obj).ToObject<ValueType>();
    23       else if (obj is IConvertible)
    24         return (ValueType)Convert.ChangeType(obj, typeof(ValueType));
    25       else return (ValueType)obj;
    26     }
    2720
    2821    protected IItem Instantiate(Type type, params object[] args) =>
    2922      (IItem)Activator.CreateInstance(type,args);
    30 
    31    
    32     protected object GetMaxValue(Type t) {
    33       TypeCode typeCode = Type.GetTypeCode(t);
    34 
    35       if (t.IsEqualTo(typeof(PercentValue)))
    36         return 1.0d;
    37 
    38       if(t == typeof(IntValue)) {
    39         return int.MaxValue;
    40       }
    41 
    42       switch (typeCode) {
    43         case TypeCode.Int16: return short.MaxValue;
    44         case TypeCode.Int32: return int.MaxValue;
    45         case TypeCode.Int64: return long.MaxValue;
    46         case TypeCode.UInt16: return ushort.MaxValue;
    47         case TypeCode.UInt32: return uint.MaxValue;
    48         case TypeCode.UInt64: return ulong.MaxValue;
    49         case TypeCode.Single: return float.MaxValue;
    50         case TypeCode.Double: return double.MaxValue;
    51         case TypeCode.Decimal: return decimal.MaxValue;
    52         case TypeCode.Byte: return byte.MaxValue;
    53         case TypeCode.Boolean: return true;
    54         default: return GetDefaultValue(t);
    55       }
    56     }
    57 
    58     protected object GetMinValue(Type t) {
    59       TypeCode typeCode = Type.GetTypeCode(t);
    60 
    61       if (t.IsEqualTo(typeof(PercentValue)))
    62         return 0.0d;
    63 
    64       if (t == typeof(IntValue)) {
    65         return int.MinValue;
    66       }
    67 
    68       switch (typeCode) {
    69         case TypeCode.Int16: return short.MinValue;
    70         case TypeCode.Int32: return int.MinValue;
    71         case TypeCode.Int64: return long.MinValue;
    72         case TypeCode.UInt16: return ushort.MinValue;
    73         case TypeCode.UInt32: return uint.MinValue;
    74         case TypeCode.UInt64: return ulong.MinValue;
    75         case TypeCode.Single: return float.MinValue;
    76         case TypeCode.Double: return double.MinValue;
    77         case TypeCode.Decimal: return decimal.MinValue;
    78         case TypeCode.Byte: return byte.MinValue;
    79         case TypeCode.Boolean: return false;
    80         default: return GetDefaultValue(t);
    81       }
    82     }
    83 
    84     protected object GetDefaultValue(Type t) => t.IsValueType ? Activator.CreateInstance(t) : null;
    8523    #endregion
    8624  }
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/ConstrainedValueParameterConverter.cs

    r17473 r17477  
    1616      IParameter parameter = item as IParameter;
    1717      foreach (var x in GetValidValues(parameter))
    18         if(x.ToString() == CastValue<string>(cdata.Value))
     18        if(x.ToString() == cdata.Value)
    1919          parameter.ActualValue = x;
    2020
    21       if (parameter.ActualValue != null && /*parameter.ActualValue is IParameterizedItem &&*/ cdata.Children != null) {
    22         foreach(var param in cdata.Children) {
    23           if(param.Name == parameter.ActualValue.ItemName)
    24             root.Inject(parameter.ActualValue, param, root);
     21      if (parameter.ActualValue != null && parameter.ActualValue is IParameterizedItem && cdata.Children != null) {
     22        foreach(var child in cdata.Children) {
     23          if(child.Name == parameter.ActualValue.ItemName) // name kann verändert werden? egal da bei inject der original name vorhanden ist
     24            root.Inject(parameter.ActualValue, child, root);
    2525        }
    2626      }
     
    3636        ConcreteRestrictedItems = GetValidValues(parameter).Select(x => x.ToString())
    3737      };
    38       item.AddChildren(GetParameterizedChilds(parameter));
     38      item.AddChildren(GetParameterizedChilds(parameter, root));
    3939
    4040      return item;
     
    4949    }
    5050
    51     private IList<IJsonItem> GetParameterizedChilds(IParameter value) {
     51    private IList<IJsonItem> GetParameterizedChilds(IParameter value, IJsonItemConverter root) {
    5252      List<IJsonItem> list = new List<IJsonItem>();
    5353      var values = ((dynamic)value).ValidValues;
    5454      foreach(var x in values) {
    55         if (x is IParameterizedItem) {
    56           IJsonItem tmp = JsonItemConverter.Extract(x);
     55        if (x is IParameterizedItem) { // only makes sense for IParameterizedItems to go deeper
     56          IJsonItem tmp = root.Extract(x, root);
    5757          if(!(tmp is UnsupportedJsonItem))
    5858            list.Add(tmp);
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/ValueLookupParameterConverter.cs

    r17473 r17477  
    1515      IValueLookupJsonItem lookupItem = data as IValueLookupJsonItem;
    1616      param.ActualName = lookupItem.ActualName;
    17       if (param.Value != null)
    18         root.Inject(param.Value, lookupItem.JsonItemReference, root);
     17      if (param.Value != null && lookupItem.ActualValue != null)
     18        root.Inject(param.Value, lookupItem.ActualValue, root);
    1919    }
    2020
     
    2626      if (param.Value != null) {
    2727        IJsonItem tmp = root.Extract(param.Value, root);
    28         item.AddChildren(tmp.Children);
    29         item.JsonItemReference = tmp;
     28        tmp.Parent = item;
     29        //item.AddChildren(tmp.Children);
     30        item.ActualValue = tmp;
    3031      }
    3132      item.Name = param.Name;
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/ValueTypeArrayConverter.cs

    r17473 r17477  
    1616    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
    1717      IntArray arr = item as IntArray;
    18       int[] d = CastValue<int[]>(data);
     18      IntArrayJsonItem intArrayItem = data as IntArrayJsonItem;
    1919      bool resizeTmp = arr.Resizable;
    2020      arr.Resizable = true;
    21       arr.Length = d.Length;
    22       for (int i = 0; i < d.Length; ++i)
    23         arr[i] = d[i];
     21      arr.Length = intArrayItem.Value.Length;
     22      for (int i = 0; i < intArrayItem.Value.Length; ++i)
     23        arr[i] = intArrayItem.Value[i];
    2424      arr.Resizable = resizeTmp;
    2525    }
     
    2727    public override IJsonItem Extract(IItem value, IJsonItemConverter root) =>
    2828      new IntArrayJsonItem() {
    29         Name = "[OverridableParamName]",
     29        Name = value.ItemName,
    3030        Description = value.ItemDescription,
    3131        Value = ((IntArray)value).CloneAsArray(),
     
    4141    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
    4242      DoubleArray arr = item as DoubleArray;
    43       DoubleArrayJsonItem doubleItem = data as DoubleArrayJsonItem;
    44       //double[] d = CastValue<double[]>(data);
     43      DoubleArrayJsonItem doubleArrayItem = data as DoubleArrayJsonItem;
    4544      bool resizeTmp = arr.Resizable;
    4645      arr.Resizable = true;
    47       //arr.Length = d.Length;
    48       arr.Length = doubleItem.Value.Length;
    49       for (int i = 0; i < doubleItem.Value.Length; ++i)
    50         arr[i] = doubleItem.Value[i];
     46      arr.Length = doubleArrayItem.Value.Length;
     47      for (int i = 0; i < doubleArrayItem.Value.Length; ++i)
     48        arr[i] = doubleArrayItem.Value[i];
    5149      arr.Resizable = resizeTmp;
    5250    }
     
    5452    public override IJsonItem Extract(IItem value, IJsonItemConverter root) =>
    5553      new DoubleArrayJsonItem() {
    56         Name = "[OverridableParamName]",
     54        Name = value.ItemName,
    5755        Description = value.ItemDescription,
    5856        Value = ((DoubleArray)value).CloneAsArray(),
     
    6866    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
    6967      PercentArray arr = item as PercentArray;
    70       double[] d = CastValue<double[]>(data);
     68      DoubleArrayJsonItem doubleArrayItem = data as DoubleArrayJsonItem;
    7169      bool resizeTmp = arr.Resizable;
    7270      arr.Resizable = true;
    73       arr.Length = d.Length;
    74       for (int i = 0; i < d.Length; ++i)
    75         arr[i] = d[i];
     71      arr.Length = doubleArrayItem.Value.Length;
     72      for (int i = 0; i < doubleArrayItem.Value.Length; ++i)
     73        arr[i] = doubleArrayItem.Value[i];
    7674      arr.Resizable = resizeTmp;
    7775    }
     
    7977    public override IJsonItem Extract(IItem value, IJsonItemConverter root) =>
    8078      new DoubleArrayJsonItem() {
    81         Name = "[OverridableParamName]",
     79        Name = value.ItemName,
    8280        Description = value.ItemDescription,
    8381        Value = ((PercentArray)value).CloneAsArray(),
     
    9391    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
    9492      BoolArray arr = item as BoolArray;
    95       bool[] d = CastValue<bool[]>(data);
     93      BoolArrayJsonItem boolArrayItem = data as BoolArrayJsonItem;
    9694      bool resizeTmp = arr.Resizable;
    9795      arr.Resizable = true;
    98       arr.Length = d.Length;
    99       for(int i = 0; i < d.Length; ++i)
    100         arr[i] = d[i];
     96      arr.Length = boolArrayItem.Value.Length;
     97      for(int i = 0; i < boolArrayItem.Value.Length; ++i)
     98        arr[i] = boolArrayItem.Value[i];
    10199      arr.Resizable = resizeTmp;
    102100    }
     
    104102    public override IJsonItem Extract(IItem value, IJsonItemConverter root) =>
    105103      new BoolArrayJsonItem() {
    106         //Name = "[OverridableParamName]",
    107104        Name = value.ItemName,
    108105        Description = value.ItemDescription,
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Interfaces/IIntervalRestrictedJsonItem.cs

    r17473 r17477  
    77namespace HeuristicLab.JsonInterface {
    88  public interface IIntervalRestrictedJsonItem<T> : IJsonItem
    9     where T : IComparable
    10   {
     9    where T : IComparable {
    1110    T Minimum { get; set; }
    1211    T Maximum { get; set; }
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Interfaces/IJsonItem.cs

    r17473 r17477  
    11using System;
     2using System.Collections;
    23using System.Collections.Generic;
    34using System.Linq;
     
    89
    910namespace HeuristicLab.JsonInterface {
    10   public interface IJsonItem {
     11  [JsonObject]
     12  public interface IJsonItem : IEnumerable<IJsonItem> {
    1113    bool Active { get; set; }
    1214    string Name { get; set; }
     
    2325    [JsonIgnore]
    2426    IJsonItem Parent { get; set; }
    25 
    26     //object Value { get; set; }
    27 
    28     //IEnumerable<object> Range { get; set; }
    29    
     27       
    3028    IJsonItemValidator GetValidator();
    3129
     
    4644    void LoosenPath();
    4745
    48     void SetFromJObject(JObject jObject);
     46    JObject GenerateJObject();
     47    void SetJObject(JObject jObject);
    4948  }
    5049}
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Interfaces/IValueJsonItem.cs

    r17473 r17477  
    88  public interface IValueJsonItem : IJsonItem {
    99    object Value { get; set; }
    10     //IEnumerable<object> Range { get; set; }
    1110  }
    1211
    1312  public interface IValueJsonItem<T> : IValueJsonItem {
    1413    new T Value { get; set; }
    15     //new IEnumerable<T> Range { get; set; }
    1614  }
    1715}
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Interfaces/IValueLookupJsonItem.cs

    r17471 r17477  
    99  public interface IValueLookupJsonItem : ILookupJsonItem {
    1010    [JsonIgnore]
    11     IJsonItem JsonItemReference { get; set; }
     11    IJsonItem ActualValue { get; set; }
    1212  }
    1313}
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/JCGenerator.cs

    r17473 r17477  
    1717  public class JCGenerator {
    1818
    19     public void GenerateTemplate(string path, IOptimizer optimizer) =>
     19    public static void GenerateTemplate(string path, IOptimizer optimizer) =>
    2020      GenerateTemplate(path, optimizer.Name, optimizer);
    2121
    22     public void GenerateTemplate(string path, string templateName, IOptimizer optimizer) =>
     22    public static void GenerateTemplate(string path, string templateName, IOptimizer optimizer) =>
    2323      GenerateTemplate(path, templateName, optimizer, JsonItemConverter.Extract(optimizer));
    2424
    25     public void GenerateTemplate(string path, string templateName, IOptimizer optimizer, IJsonItem rootItem) {
    26       // init
     25    public static void GenerateTemplate(string path, string templateName, IOptimizer optimizer, IJsonItem rootItem) {
     26      #region Init
    2727      JObject template = JObject.Parse(Constants.Template);
    2828      JArray parameterItems = new JArray();
     
    3030      IList<IJsonItem> jsonItems = new List<IJsonItem>();
    3131      string fullPath = Path.GetFullPath(path);
    32      
     32      #endregion
     33
    3334      // recursively filter items with values/ranges/actualNames
    3435      PopulateJsonItems(rootItem, jsonItems);
    3536
    36       // serialize hl file
     37      #region Serialize HL File
    3738      ProtoBufSerializer serializer = new ProtoBufSerializer();
    3839      string hlFilePath = fullPath + @"\" + templateName + ".hl";
    3940      serializer.Serialize(optimizer, hlFilePath);
     41      #endregion
    4042
    41       // filter result items
     43      #region Filter Items
    4244      foreach (var item in jsonItems) {
    4345        if (item is IResultJsonItem)
    44           resultItems.Add(Serialize(item));
     46          resultItems.Add(item.GenerateJObject());
    4547        else
    46           parameterItems.Add(Serialize(item));
     48          parameterItems.Add(item.GenerateJObject());
    4749      }
    48      
    49       // set template data
     50      #endregion
     51
     52      #region Set Template Data
    5053      template[Constants.Metadata][Constants.TemplateName] = templateName;
    5154      template[Constants.Metadata][Constants.HLFileLocation] = hlFilePath;
    5255      template[Constants.Parameters] = parameterItems;
    5356      template[Constants.Results] = resultItems;
     57      #endregion
    5458
    55       // serialize template and write file
     59      #region Serialize and write to file
    5660      File.WriteAllText(fullPath + @"\" + templateName + ".json", SingleLineArrayJsonWriter.Serialize(template));
     61      #endregion
    5762    }
    5863
    5964    #region Helper   
    60     // serializes ParameterizedItems and saves them in list "JsonItems".
    61     private void PopulateJsonItems(IJsonItem item, IList<IJsonItem> jsonItems) {
    62       IEnumerable<IJsonItem> children = item.Children;
    63      
    64       if (item.Active && !(item is EmptyJsonItem)) {
    65         jsonItems.Add(item);
    66       }
    67 
    68       if (children != null) {
    69         foreach (var p in children) {
    70           PopulateJsonItems(p, jsonItems);
     65    private static void PopulateJsonItems(IJsonItem item, IList<IJsonItem> jsonItems) {
     66      foreach(var x in item) {
     67        if (x.Active && !(x is EmptyJsonItem)) {
     68          jsonItems.Add(x);
    7169        }
    7270      }
    7371    }
    74 
    75     private static JObject Serialize(IJsonItem item) =>
    76       JObject.FromObject(item, Settings());
    77 
    78     /// <summary>
    79     /// Settings for the json serialization.
    80     /// </summary>
    81     /// <returns>A configured JsonSerializer.</returns>
    82     private static JsonSerializer Settings() => new JsonSerializer() {
    83       TypeNameHandling = TypeNameHandling.None,
    84       NullValueHandling = NullValueHandling.Ignore,
    85       ReferenceLoopHandling = ReferenceLoopHandling.Serialize
    86     };
    8772    #endregion
    8873  }
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/JsonTemplateInstantiator.cs

    r17473 r17477  
    1414
    1515namespace HeuristicLab.JsonInterface {
    16   /// <summary>
    17   /// Static class to instantiate an IAlgorithm object with a json interface template and config.
    18   /// </summary>
    19   public static class JsonTemplateInstantiator {
    20     private struct InstData {
    21       public JToken Template { get; set; }
    22       public JArray Config { get; set; }
    23       public IDictionary<string, IJsonItem> Objects { get; set; }
    24       public IOptimizer Optimizer { get; set; }
    25     }
    26 
    27     public static IOptimizer Instantiate(string templateFile) =>
    28       Instantiate(templateFile, null, out IEnumerable<string> allowedResultNames);
     16  public struct InstantiatorResult {
     17    public IOptimizer Optimizer { get; set; }
     18    public IEnumerable<IResultJsonItem> ConfiguredResultItems { get; set; }
     19  }
    2920
    3021
    31     public static IOptimizer Instantiate(string templateFile, out IEnumerable<string> allowedResultNames) =>
    32       Instantiate(templateFile, null, out allowedResultNames);
     22  /// <summary>
     23  /// Class to instantiate an IAlgorithm object with a json interface template and config.
     24  /// </summary>
     25  public class JsonTemplateInstantiator {
    3326
    34    
     27    #region Private Properties
     28    private JToken Template { get; set; }
     29    private JArray Config { get; set; }
     30    private IDictionary<string, IJsonItem> Objects { get; set; } = new Dictionary<string, IJsonItem>();
     31    #endregion
    3532
    3633    /// <summary>
     
    4037    /// <param name="configFile">Config file (json) for the template.</param>
    4138    /// <returns>confugrated IOptimizer object</returns>
    42     public static IOptimizer Instantiate(string templateFile, string configFile, out IEnumerable<string> allowedResultNames) {
    43       InstData instData = new InstData() {
    44         Objects = new Dictionary<string, IJsonItem>()
    45       };
    46 
    47       // parse template and config files
    48       instData.Template = JToken.Parse(File.ReadAllText(templateFile));
    49       if(!string.IsNullOrEmpty(configFile))
    50         instData.Config = JArray.Parse(File.ReadAllText(configFile));
    51 
    52       // extract metadata information
    53       string hLFileLocation = Path.GetFullPath(instData.Template[Constants.Metadata][Constants.HLFileLocation].ToString());
    54 
    55       // deserialize hl file
    56       ProtoBufSerializer serializer = new ProtoBufSerializer();
    57       IOptimizer optimizer = (IOptimizer)serializer.Deserialize(hLFileLocation);
    58       instData.Optimizer = optimizer;
    59 
    60       // collect all parameterizedItems from template
    61       CollectParameterizedItems(instData);
    62      
    63       // if config != null -> merge Template and Config
    64       if (instData.Config != null)
    65         MergeTemplateWithConfig(instData);
    66 
    67       // get algorthm data and object
    68       IJsonItem optimizerData = instData.Objects.First().Value;
    69      
    70       // inject configuration
    71       JsonItemConverter.Inject(optimizer, optimizerData);
    72 
    73       allowedResultNames = CollectResults(instData);
    74 
    75       return optimizer;
     39    public static InstantiatorResult Instantiate(string templateFile, string configFile = null) {
     40      JsonTemplateInstantiator instantiator = new JsonTemplateInstantiator();
     41      return instantiator.ExecuteInstantiaton(templateFile, configFile);
    7642    }
    7743
    7844    #region Helper
    79     private static IEnumerable<string> CollectResults(InstData instData) {
    80       IList<string> res = new List<string>();
    81       foreach(JObject obj in instData.Template[Constants.Results]) {
    82         res.Add(obj.Property("Name").Value.ToString());
     45    private InstantiatorResult ExecuteInstantiaton(string templateFile, string configFile = null) {
     46      InstantiatorResult result = new InstantiatorResult();
     47
     48      #region Parse Files
     49      Template = JToken.Parse(File.ReadAllText(templateFile));
     50      if(!string.IsNullOrEmpty(configFile))
     51        Config = JArray.Parse(File.ReadAllText(configFile));
     52      #endregion
     53
     54      // extract metadata information
     55      string hLFileLocation = Path.GetFullPath(Template[Constants.Metadata][Constants.HLFileLocation].ToString());
     56
     57      #region Deserialize HL File
     58      ProtoBufSerializer serializer = new ProtoBufSerializer();
     59      IOptimizer optimizer = (IOptimizer)serializer.Deserialize(hLFileLocation);
     60      result.Optimizer = optimizer;
     61      #endregion
     62
     63      // collect all parameterizedItems from template
     64      CollectParameterizedItems(optimizer);
     65     
     66      if (Config != null)
     67        MergeTemplateWithConfig();
     68
     69      // get algorithm root item
     70      IJsonItem rootItem = Objects.First().Value;
     71     
     72      // inject configuration
     73      JsonItemConverter.Inject(optimizer, rootItem);
     74
     75      result.ConfiguredResultItems = CollectResults();
     76
     77      return result;
     78    }
     79
     80   
     81    private IEnumerable<IResultJsonItem> CollectResults() {
     82      IList<IResultJsonItem> res = new List<IResultJsonItem>();
     83      foreach(JObject obj in Template[Constants.Results]) {
     84        string name = obj.Property("Name").Value.ToString();
     85        res.Add(new ResultJsonItem() { Name = name });
    8386      }
    8487      return res;
    8588    }
    8689
    87     private static void CollectParameterizedItems(InstData instData) {
    88       IJsonItem root = JsonItemConverter.Extract(instData.Optimizer);
    89       instData.Objects.Add(root.Path, root);
     90    private void CollectParameterizedItems(IOptimizer optimizer) {
     91      IJsonItem root = JsonItemConverter.Extract(optimizer);
     92      Objects.Add(root.Path, root);
    9093
    91       foreach (JObject obj in instData.Template[Constants.Parameters]) {
    92         string[] pathParts = obj.Property("Path").Value.ToString().Split('.');
    93         IJsonItem tmp = root;
    94         IJsonItem old = null;
    95         for(int i = 1; i < pathParts.Length; ++i) {
    96           foreach(var c in tmp.Children) {
    97             if (c.Name == pathParts[i])
    98               tmp = c;
     94      foreach (JObject obj in Template[Constants.Parameters]) {
     95        string path = obj.Property("Path").Value.ToString();
     96        foreach(var tmp in root) {
     97          if(tmp.Path == path) {
     98            tmp.SetJObject(obj);
     99            Objects.Add(tmp.Path, tmp);
    99100          }
    100           if (old == tmp)
    101             throw new Exception($"Invalid path '{string.Join(".", pathParts)}'");
    102           else old = tmp;
    103101        }
    104         tmp.SetFromJObject(obj);
    105         instData.Objects.Add(tmp.Path, tmp);
    106102      }
    107       //IList<IJsonItem> faultyItems = new List<IJsonItem>();
    108       //root.GetValidator().Validate(ref faultyItems);
    109103    }
    110104   
    111     private static void MergeTemplateWithConfig(InstData instData) {
    112       foreach (JObject obj in instData.Config) {
     105    private void MergeTemplateWithConfig() {
     106      foreach (JObject obj in Config) {
    113107        // build item from config object
    114108        string path = obj.Property("Path").Value.ToString();
    115109        // override default value
    116         if (instData.Objects.TryGetValue(path, out IJsonItem param)) {
     110        if (Objects.TryGetValue(path, out IJsonItem param)) {
    117111          // remove fixed template parameter => dont allow to copy them from concrete config
    118112          obj.Property(nameof(IIntervalRestrictedJsonItem<int>.Minimum))?.Remove();
     
    120114          obj.Property(nameof(IConcreteRestrictedJsonItem<string>.ConcreteRestrictedItems))?.Remove();
    121115          // merge
    122           param.SetFromJObject(obj);
     116          param.SetJObject(obj);
    123117        } else throw new InvalidDataException($"No parameter with path='{path}' defined!");
    124118      }
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Models/ArrayJsonItem.cs

    r17473 r17477  
    99  public abstract class ArrayJsonItem<T> : ValueJsonItem<T[]>, IArrayJsonItem {
    1010    public virtual bool Resizable { get; set; }
    11     public override void SetFromJObject(JObject jObject) {
    12       base.SetFromJObject(jObject);
     11    public override void SetJObject(JObject jObject) {
     12      base.SetJObject(jObject);
    1313      Resizable = (jObject[nameof(IArrayJsonItem.Resizable)]?.ToObject<bool>()).GetValueOrDefault();
    1414    }
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Models/DateTimeJsonItem.cs

    r17473 r17477  
    44using System.Text;
    55using System.Threading.Tasks;
     6using Newtonsoft.Json.Linq;
    67
    78namespace HeuristicLab.JsonInterface {
    89  public class DateTimeJsonItem : IntervalRestrictedValueJsonItem<DateTime> {
    910    protected override bool Validate() => Minimum.CompareTo(Value) >= 0 && Maximum.CompareTo(Value) <= 0;
     11   
     12    public override JObject GenerateJObject() {
     13      var obj = base.GenerateJObject();
     14
     15      if (Minimum.CompareTo(DateTime.MinValue) == 0)
     16        obj.Property("Minimum").Remove();
     17
     18      if (Maximum.CompareTo(DateTime.MaxValue) == 0)
     19        obj.Property("Maximum").Remove();
     20
     21      return obj;
     22    }
     23
     24    public override void SetJObject(JObject jObject) {
     25      Minimum = DateTime.MinValue;
     26      Maximum = DateTime.MaxValue;
     27      base.SetJObject(jObject);
     28    }
    1029  }
    1130}
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Models/DoubleJsonItems.cs

    r17473 r17477  
    44using System.Text;
    55using System.Threading.Tasks;
     6using Newtonsoft.Json.Linq;
    67
    78namespace HeuristicLab.JsonInterface {
    8   public class DoubleJsonItem : IntervalRestrictedValueJsonItem<double> { }
    9   public class DoubleArrayJsonItem : IntervalRestrictedArrayJsonItem<double> { }
    10   public class DoubleRangeJsonItem : RangedJsonItem<double> { }
     9  public class DoubleJsonItem : IntervalRestrictedValueJsonItem<double> {
     10    public override JObject GenerateJObject() {
     11      var obj = base.GenerateJObject();
    1112
     13      if (Minimum.CompareTo(double.MinValue) == 0)
     14        obj.Property("Minimum").Remove();
     15
     16      if (Maximum.CompareTo(double.MaxValue) == 0)
     17        obj.Property("Maximum").Remove();
     18
     19      return obj;
     20    }
     21
     22    public override void SetJObject(JObject jObject) {
     23      Minimum = double.MinValue;
     24      Maximum = double.MaxValue;
     25      base.SetJObject(jObject);
     26    }
     27  }
     28  public class DoubleArrayJsonItem : IntervalRestrictedArrayJsonItem<double> {
     29    public override JObject GenerateJObject() {
     30      var obj = base.GenerateJObject();
     31
     32      if (Minimum.CompareTo(double.MinValue) == 0)
     33        obj.Property("Minimum").Remove();
     34
     35      if (Maximum.CompareTo(double.MaxValue) == 0)
     36        obj.Property("Maximum").Remove();
     37
     38      return obj;
     39    }
     40
     41    public override void SetJObject(JObject jObject) {
     42      Minimum = double.MinValue;
     43      Maximum = double.MaxValue;
     44      base.SetJObject(jObject);
     45    }
     46  }
     47  public class DoubleRangeJsonItem : RangedJsonItem<double> {
     48    public override JObject GenerateJObject() {
     49      var obj = base.GenerateJObject();
     50
     51      if (Minimum.CompareTo(double.MinValue) == 0)
     52        obj.Property("Minimum").Remove();
     53
     54      if (Maximum.CompareTo(double.MaxValue) == 0)
     55        obj.Property("Maximum").Remove();
     56
     57      return obj;
     58    }
     59
     60    public override void SetJObject(JObject jObject) {
     61      Minimum = double.MinValue;
     62      Maximum = double.MaxValue;
     63      base.SetJObject(jObject);
     64    }
     65  }
    1266  public class DoubleMatrixJsonItem : IntervalRestrictedMatrixJsonItem<double> {
    13     /*
    14     protected override bool IsInRange() {
    15       for (int c = 0; c < Value.Length; ++c) {
    16         for (int r = 0; r < Value[c].Length; ++r) {
    17           if (Value[c][r] < Range.First() && Range.Last() < Value[c][r])
    18             return false;
    19         }
    20       }
    21       return true;
     67    public override JObject GenerateJObject() {
     68      var obj = base.GenerateJObject();
     69
     70      if (Minimum.CompareTo(double.MinValue) == 0)
     71        obj.Property("Minimum").Remove();
     72
     73      if (Maximum.CompareTo(double.MaxValue) == 0)
     74        obj.Property("Maximum").Remove();
     75
     76      return obj;
    2277    }
    23     */
     78
     79    public override void SetJObject(JObject jObject) {
     80      Minimum = double.MinValue;
     81      Maximum = double.MaxValue;
     82      base.SetJObject(jObject);
     83    }
    2484  }
    2585}
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Models/IntJsonItems.cs

    r17473 r17477  
    44using System.Text;
    55using System.Threading.Tasks;
     6using Newtonsoft.Json.Linq;
    67
    78namespace HeuristicLab.JsonInterface {
    8   public class IntJsonItem : IntervalRestrictedValueJsonItem<int> { }
    9   public class IntArrayJsonItem : IntervalRestrictedArrayJsonItem<int> { }
    10   public class IntRangeJsonItem : RangedJsonItem<int> { }
     9  public class IntJsonItem : IntervalRestrictedValueJsonItem<int> {
     10    public override JObject GenerateJObject() {
     11      var obj = base.GenerateJObject();
     12
     13      if (Minimum.CompareTo(int.MinValue) == 0)
     14        obj.Property("Minimum").Remove();
     15
     16      if (Maximum.CompareTo(int.MaxValue) == 0)
     17        obj.Property("Maximum").Remove();
     18
     19      return obj;
     20    }
     21
     22    public override void SetJObject(JObject jObject) {
     23      Minimum = int.MinValue;
     24      Maximum = int.MaxValue;
     25      base.SetJObject(jObject);
     26    }
     27  }
     28  public class IntArrayJsonItem : IntervalRestrictedArrayJsonItem<int> {
     29    public override JObject GenerateJObject() {
     30      var obj = base.GenerateJObject();
     31
     32      if (Minimum.CompareTo(int.MinValue) == 0)
     33        obj.Property("Minimum").Remove();
     34
     35      if (Maximum.CompareTo(int.MaxValue) == 0)
     36        obj.Property("Maximum").Remove();
     37
     38      return obj;
     39    }
     40
     41    public override void SetJObject(JObject jObject) {
     42      Minimum = int.MinValue;
     43      Maximum = int.MaxValue;
     44      base.SetJObject(jObject);
     45    }
     46  }
     47  public class IntRangeJsonItem : RangedJsonItem<int> {
     48    public override JObject GenerateJObject() {
     49      var obj = base.GenerateJObject();
     50
     51      if (Minimum.CompareTo(int.MinValue) == 0)
     52        obj.Property("Minimum").Remove();
     53
     54      if (Maximum.CompareTo(int.MaxValue) == 0)
     55        obj.Property("Maximum").Remove();
     56
     57      return obj;
     58    }
     59
     60    public override void SetJObject(JObject jObject) {
     61      Minimum = int.MinValue;
     62      Maximum = int.MaxValue;
     63      base.SetJObject(jObject);
     64    }
     65  }
    1166  public class IntMatrixJsonItem : IntervalRestrictedMatrixJsonItem<int> {
    12     /*
    13     protected override bool IsInRange() {
    14       for (int c = 0; c < Value.Length; ++c) {
    15         for (int r = 0; r < Value[c].Length; ++r) {
    16           if (Value[c][r] < Range.First() && Range.Last() < Value[c][r])
    17             return false;
    18         }
    19       }
    20       return true;
     67    public override JObject GenerateJObject() {
     68      var obj = base.GenerateJObject();
     69
     70      if (Minimum.CompareTo(int.MinValue) == 0)
     71        obj.Property("Minimum").Remove();
     72
     73      if (Maximum.CompareTo(int.MaxValue) == 0)
     74        obj.Property("Maximum").Remove();
     75
     76      return obj;
    2177    }
    22     */
     78
     79    public override void SetJObject(JObject jObject) {
     80      Minimum = int.MinValue;
     81      Maximum = int.MaxValue;
     82      base.SetJObject(jObject);
     83    }
    2384  }
    2485}
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Models/IntervalRestrictedArrayJsonItem.cs

    r17473 r17477  
    2020    }
    2121
    22     public override void SetFromJObject(JObject jObject) {
    23       base.SetFromJObject(jObject);
    24       Minimum = jObject[nameof(IIntervalRestrictedJsonItem<T>.Minimum)].ToObject<T>();
    25       Maximum = jObject[nameof(IIntervalRestrictedJsonItem<T>.Maximum)].ToObject<T>();
     22    public override void SetJObject(JObject jObject) {
     23      base.SetJObject(jObject);
     24
     25      var minProp = jObject[nameof(IIntervalRestrictedJsonItem<T>.Minimum)];
     26      if (minProp != null) Minimum = minProp.ToObject<T>();
     27
     28
     29      var maxProp = jObject[nameof(IIntervalRestrictedJsonItem<T>.Maximum)];
     30      if (maxProp != null) Maximum = maxProp.ToObject<T>();
    2631    }
    2732  }
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Models/IntervalRestrictedMatrixJsonItem.cs

    r17473 r17477  
    2020      return true;
    2121    }
    22     public override void SetFromJObject(JObject jObject) {
    23       base.SetFromJObject(jObject);
    24       Minimum = jObject[nameof(IIntervalRestrictedJsonItem<T>.Minimum)].ToObject<T>();
    25       Maximum = jObject[nameof(IIntervalRestrictedJsonItem<T>.Maximum)].ToObject<T>();
     22    public override void SetJObject(JObject jObject) {
     23      base.SetJObject(jObject);
     24
     25      var minProp = jObject[nameof(IIntervalRestrictedJsonItem<T>.Minimum)];
     26      if (minProp != null) Minimum = minProp.ToObject<T>();
     27
     28
     29      var maxProp = jObject[nameof(IIntervalRestrictedJsonItem<T>.Maximum)];
     30      if (maxProp != null) Maximum = maxProp.ToObject<T>();
    2631    }
    2732  }
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Models/IntervalRestrictedValueJsonItem.cs

    r17473 r17477  
    1414    protected override bool Validate() => Minimum.CompareTo(Value) <= 0 && Maximum.CompareTo(Value) >= 0;
    1515
    16     public override void SetFromJObject(JObject jObject) {
    17       base.SetFromJObject(jObject);
    18       Minimum = jObject[nameof(IIntervalRestrictedJsonItem<T>.Minimum)].ToObject<T>();
    19       Maximum = jObject[nameof(IIntervalRestrictedJsonItem<T>.Maximum)].ToObject<T>();
     16    public override void SetJObject(JObject jObject) {
     17      base.SetJObject(jObject);
     18
     19      var minProp = jObject[nameof(IIntervalRestrictedJsonItem<T>.Minimum)];
     20      if (minProp != null) Minimum = minProp.ToObject<T>();
     21
     22
     23      var maxProp = jObject[nameof(IIntervalRestrictedJsonItem<T>.Maximum)];
     24      if (maxProp != null) Maximum = maxProp.ToObject<T>();
    2025    }
    2126  }
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Models/JsonItem.cs

    r17473 r17477  
    6262      }
    6363    }
    64 
    65     //public virtual object Value { get; set; }
    66 
    67     //public virtual IEnumerable<object> Range { get; set; }
    68    
     64       
    6965    // TODO jsonIgnore dataType?
    7066
     
    105101    public void LoosenPath() => fixedPath = "";
    106102
    107     public virtual void SetFromJObject(JObject jObject) {
    108       //Value = jObject[nameof(IJsonItem.Value)]?.ToObject<object>();
    109       //Range = jObject[nameof(IJsonItem.Range)]?.ToObject<object[]>();
    110     }
     103    public virtual JObject GenerateJObject() =>
     104      JObject.FromObject(this, new JsonSerializer() {
     105        TypeNameHandling = TypeNameHandling.None,
     106        NullValueHandling = NullValueHandling.Ignore,
     107        ReferenceLoopHandling = ReferenceLoopHandling.Ignore
     108      });
     109
     110    public virtual void SetJObject(JObject jObject) { }
    111111    #endregion
    112112
    113     #region Helper
    114     /*
    115      * TODO protected abstract bool Validate();
    116      */
     113    #region Abstract Methods
    117114    protected abstract bool Validate();
    118     /*
    119     protected virtual bool IsInRange() {
    120       bool b1 = true, b2 = true;
    121       if (Value is IEnumerable && !(Value is string)) {
    122         foreach (var x in (IEnumerable)Value) {
    123           b1 = b1 ? IsInRangeList(x) : b1;
    124           b2 = b2 ? IsInNumericRange(x) : b2;
     115    #endregion
     116
     117    #region IEnumerable Support
     118    public virtual IEnumerator<IJsonItem> GetEnumerator() {
     119      yield return this;
     120     
     121      if (Children != null) {
     122        foreach (var x in Children) {
     123          foreach (var c in x) {
     124            yield return c;
     125          }
    125126        }
    126       }
    127       else {
    128         b1 = IsInRangeList(Value);
    129         b2 = IsInNumericRange(Value);
    130       }
    131       return b1 || b2;
     127      }
    132128    }
    133129
    134     protected bool IsInRangeList(object value) {
    135       foreach (var x in Range)
    136         if (x.Equals(value)) return true;
    137       return false;
    138     }
    139 
    140     protected bool IsInNumericRange(object value) =>
    141       IsInNumericRange<ulong>(value)
    142       || IsInNumericRange<uint>(value)
    143       || IsInNumericRange<ushort>(value)
    144       || IsInNumericRange<long>(value)
    145       || IsInNumericRange<int>(value)
    146       || IsInNumericRange<short>(value)
    147       || IsInNumericRange<byte>(value)
    148       || IsInNumericRange<float>(value)
    149       || IsInNumericRange<double>(value)
    150       || (value is float && float.IsNaN((float)value))
    151       || (value is double && double.IsNaN((double)value));
    152 
    153     protected bool IsInNumericRange<T>(object value) where T : IComparable {
    154       object min = Range.First(), max = Range.Last();
    155       return
    156         value != null && min != null && max != null && value is T && min is T && max is T &&
    157         (((T)min).CompareTo(value) == -1 || ((T)min).CompareTo(value) == 0) &&
    158         (((T)max).CompareTo(value) == 1 || ((T)max).CompareTo(value) == 0);
    159     }
    160     */
     130    IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
    161131    #endregion
    162132  }
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Models/LookupJsonItem.cs

    r17473 r17477  
    1010    public virtual string ActualName { get; set; }
    1111
    12     public override void SetFromJObject(JObject jObject) {
    13       base.SetFromJObject(jObject);
     12    public override void SetJObject(JObject jObject) {
     13      base.SetJObject(jObject);
    1414      ActualName = jObject[nameof(ILookupJsonItem.ActualName)]?.ToString();
    1515    }
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Models/MatrixJsonItem.cs

    r17473 r17477  
    2323    }
    2424
    25     public override void SetFromJObject(JObject jObject) {
    26       base.SetFromJObject(jObject);
     25    public override void SetJObject(JObject jObject) {
     26      base.SetJObject(jObject);
    2727      RowsResizable = (jObject[nameof(IMatrixJsonItem.RowsResizable)]?.ToObject<bool>()).GetValueOrDefault();
    2828      ColumnsResizable = (jObject[nameof(IMatrixJsonItem.ColumnsResizable)]?.ToObject<bool>()).GetValueOrDefault();
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Models/ValueJsonItem.cs

    r17473 r17477  
    1010    public object Value { get; set; }
    1111
    12     //public IEnumerable<object> Range { get; set; }
    13 
    14     public override void SetFromJObject(JObject jObject) {
     12    public override void SetJObject(JObject jObject) {
    1513      Value = jObject[nameof(IValueJsonItem.Value)]?.ToObject<object>();
    16       //Range = jObject[nameof(IValueJsonItem.Range)]?.ToObject<object[]>();
    1714    }
    1815
     
    2421      set => base.Value = value;
    2522    }
    26 
    27     /*
    28     public new IEnumerable<T> Range {
    29       get => base.Range?.Cast<T>();
    30       set => base.Range = value.Cast<object>();
    31     }
    32     */
    3323
    3424    private T ConvertObject(object obj) {
     
    4232    }
    4333
    44     public override void SetFromJObject(JObject jObject) {
     34    public override void SetJObject(JObject jObject) {
    4535      if(jObject[nameof(IValueJsonItem<T>.Value)] != null)
    4636        Value = jObject[nameof(IValueJsonItem<T>.Value)].ToObject<T>();
    47       //Range = jObject[nameof(IValueJsonItem<T>.Range)]?.ToObject<T[]>();
    4837    }
    4938  }
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Models/ValueLookupJsonItem.cs

    r17473 r17477  
    55using System.Threading.Tasks;
    66using Newtonsoft.Json;
     7using Newtonsoft.Json.Linq;
    78
    89namespace HeuristicLab.JsonInterface {
    910  public class ValueLookupJsonItem : LookupJsonItem, IValueLookupJsonItem {
    10     [JsonIgnore]
    11     public IJsonItem JsonItemReference { get; set; }
     11   
     12    public IJsonItem ActualValue { get; set; }
    1213
    1314    protected override bool Validate() {
    14       if (JsonItemReference == null) return true;
     15      if (ActualValue == null) return true;
    1516      IList<IJsonItem> faultyItems = new List<IJsonItem>();
    16       return JsonItemReference.GetValidator().Validate(ref faultyItems);
     17      return ActualValue.GetValidator().Validate(ref faultyItems);
     18    }
     19
     20    public override JObject GenerateJObject() {
     21      var obj = base.GenerateJObject();
     22      if(ActualValue != null) {
     23        var actualValue = ActualValue.GenerateJObject();
     24        obj.Add(nameof(IValueLookupJsonItem.ActualValue), actualValue);
     25      }
     26      return obj;
     27    }
     28
     29    public override IEnumerator<IJsonItem> GetEnumerator() {
     30      using (var it = base.GetEnumerator()) {
     31        while(it.MoveNext()) {
     32          yield return it.Current;
     33        }
     34      }
     35      if(ActualValue != null) {
     36        using (var it = ActualValue.GetEnumerator()) {
     37          while (it.MoveNext()) {
     38            yield return it.Current;
     39          }
     40        }
     41      }
    1742    }
    1843  }
  • branches/3026_IntegrationIntoSymSpace/Heuristiclab.ConfigStarter/Program.cs

    r17473 r17477  
    4747      IJsonItem root = JsonItemConverter.Extract(alg);
    4848      ActivateJsonItems(root);
    49       //var x = root.Children[0];
    50       //root.Children.Remove(x);
    51       //x.Parent = null;
    5249
    53 
    54       JCGenerator generator = new JCGenerator();
    55       generator.GenerateTemplate(@"C:\Workspace", "Template", alg, root);
    56       //JsonTemplateInstantiator.Instantiate(@"C:\Workspace\Template.json");
     50      JCGenerator.GenerateTemplate(@"C:\Workspace", "Template", alg, root);
    5751     
    5852      List<ICommandLineArgument> arguments = new List<ICommandLineArgument>();
    5953      arguments.Add(new StartArgument("JsonInterface"));
    6054      arguments.Add(new OpenArgument(@"C:\Workspace\Template.json"));
    61       arguments.Add(new OpenArgument(@"C:\Workspace\ConfigProto1.json"));
     55      arguments.Add(new OpenArgument(@"C:\Workspace\Config.json"));
    6256      arguments.Add(new StringArgument(@"C:\Workspace\Output.json"));
    6357
     
    6761
    6862    private static void ActivateJsonItems(IJsonItem item) {
    69       item.Active = true;
    70       if(item.Children != null) {
    71         foreach (var x in item.Children)
    72           ActivateJsonItems(x);
     63      foreach (var x in item) {
     64        x.Active = true;
     65        if (x is ValueLookupJsonItem i) {
     66          i.Active = true;
     67        }
    7368      }
    7469    }
Note: See TracChangeset for help on using the changeset viewer.