1 | using System;
|
---|
2 | using System.Collections;
|
---|
3 | using System.Collections.Generic;
|
---|
4 | using System.IO;
|
---|
5 | using System.Linq;
|
---|
6 | using System.Reflection;
|
---|
7 | using System.Text;
|
---|
8 | using System.Threading.Tasks;
|
---|
9 | using HEAL.Attic;
|
---|
10 | using HeuristicLab.Core;
|
---|
11 | using HeuristicLab.Data;
|
---|
12 | using HeuristicLab.Optimization;
|
---|
13 | using HeuristicLab.SequentialEngine;
|
---|
14 | using Newtonsoft.Json.Linq;
|
---|
15 |
|
---|
16 | namespace HeuristicLab.JsonInterface {
|
---|
17 | /// <summary>
|
---|
18 | /// Static class to instantiate an IAlgorithm object with a json interface template and config.
|
---|
19 | /// </summary>
|
---|
20 | public static class JCInstantiator {
|
---|
21 | private struct InstData {
|
---|
22 | public JToken Template { get; set; }
|
---|
23 | public JArray Config { get; set; }
|
---|
24 | public IDictionary<string, JsonItem> Objects { get; set; }
|
---|
25 | public IDictionary<string, JsonItem> ResolvedItems { get; set; }
|
---|
26 | }
|
---|
27 |
|
---|
28 | /// <summary>
|
---|
29 | /// Instantiate an IAlgorithm object with a template and config.
|
---|
30 | /// </summary>
|
---|
31 | /// <param name="templateFile">Template file (json), generated with JCGenerator.</param>
|
---|
32 | /// <param name="configFile">Config file (json) for the template.</param>
|
---|
33 | /// <returns>confugrated IAlgorithm object</returns>
|
---|
34 | public static IAlgorithm Instantiate(string templateFile, string configFile = "") {
|
---|
35 | InstData instData = new InstData() {
|
---|
36 | Objects = new Dictionary<string, JsonItem>(),
|
---|
37 | ResolvedItems = new Dictionary<string, JsonItem>()
|
---|
38 | };
|
---|
39 |
|
---|
40 | // parse template and config files
|
---|
41 | instData.Template = JToken.Parse(File.ReadAllText(templateFile));
|
---|
42 | if(!string.IsNullOrEmpty(configFile))
|
---|
43 | instData.Config = JArray.Parse(File.ReadAllText(configFile));
|
---|
44 |
|
---|
45 | // extract metadata information
|
---|
46 | string algorithmName = instData.Template[Constants.Metadata][Constants.Algorithm].ToString();
|
---|
47 | string problemName = instData.Template[Constants.Metadata][Constants.Problem].ToString();
|
---|
48 | string hLFileLocation = instData.Template[Constants.Metadata][Constants.HLFileLocation].ToString();
|
---|
49 |
|
---|
50 | // deserialize hl file
|
---|
51 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
52 | IAlgorithm algorithm = (IAlgorithm)serializer.Deserialize(hLFileLocation);
|
---|
53 |
|
---|
54 | // collect all parameterizedItems from template
|
---|
55 | CollectParameterizedItems(instData);
|
---|
56 |
|
---|
57 | // rebuild tree with paths
|
---|
58 | RebuildTree(instData);
|
---|
59 |
|
---|
60 | // if config != null -> merge Template and Config
|
---|
61 | if (instData.Config != null)
|
---|
62 | MergeTemplateWithConfig(instData);
|
---|
63 |
|
---|
64 | // get algorthm data and object
|
---|
65 | JsonItem algorithmData = GetData(algorithmName, instData);
|
---|
66 |
|
---|
67 | // get problem data and object
|
---|
68 | JsonItem problemData = GetData(problemName, instData);
|
---|
69 |
|
---|
70 | // inject configuration
|
---|
71 | JsonItemConverter.Inject(algorithm, algorithmData);
|
---|
72 | if(algorithm.Problem != null)
|
---|
73 | JsonItemConverter.Inject(algorithm.Problem, problemData);
|
---|
74 |
|
---|
75 | return algorithm;
|
---|
76 | }
|
---|
77 |
|
---|
78 | #region Helper
|
---|
79 | private static void CollectParameterizedItems(InstData instData) {
|
---|
80 | 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]);
|
---|
112 | }
|
---|
113 |
|
---|
114 | JsonItem.Merge(target, x.Value);
|
---|
115 | }
|
---|
116 | foreach(var val in values) {
|
---|
117 | instData.ResolvedItems.Add(val.Name, val);
|
---|
118 | }
|
---|
119 | }
|
---|
120 |
|
---|
121 | private static void MergeTemplateWithConfig(InstData instData) {
|
---|
122 | foreach (JObject obj in instData.Config) {
|
---|
123 | // build item from config object
|
---|
124 | JsonItem item = JsonItem.BuildJsonItem(obj);
|
---|
125 | // override default value
|
---|
126 | if (instData.Objects.TryGetValue(item.Path, out JsonItem param)) {
|
---|
127 | param.Value = item.Value;
|
---|
128 | // override ActualName (for LookupParameters)
|
---|
129 | if (param.ActualName != null)
|
---|
130 | param.ActualName = item.ActualName;
|
---|
131 | } else throw new InvalidDataException($"No parameter with path='{item.Path}' defined!");
|
---|
132 | }
|
---|
133 | }
|
---|
134 |
|
---|
135 | private static JsonItem GetData(string key, InstData instData)
|
---|
136 | {
|
---|
137 | if (instData.ResolvedItems.TryGetValue(key, out JsonItem value))
|
---|
138 | return value;
|
---|
139 | else
|
---|
140 | throw new InvalidDataException($"Type of item '{key}' is not defined!");
|
---|
141 | }
|
---|
142 | #endregion
|
---|
143 | }
|
---|
144 | }
|
---|