1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using HeuristicLab.Common;
|
---|
5 | using HeuristicLab.Core;
|
---|
6 | using HeuristicLab.Data;
|
---|
7 | using HeuristicLab.Optimization;
|
---|
8 | using Newtonsoft.Json;
|
---|
9 | using Newtonsoft.Json.Linq;
|
---|
10 | using HEAL.Attic;
|
---|
11 |
|
---|
12 | namespace HeuristicLab.JsonInterface {
|
---|
13 | /// <summary>
|
---|
14 | /// Static class to generate json interface templates.
|
---|
15 | /// </summary>
|
---|
16 | public static class JCGenerator {
|
---|
17 | private struct GenData {
|
---|
18 | public JObject Template { get; set; }
|
---|
19 | public JArray JsonItems { get; set; }
|
---|
20 | }
|
---|
21 |
|
---|
22 | public static string GenerateTemplate(IOptimizer optimizer) {
|
---|
23 | // data container
|
---|
24 | GenData genData = new GenData() {
|
---|
25 | Template = JObject.Parse(Constants.Template),
|
---|
26 | JsonItems = new JArray()
|
---|
27 | };
|
---|
28 |
|
---|
29 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
30 | serializer.Serialize(optimizer, @"C:\Workspace\template.hl");
|
---|
31 | genData.Template[Constants.Metadata][Constants.HLFileLocation] = @"C:\Workspace\template.hl";
|
---|
32 |
|
---|
33 | // extract JsonItem, save the name in the metadata section of the
|
---|
34 | // template and save it an JArray incl. all parameters of the JsonItem,
|
---|
35 | // which have parameters aswell
|
---|
36 | AddInstantiableIItem(Constants.Optimizer, optimizer, genData);
|
---|
37 |
|
---|
38 | // save the JArray with JsonItems (= IParameterizedItems)
|
---|
39 | genData.Template[Constants.Parameters] = genData.JsonItems;
|
---|
40 | // serialize template and return string
|
---|
41 | return SingleLineArrayJsonWriter.Serialize(genData.Template);
|
---|
42 | }
|
---|
43 |
|
---|
44 | #region Helper
|
---|
45 |
|
---|
46 | private static void AddInstantiableIItem(string metaDataTagName, IItem item, GenData genData) {
|
---|
47 | JsonItem jsonItem = JsonItemConverter.Extract(item);
|
---|
48 |
|
---|
49 | genData.Template[Constants.Metadata][metaDataTagName] = item.ItemName;
|
---|
50 | PopulateJsonItems(jsonItem, genData);
|
---|
51 | }
|
---|
52 |
|
---|
53 | // serializes ParameterizedItems and saves them in list "JsonItems".
|
---|
54 | private static void PopulateJsonItems(JsonItem item, GenData genData) {
|
---|
55 | IEnumerable<JsonItem> tmpParameter = item.Children;
|
---|
56 |
|
---|
57 | if (item.Value != null || item.Range != null) {
|
---|
58 | genData.JsonItems.Add(Serialize(item));
|
---|
59 | }
|
---|
60 |
|
---|
61 | if (tmpParameter != null) {
|
---|
62 | foreach (var p in tmpParameter) {
|
---|
63 | PopulateJsonItems(p, genData);
|
---|
64 | }
|
---|
65 | }
|
---|
66 | }
|
---|
67 |
|
---|
68 | private static JObject Serialize(JsonItem item) =>
|
---|
69 | JObject.FromObject(item, Settings());
|
---|
70 |
|
---|
71 | /// <summary>
|
---|
72 | /// Settings for the json serialization.
|
---|
73 | /// </summary>
|
---|
74 | /// <returns>A configured JsonSerializer.</returns>
|
---|
75 | private static JsonSerializer Settings() => new JsonSerializer() {
|
---|
76 | TypeNameHandling = TypeNameHandling.None,
|
---|
77 | NullValueHandling = NullValueHandling.Ignore,
|
---|
78 | ReferenceLoopHandling = ReferenceLoopHandling.Serialize
|
---|
79 | };
|
---|
80 | #endregion
|
---|
81 | }
|
---|
82 | }
|
---|