1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 |
|
---|
28 | namespace HeuristicLab.Problems.ExternalEvaluation {
|
---|
29 | public static class SolutionMessageBuildRegistry {
|
---|
30 | private static readonly Dictionary<Type, Action<string, IItem, SolutionMessage.Builder>> dispatcher = new Dictionary<Type, Action<string, IItem, SolutionMessage.Builder>>();
|
---|
31 |
|
---|
32 | static SolutionMessageBuildRegistry() {
|
---|
33 | dispatcher.Add(typeof(ValueTypeValue<int>), (name, item, builder) => DefaultSolutionMessageBuilder.AddAsIntegerVar(name, (ValueTypeValue<int>)item, builder));
|
---|
34 | dispatcher.Add(typeof(ValueTypeArray<int>), (name, item, builder) => DefaultSolutionMessageBuilder.AddAsIntegerArrayVar(name, (ValueTypeArray<int>)item, builder));
|
---|
35 | dispatcher.Add(typeof(ValueTypeValue<double>), (name, item, builder) => DefaultSolutionMessageBuilder.AddAsDoubleVar(name, (ValueTypeValue<double>)item, builder));
|
---|
36 | dispatcher.Add(typeof(ValueTypeArray<double>), (name, item, builder) => DefaultSolutionMessageBuilder.AddAsDoubleArrayVar(name, (ValueTypeArray<double>)item, builder));
|
---|
37 | dispatcher.Add(typeof(ValueTypeValue<bool>), (name, item, builder) => DefaultSolutionMessageBuilder.AddAsBoolVar(name, (ValueTypeValue<bool>)item, builder));
|
---|
38 | dispatcher.Add(typeof(ValueTypeArray<bool>), (name, item, builder) => DefaultSolutionMessageBuilder.AddAsBoolArrayVar(name, (ValueTypeArray<bool>)item, builder));
|
---|
39 | dispatcher.Add(typeof(IStringConvertibleValue), (name, item, builder) => DefaultSolutionMessageBuilder.AddAsStringVar(name, (IStringConvertibleValue)item, builder));
|
---|
40 | }
|
---|
41 |
|
---|
42 | internal static void AddToSolutionMessage(this IItem item, string name, SolutionMessage.Builder builder) {
|
---|
43 | Type type = item.GetType();
|
---|
44 | while (!dispatcher.ContainsKey(type)) {
|
---|
45 | if (type.BaseType != null) type = type.BaseType;
|
---|
46 | else break;
|
---|
47 | }
|
---|
48 | if (type.BaseType == null && !dispatcher.ContainsKey(type)) {
|
---|
49 | IEnumerable<Type> interfaces = item.GetType().GetInterfaces().Where(x => dispatcher.ContainsKey(x));
|
---|
50 | if (interfaces.Count() != 1) throw new ArgumentException("It is unknown how to handle values of type " + item.GetType().FullName);
|
---|
51 | else type = interfaces.Single();
|
---|
52 | }
|
---|
53 |
|
---|
54 | dispatcher[type](name, item, builder);
|
---|
55 | }
|
---|
56 |
|
---|
57 | public static bool IsRegistered(Type type) {
|
---|
58 | return dispatcher.ContainsKey(type);
|
---|
59 | }
|
---|
60 |
|
---|
61 | public static void Register(Type type, Action<string, IItem, SolutionMessage.Builder> registrar) {
|
---|
62 | if (dispatcher.ContainsKey(type)) throw new ArgumentException("Type " + type.FullName + " is already registered.", "type");
|
---|
63 | dispatcher.Add(type, registrar);
|
---|
64 | }
|
---|
65 |
|
---|
66 | public static void Deregister(Type type) {
|
---|
67 | if (!dispatcher.ContainsKey(type)) throw new ArgumentException("Type " + type.FullName + " is not registered.", "type");
|
---|
68 | dispatcher.Remove(type);
|
---|
69 | }
|
---|
70 | }
|
---|
71 |
|
---|
72 | public static class DefaultSolutionMessageBuilder {
|
---|
73 | public static void AddAsIntegerVar(string name, ValueTypeValue<int> value, SolutionMessage.Builder builder) {
|
---|
74 | builder.AddIntegerVars(SolutionMessage.Types.IntegerVariable.CreateBuilder().SetName(name).SetData(value.Value).Build());
|
---|
75 | }
|
---|
76 | public static void AddAsDoubleVar(string name, ValueTypeValue<double> value, SolutionMessage.Builder builder) {
|
---|
77 | builder.AddDoubleVars(SolutionMessage.Types.DoubleVariable.CreateBuilder().SetName(name).SetData(value.Value).Build());
|
---|
78 | }
|
---|
79 | public static void AddAsBoolVar(string name, ValueTypeValue<bool> value, SolutionMessage.Builder builder) {
|
---|
80 | builder.AddBoolVars(SolutionMessage.Types.BoolVariable.CreateBuilder().SetName(name).SetData(value.Value).Build());
|
---|
81 | }
|
---|
82 | public static void AddAsStringVar(string name, IStringConvertibleValue value, SolutionMessage.Builder builder) {
|
---|
83 | builder.AddStringVars(SolutionMessage.Types.StringVariable.CreateBuilder().SetName(name).SetData(value.GetValue()).Build());
|
---|
84 | }
|
---|
85 | public static void AddAsIntegerArrayVar(string name, ValueTypeArray<int> valueTypeArray, SolutionMessage.Builder builder) {
|
---|
86 | SolutionMessage.Types.IntegerArrayVariable.Builder tmp = SolutionMessage.Types.IntegerArrayVariable.CreateBuilder();
|
---|
87 | tmp.SetName(name);
|
---|
88 | for (int i = 0; i < valueTypeArray.Length; i++)
|
---|
89 | tmp.AddData(valueTypeArray[i]);
|
---|
90 | builder.AddIntegerArrayVars(tmp.Build());
|
---|
91 | }
|
---|
92 | public static void AddAsDoubleArrayVar(string name, ValueTypeArray<double> valueTypeArray, SolutionMessage.Builder builder) {
|
---|
93 | SolutionMessage.Types.DoubleArrayVariable.Builder tmp = SolutionMessage.Types.DoubleArrayVariable.CreateBuilder();
|
---|
94 | tmp.SetName(name);
|
---|
95 | for (int i = 0; i < valueTypeArray.Length; i++)
|
---|
96 | tmp.AddData(valueTypeArray[i]);
|
---|
97 | builder.AddDoubleArrayVars(tmp.Build());
|
---|
98 | }
|
---|
99 | public static void AddAsBoolArrayVar(string name, ValueTypeArray<bool> valueTypeArray, SolutionMessage.Builder builder) {
|
---|
100 | SolutionMessage.Types.BoolArrayVariable.Builder tmp = SolutionMessage.Types.BoolArrayVariable.CreateBuilder();
|
---|
101 | tmp.SetName(name);
|
---|
102 | for (int i = 0; i < valueTypeArray.Length; i++)
|
---|
103 | tmp.AddData(valueTypeArray[i]);
|
---|
104 | builder.AddBoolArrayVars(tmp.Build());
|
---|
105 | }
|
---|
106 | }
|
---|
107 | }
|
---|