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.Manufacture {
|
---|
15 |
|
---|
16 |
|
---|
17 | public class JCInstantiator {
|
---|
18 |
|
---|
19 | private JToken Config { get; set; }
|
---|
20 |
|
---|
21 | public IAlgorithm Instantiate(string configFile) {
|
---|
22 | Config = JToken.Parse(File.ReadAllText(configFile));
|
---|
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 | Transformer.Inject(algorithm, algorithmData);
|
---|
34 | Transformer.Inject(algorithm, problemData);
|
---|
35 |
|
---|
36 | return algorithm;
|
---|
37 | }
|
---|
38 |
|
---|
39 | /*
|
---|
40 | * resolve references
|
---|
41 | */
|
---|
42 |
|
---|
43 | private void ResolveReferences(Component data) {
|
---|
44 | foreach (var p in data.Parameters) {
|
---|
45 | if (p.Default is string && p.Reference == null) {
|
---|
46 | p.Reference = GetData(p.Default.Cast<string>());
|
---|
47 | }
|
---|
48 | }
|
---|
49 | }
|
---|
50 |
|
---|
51 | private Component GetData(string key)
|
---|
52 | {
|
---|
53 | foreach(JObject item in Config["Objects"])
|
---|
54 | {
|
---|
55 | Component data = BuildDataFromJObject(item);
|
---|
56 | if (data.Name == key) return data;
|
---|
57 | }
|
---|
58 | return null;
|
---|
59 | }
|
---|
60 |
|
---|
61 | private Component BuildDataFromJObject(JObject obj) {
|
---|
62 | Component data = new Component() {
|
---|
63 | Name = obj["Name"]?.ToString(),
|
---|
64 | Default = obj["Default"]?.ToObject<object>(),
|
---|
65 | Range = obj["Range"]?.ToObject<object[]>(),
|
---|
66 | Type = obj["Type"]?.ToObject<string>()
|
---|
67 | };
|
---|
68 |
|
---|
69 | if(obj["StaticParameters"] != null)
|
---|
70 | foreach (JObject sp in obj["StaticParameters"])
|
---|
71 | data[sp["Name"].ToString()] = BuildDataFromJObject(sp);
|
---|
72 |
|
---|
73 | if (obj["FreeParameters"] != null)
|
---|
74 | foreach (JObject sp in obj["FreeParameters"])
|
---|
75 | data[sp["Name"].ToString()] = BuildDataFromJObject(sp);
|
---|
76 |
|
---|
77 | if (obj["Operators"] != null) {
|
---|
78 | data.Operators = new List<Component>();
|
---|
79 | foreach (JObject sp in obj["Operators"])
|
---|
80 | data.Operators.Add(BuildDataFromJObject(sp));
|
---|
81 | }
|
---|
82 |
|
---|
83 | return data;
|
---|
84 | }
|
---|
85 |
|
---|
86 | private T CreateObject<T>(Component data) {
|
---|
87 | Type type = Type.GetType(data.Type);
|
---|
88 | return (T)Activator.CreateInstance(type);
|
---|
89 | }
|
---|
90 |
|
---|
91 | }
|
---|
92 | }
|
---|