[17263] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Linq;
|
---|
[17353] | 4 | using HeuristicLab.Common;
|
---|
[17263] | 5 | using HeuristicLab.Core;
|
---|
| 6 | using HeuristicLab.Data;
|
---|
| 7 | using HeuristicLab.Optimization;
|
---|
| 8 | using Newtonsoft.Json;
|
---|
| 9 | using Newtonsoft.Json.Linq;
|
---|
[17374] | 10 | using HEAL.Attic;
|
---|
[17263] | 11 |
|
---|
[17284] | 12 | namespace 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 | }
|
---|