Changeset 17394 for branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/JsonTemplateInstantiator.cs
- Timestamp:
- 01/07/20 16:25:39 (4 years ago)
- File:
-
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/JsonTemplateInstantiator.cs
r17393 r17394 11 11 using HeuristicLab.Data; 12 12 using HeuristicLab.Optimization; 13 using HeuristicLab.SequentialEngine;14 13 using Newtonsoft.Json.Linq; 15 14 … … 18 17 /// Static class to instantiate an IAlgorithm object with a json interface template and config. 19 18 /// </summary> 20 public static class J CInstantiator {19 public static class JsonTemplateInstantiator { 21 20 private struct InstData { 22 21 public JToken Template { get; set; } 23 22 public JArray Config { get; set; } 24 23 public IDictionary<string, JsonItem> Objects { get; set; } 25 public IDictionary<string, JsonItem> ResolvedItems { get; set; }26 24 } 27 25 … … 34 32 public static IAlgorithm Instantiate(string templateFile, string configFile = "") { 35 33 InstData instData = new InstData() { 36 Objects = new Dictionary<string, JsonItem>(), 37 ResolvedItems = new Dictionary<string, JsonItem>() 34 Objects = new Dictionary<string, JsonItem>() 38 35 }; 39 36 … … 54 51 // collect all parameterizedItems from template 55 52 CollectParameterizedItems(instData); 56 57 // rebuild tree with paths 58 RebuildTree(instData); 59 53 60 54 // if config != null -> merge Template and Config 61 55 if (instData.Config != null) … … 79 73 private static void CollectParameterizedItems(InstData instData) { 80 74 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; 112 92 } 113 93 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); 118 97 } 119 98 } … … 135 114 private static JsonItem GetData(string key, InstData instData) 136 115 { 137 if (instData. ResolvedItems.TryGetValue(key, out JsonItem value))116 if (instData.Objects.TryGetValue(key, out JsonItem value)) 138 117 return value; 139 118 else
Note: See TracChangeset
for help on using the changeset viewer.