source: branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/JsonItemConverter.cs

Last change on this file was 17843, checked in by dpiringe, 5 days ago

#3026

  • removed property ConvertableType from all converters
  • removed the option to fixate or loosen the path of JsonItems (obsolete)
  • added a abstract formatter SymbolicRegressionSolutionFormatterBase as base formatter for ISymbolicRegressionSolution
  • unified the construction of exporter controls
  • code cleanup
File size: 3.1 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using HeuristicLab.Core;
5namespace HeuristicLab.JsonInterface {
6  /// <summary>
7  /// Class for handling json converters.
8  /// </summary>
9  public class JsonItemConverter : IJsonItemConverter {
10
11    #region Properties
12    private IEnumerable<IJsonItemConverter> Converters { get; set; }
13      = Enumerable.Empty<IJsonItemConverter>();
14
15    private IDictionary<int, IJsonItem> InjectCache { get; set; }
16      = new Dictionary<int, IJsonItem>();
17
18    private IDictionary<int, IJsonItem> ExtractCache { get; set; }
19      = new Dictionary<int, IJsonItem>();
20
21    public int Priority => throw new NotImplementedException();
22
23    public Type ConvertableType => throw new NotImplementedException();
24
25    public bool CanConvertType(Type t) => throw new NotImplementedException();
26    #endregion
27
28    /// <summary>
29    /// GetConverter a converter for a specific type.
30    /// </summary>
31    /// <param name="type">The type for which the converter will be selected.</param>
32    /// <returns>An IJsonItemConverter object.</returns>
33    public IJsonItemConverter GetConverter(Type type) {
34      IList<IJsonItemConverter> possibleConverters = new List<IJsonItemConverter>();
35     
36      foreach (var x in Converters) {
37        if (x.CanConvertType(type))
38          possibleConverters.Add(x);
39      }
40
41      if(possibleConverters.Count > 0) {
42        IJsonItemConverter best = possibleConverters.First();
43        foreach (var x in possibleConverters) {
44          if (x.Priority > best.Priority)
45            best = x;
46        }
47        return best;
48      }
49      return null;
50    }
51   
52    public void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
53      if (item != null && !InjectCache.ContainsKey(item.GetHashCode())) {
54        IJsonItemConverter converter = GetConverter(item.GetType());
55        if(converter != null) converter.Inject(item, data, root);
56        InjectCache.Add(item.GetHashCode(), data);
57      }
58    }
59
60    public IJsonItem Extract(IItem item, IJsonItemConverter root) {
61      int hash = item.GetHashCode();
62      if (ExtractCache.TryGetValue(hash, out IJsonItem val))
63        return val;
64      else {
65        IJsonItemConverter converter = GetConverter(item.GetType());
66        if (converter == null)
67          return new UnsupportedJsonItem() { Name = $"{item.ItemName} (unsupported)" };
68        IJsonItem tmp = converter.Extract(item, root);
69        ExtractCache.Add(hash, tmp);
70        return tmp;
71      }
72    }
73   
74    public static void Inject(IItem item, IJsonItem data) {
75      IJsonItemConverter c = JsonItemConverterFactory.Create();
76      c.Inject(item, data, c);
77    }
78
79    public static IJsonItem Extract(IItem item) {
80      IJsonItemConverter c = JsonItemConverterFactory.Create();
81      return c.Extract(item, c);
82    }
83
84    /// <summary>
85    /// Static constructor for default converter configuration.
86    /// </summary>
87    internal JsonItemConverter(IEnumerable<IJsonItemConverter> converters) {
88      Converters = converters;
89    }
90  }
91}
Note: See TracBrowser for help on using the repository browser.