1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using HeuristicLab.Core;
|
---|
5 | using HeuristicLab.Data;
|
---|
6 | using HeuristicLab.Optimization;
|
---|
7 | using Newtonsoft.Json;
|
---|
8 | using Newtonsoft.Json.Linq;
|
---|
9 |
|
---|
10 | namespace HeuristicLab.JsonInterface {
|
---|
11 | public class JCGenerator {
|
---|
12 | private JObject template = JObject.Parse(Constants.Template);
|
---|
13 | private Dictionary<string, string> TypeList = new Dictionary<string, string>();
|
---|
14 | private JArray JsonItems { get; set; } = new JArray();
|
---|
15 |
|
---|
16 | public string GenerateTemplate(IAlgorithm algorithm) {
|
---|
17 | JsonItems.Clear();
|
---|
18 | TypeList.Clear();
|
---|
19 |
|
---|
20 | // 1.1. extract JsonItem, save the name in the metadata section of the
|
---|
21 | // template and save it an JArray incl. all parameters of the JsonItem,
|
---|
22 | // which have parameters aswell
|
---|
23 | AddInstantiableIItem(Constants.Algorithm, algorithm);
|
---|
24 | if (algorithm.Problem != null) // 1.2. only when an problem exists
|
---|
25 | AddInstantiableIItem(Constants.Problem, algorithm.Problem);
|
---|
26 |
|
---|
27 | // 2. save the JArray with JsonItems (= IParameterizedItems)
|
---|
28 | template[Constants.Objects] = JsonItems;
|
---|
29 | // 3. save the types of the JsonItems (for instatiation)
|
---|
30 | template[Constants.Types] = JObject.FromObject(TypeList);
|
---|
31 | // 4. serialize template and return string
|
---|
32 | return CustomJsonWriter.Serialize(template);
|
---|
33 | }
|
---|
34 |
|
---|
35 | #region Helper
|
---|
36 | private void AddInstantiableIItem(string metaDataTagName, IItem item) {
|
---|
37 | JsonItem jsonItem = JsonItemConverter.Extract(item);
|
---|
38 | template[Constants.Metadata][metaDataTagName] = item.ItemName;
|
---|
39 | PopulateJsonItems(jsonItem);
|
---|
40 | }
|
---|
41 |
|
---|
42 | private void PopulateJsonItems(JsonItem item) {
|
---|
43 | if (item.Parameters != null) {
|
---|
44 | if (item.Range == null)
|
---|
45 | JsonItems.Add(Serialize(item));
|
---|
46 | foreach (var p in item.Parameters)
|
---|
47 | if (p.Parameters != null)
|
---|
48 | PopulateJsonItems(p);
|
---|
49 | }
|
---|
50 | }
|
---|
51 |
|
---|
52 | private JObject Serialize(JsonItem item) {
|
---|
53 | JObject obj = JObject.FromObject(item, Settings());
|
---|
54 | obj[Constants.StaticParameters] = obj[nameof(JsonItem.Parameters)];
|
---|
55 | obj[Constants.FreeParameters] = obj[nameof(JsonItem.Parameters)];
|
---|
56 |
|
---|
57 | obj.Property(nameof(JsonItem.Parameters))?.Remove();
|
---|
58 | RefactorFreeParameters(obj);
|
---|
59 | RefactorStaticParameters(obj);
|
---|
60 |
|
---|
61 | obj.Property(nameof(JsonItem.Default))?.Remove();
|
---|
62 | obj.Property(nameof(JsonItem.Type))?.Remove();
|
---|
63 |
|
---|
64 | TypeList.Add(item.Path, item.Type);
|
---|
65 | return obj;
|
---|
66 | }
|
---|
67 |
|
---|
68 | private void RefactorFreeParameters(JToken token) {
|
---|
69 | IList<JObject> objToRemove = new List<JObject>();
|
---|
70 | TransformNodes(x => {
|
---|
71 | var p = x.ToObject<JsonItem>();
|
---|
72 | if (p.Default == null || (p.Default != null && p.Default.GetType() == typeof(string) && p.Range == null)) {
|
---|
73 | objToRemove.Add(x);
|
---|
74 | } else {
|
---|
75 | x.Property(nameof(JsonItem.Type))?.Remove();
|
---|
76 | x.Property(nameof(JsonItem.Parameters))?.Remove();
|
---|
77 | }
|
---|
78 | }, token[Constants.FreeParameters]);
|
---|
79 | foreach (var x in objToRemove) x.Remove();
|
---|
80 | }
|
---|
81 |
|
---|
82 | private void RefactorStaticParameters(JToken token) {
|
---|
83 | IList<JObject> objToRemove = new List<JObject>();
|
---|
84 | TransformNodes(x => {
|
---|
85 | var p = x.ToObject<JsonItem>();
|
---|
86 | x.Property(nameof(JsonItem.Range))?.Remove();
|
---|
87 | x.Property(nameof(JsonItem.Operators))?.Remove();
|
---|
88 | x.Property(nameof(JsonItem.Parameters))?.Remove();
|
---|
89 | x.Property(nameof(JsonItem.Type))?.Remove();
|
---|
90 | if (p.Default == null) objToRemove.Add(x);
|
---|
91 | }, token[Constants.StaticParameters]);
|
---|
92 | foreach (var x in objToRemove) x.Remove();
|
---|
93 | }
|
---|
94 |
|
---|
95 | private JsonSerializer Settings() => new JsonSerializer() {
|
---|
96 | TypeNameHandling = TypeNameHandling.None,
|
---|
97 | NullValueHandling = NullValueHandling.Ignore,
|
---|
98 | ReferenceLoopHandling = ReferenceLoopHandling.Serialize
|
---|
99 | };
|
---|
100 |
|
---|
101 | private void TransformNodes(Action<JObject> action, params JToken[] tokens) {
|
---|
102 | foreach(JObject obj in tokens.SelectMany(x => x.Children<JObject>()))
|
---|
103 | action(obj);
|
---|
104 | }
|
---|
105 | #endregion
|
---|
106 | }
|
---|
107 | }
|
---|