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