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

Last change on this file since 17843 was 17843, checked in by dpiringe, 3 months 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
RevLine 
[17263]1using System;
2using System.Collections.Generic;
3using System.Linq;
4using HeuristicLab.Core;
[17284]5namespace HeuristicLab.JsonInterface {
[17353]6  /// <summary>
[17394]7  /// Class for handling json converters.
[17353]8  /// </summary>
[17394]9  public class JsonItemConverter : IJsonItemConverter {
[17843]10
[17394]11    #region Properties
[17843]12    private IEnumerable<IJsonItemConverter> Converters { get; set; }
13      = Enumerable.Empty<IJsonItemConverter>();
[17263]14
[17439]15    private IDictionary<int, IJsonItem> InjectCache { get; set; }
[17406]16      = new Dictionary<int, IJsonItem>();
[17371]17
[17439]18    private IDictionary<int, IJsonItem> ExtractCache { get; set; }
19      = new Dictionary<int, IJsonItem>();
20
[17394]21    public int Priority => throw new NotImplementedException();
[17371]22
[17394]23    public Type ConvertableType => throw new NotImplementedException();
[17828]24
25    public bool CanConvertType(Type t) => throw new NotImplementedException();
[17394]26    #endregion
[17263]27
[17353]28    /// <summary>
[17394]29    /// GetConverter a converter for a specific type.
[17353]30    /// </summary>
31    /// <param name="type">The type for which the converter will be selected.</param>
32    /// <returns>An IJsonItemConverter object.</returns>
[17394]33    public IJsonItemConverter GetConverter(Type type) {
34      IList<IJsonItemConverter> possibleConverters = new List<IJsonItemConverter>();
[17353]35     
[17828]36      foreach (var x in Converters) {
[17843]37        if (x.CanConvertType(type))
38          possibleConverters.Add(x);
[17828]39      }
[17266]40
[17282]41      if(possibleConverters.Count > 0) {
[17394]42        IJsonItemConverter best = possibleConverters.First();
[17282]43        foreach (var x in possibleConverters) {
44          if (x.Priority > best.Priority)
45            best = x;
[17266]46        }
[17394]47        return best;
[17266]48      }
[17394]49      return null;
[17263]50    }
[17394]51   
[17406]52    public void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
[17439]53      if (item != null && !InjectCache.ContainsKey(item.GetHashCode())) {
[17394]54        IJsonItemConverter converter = GetConverter(item.GetType());
55        if(converter != null) converter.Inject(item, data, root);
[17439]56        InjectCache.Add(item.GetHashCode(), data);
[17394]57      }
58    }
[17263]59
[17406]60    public IJsonItem Extract(IItem item, IJsonItemConverter root) {
[17394]61      int hash = item.GetHashCode();
[17439]62      if (ExtractCache.TryGetValue(hash, out IJsonItem val))
[17394]63        return val;
64      else {
65        IJsonItemConverter converter = GetConverter(item.GetType());
[17828]66        if (converter == null)
67          return new UnsupportedJsonItem() { Name = $"{item.ItemName} (unsupported)" };
[17540]68        IJsonItem tmp = converter.Extract(item, root);
[17439]69        ExtractCache.Add(hash, tmp);
[17394]70        return tmp;
71      }
72    }
73   
[17406]74    public static void Inject(IItem item, IJsonItem data) {
[17394]75      IJsonItemConverter c = JsonItemConverterFactory.Create();
76      c.Inject(item, data, c);
77    }
[17263]78
[17406]79    public static IJsonItem Extract(IItem item) {
[17394]80      IJsonItemConverter c = JsonItemConverterFactory.Create();
81      return c.Extract(item, c);
82    }
[17263]83
[17353]84    /// <summary>
85    /// Static constructor for default converter configuration.
86    /// </summary>
[17843]87    internal JsonItemConverter(IEnumerable<IJsonItemConverter> converters) {
[17394]88      Converters = converters;
[17263]89    }
90  }
91}
Note: See TracBrowser for help on using the repository browser.