Free cookie consent management tool by TermsFeed Policy Generator

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

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

#3026:

  • set read-only to false for name input (in JsonItemBaseControl)
  • result items now can be selected/deselected in export dialog
  • removed metadata.problem property and renamed metadata.optimizer property to TemplateName in json template
  • fixed bug with wrong description for result items in AlgorithmConverter
  • refactored a lot of code in JCGenerator and added the need for a directory path (location for the template files)
  • fixed a null reference bug in JsonItemValidator (cannot try to iterate through possible null referenced collections)
File size: 3.2 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using HeuristicLab.Common;
5using HeuristicLab.Core;
6using HeuristicLab.Data;
7using HeuristicLab.Optimization;
8using Newtonsoft.Json;
9using Newtonsoft.Json.Linq;
10using HEAL.Attic;
11using System.IO;
12
13namespace HeuristicLab.JsonInterface {
14  /// <summary>
15  /// Class to generate json interface templates.
16  /// </summary>
17  public class JCGenerator {
18
19    public void GenerateTemplate(string path, IOptimizer optimizer) =>
20      GenerateTemplate(path, optimizer.Name, optimizer);
21
22    public void GenerateTemplate(string path, string templateName, IOptimizer optimizer) =>
23      GenerateTemplate(path, templateName, optimizer, JsonItemConverter.Extract(optimizer));
24
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>();
31      string fullPath = Path.GetDirectoryName(Path.GetFullPath(path));
32     
33      // recursively filter items with values/ranges/actualNames
34      PopulateJsonItems(rootItem, jsonItems);
35
36      // serialize hl file
37      ProtoBufSerializer serializer = new ProtoBufSerializer();
38      string hlFilePath = fullPath + @"\" + templateName + ".hl";
39      serializer.Serialize(optimizer, hlFilePath);
40
41      // filter result items
42      foreach (var item in jsonItems) {
43        if (item is ResultItem)
44          resultItems.Add(Serialize(item));
45        else
46          parameterItems.Add(Serialize(item));
47      }
48     
49      // set template data
50      template[Constants.Metadata][Constants.TemplateName] = templateName;
51      template[Constants.Metadata][Constants.HLFileLocation] = hlFilePath;
52      template[Constants.Parameters] = parameterItems;
53      template[Constants.ActivatedResults] = resultItems;
54
55      // serialize template and write file
56      File.WriteAllText(fullPath + @"\" + templateName + ".json", SingleLineArrayJsonWriter.Serialize(template));
57    }
58
59    #region Helper   
60    // serializes ParameterizedItems and saves them in list "JsonItems".
61    private void PopulateJsonItems(IJsonItem item, IList<IJsonItem> jsonItems) {
62      IEnumerable<IJsonItem> tmpParameter = item.Children;
63     
64      if (item.Value != null || item.Range != null || item is ResultItem || item.ActualName != null) {
65        jsonItems.Add(item);
66      }
67
68      if (tmpParameter != null) {
69        foreach (var p in tmpParameter) {
70          PopulateJsonItems(p, jsonItems);
71        }
72      }
73    }
74
75    private static JObject Serialize(IJsonItem item) =>
76      JObject.FromObject(item, Settings());
77
78    /// <summary>
79    /// Settings for the json serialization.
80    /// </summary>
81    /// <returns>A configured JsonSerializer.</returns>
82    private static JsonSerializer Settings() => new JsonSerializer() {
83      TypeNameHandling = TypeNameHandling.None,
84      NullValueHandling = NullValueHandling.Ignore,
85      ReferenceLoopHandling = ReferenceLoopHandling.Serialize
86    };
87    #endregion
88  }
89}
Note: See TracBrowser for help on using the repository browser.