Free cookie consent management tool by TermsFeed Policy Generator

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

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

#3026:

  • moved from usage of IAlgorithm to IOptimizer (in JCGenerator and JsonTemplateInstantiator)
  • added new converter: AlgorithmConverter
File size: 2.8 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;
[17263]11
[17284]12namespace HeuristicLab.JsonInterface {
[17353]13  /// <summary>
14  /// Static class to generate json interface templates.
15  /// </summary>
[17349]16  public static class JCGenerator {
17    private struct GenData {
18      public JObject Template { get; set; }
19      public JArray JsonItems { get; set; }
20    }
[17353]21   
[17395]22    public static string GenerateTemplate(IOptimizer optimizer) {
[17349]23      // data container
24      GenData genData = new GenData() {
25        Template = JObject.Parse(Constants.Template),
26        JsonItems = new JArray()
27      };
28
[17374]29      ProtoBufSerializer serializer = new ProtoBufSerializer();
[17395]30      serializer.Serialize(optimizer, @"C:\Workspace\template.hl");
[17379]31      genData.Template[Constants.Metadata][Constants.HLFileLocation] = @"C:\Workspace\template.hl";
[17374]32
[17379]33      // extract JsonItem, save the name in the metadata section of the
[17330]34      // template and save it an JArray incl. all parameters of the JsonItem,
35      // which have parameters aswell
[17395]36      AddInstantiableIItem(Constants.Optimizer, optimizer, genData);
[17330]37
[17379]38      // save the JArray with JsonItems (= IParameterizedItems)
39      genData.Template[Constants.Parameters] = genData.JsonItems;
40      // serialize template and return string
[17394]41      return SingleLineArrayJsonWriter.Serialize(genData.Template);
[17330]42    }
[17349]43   
[17330]44    #region Helper
[17353]45
[17349]46    private static void AddInstantiableIItem(string metaDataTagName, IItem item, GenData genData) {
[17330]47      JsonItem jsonItem = JsonItemConverter.Extract(item);
[17394]48     
[17349]49      genData.Template[Constants.Metadata][metaDataTagName] = item.ItemName;
50      PopulateJsonItems(jsonItem, genData);
[17330]51    }
52
[17349]53    // serializes ParameterizedItems and saves them in list "JsonItems".
54    private static void PopulateJsonItems(JsonItem item, GenData genData) {
[17379]55      IEnumerable<JsonItem> tmpParameter = item.Children;
56
57      if (item.Value != null || item.Range != null) {
58        genData.JsonItems.Add(Serialize(item));
59      }
60
61      if (tmpParameter != null) {
62        foreach (var p in tmpParameter) {
[17374]63          PopulateJsonItems(p, genData);
64        }
65      }
[17280]66    }
67
[17379]68    private static JObject Serialize(JsonItem item) =>
69      JObject.FromObject(item, Settings());
[17280]70
[17349]71    /// <summary>
72    /// Settings for the json serialization.
73    /// </summary>
74    /// <returns>A configured JsonSerializer.</returns>
75    private static JsonSerializer Settings() => new JsonSerializer() {
[17263]76      TypeNameHandling = TypeNameHandling.None,
[17280]77      NullValueHandling = NullValueHandling.Ignore,
78      ReferenceLoopHandling = ReferenceLoopHandling.Serialize
[17263]79    };
80    #endregion
81  }
82}
Note: See TracBrowser for help on using the repository browser.