Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/JCGenerator.cs @ 17519

Last change on this file since 17519 was 17519, checked in by dpiringe, 4 years ago

#3026:

  • added error output for failed runner initialization
  • reorganised some final view models
  • TargetedJsonItemType (in JsonItemVMBase) now automatically returns the type of the defined JsonItem
  • code cleanup
  • refactored RegressionProblemDataConverter
  • added lots of comments
  • added new view for StringArrayJsonItem
  • added new UI component for concrete restricted items and used it in JsonItemConcreteItemArrayControl and JsonItemValidValuesControl
File size: 2.4 KB
RevLine 
[17519]1using System.Collections.Generic;
[17263]2using HeuristicLab.Optimization;
3using Newtonsoft.Json.Linq;
[17374]4using HEAL.Attic;
[17435]5using System.IO;
[17263]6
[17284]7namespace 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}
Note: See TracBrowser for help on using the repository browser.