Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/07/20 16:25:39 (4 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
File:
1 moved

Legend:

Unmodified
Added
Removed
  • 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
Note: See TracChangeset for help on using the changeset viewer.