Changeset 17282
- Timestamp:
- 10/01/19 14:29:59 (5 years ago)
- Location:
- branches/3026_IntegrationIntoSymSpace/HeuristicLab.Manufacture
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/3026_IntegrationIntoSymSpace/HeuristicLab.Manufacture/Extensions/TypeExtensions.cs
r17269 r17282 8 8 namespace HeuristicLab.Manufacture { 9 9 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 }26 10 27 11 public static bool IsEqualTo(this Type type, Type other) { -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.Manufacture/HeuristicLab.Manufacture.csproj
r17280 r17282 56 56 <Compile Include="JCGenerator.cs" /> 57 57 <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" /> 71 71 <Compile Include="Program.cs" /> 72 72 <Compile Include="Properties\AssemblyInfo.cs" /> 73 73 <Compile Include="Component.cs" /> 74 <Compile Include="Interfaces\I TypeTransformer.cs" />75 <Compile Include=" Transformer.cs" />74 <Compile Include="Interfaces\IJsonItemConverter.cs" /> 75 <Compile Include="JsonItemConverter.cs" /> 76 76 </ItemGroup> 77 77 <ItemGroup> … … 225 225 </ProjectReference> 226 226 </ItemGroup> 227 <ItemGroup /> 227 228 <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> 228 229 </Project> -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.Manufacture/JsonItemConverter.cs
r17281 r17282 9 9 namespace HeuristicLab.Manufacture { 10 10 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>(); 12 18 13 public static void Register(Type type, IJsonItemConverter transformer) { // TODO: explizit19 public static void Register(Type type, IJsonItemConverter converter, int priority) { 14 20 if (!transformers.ContainsKey(type)) 15 transformers.Add(type, transformer);21 transformers.Add(type, new ConverterPriorityContainer() { Converter = converter, Priority = priority }); 16 22 } 17 23 18 24 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); 20 26 21 public static I Enumerable<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>(); 23 29 30 foreach (var x in transformers) 31 if (type.IsEqualTo(x.Key)) 32 possibleConverters.Add(x.Value); 24 33 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; 28 41 } 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(); 49 43 } 50 44 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); 56 47 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); 64 50 65 51 66 52 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); 73 59 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); 78 64 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); 83 69 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); 90 76 } 91 77 -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.Manufacture/Program.cs
r17280 r17282 35 35 36 36 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)); 38 38 39 39 JCInstantiator configurator = new JCInstantiator();
Note: See TracChangeset
for help on using the changeset viewer.