Changeset 17282


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

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

Location:
branches/3026_IntegrationIntoSymSpace/HeuristicLab.Manufacture
Files:
4 edited

Legend:

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

    r17269 r17282  
    88namespace HeuristicLab.Manufacture {
    99  public static class TypeExtensions {
    10     public static int GetInterfaceDistance(this Type type, Type interfaceType) {
    11       if (!interfaceType.IsInterface) return -1;
    12       int distance = int.MaxValue;
    13       Type baseType = type;
    14       while (baseType != typeof(object)) {
    15         var interfaces = baseType.GetInterfaces();
    16         var minimalInterfaces = interfaces.Except(interfaces.SelectMany(i => i.GetInterfaces()));
    17         if (minimalInterfaces.Any(i => {
    18           if (i.IsGenericType)
    19             return i.GetGenericTypeDefinition() == interfaceType;
    20           return i == interfaceType;
    21         })) --distance;
    22         baseType = baseType.BaseType;
    23       }
    24       return distance;
    25     }
    2610
    2711    public static bool IsEqualTo(this Type type, Type other) {
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.Manufacture/HeuristicLab.Manufacture.csproj

    r17280 r17282  
    5656    <Compile Include="JCGenerator.cs" />
    5757    <Compile Include="JCInstantiator.cs" />
    58     <Compile Include="ParameterTransformer\ConstrainedValueParameterTransformer.cs" />
    59     <Compile Include="ParameterTransformer\ParameterBaseTransformer.cs" />
    60     <Compile Include="ParameterTransformer\ParameterizedItemTransformer.cs" />
    61     <Compile Include="ParameterTransformer\ValueParameterTransformer.cs" />
    62     <Compile Include="TypeTransformer\EnumTypeTransformer.cs" />
    63     <Compile Include="TypeTransformer\DummyTransformer.cs" />
    64     <Compile Include="ParameterTransformer\LookupParameterTransformer.cs" />
    65     <Compile Include="TypeTransformer\MultiCheckedOperatorTransformer.cs" />
    66     <Compile Include="TypeTransformer\StringValueTransformer.cs" />
    67     <Compile Include="TypeTransformer\ValueTypeArrayTransformer.cs" />
    68     <Compile Include="TypeTransformer\BaseTransformer.cs" />
    69     <Compile Include="TypeTransformer\ValueTypeMatrixTransformer.cs" />
    70     <Compile Include="TypeTransformer\ValueTypeValueTransformer.cs" />
     58    <Compile Include="Converters\ConstrainedValueParameterConverter.cs" />
     59    <Compile Include="Converters\ParameterBaseConverter.cs" />
     60    <Compile Include="Converters\ParameterizedItemConverter.cs" />
     61    <Compile Include="Converters\ValueParameterConverter.cs" />
     62    <Compile Include="Converters\EnumTypeConverter.cs" />
     63    <Compile Include="Converters\DummyConverter.cs" />
     64    <Compile Include="Converters\LookupParameterConverter.cs" />
     65    <Compile Include="Converters\MultiCheckedOperatorConverter.cs" />
     66    <Compile Include="Converters\StringValueConverter.cs" />
     67    <Compile Include="Converters\ValueTypeArrayConverter.cs" />
     68    <Compile Include="Converters\BaseConverter.cs" />
     69    <Compile Include="Converters\ValueTypeMatrixConverter.cs" />
     70    <Compile Include="Converters\ValueTypeValueConverter.cs" />
    7171    <Compile Include="Program.cs" />
    7272    <Compile Include="Properties\AssemblyInfo.cs" />
    7373    <Compile Include="Component.cs" />
    74     <Compile Include="Interfaces\ITypeTransformer.cs" />
    75     <Compile Include="Transformer.cs" />
     74    <Compile Include="Interfaces\IJsonItemConverter.cs" />
     75    <Compile Include="JsonItemConverter.cs" />
    7676  </ItemGroup>
    7777  <ItemGroup>
     
    225225    </ProjectReference>
    226226  </ItemGroup>
     227  <ItemGroup />
    227228  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
    228229</Project>
  • 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
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.Manufacture/Program.cs

    r17280 r17282  
    3535
    3636      JCGenerator gen = new JCGenerator();
    37       //File.WriteAllText(@"C:\Workspace\TemplateProto11.json", gen.GenerateTemplate(alg, tsp));
     37      File.WriteAllText(@"C:\Workspace\TemplateProto11.json", gen.GenerateTemplate(alg, tsp));
    3838     
    3939      JCInstantiator configurator = new JCInstantiator();
Note: See TracChangeset for help on using the changeset viewer.