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(IAlgorithm algorithm) {
|
---|
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(algorithm, @"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.Algorithm, algorithm, genData);
|
---|
37 | if (algorithm.Problem != null) // only when an problem exists
|
---|
38 | AddInstantiableIItem(Constants.Problem, algorithm.Problem, genData);
|
---|
39 |
|
---|
40 | // save the JArray with JsonItems (= IParameterizedItems)
|
---|
41 | genData.Template[Constants.Parameters] = genData.JsonItems;
|
---|
42 | // serialize template and return string
|
---|
43 | return SingleLineArrayJsonWriter.Serialize(genData.Template);
|
---|
44 | }
|
---|
45 |
|
---|
46 | #region Helper
|
---|
47 |
|
---|
48 | private static void AddInstantiableIItem(string metaDataTagName, IItem item, GenData genData) {
|
---|
49 | JsonItem jsonItem = JsonItemConverter.Extract(item);
|
---|
50 |
|
---|
51 | genData.Template[Constants.Metadata][metaDataTagName] = item.ItemName;
|
---|
52 | PopulateJsonItems(jsonItem, genData);
|
---|
53 | }
|
---|
54 |
|
---|
55 | // serializes ParameterizedItems and saves them in list "JsonItems".
|
---|
56 | private static void PopulateJsonItems(JsonItem item, GenData genData) {
|
---|
57 | IEnumerable<JsonItem> tmpParameter = item.Children;
|
---|
58 |
|
---|
59 | if (item.Value != null || item.Range != null) {
|
---|
60 | genData.JsonItems.Add(Serialize(item));
|
---|
61 | }
|
---|
62 |
|
---|
63 | if (tmpParameter != null) {
|
---|
64 | foreach (var p in tmpParameter) {
|
---|
65 | PopulateJsonItems(p, genData);
|
---|
66 | }
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | private static JObject Serialize(JsonItem item) =>
|
---|
71 | JObject.FromObject(item, Settings());
|
---|
72 |
|
---|
73 | /// <summary>
|
---|
74 | /// Settings for the json serialization.
|
---|
75 | /// </summary>
|
---|
76 | /// <returns>A configured JsonSerializer.</returns>
|
---|
77 | private static JsonSerializer Settings() => new JsonSerializer() {
|
---|
78 | TypeNameHandling = TypeNameHandling.None,
|
---|
79 | NullValueHandling = NullValueHandling.Ignore,
|
---|
80 | ReferenceLoopHandling = ReferenceLoopHandling.Serialize
|
---|
81 | };
|
---|
82 | #endregion
|
---|
83 | }
|
---|
84 | }
|
---|