Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/JsonTemplateGenerator.cs @ 17828

Last change on this file since 17828 was 17828, checked in by dpiringe, 3 years ago

#3026

  • removed the option to set the value for JsonItems via exporter
    • reworked some base controls
    • added new controls for JsonItem specific properties (e.g. ArrayResizable)
    • deleted a lot of obsolet controls
  • removed the Enable checkbox in the detail view of JsonItems
  • exporter now clones the IOptimizer object
  • added a check + message for unsupported exports
  • list of JsonItems now includes unsupported JsonItems (disabled and marked with 'unsupported')
  • refactored the converter type check
    • now every converter has to specify its supported type(s)
File size: 2.9 KB
Line 
1using System.Collections.Generic;
2using HeuristicLab.Optimization;
3using Newtonsoft.Json.Linq;
4using HEAL.Attic;
5using System.IO;
6using HeuristicLab.Core;
7
8namespace 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)) {
76          jsonItems.Add(x);
77        }
78      }
79    }
80    #endregion
81  }
82}
Note: See TracBrowser for help on using the repository browser.