1 | using System.Collections.Generic;
|
---|
2 | using HeuristicLab.Optimization;
|
---|
3 | using Newtonsoft.Json.Linq;
|
---|
4 | using HEAL.Attic;
|
---|
5 | using System.IO;
|
---|
6 | using HeuristicLab.Core;
|
---|
7 |
|
---|
8 | namespace HeuristicLab.JsonInterface {
|
---|
9 | /// <summary>
|
---|
10 | /// Class to generate json interface templates.
|
---|
11 | /// </summary>
|
---|
12 | public class JsonTemplateGenerator {
|
---|
13 |
|
---|
14 | public static void GenerateTemplate(string path, IOptimizer optimizer) =>
|
---|
15 | GenerateTemplate(path, optimizer.Name, optimizer);
|
---|
16 |
|
---|
17 | public static void GenerateTemplate(string path, string templateName, IOptimizer optimizer) =>
|
---|
18 | GenerateTemplate(path, templateName, optimizer, JsonItemConverter.Extract(optimizer));
|
---|
19 |
|
---|
20 | public static void GenerateTemplate(string path, string templateName, IOptimizer optimizer, IJsonItem rootItem) {
|
---|
21 | #region Init
|
---|
22 | JObject template = JObject.Parse(Constants.Template);
|
---|
23 | JArray parameterItems = new JArray();
|
---|
24 | JArray resultItems = new JArray();
|
---|
25 | IList<IJsonItem> jsonItems = new List<IJsonItem>();
|
---|
26 | string fullPath = Path.GetFullPath(path);
|
---|
27 | #endregion
|
---|
28 |
|
---|
29 | if(optimizer.ExecutionState == ExecutionState.Paused)
|
---|
30 | optimizer.Stop();
|
---|
31 |
|
---|
32 | // recursively filter items with values/ranges/actualNames
|
---|
33 | PopulateJsonItems(rootItem, jsonItems);
|
---|
34 |
|
---|
35 | #region Serialize HL File
|
---|
36 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
37 | string hlFilePath = Path.Combine(fullPath, templateName + ".hl");
|
---|
38 | serializer.Serialize(optimizer, hlFilePath);
|
---|
39 | #endregion
|
---|
40 |
|
---|
41 | #region Filter Items
|
---|
42 | foreach (var item in jsonItems) {
|
---|
43 | if (item is IResultJsonItem)
|
---|
44 | resultItems.Add(item.GenerateJObject());
|
---|
45 | else
|
---|
46 | parameterItems.Add(item.GenerateJObject());
|
---|
47 | }
|
---|
48 | #endregion
|
---|
49 |
|
---|
50 | #region Set Template Data
|
---|
51 | template[Constants.Metadata][Constants.TemplateName] = templateName;
|
---|
52 | template[Constants.Metadata][Constants.HLFileLocation] = hlFilePath;
|
---|
53 | template[Constants.Parameters] = parameterItems;
|
---|
54 | template[Constants.Results] = resultItems;
|
---|
55 | #endregion
|
---|
56 |
|
---|
57 | #region Serialize and write to file
|
---|
58 | File.WriteAllText(fullPath + @"\" + templateName + ".json", SingleLineArrayJsonWriter.Serialize(template));
|
---|
59 | #endregion
|
---|
60 | }
|
---|
61 |
|
---|
62 | #region Helper
|
---|
63 | private static void PopulateJsonItems(IJsonItem item, IList<IJsonItem> jsonItems) {
|
---|
64 | foreach(var x in item) {
|
---|
65 | if (x.Active && !(x is EmptyJsonItem)) {
|
---|
66 | jsonItems.Add(x);
|
---|
67 | }
|
---|
68 | }
|
---|
69 | }
|
---|
70 | #endregion
|
---|
71 | }
|
---|
72 | }
|
---|