Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17271 was 17271, checked in by dpiringe, 5 years ago

#3026

  • renamed ParameterData to Component
  • renamed File Template.cs to Component.cs
File size: 2.6 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
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}
Note: See TracBrowser for help on using the repository browser.