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 | using System.IO;
|
---|
12 |
|
---|
13 | namespace HeuristicLab.JsonInterface {
|
---|
14 | /// <summary>
|
---|
15 | /// Class to generate json interface templates.
|
---|
16 | /// </summary>
|
---|
17 | public class JCGenerator {
|
---|
18 |
|
---|
19 | public void GenerateTemplate(string path, IOptimizer optimizer) =>
|
---|
20 | GenerateTemplate(path, optimizer.Name, optimizer);
|
---|
21 |
|
---|
22 | public void GenerateTemplate(string path, string templateName, IOptimizer optimizer) =>
|
---|
23 | GenerateTemplate(path, templateName, optimizer, JsonItemConverter.Extract(optimizer));
|
---|
24 |
|
---|
25 | public void GenerateTemplate(string path, string templateName, IOptimizer optimizer, IJsonItem rootItem) {
|
---|
26 | // init
|
---|
27 | JObject template = JObject.Parse(Constants.Template);
|
---|
28 | JArray parameterItems = new JArray();
|
---|
29 | JArray resultItems = new JArray();
|
---|
30 | IList<IJsonItem> jsonItems = new List<IJsonItem>();
|
---|
31 | string fullPath = Path.GetFullPath(path);
|
---|
32 |
|
---|
33 | // recursively filter items with values/ranges/actualNames
|
---|
34 | PopulateJsonItems(rootItem, jsonItems);
|
---|
35 |
|
---|
36 | // serialize hl file
|
---|
37 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
38 | string hlFilePath = fullPath + @"\" + templateName + ".hl";
|
---|
39 | serializer.Serialize(optimizer, hlFilePath);
|
---|
40 |
|
---|
41 | // filter result items
|
---|
42 | foreach (var item in jsonItems) {
|
---|
43 | if (item is ResultItem)
|
---|
44 | resultItems.Add(Serialize(item));
|
---|
45 | else
|
---|
46 | parameterItems.Add(Serialize(item));
|
---|
47 | }
|
---|
48 |
|
---|
49 | // set template data
|
---|
50 | template[Constants.Metadata][Constants.TemplateName] = templateName;
|
---|
51 | template[Constants.Metadata][Constants.HLFileLocation] = hlFilePath;
|
---|
52 | template[Constants.Parameters] = parameterItems;
|
---|
53 | template[Constants.Results] = resultItems;
|
---|
54 |
|
---|
55 | // serialize template and write file
|
---|
56 | File.WriteAllText(fullPath + @"\" + templateName + ".json", SingleLineArrayJsonWriter.Serialize(template));
|
---|
57 | }
|
---|
58 |
|
---|
59 | #region Helper
|
---|
60 | // serializes ParameterizedItems and saves them in list "JsonItems".
|
---|
61 | private void PopulateJsonItems(IJsonItem item, IList<IJsonItem> jsonItems) {
|
---|
62 | IEnumerable<IJsonItem> tmpParameter = item.Children;
|
---|
63 |
|
---|
64 | if (item.Value != null || item.Range != null || item is ResultItem || item.ActualName != null) {
|
---|
65 | jsonItems.Add(item);
|
---|
66 | }
|
---|
67 |
|
---|
68 | if (tmpParameter != null) {
|
---|
69 | foreach (var p in tmpParameter) {
|
---|
70 | PopulateJsonItems(p, jsonItems);
|
---|
71 | }
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 | private static JObject Serialize(IJsonItem item) =>
|
---|
76 | JObject.FromObject(item, Settings());
|
---|
77 |
|
---|
78 | /// <summary>
|
---|
79 | /// Settings for the json serialization.
|
---|
80 | /// </summary>
|
---|
81 | /// <returns>A configured JsonSerializer.</returns>
|
---|
82 | private static JsonSerializer Settings() => new JsonSerializer() {
|
---|
83 | TypeNameHandling = TypeNameHandling.None,
|
---|
84 | NullValueHandling = NullValueHandling.Ignore,
|
---|
85 | ReferenceLoopHandling = ReferenceLoopHandling.Serialize
|
---|
86 | };
|
---|
87 | #endregion
|
---|
88 | }
|
---|
89 | }
|
---|