source: branches/3026_IntegrationIntoSymSpace/HeuristicLab.Manufacture/Transformer.cs @ 17280

Last change on this file since 17280 was 17280, checked in by dpiringe, 3 years ago

#3026

  • renamed CustomWriter to CustomJsonWriter and extracted it into a separate file
  • removed property ParameterizedItems from Component
  • added helper methods for path generation in Component
  • reverted the single parameter array idea back to the FreeParameters and StaticParameters idea
  • now there hidden parameters are also handled
File size: 3.7 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.Manufacture {
10  public static class Transformer {
11    private static IDictionary<Type, ITypeTransformer> transformers = new Dictionary<Type, ITypeTransformer>();
12   
13    public static void Register(Type type, ITypeTransformer transformer) { // TODO: explizit
14      if (!transformers.ContainsKey(type))
15        transformers.Add(type, transformer);
16    }
17
18
19    public static void Register<T>(ITypeTransformer transformer) => Register(typeof(T), transformer);
20
21    public static IEnumerable<ITypeTransformer> Get(Type type) {
22      IList<KeyValuePair<Type, ITypeTransformer>> possibleTransformers = new List<KeyValuePair<Type, ITypeTransformer>>();
23
24
25      foreach (var x in transformers) {
26        if (type.IsEqualTo(x.Key))
27          possibleTransformers.Add(x);
28      }
29
30
31      if(possibleTransformers.Count > 0) {
32        ITypeTransformer 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 ITypeTransformer[] { nearest };
42        /*
43        return possibleTransformers.OrderBy(x => {
44          return type.GetInterfaceDistance(x.Key);
45        }).Select(x => x.Value);
46        */
47      }
48      return new ITypeTransformer[] { new DummyTransformer() };
49    }
50
51    internal static void Inject(IItem item, Component data) {
52      IEnumerable<ITypeTransformer> arr = Get(item.GetType());
53      foreach (var transformer in arr)
54        transformer.Inject(item, data);
55    }
56
57    internal static Component Extract(IItem item) {
58      IEnumerable<ITypeTransformer> 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    }
64
65
66    static Transformer() {
67      Register<IntValue>(new ValueTypeValueTransformer<IntValue, int>());
68      Register<DoubleValue>(new ValueTypeValueTransformer<DoubleValue, double>());
69      Register<PercentValue>(new ValueTypeValueTransformer<PercentValue, double>());
70      Register<BoolValue>(new ValueTypeValueTransformer<BoolValue, bool>());
71      Register<DateTimeValue>(new ValueTypeValueTransformer<DateTimeValue, DateTime>());
72      Register<StringValue>(new StringValueTransformer());
73
74      Register<IntArray>(new ValueTypeArrayTransformer<IntArray, int>());
75      Register<DoubleArray>(new ValueTypeArrayTransformer<DoubleArray, double>());
76      Register<PercentArray>(new ValueTypeArrayTransformer<PercentArray, double>());
77      Register<BoolArray>(new ValueTypeArrayTransformer<BoolArray, bool>());
78
79      Register<IntMatrix>(new ValueTypeMatrixTransformer<IntMatrix, int>());
80      Register<DoubleMatrix>(new ValueTypeMatrixTransformer<DoubleMatrix, double>());
81      Register<PercentMatrix>(new ValueTypeMatrixTransformer<PercentMatrix, double>());
82      Register<BoolMatrix>(new ValueTypeMatrixTransformer<BoolMatrix, bool>());
83
84      Register(typeof(IConstrainedValueParameter<>), new ConstrainedValueParameterTransformer());
85      Register(typeof(ILookupParameter), new LookupParameterTransformer());
86      Register(typeof(IValueParameter), new ValueParameterTransformer());
87      Register(typeof(IParameterizedItem), new ParameterizedItemTransformer());
88      Register(typeof(ICheckedMultiOperator<>), new MultiCheckedOperatorTransformer());
89    }
90
91  }
92}
Note: See TracBrowser for help on using the repository browser.