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