Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17540 was 17540, checked in by dpiringe, 4 years ago

#3026:

  • added new converter: BatchRunConverter, ExperimentConverter
  • fixed a bug with the path of the .hl file in JCGenerator
  • fixed a bug with a redundant search for a suitable converter in JsonItemConverter
  • added a restriction for TargetVariable and AllowedInputVariables in RegressionProblemDataConverter
  • removed unnecessary code in Runner
File size: 3.4 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using HeuristicLab.Core;
5namespace HeuristicLab.JsonInterface {
6  /// <summary>
7  /// Class for handling json converters.
8  /// </summary>
9  public class JsonItemConverter : IJsonItemConverter {
10   
11    #region Properties
12    private IDictionary<Type, IJsonItemConverter> Converters { get; set; }
13      = new Dictionary<Type, IJsonItemConverter>();
14
15    private IDictionary<int, IJsonItem> InjectCache { get; set; }
16      = new Dictionary<int, IJsonItem>();
17
18    private IDictionary<int, IJsonItem> ExtractCache { get; set; }
19      = new Dictionary<int, IJsonItem>();
20
21    public int Priority => throw new NotImplementedException();
22
23    public Type ConvertableType => throw new NotImplementedException();
24    #endregion
25
26    /// <summary>
27    /// GetConverter a converter for a specific type.
28    /// </summary>
29    /// <param name="type">The type for which the converter will be selected.</param>
30    /// <returns>An IJsonItemConverter object.</returns>
31    public IJsonItemConverter GetConverter(Type type) {
32      IList<IJsonItemConverter> possibleConverters = new List<IJsonItemConverter>();
33     
34      foreach (var x in Converters)
35        if (CompareTypes(type, x.Key))
36          possibleConverters.Add(x.Value);
37
38      if(possibleConverters.Count > 0) {
39        IJsonItemConverter best = possibleConverters.First();
40        foreach (var x in possibleConverters) {
41          if (x.Priority > best.Priority)
42            best = x;
43        }
44        return best;
45      }
46      return null;
47    }
48   
49    public void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
50      if (item != null && !InjectCache.ContainsKey(item.GetHashCode())) {
51        IJsonItemConverter converter = GetConverter(item.GetType());
52        if(converter != null) converter.Inject(item, data, root);
53        InjectCache.Add(item.GetHashCode(), data);
54      }
55    }
56
57    public IJsonItem Extract(IItem item, IJsonItemConverter root) {
58      int hash = item.GetHashCode();
59      if (ExtractCache.TryGetValue(hash, out IJsonItem val))
60        return val;
61      else {
62        IJsonItemConverter converter = GetConverter(item.GetType());
63        if (converter == null) return new UnsupportedJsonItem();
64        IJsonItem tmp = converter.Extract(item, root);
65        ExtractCache.Add(hash, tmp);
66        return tmp;
67      }
68    }
69   
70    public static void Inject(IItem item, IJsonItem data) {
71      IJsonItemConverter c = JsonItemConverterFactory.Create();
72      c.Inject(item, data, c);
73    }
74
75    public static IJsonItem Extract(IItem item) {
76      IJsonItemConverter c = JsonItemConverterFactory.Create();
77      return c.Extract(item, c);
78    }
79
80    /// <summary>
81    /// Static constructor for default converter configuration.
82    /// </summary>
83    internal JsonItemConverter(IDictionary<Type, IJsonItemConverter> converters) {
84      Converters = converters;
85    }
86
87    private bool CompareGenericTypes(Type t1, Type t2) =>
88      (t1.IsGenericType && t1.GetGenericTypeDefinition() == t2) /*||
89      (t2.IsGenericType && t2.GetGenericTypeDefinition() == t1)*/;
90
91    private bool CompareTypes(Type t1, Type t2) =>
92      t1 == t2 || /*t1.IsAssignableFrom(t2) ||*/
93      t1.GetInterfaces().Any(
94        i => i == t2 || CompareGenericTypes(i, t2)
95      ) ||
96      CompareGenericTypes(t1, t2);
97  }
98}
Note: See TracBrowser for help on using the repository browser.