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

Last change on this file since 17353 was 17353, checked in by dpiringe, 12 months ago

#3026:

  • relocated GetMaxValue and GetMinValue from ValueTypeValueConverter into BaseConverter
  • fixed a bug in ConstrainedValueParameterConverter (from GetType().Name to ToString())
  • printing now PrettyNames for types
  • added comments
  • added StorableConverter.cs (not finished, maybe not a good converter)
  • added ValueRangeConverter.cs for DoubleRange and IntRange
  • added ParameterConverter.cs for default parameter conversion
File size: 4.9 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
6using HeuristicLab.Core;
7using HeuristicLab.Data;
8
9namespace HeuristicLab.JsonInterface {
10  /// <summary>
11  /// Static class for handling json converters.
12  /// </summary>
13  public static class JsonItemConverter {
14
15    private struct ConverterPriorityContainer {
16      public IJsonItemConverter Converter { get; set; }
17      public int Priority { get; set; }
18    }
19
20    private static IDictionary<Type, ConverterPriorityContainer> Converters { get; set; }
21      = new Dictionary<Type, ConverterPriorityContainer>();
22   
23    /// <summary>
24    /// Register a converter for a given type and priority.
25    /// </summary>
26    /// <param name="type">The type for which the converter will be selected.</param>
27    /// <param name="converter">The implemented converter.</param>
28    /// <param name="priority">The priority for the converter selection (when multiple converter match for a given type). Higher is better.</param>
29    public static void Register(Type type, IJsonItemConverter converter, int priority) {
30      if (!Converters.ContainsKey(type))
31        Converters.Add(type, new ConverterPriorityContainer() { Converter = converter, Priority = priority });
32    }
33
34    public static void Register<T>(IJsonItemConverter converter, int priority) =>
35      Register(typeof(T), converter, priority);
36
37    /// <summary>
38    /// Deregister a converter (same object has to be already registered).
39    /// </summary>
40    /// <param name="converter">Converter to deregister.</param>
41    public static void Deregister(IJsonItemConverter converter) {
42      var types =
43        Converters
44        .Where(x => x.Value.Converter.GetHashCode() == converter.GetHashCode())
45        .Select(x => x.Key);
46      foreach (var x in types)
47        Converters.Remove(x);
48    }
49
50    /// <summary>
51    /// Get a converter for a specific type.
52    /// </summary>
53    /// <param name="type">The type for which the converter will be selected.</param>
54    /// <returns>An IJsonItemConverter object.</returns>
55    public static IJsonItemConverter Get(Type type) {
56      IList<ConverterPriorityContainer> possibleConverters = new List<ConverterPriorityContainer>();
57     
58      foreach (var x in Converters)
59        if (type.IsEqualTo(x.Key))
60          possibleConverters.Add(x.Value);
61
62      if(possibleConverters.Count > 0) {
63        ConverterPriorityContainer best = possibleConverters.First();
64        foreach (var x in possibleConverters) {
65          if (x.Priority > best.Priority)
66            best = x;
67        }
68        return best.Converter;
69      }
70      return new DummyConverter();
71    }
72
73    internal static void Inject(IItem item, JsonItem data) =>
74      Get(item.GetType()).Inject(item, data);
75
76    internal static JsonItem Extract(IItem item) =>
77      Get(item.GetType()).Extract(item);
78
79
80    /// <summary>
81    /// Static constructor for default converter configuration.
82    /// </summary>
83    static JsonItemConverter() {
84      Register<IntValue>(new ValueTypeValueConverter<IntValue, int>(), 1);
85      Register<DoubleValue>(new ValueTypeValueConverter<DoubleValue, double>(), 1);
86      Register<PercentValue>(new ValueTypeValueConverter<PercentValue, double>(), 2);
87      Register<BoolValue>(new ValueTypeValueConverter<BoolValue, bool>(), 1);
88      Register<DateTimeValue>(new ValueTypeValueConverter<DateTimeValue, DateTime>(), 1);
89      Register<StringValue>(new StringValueConverter(), 1);
90
91      Register<IntArray>(new ValueTypeArrayConverter<IntArray, int>(), 1);
92      Register<DoubleArray>(new ValueTypeArrayConverter<DoubleArray, double>(), 1);
93      Register<PercentArray>(new ValueTypeArrayConverter<PercentArray, double>(), 2);
94      Register<BoolArray>(new ValueTypeArrayConverter<BoolArray, bool>(), 1);
95
96      Register<IntMatrix>(new ValueTypeMatrixConverter<IntMatrix, int>(), 1);
97      Register<DoubleMatrix>(new ValueTypeMatrixConverter<DoubleMatrix, double>(), 1);
98      Register<PercentMatrix>(new ValueTypeMatrixConverter<PercentMatrix, double>(), 2);
99      Register<BoolMatrix>(new ValueTypeMatrixConverter<BoolMatrix, bool>(), 1);
100
101      Register<DoubleRange>(new DoubleRangeConverter(), 1);
102      Register<IntRange>(new IntRangeConverter(), 1);
103
104      Register(typeof(EnumValue<>), new EnumTypeConverter(), 1);
105
106      Register<IParameter>(new ParameterConverter(), 1);
107
108      Register<IValueParameter>(new ValueParameterConverter(), 2);
109      Register<IParameterizedItem>(new ParameterizedItemConverter(), 2);
110      Register<ILookupParameter>(new LookupParameterConverter(), 3);
111      Register<IValueLookupParameter>(new ValueLookupParameterConverter(), 4);
112
113      Register(typeof(IConstrainedValueParameter<>), new ConstrainedValueParameterConverter(), 3);
114      Register(typeof(ICheckedMultiOperator<>), new MultiCheckedOperatorConverter(), 3);
115    }
116  }
117}
Note: See TracBrowser for help on using the repository browser.