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 Newtonsoft.Json.Linq;
|
---|
14 |
|
---|
15 | namespace HeuristicLab.JsonInterface {
|
---|
16 | public readonly struct InstantiatorResult {
|
---|
17 | public InstantiatorResult(IOptimizer optimizer, IEnumerable<IResultJsonItem> configuredResultItems) {
|
---|
18 | Optimizer = optimizer;
|
---|
19 | ConfiguredResultItems = configuredResultItems;
|
---|
20 | }
|
---|
21 |
|
---|
22 | public IOptimizer Optimizer { get; }
|
---|
23 | public IEnumerable<IResultJsonItem> ConfiguredResultItems { get; }
|
---|
24 | }
|
---|
25 |
|
---|
26 |
|
---|
27 | /// <summary>
|
---|
28 | /// Class to instantiate an IAlgorithm object with a json interface template and config.
|
---|
29 | /// </summary>
|
---|
30 | public class JsonTemplateInstantiator {
|
---|
31 |
|
---|
32 | #region Private Properties
|
---|
33 | private JToken Template { get; set; }
|
---|
34 | private JArray Config { get; set; }
|
---|
35 | private IDictionary<string, IJsonItem> Objects { get; set; } = new Dictionary<string, IJsonItem>();
|
---|
36 | #endregion
|
---|
37 |
|
---|
38 | /// <summary>
|
---|
39 | /// Instantiate an IAlgorithm object with a template and config.
|
---|
40 | /// </summary>
|
---|
41 | /// <param name="templateFile">Template file (json), generated with JCGenerator.</param>
|
---|
42 | /// <param name="configFile">Config file (json) for the template.</param>
|
---|
43 | /// <returns>confugrated IOptimizer object</returns>
|
---|
44 | public static InstantiatorResult Instantiate(string templateFile, string configFile = null) {
|
---|
45 | JsonTemplateInstantiator instantiator = new JsonTemplateInstantiator();
|
---|
46 | return instantiator.ExecuteInstantiaton(templateFile, configFile);
|
---|
47 | }
|
---|
48 |
|
---|
49 | #region Helper
|
---|
50 | private InstantiatorResult ExecuteInstantiaton(string templateFile, string configFile = null) {
|
---|
51 |
|
---|
52 | #region Parse Files
|
---|
53 | Template = JToken.Parse(File.ReadAllText(templateFile));
|
---|
54 | if(!string.IsNullOrEmpty(configFile))
|
---|
55 | Config = JArray.Parse(File.ReadAllText(configFile));
|
---|
56 | #endregion
|
---|
57 |
|
---|
58 | // extract metadata information
|
---|
59 | string hLFileLocation = Path.GetFullPath(Template[Constants.Metadata][Constants.HLFileLocation].ToString());
|
---|
60 |
|
---|
61 | #region Deserialize HL File
|
---|
62 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
63 | IOptimizer optimizer = (IOptimizer)serializer.Deserialize(hLFileLocation);
|
---|
64 | #endregion
|
---|
65 |
|
---|
66 | // collect all parameterizedItems from template
|
---|
67 | CollectParameterizedItems(optimizer);
|
---|
68 |
|
---|
69 | if (Config != null)
|
---|
70 | MergeTemplateWithConfig();
|
---|
71 |
|
---|
72 | // get algorithm root item
|
---|
73 | IJsonItem rootItem = Objects.First().Value;
|
---|
74 |
|
---|
75 | // inject configuration
|
---|
76 | JsonItemConverter.Inject(optimizer, rootItem);
|
---|
77 |
|
---|
78 | return new InstantiatorResult(optimizer, CollectResults());
|
---|
79 | }
|
---|
80 |
|
---|
81 |
|
---|
82 | private IEnumerable<IResultJsonItem> CollectResults() {
|
---|
83 | IList<IResultJsonItem> res = new List<IResultJsonItem>();
|
---|
84 | foreach(JObject obj in Template[Constants.Results]) {
|
---|
85 | string name = obj.Property("Name").Value.ToString();
|
---|
86 | res.Add(new ResultJsonItem() { Name = name });
|
---|
87 | }
|
---|
88 | return res;
|
---|
89 | }
|
---|
90 |
|
---|
91 | private void CollectParameterizedItems(IOptimizer optimizer) {
|
---|
92 | IJsonItem root = JsonItemConverter.Extract(optimizer);
|
---|
93 | Objects.Add(root.Path, root);
|
---|
94 |
|
---|
95 | foreach (JObject obj in Template[Constants.Parameters]) {
|
---|
96 | string path = obj.Property("Path").Value.ToString();
|
---|
97 | foreach(var tmp in root) {
|
---|
98 | if(tmp.Path == path) {
|
---|
99 | tmp.SetJObject(obj);
|
---|
100 | Objects.Add(tmp.Path, tmp);
|
---|
101 | }
|
---|
102 | }
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 | private void MergeTemplateWithConfig() {
|
---|
107 | foreach (JObject obj in Config) {
|
---|
108 | // build item from config object
|
---|
109 | string path = obj.Property("Path").Value.ToString();
|
---|
110 | // override default value
|
---|
111 | if (Objects.TryGetValue(path, out IJsonItem param)) {
|
---|
112 | // remove fixed template parameter from config => dont allow to copy them from concrete config
|
---|
113 | obj.Property(nameof(IIntervalRestrictedJsonItem<int>.Minimum))?.Remove();
|
---|
114 | obj.Property(nameof(IIntervalRestrictedJsonItem<int>.Maximum))?.Remove();
|
---|
115 | obj.Property(nameof(IConcreteRestrictedJsonItem<string>.ConcreteRestrictedItems))?.Remove();
|
---|
116 | // merge
|
---|
117 | param.SetJObject(obj);
|
---|
118 | } else throw new InvalidDataException($"No parameter with path='{path}' defined!");
|
---|
119 | }
|
---|
120 | }
|
---|
121 | #endregion
|
---|
122 | }
|
---|
123 | }
|
---|