Free cookie consent management tool by TermsFeed Policy Generator

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

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

#3026:

  • fixed a bug in HeuristicLab.JsonInterface.App -> now the Runner checks if the instantiated optimizer is an EngineAlgorithm, if true: Engine = SequentialEngine (engine can be configured in later versions)
  • added a TabControl in ExportJsonDialog for parameters and results
  • updated parameter tree view with checkboxes (and linked them with VM)
  • renamed ActivatedResults to Results for templates
  • fixed a bug with injection of operators in MultiCheckedOperatorConverter -> now operators of an ValueParameter get set correctly
  • updated MultiCheckedOperatorConverter to extract/inject parameters of operators
  • fixed bug with path for template -> removed usage of method Path.GetDirectoryName, returned wrong results
  • splitted cache for JsonItemConverter into a cache for injection and extraction
  • JsonTemplateInstantiator now uses first item in objects array instead of searching for an object with template name
File size: 3.1 KB
RevLine 
[17263]1using System;
2using System.Collections.Generic;
3using System.Linq;
[17353]4using HeuristicLab.Common;
[17263]5using HeuristicLab.Core;
6using HeuristicLab.Data;
7using HeuristicLab.Optimization;
8using Newtonsoft.Json;
9using Newtonsoft.Json.Linq;
[17374]10using HEAL.Attic;
[17435]11using System.IO;
[17263]12
[17284]13namespace HeuristicLab.JsonInterface {
[17353]14  /// <summary>
[17404]15  /// Class to generate json interface templates.
[17353]16  /// </summary>
[17404]17  public class JCGenerator {
18
[17435]19    public void GenerateTemplate(string path, IOptimizer optimizer) =>
20      GenerateTemplate(path, optimizer.Name, optimizer);
[17349]21
[17435]22    public void GenerateTemplate(string path, string templateName, IOptimizer optimizer) =>
23      GenerateTemplate(path, templateName, optimizer, JsonItemConverter.Extract(optimizer));
[17374]24
[17435]25    public void GenerateTemplate(string path, string templateName, IOptimizer optimizer, IJsonItem rootItem) {
26      // init
27      JObject template = JObject.Parse(Constants.Template);
28      JArray parameterItems = new JArray();
29      JArray resultItems = new JArray();
30      IList<IJsonItem> jsonItems = new List<IJsonItem>();
[17439]31      string fullPath = Path.GetFullPath(path);
[17435]32     
33      // recursively filter items with values/ranges/actualNames
34      PopulateJsonItems(rootItem, jsonItems);
[17410]35
[17435]36      // serialize hl file
[17404]37      ProtoBufSerializer serializer = new ProtoBufSerializer();
[17435]38      string hlFilePath = fullPath + @"\" + templateName + ".hl";
39      serializer.Serialize(optimizer, hlFilePath);
[17404]40
[17435]41      // filter result items
42      foreach (var item in jsonItems) {
[17405]43        if (item is ResultItem)
[17435]44          resultItems.Add(Serialize(item));
[17405]45        else
[17435]46          parameterItems.Add(Serialize(item));
[17404]47      }
[17435]48     
49      // set template data
50      template[Constants.Metadata][Constants.TemplateName] = templateName;
51      template[Constants.Metadata][Constants.HLFileLocation] = hlFilePath;
52      template[Constants.Parameters] = parameterItems;
[17439]53      template[Constants.Results] = resultItems;
[17404]54
[17435]55      // serialize template and write file
56      File.WriteAllText(fullPath + @"\" + templateName + ".json", SingleLineArrayJsonWriter.Serialize(template));
[17404]57    }
58
[17435]59    #region Helper   
[17349]60    // serializes ParameterizedItems and saves them in list "JsonItems".
[17435]61    private void PopulateJsonItems(IJsonItem item, IList<IJsonItem> jsonItems) {
[17406]62      IEnumerable<IJsonItem> tmpParameter = item.Children;
[17405]63     
64      if (item.Value != null || item.Range != null || item is ResultItem || item.ActualName != null) {
[17435]65        jsonItems.Add(item);
[17379]66      }
67
68      if (tmpParameter != null) {
69        foreach (var p in tmpParameter) {
[17435]70          PopulateJsonItems(p, jsonItems);
[17374]71        }
72      }
[17280]73    }
74
[17406]75    private static JObject Serialize(IJsonItem item) =>
[17379]76      JObject.FromObject(item, Settings());
[17280]77
[17349]78    /// <summary>
79    /// Settings for the json serialization.
80    /// </summary>
81    /// <returns>A configured JsonSerializer.</returns>
82    private static JsonSerializer Settings() => new JsonSerializer() {
[17263]83      TypeNameHandling = TypeNameHandling.None,
[17280]84      NullValueHandling = NullValueHandling.Ignore,
85      ReferenceLoopHandling = ReferenceLoopHandling.Serialize
[17263]86    };
87    #endregion
88  }
89}
Note: See TracBrowser for help on using the repository browser.