Ignore:
Timestamp:
10/01/19 14:29:59 (19 months ago)
Author:
dpiringe
Message:

#3026: changed the converter selection from an automated mechanism (with interface distances) to an explicit mechanism (with priorities)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.Manufacture/JsonItemConverter.cs

    r17281 r17282  
    99namespace HeuristicLab.Manufacture {
    1010  public static class JsonItemConverter {
    11     private static IDictionary<Type, IJsonItemConverter> transformers = new Dictionary<Type, IJsonItemConverter>();
     11
     12    private struct ConverterPriorityContainer {
     13      public IJsonItemConverter Converter { get; set; }
     14      public int Priority { get; set; }
     15    }
     16
     17    private static IDictionary<Type, ConverterPriorityContainer> transformers = new Dictionary<Type, ConverterPriorityContainer>();
    1218   
    13     public static void Register(Type type, IJsonItemConverter transformer) { // TODO: explizit
     19    public static void Register(Type type, IJsonItemConverter converter, int priority) {
    1420      if (!transformers.ContainsKey(type))
    15         transformers.Add(type, transformer);
     21        transformers.Add(type, new ConverterPriorityContainer() { Converter = converter, Priority = priority });
    1622    }
    1723
    1824
    19     public static void Register<T>(IJsonItemConverter transformer) => Register(typeof(T), transformer);
     25    public static void Register<T>(IJsonItemConverter converter, int priority) => Register(typeof(T), converter, priority);
    2026
    21     public static IEnumerable<IJsonItemConverter> Get(Type type) {
    22       IList<KeyValuePair<Type, IJsonItemConverter>> possibleTransformers = new List<KeyValuePair<Type, IJsonItemConverter>>();
     27    public static IJsonItemConverter Get(Type type) {
     28      IList<ConverterPriorityContainer> possibleConverters = new List<ConverterPriorityContainer>();
    2329
     30      foreach (var x in transformers)
     31        if (type.IsEqualTo(x.Key))
     32          possibleConverters.Add(x.Value);
    2433
    25       foreach (var x in transformers) {
    26         if (type.IsEqualTo(x.Key))
    27           possibleTransformers.Add(x);
     34      if(possibleConverters.Count > 0) {
     35        ConverterPriorityContainer best = possibleConverters.First();
     36        foreach (var x in possibleConverters) {
     37          if (x.Priority > best.Priority)
     38            best = x;
     39        }
     40        return best.Converter;
    2841      }
    29 
    30 
    31       if(possibleTransformers.Count > 0) {
    32         IJsonItemConverter nearest = possibleTransformers.First().Value;
    33         int nearestDistance = -1;
    34         foreach (var x in possibleTransformers) {
    35           int d = type.GetInterfaceDistance(x.Key);
    36           if (d != -1 && (nearestDistance == -1 || d < nearestDistance)) {
    37             nearestDistance = d;
    38             nearest = x.Value;
    39           }
    40         }
    41         return new IJsonItemConverter[] { nearest };
    42         /*
    43         return possibleTransformers.OrderBy(x => {
    44           return type.GetInterfaceDistance(x.Key);
    45         }).Select(x => x.Value);
    46         */
    47       }
    48       return new IJsonItemConverter[] { new DummyConverter() };
     42      return new DummyConverter();
    4943    }
    5044
    51     internal static void Inject(IItem item, Component data) {
    52       IEnumerable<IJsonItemConverter> arr = Get(item.GetType());
    53       foreach (var transformer in arr)
    54         transformer.Inject(item, data);
    55     }
     45    internal static void Inject(IItem item, Component data) =>
     46      Get(item.GetType()).Inject(item, data);
    5647
    57     internal static Component Extract(IItem item) {
    58       IEnumerable<IJsonItemConverter> arr = Get(item.GetType());
    59       Component data = new Component();
    60       foreach (var transformer in arr)
    61         Component.Merge(data, transformer.Extract(item));
    62       return data;
    63     }
     48    internal static Component Extract(IItem item) =>
     49      Get(item.GetType()).Extract(item);
    6450
    6551
    6652    static JsonItemConverter() {
    67       Register<IntValue>(new ValueTypeValueConverter<IntValue, int>());
    68       Register<DoubleValue>(new ValueTypeValueConverter<DoubleValue, double>());
    69       Register<PercentValue>(new ValueTypeValueConverter<PercentValue, double>());
    70       Register<BoolValue>(new ValueTypeValueConverter<BoolValue, bool>());
    71       Register<DateTimeValue>(new ValueTypeValueConverter<DateTimeValue, DateTime>());
    72       Register<StringValue>(new StringValueConverter());
     53      Register<IntValue>(new ValueTypeValueConverter<IntValue, int>(), 1);
     54      Register<DoubleValue>(new ValueTypeValueConverter<DoubleValue, double>(), 1);
     55      Register<PercentValue>(new ValueTypeValueConverter<PercentValue, double>(), 1);
     56      Register<BoolValue>(new ValueTypeValueConverter<BoolValue, bool>(), 1);
     57      Register<DateTimeValue>(new ValueTypeValueConverter<DateTimeValue, DateTime>(), 1);
     58      Register<StringValue>(new StringValueConverter(), 1);
    7359
    74       Register<IntArray>(new ValueTypeArrayConverter<IntArray, int>());
    75       Register<DoubleArray>(new ValueTypeArrayConverter<DoubleArray, double>());
    76       Register<PercentArray>(new ValueTypeArrayConverter<PercentArray, double>());
    77       Register<BoolArray>(new ValueTypeArrayConverter<BoolArray, bool>());
     60      Register<IntArray>(new ValueTypeArrayConverter<IntArray, int>(), 1);
     61      Register<DoubleArray>(new ValueTypeArrayConverter<DoubleArray, double>(), 1);
     62      Register<PercentArray>(new ValueTypeArrayConverter<PercentArray, double>(), 1);
     63      Register<BoolArray>(new ValueTypeArrayConverter<BoolArray, bool>(), 1);
    7864
    79       Register<IntMatrix>(new ValueTypeMatrixConverter<IntMatrix, int>());
    80       Register<DoubleMatrix>(new ValueTypeMatrixConverter<DoubleMatrix, double>());
    81       Register<PercentMatrix>(new ValueTypeMatrixConverter<PercentMatrix, double>());
    82       Register<BoolMatrix>(new ValueTypeMatrixConverter<BoolMatrix, bool>());
     65      Register<IntMatrix>(new ValueTypeMatrixConverter<IntMatrix, int>(), 1);
     66      Register<DoubleMatrix>(new ValueTypeMatrixConverter<DoubleMatrix, double>(), 1);
     67      Register<PercentMatrix>(new ValueTypeMatrixConverter<PercentMatrix, double>(), 1);
     68      Register<BoolMatrix>(new ValueTypeMatrixConverter<BoolMatrix, bool>(), 1);
    8369
    84       Register(typeof(IConstrainedValueParameter<>), new ConstrainedValueParameterConverter());
    85       Register(typeof(ILookupParameter), new LookupParameterConverter());
    86       Register(typeof(IValueParameter), new ValueParameterConverter());
    87       Register(typeof(IParameterizedItem), new ParameterizedItemConverter());
    88       Register(typeof(ICheckedMultiOperator<>), new MultiCheckedOperatorConverter());
    89       Register(typeof(EnumValue<>), new EnumTypeConverter())
     70      Register(typeof(IConstrainedValueParameter<>), new ConstrainedValueParameterConverter(), 1);
     71      Register(typeof(ILookupParameter), new LookupParameterConverter(), 1);
     72      Register(typeof(IValueParameter), new ValueParameterConverter(), 1);
     73      Register(typeof(IParameterizedItem), new ParameterizedItemConverter(), 1);
     74      Register(typeof(ICheckedMultiOperator<>), new MultiCheckedOperatorConverter(), 2);
     75      Register(typeof(EnumValue<>), new EnumTypeConverter(), 1);
    9076    }
    9177
Note: See TracChangeset for help on using the changeset viewer.