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 | /// <summary>
|
---|
15 | /// static Function to generate a template.
|
---|
16 | /// </summary>
|
---|
17 | /// <param name="templatePath">the path for the template files</param>
|
---|
18 | /// <param name="optimizer">the optimizer object to serialize</param>
|
---|
19 | /// <param name="rootItem">Root JsonItem for serialization, considers only active JsonItems for serialization</param>
|
---|
20 | public static void GenerateTemplate(string templatePath, IOptimizer optimizer, IJsonItem rootItem) {
|
---|
21 | // clear all runs
|
---|
22 | optimizer.Runs.Clear();
|
---|
23 |
|
---|
24 | // validation
|
---|
25 | ValidationResult validationResult = rootItem.GetValidator().Validate();
|
---|
26 | if (!validationResult.Success)
|
---|
27 | throw validationResult.GenerateException();
|
---|
28 |
|
---|
29 | #region Init
|
---|
30 | JObject template = JObject.Parse(Constants.Template);
|
---|
31 | JArray parameterItems = new JArray();
|
---|
32 | JArray resultItems = new JArray();
|
---|
33 | IList<IJsonItem> jsonItems = new List<IJsonItem>();
|
---|
34 | string templateName = Path.GetFileName(templatePath);
|
---|
35 | string templateDirectory = Path.GetDirectoryName(templatePath);
|
---|
36 |
|
---|
37 | #endregion
|
---|
38 |
|
---|
39 | if(optimizer.ExecutionState == ExecutionState.Paused)
|
---|
40 | optimizer.Stop();
|
---|
41 |
|
---|
42 | // recursively filter items with values/ranges/actualNames
|
---|
43 | PopulateJsonItems(rootItem, jsonItems);
|
---|
44 |
|
---|
45 | #region Serialize HL File
|
---|
46 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
47 | string hlFilePath = Path.Combine(templateDirectory, $"{templateName}.hl");
|
---|
48 | serializer.Serialize(optimizer, hlFilePath);
|
---|
49 | #endregion
|
---|
50 |
|
---|
51 | #region Filter Items
|
---|
52 | foreach (var item in jsonItems) {
|
---|
53 | if (item is IResultJsonItem)
|
---|
54 | resultItems.Add(item.GenerateJObject());
|
---|
55 | else
|
---|
56 | parameterItems.Add(item.GenerateJObject());
|
---|
57 | }
|
---|
58 | #endregion
|
---|
59 |
|
---|
60 | #region Set Template Data
|
---|
61 | template[Constants.Metadata][Constants.TemplateName] = templateName;
|
---|
62 | template[Constants.Metadata][Constants.HLFileLocation] = hlFilePath;
|
---|
63 | template[Constants.Parameters] = parameterItems;
|
---|
64 | template[Constants.Results] = resultItems;
|
---|
65 | #endregion
|
---|
66 |
|
---|
67 | #region Serialize and write to file
|
---|
68 | File.WriteAllText(Path.Combine(templateDirectory, $"{templateName}.json"), SingleLineArrayJsonWriter.Serialize(template));
|
---|
69 | #endregion
|
---|
70 | }
|
---|
71 |
|
---|
72 | #region Helper
|
---|
73 | private static void PopulateJsonItems(IJsonItem item, IList<IJsonItem> jsonItems) {
|
---|
74 | foreach(var x in item) {
|
---|
75 | if (x.Active && !(x is EmptyJsonItem) && !(x is UnsupportedJsonItem)) {
|
---|
76 | jsonItems.Add(x);
|
---|
77 | }
|
---|
78 | }
|
---|
79 | }
|
---|
80 | #endregion
|
---|
81 | }
|
---|
82 | }
|
---|