1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using System.Threading.Tasks;
|
---|
6 | using HeuristicLab.Core;
|
---|
7 | using HeuristicLab.Data;
|
---|
8 |
|
---|
9 | namespace HeuristicLab.JsonInterface {
|
---|
10 | public static class JsonItemConverter {
|
---|
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>();
|
---|
18 |
|
---|
19 | public static void Register(Type type, IJsonItemConverter converter, int priority) {
|
---|
20 | if (!transformers.ContainsKey(type))
|
---|
21 | transformers.Add(type, new ConverterPriorityContainer() { Converter = converter, Priority = priority });
|
---|
22 | }
|
---|
23 |
|
---|
24 | public static void Register<T>(IJsonItemConverter converter, int priority) => Register(typeof(T), converter, priority);
|
---|
25 |
|
---|
26 | public static IJsonItemConverter Get(Type type) {
|
---|
27 | IList<ConverterPriorityContainer> possibleConverters = new List<ConverterPriorityContainer>();
|
---|
28 |
|
---|
29 | foreach (var x in transformers)
|
---|
30 | if (type.IsEqualTo(x.Key))
|
---|
31 | possibleConverters.Add(x.Value);
|
---|
32 |
|
---|
33 | if(possibleConverters.Count > 0) {
|
---|
34 | ConverterPriorityContainer best = possibleConverters.First();
|
---|
35 | foreach (var x in possibleConverters) {
|
---|
36 | if (x.Priority > best.Priority)
|
---|
37 | best = x;
|
---|
38 | }
|
---|
39 | return best.Converter;
|
---|
40 | }
|
---|
41 | return new DummyConverter();
|
---|
42 | }
|
---|
43 |
|
---|
44 | internal static void Inject(IItem item, JsonItem data) =>
|
---|
45 | Get(item.GetType()).Inject(item, data);
|
---|
46 |
|
---|
47 | internal static JsonItem Extract(IItem item) =>
|
---|
48 | Get(item.GetType()).Extract(item);
|
---|
49 |
|
---|
50 |
|
---|
51 | static JsonItemConverter() {
|
---|
52 | Register<IntValue>(new ValueTypeValueConverter<IntValue, int>(), 1);
|
---|
53 | Register<DoubleValue>(new ValueTypeValueConverter<DoubleValue, double>(), 1);
|
---|
54 | Register<PercentValue>(new ValueTypeValueConverter<PercentValue, double>(), 1);
|
---|
55 | Register<BoolValue>(new ValueTypeValueConverter<BoolValue, bool>(), 1);
|
---|
56 | Register<DateTimeValue>(new ValueTypeValueConverter<DateTimeValue, DateTime>(), 1);
|
---|
57 | Register<StringValue>(new StringValueConverter(), 1);
|
---|
58 |
|
---|
59 | Register<IntArray>(new ValueTypeArrayConverter<IntArray, int>(), 1);
|
---|
60 | Register<DoubleArray>(new ValueTypeArrayConverter<DoubleArray, double>(), 1);
|
---|
61 | Register<PercentArray>(new ValueTypeArrayConverter<PercentArray, double>(), 1);
|
---|
62 | Register<BoolArray>(new ValueTypeArrayConverter<BoolArray, bool>(), 1);
|
---|
63 |
|
---|
64 | Register<IntMatrix>(new ValueTypeMatrixConverter<IntMatrix, int>(), 1);
|
---|
65 | Register<DoubleMatrix>(new ValueTypeMatrixConverter<DoubleMatrix, double>(), 1);
|
---|
66 | Register<PercentMatrix>(new ValueTypeMatrixConverter<PercentMatrix, double>(), 1);
|
---|
67 | Register<BoolMatrix>(new ValueTypeMatrixConverter<BoolMatrix, bool>(), 1);
|
---|
68 |
|
---|
69 | Register(typeof(IConstrainedValueParameter<>), new ConstrainedValueParameterConverter(), 1);
|
---|
70 | Register(typeof(ILookupParameter), new LookupParameterConverter(), 1);
|
---|
71 | Register(typeof(IValueParameter), new ValueParameterConverter(), 1);
|
---|
72 | Register(typeof(IParameterizedItem), new ParameterizedItemConverter(), 1);
|
---|
73 | Register(typeof(ICheckedMultiOperator<>), new MultiCheckedOperatorConverter(), 2);
|
---|
74 | Register(typeof(EnumValue<>), new EnumTypeConverter(), 1);
|
---|
75 | }
|
---|
76 |
|
---|
77 | }
|
---|
78 | }
|
---|