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

Last change on this file since 17394 was 17394, checked in by dpiringe, 8 weeks ago

#3026:

  • deleted: ConvertableAttribute, DummyConverter, ObjectExtensions
  • renamed: CustomJsonWriter -> SingleLineArrayJsonWriter, JCInstantiator -> JsonTemplateInstantiator
  • added: JsonItemConverterFactory, UnsupportedJsonItem
  • IJsonItemConverter:
    • added two new properties: Priority and ConvertableType -> because converters are automatically collected by plugin infrastructure now
    • Extract, Inject references a root converter now -> typically an instance of JsonItemConverter -> to prevent cycles
  • JsonItemConverter:
    • now implements the interface IJsonItemConverter
    • is now a dynamic class
    • is only instantiable with an factory (JsonItemConverterFactory)
    • still has the old (but now public) static methods Extract and Inject (without ref param IJsonItemConverter root) -> creates instance with factory and calls methods of instance
    • removed register and unregister methods, because the factory collects all converters automatically now (on first call of Create)
    • has cycle detection for Extract and Inject
    • renamed method Get to GetConverter
File size: 3.0 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;
11
12namespace HeuristicLab.JsonInterface {
13  /// <summary>
14  /// Static class to generate json interface templates.
15  /// </summary>
16  public static class JCGenerator {
17    private struct GenData {
18      public JObject Template { get; set; }
19      public JArray JsonItems { get; set; }
20    }
21   
22    public static string GenerateTemplate(IAlgorithm algorithm) {
23      // data container
24      GenData genData = new GenData() {
25        Template = JObject.Parse(Constants.Template),
26        JsonItems = new JArray()
27      };
28
29      ProtoBufSerializer serializer = new ProtoBufSerializer();
30      serializer.Serialize(algorithm, @"C:\Workspace\template.hl");
31      genData.Template[Constants.Metadata][Constants.HLFileLocation] = @"C:\Workspace\template.hl";
32
33      // extract JsonItem, save the name in the metadata section of the
34      // template and save it an JArray incl. all parameters of the JsonItem,
35      // which have parameters aswell
36      AddInstantiableIItem(Constants.Algorithm, algorithm, genData);
37      if (algorithm.Problem != null) // only when an problem exists
38        AddInstantiableIItem(Constants.Problem, algorithm.Problem, genData);
39
40      // save the JArray with JsonItems (= IParameterizedItems)
41      genData.Template[Constants.Parameters] = genData.JsonItems;
42      // serialize template and return string
43      return SingleLineArrayJsonWriter.Serialize(genData.Template);
44    }
45   
46    #region Helper
47
48    private static void AddInstantiableIItem(string metaDataTagName, IItem item, GenData genData) {
49      JsonItem jsonItem = JsonItemConverter.Extract(item);
50     
51      genData.Template[Constants.Metadata][metaDataTagName] = item.ItemName;
52      PopulateJsonItems(jsonItem, genData);
53    }
54
55    // serializes ParameterizedItems and saves them in list "JsonItems".
56    private static void PopulateJsonItems(JsonItem item, GenData genData) {
57      IEnumerable<JsonItem> tmpParameter = item.Children;
58
59      if (item.Value != null || item.Range != null) {
60        genData.JsonItems.Add(Serialize(item));
61      }
62
63      if (tmpParameter != null) {
64        foreach (var p in tmpParameter) {
65          PopulateJsonItems(p, genData);
66        }
67      }
68    }
69
70    private static JObject Serialize(JsonItem item) => 
71      JObject.FromObject(item, Settings());
72
73    /// <summary>
74    /// Settings for the json serialization.
75    /// </summary>
76    /// <returns>A configured JsonSerializer.</returns>
77    private static JsonSerializer Settings() => new JsonSerializer() {
78      TypeNameHandling = TypeNameHandling.None,
79      NullValueHandling = NullValueHandling.Ignore,
80      ReferenceLoopHandling = ReferenceLoopHandling.Serialize
81    };
82    #endregion
83  }
84}
Note: See TracBrowser for help on using the repository browser.