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