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