[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;
|
---|
[18046] | 7 | using System.Linq;
|
---|
[17263] | 8 |
|
---|
[17284] | 9 | namespace HeuristicLab.JsonInterface {
|
---|
[17353] | 10 | /// <summary>
|
---|
[17404] | 11 | /// Class to generate json interface templates.
|
---|
[17353] | 12 | /// </summary>
|
---|
[17600] | 13 | public class JsonTemplateGenerator {
|
---|
[17404] | 14 |
|
---|
[17828] | 15 | /// <summary>
|
---|
| 16 | /// static Function to generate a template.
|
---|
| 17 | /// </summary>
|
---|
| 18 | /// <param name="templatePath">the path for the template files</param>
|
---|
| 19 | /// <param name="optimizer">the optimizer object to serialize</param>
|
---|
| 20 | /// <param name="rootItem">Root JsonItem for serialization, considers only active JsonItems for serialization</param>
|
---|
[18050] | 21 | public static void GenerateTemplate(string templatePath, IOptimizer optimizer, IJsonItem rootItem, IEnumerable<IResultCollectionProcessor> resultCollectionProcessors) {
|
---|
[17828] | 22 | // clear all runs
|
---|
[18050] | 23 | if (optimizer.ExecutionState == ExecutionState.Paused)
|
---|
| 24 | optimizer.Stop();
|
---|
[17828] | 25 | optimizer.Runs.Clear();
|
---|
[17349] | 26 |
|
---|
[17828] | 27 | // validation
|
---|
| 28 | ValidationResult validationResult = rootItem.GetValidator().Validate();
|
---|
| 29 | if (!validationResult.Success)
|
---|
| 30 | throw validationResult.GenerateException();
|
---|
[17374] | 31 |
|
---|
[17477] | 32 | #region Init
|
---|
[17435] | 33 | JObject template = JObject.Parse(Constants.Template);
|
---|
| 34 | JArray parameterItems = new JArray();
|
---|
| 35 | JArray resultItems = new JArray();
|
---|
[18050] | 36 | JArray resultCollectionProcessorItems = new JArray();
|
---|
[17828] | 37 | string templateName = Path.GetFileName(templatePath);
|
---|
| 38 | string templateDirectory = Path.GetDirectoryName(templatePath);
|
---|
[17477] | 39 | #endregion
|
---|
| 40 |
|
---|
[18050] | 41 | // filter items with values/ranges/actualNames
|
---|
| 42 | var jsonItems = rootItem.Where(x => x.Active && !(x is EmptyJsonItem) && !(x is UnsupportedJsonItem));
|
---|
[17689] | 43 |
|
---|
[17477] | 44 | #region Serialize HL File
|
---|
[17404] | 45 | ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
[18030] | 46 | // get absolute path for serialization
|
---|
[17828] | 47 | string hlFilePath = Path.Combine(templateDirectory, $"{templateName}.hl");
|
---|
[17435] | 48 | serializer.Serialize(optimizer, hlFilePath);
|
---|
[18030] | 49 | // overwrite string for relative path
|
---|
[18040] | 50 | hlFilePath = Path.Combine($".", $"{templateName}.hl");
|
---|
[17477] | 51 | #endregion
|
---|
[17404] | 52 |
|
---|
[17477] | 53 | #region Filter Items
|
---|
[17435] | 54 | foreach (var item in jsonItems) {
|
---|
[17471] | 55 | if (item is IResultJsonItem)
|
---|
[17477] | 56 | resultItems.Add(item.GenerateJObject());
|
---|
[17405] | 57 | else
|
---|
[17477] | 58 | parameterItems.Add(item.GenerateJObject());
|
---|
[17404] | 59 | }
|
---|
[17477] | 60 | #endregion
|
---|
| 61 |
|
---|
[18050] | 62 | #region ResultCollectionProcessor Serialization
|
---|
| 63 | foreach (var proc in resultCollectionProcessors) {
|
---|
| 64 | JArray rcpParameterItems = new JArray();
|
---|
| 65 | var guid = StorableTypeAttribute.GetStorableTypeAttribute(proc.GetType()).Guid.ToString();
|
---|
| 66 | var item = JsonItemConverter.Extract(proc);
|
---|
| 67 |
|
---|
| 68 | var rcpItems = item
|
---|
| 69 | .Where(x => !(x is EmptyJsonItem) && !(x is UnsupportedJsonItem))
|
---|
| 70 | .Select(x => x.GenerateJObject());
|
---|
| 71 |
|
---|
| 72 | foreach (var i in rcpItems)
|
---|
| 73 | rcpParameterItems.Add(i);
|
---|
| 74 |
|
---|
| 75 | JObject processorObj = new JObject();
|
---|
| 76 | processorObj.Add(nameof(IJsonItem.Name), item.Name);
|
---|
| 77 | processorObj.Add("GUID", guid);
|
---|
| 78 | processorObj.Add(Constants.Parameters, rcpParameterItems);
|
---|
| 79 | resultCollectionProcessorItems.Add(processorObj);
|
---|
| 80 | }
|
---|
| 81 | #endregion
|
---|
| 82 |
|
---|
[17477] | 83 | #region Set Template Data
|
---|
[17435] | 84 | template[Constants.Metadata][Constants.TemplateName] = templateName;
|
---|
| 85 | template[Constants.Metadata][Constants.HLFileLocation] = hlFilePath;
|
---|
| 86 | template[Constants.Parameters] = parameterItems;
|
---|
[17439] | 87 | template[Constants.Results] = resultItems;
|
---|
[18050] | 88 | template[Constants.ResultCollectionProcessorItems] = resultCollectionProcessorItems;
|
---|
[17477] | 89 | #endregion
|
---|
[17404] | 90 |
|
---|
[17477] | 91 | #region Serialize and write to file
|
---|
[17828] | 92 | File.WriteAllText(Path.Combine(templateDirectory, $"{templateName}.json"), SingleLineArrayJsonWriter.Serialize(template));
|
---|
[17477] | 93 | #endregion
|
---|
[17404] | 94 | }
|
---|
[17263] | 95 | }
|
---|
| 96 | }
|
---|