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 HeuristicLab.Core;
|
---|
10 | using HeuristicLab.Data;
|
---|
11 | using HeuristicLab.Optimization;
|
---|
12 | using Newtonsoft.Json.Linq;
|
---|
13 |
|
---|
14 | namespace HeuristicLab.JsonInterface {
|
---|
15 | public class JCInstantiator {
|
---|
16 |
|
---|
17 | private JToken Config { get; set; }
|
---|
18 | private Dictionary<string, string> TypeList = new Dictionary<string, string>();
|
---|
19 |
|
---|
20 | public IAlgorithm Instantiate(string configFile) {
|
---|
21 | Config = JToken.Parse(File.ReadAllText(configFile));
|
---|
22 | TypeList = Config["Types"].ToObject<Dictionary<string, string>>();
|
---|
23 |
|
---|
24 | JsonItem algorithmData = GetData(Config["Metadata"]["Algorithm"].ToString());
|
---|
25 | ResolveReferences(algorithmData);
|
---|
26 | IAlgorithm algorithm = CreateObject<IAlgorithm>(algorithmData);
|
---|
27 |
|
---|
28 | JsonItem problemData = GetData(Config["Metadata"]["Problem"].ToString());
|
---|
29 | ResolveReferences(problemData);
|
---|
30 | IProblem problem = CreateObject<IProblem>(problemData);
|
---|
31 | algorithm.Problem = problem;
|
---|
32 |
|
---|
33 | JsonItemConverter.Inject(algorithm, algorithmData);
|
---|
34 | JsonItemConverter.Inject(algorithm, problemData);
|
---|
35 |
|
---|
36 | return algorithm;
|
---|
37 | }
|
---|
38 |
|
---|
39 | private void ResolveReferences(JsonItem data) {
|
---|
40 | foreach (var p in data.Parameters)
|
---|
41 | if (p.Default is string && p.Reference == null)
|
---|
42 | p.Reference = GetData(p.Default.Cast<string>());
|
---|
43 | }
|
---|
44 |
|
---|
45 | private JsonItem GetData(string key)
|
---|
46 | {
|
---|
47 | foreach(JObject item in Config["Objects"])
|
---|
48 | {
|
---|
49 | JsonItem data = BuildJsonItem(item);
|
---|
50 | if (data.Name == key) return data;
|
---|
51 | }
|
---|
52 | return null;
|
---|
53 | }
|
---|
54 |
|
---|
55 | private T CreateObject<T>(JsonItem data) {
|
---|
56 | if (TypeList.TryGetValue(data.Name, out string typeName)) {
|
---|
57 | Type type = Type.GetType(typeName);
|
---|
58 | return (T)Activator.CreateInstance(type);
|
---|
59 | } else throw new TypeLoadException($"Cannot find AssemblyQualifiedName for {data.Name}.");
|
---|
60 | }
|
---|
61 |
|
---|
62 | private JsonItem BuildJsonItem(JObject obj) =>
|
---|
63 | new JsonItem() {
|
---|
64 | Name = obj[nameof(JsonItem.Name)]?.ToString(),
|
---|
65 | Default = obj[nameof(JsonItem.Default)]?.ToObject<object>(),
|
---|
66 | Range = obj[nameof(JsonItem.Range)]?.ToObject<object[]>(),
|
---|
67 | Type = obj[nameof(JsonItem.Type)]?.ToObject<string>(),
|
---|
68 | Parameters = PopulateParameters(obj),
|
---|
69 | Operators = PopulateOperators(obj)
|
---|
70 | };
|
---|
71 |
|
---|
72 | private IList<JsonItem> PopulateParameters(JObject obj) {
|
---|
73 | IList<JsonItem> list = new List<JsonItem>();
|
---|
74 | if (obj["StaticParameters"] != null)
|
---|
75 | foreach (JObject param in obj["StaticParameters"])
|
---|
76 | list.Add(BuildJsonItem(param));
|
---|
77 |
|
---|
78 | if (obj["FreeParameters"] != null) {
|
---|
79 | foreach (JObject param in obj["FreeParameters"]) {
|
---|
80 | JsonItem tmp = BuildJsonItem(param);
|
---|
81 | JsonItem comp = null;
|
---|
82 | foreach (var p in list) // TODO: nicht notwendig, da immer alle params im static block sind
|
---|
83 | if (p.Name == tmp.Name) comp = p;
|
---|
84 | if (comp != null)
|
---|
85 | JsonItem.Merge(comp, tmp);
|
---|
86 | else list.Add(tmp);
|
---|
87 | }
|
---|
88 | }
|
---|
89 | return list;
|
---|
90 | }
|
---|
91 |
|
---|
92 | private IList<JsonItem> PopulateOperators(JObject obj) {
|
---|
93 | IList<JsonItem> list = new List<JsonItem>();
|
---|
94 | if (obj[nameof(Operators)] != null)
|
---|
95 | foreach (JObject sp in obj[nameof(Operators)]) {
|
---|
96 | JsonItem tmp = BuildJsonItem(sp);
|
---|
97 | list.Add(tmp);
|
---|
98 | }
|
---|
99 | return list;
|
---|
100 | }
|
---|
101 | }
|
---|
102 | }
|
---|