Free cookie consent management tool by TermsFeed Policy Generator

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

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

#3026:

  • added a check to stop a paused IOptimizer
File size: 2.5 KB
RevLine 
[17519]1using System.Collections.Generic;
[17263]2using HeuristicLab.Optimization;
3using Newtonsoft.Json.Linq;
[17374]4using HEAL.Attic;
[17435]5using System.IO;
[17689]6using HeuristicLab.Core;
[17263]7
[17284]8namespace HeuristicLab.JsonInterface {
[17353]9  /// <summary>
[17404]10  /// Class to generate json interface templates.
[17353]11  /// </summary>
[17600]12  public class JsonTemplateGenerator {
[17404]13
[17477]14    public static void GenerateTemplate(string path, IOptimizer optimizer) =>
[17435]15      GenerateTemplate(path, optimizer.Name, optimizer);
[17349]16
[17477]17    public static void GenerateTemplate(string path, string templateName, IOptimizer optimizer) =>
[17435]18      GenerateTemplate(path, templateName, optimizer, JsonItemConverter.Extract(optimizer));
[17374]19
[17477]20    public static void GenerateTemplate(string path, string templateName, IOptimizer optimizer, IJsonItem rootItem) {
21      #region Init
[17435]22      JObject template = JObject.Parse(Constants.Template);
23      JArray parameterItems = new JArray();
24      JArray resultItems = new JArray();
25      IList<IJsonItem> jsonItems = new List<IJsonItem>();
[17439]26      string fullPath = Path.GetFullPath(path);
[17477]27      #endregion
28
[17689]29      if(optimizer.ExecutionState == ExecutionState.Paused)
30        optimizer.Stop();
31
[17435]32      // recursively filter items with values/ranges/actualNames
33      PopulateJsonItems(rootItem, jsonItems);
[17410]34
[17477]35      #region Serialize HL File
[17404]36      ProtoBufSerializer serializer = new ProtoBufSerializer();
[17540]37      string hlFilePath = Path.Combine(fullPath, templateName + ".hl");
[17435]38      serializer.Serialize(optimizer, hlFilePath);
[17477]39      #endregion
[17404]40
[17477]41      #region Filter Items
[17435]42      foreach (var item in jsonItems) {
[17471]43        if (item is IResultJsonItem)
[17477]44          resultItems.Add(item.GenerateJObject());
[17405]45        else
[17477]46          parameterItems.Add(item.GenerateJObject());
[17404]47      }
[17477]48      #endregion
49
50      #region Set Template Data
[17435]51      template[Constants.Metadata][Constants.TemplateName] = templateName;
52      template[Constants.Metadata][Constants.HLFileLocation] = hlFilePath;
53      template[Constants.Parameters] = parameterItems;
[17439]54      template[Constants.Results] = resultItems;
[17477]55      #endregion
[17404]56
[17477]57      #region Serialize and write to file
[17435]58      File.WriteAllText(fullPath + @"\" + templateName + ".json", SingleLineArrayJsonWriter.Serialize(template));
[17477]59      #endregion
[17404]60    }
61
[17435]62    #region Helper   
[17477]63    private static void PopulateJsonItems(IJsonItem item, IList<IJsonItem> jsonItems) {
64      foreach(var x in item) {
65        if (x.Active && !(x is EmptyJsonItem)) {
66          jsonItems.Add(x);
[17374]67        }
68      }
[17280]69    }
[17263]70    #endregion
71  }
72}
Note: See TracBrowser for help on using the repository browser.