source: branches/3026_IntegrationIntoSymSpace/HeuristicLab.Manufacture/JCInstantiator.cs @ 17281

Last change on this file since 17281 was 17281, checked in by dpiringe, 3 years ago

#3026: renamed Transformers -> ...Transformer to ...Converter, ITypeTransformer to IJsonItemConverter, Transformer to JsonItemConverter

File size: 3.5 KB
Line 
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using System.IO;
5using System.Linq;
6using System.Reflection;
7using System.Text;
8using System.Threading.Tasks;
9using HeuristicLab.Core;
10using HeuristicLab.Data;
11using HeuristicLab.Optimization;
12using Newtonsoft.Json.Linq;
13
14namespace HeuristicLab.Manufacture {
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      Component algorithmData = GetData(Config["Metadata"]["Algorithm"].ToString());
25      ResolveReferences(algorithmData);
26      IAlgorithm algorithm = CreateObject<IAlgorithm>(algorithmData);
27     
28      Component 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(Component 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 Component GetData(string key)
46    {
47      foreach(JObject item in Config["Objects"])
48      {
49        Component data = BuildComponent(item);
50        if (data.Name == key) return data;
51      }
52      return null;
53    }
54
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)
70      };
71
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));
77
78      if (obj["FreeParameters"] != null) {
79        foreach (JObject param in obj["FreeParameters"]) {
80          Component tmp = BuildComponent(param);
81          Component 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            Component.Merge(comp, tmp);
86          else list.Add(tmp);
87        }
88      }
89      return list;
90    }
91
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;
100    }
101  }
102}
Note: See TracBrowser for help on using the repository browser.