[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 |
|
---|
[17828] | 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();
|
---|
[17349] | 23 |
|
---|
[17828] | 24 | // validation
|
---|
| 25 | ValidationResult validationResult = rootItem.GetValidator().Validate();
|
---|
| 26 | if (!validationResult.Success)
|
---|
| 27 | throw validationResult.GenerateException();
|
---|
[17374] | 28 |
|
---|
[17477] | 29 | #region Init
|
---|
[17435] | 30 | JObject template = JObject.Parse(Constants.Template);
|
---|
| 31 | JArray parameterItems = new JArray();
|
---|
| 32 | JArray resultItems = new JArray();
|
---|
| 33 | IList<IJsonItem> jsonItems = new List<IJsonItem>();
|
---|
[17828] | 34 | string templateName = Path.GetFileName(templatePath);
|
---|
| 35 | string templateDirectory = Path.GetDirectoryName(templatePath);
|
---|
| 36 |
|
---|
[17477] | 37 | #endregion
|
---|
| 38 |
|
---|
[17689] | 39 | if(optimizer.ExecutionState == ExecutionState.Paused)
|
---|
| 40 | optimizer.Stop();
|
---|
| 41 |
|
---|
[17435] | 42 | // recursively filter items with values/ranges/actualNames
|
---|
| 43 | PopulateJsonItems(rootItem, jsonItems);
|
---|
[17410] | 44 |
|
---|
[17477] | 45 | #region Serialize HL File
|
---|
[17404] | 46 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
[17828] | 47 | string hlFilePath = Path.Combine(templateDirectory, $"{templateName}.hl");
|
---|
[17435] | 48 | serializer.Serialize(optimizer, hlFilePath);
|
---|
[17477] | 49 | #endregion
|
---|
[17404] | 50 |
|
---|
[17477] | 51 | #region Filter Items
|
---|
[17435] | 52 | foreach (var item in jsonItems) {
|
---|
[17471] | 53 | if (item is IResultJsonItem)
|
---|
[17477] | 54 | resultItems.Add(item.GenerateJObject());
|
---|
[17405] | 55 | else
|
---|
[17477] | 56 | parameterItems.Add(item.GenerateJObject());
|
---|
[17404] | 57 | }
|
---|
[17477] | 58 | #endregion
|
---|
| 59 |
|
---|
| 60 | #region Set Template Data
|
---|
[17435] | 61 | template[Constants.Metadata][Constants.TemplateName] = templateName;
|
---|
| 62 | template[Constants.Metadata][Constants.HLFileLocation] = hlFilePath;
|
---|
| 63 | template[Constants.Parameters] = parameterItems;
|
---|
[17439] | 64 | template[Constants.Results] = resultItems;
|
---|
[17477] | 65 | #endregion
|
---|
[17404] | 66 |
|
---|
[17477] | 67 | #region Serialize and write to file
|
---|
[17828] | 68 | File.WriteAllText(Path.Combine(templateDirectory, $"{templateName}.json"), SingleLineArrayJsonWriter.Serialize(template));
|
---|
[17477] | 69 | #endregion
|
---|
[17404] | 70 | }
|
---|
| 71 |
|
---|
[17435] | 72 | #region Helper
|
---|
[17477] | 73 | private static void PopulateJsonItems(IJsonItem item, IList<IJsonItem> jsonItems) {
|
---|
| 74 | foreach(var x in item) {
|
---|
[17843] | 75 | if (x.Active && !(x is EmptyJsonItem) && !(x is UnsupportedJsonItem)) {
|
---|
[17477] | 76 | jsonItems.Add(x);
|
---|
[17374] | 77 | }
|
---|
| 78 | }
|
---|
[17280] | 79 | }
|
---|
[17263] | 80 | #endregion
|
---|
| 81 | }
|
---|
| 82 | }
|
---|