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.Collections;
|
---|
26 | using HeuristicLab.Common;
|
---|
27 | using HeuristicLab.Core;
|
---|
28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Problems.ExternalEvaluation {
|
---|
31 | [Item("SolutionMessageBuilder", "Holds and uses a number of converters to translate HeuristicLab objects into appropriate fields of a solution message.")]
|
---|
32 | [StorableClass]
|
---|
33 | public class SolutionMessageBuilder : NamedItem {
|
---|
34 | public override bool CanChangeName { get { return false; } }
|
---|
35 | public override bool CanChangeDescription { get { return false; } }
|
---|
36 | private Dictionary<Type, Action<IItem, string, SolutionMessage.Builder>> dispatcher;
|
---|
37 |
|
---|
38 | // BackwardsCompatibility3.3
|
---|
39 | #region Backwards compatible code, remove with 3.4
|
---|
40 | [Storable]
|
---|
41 | [Obsolete]
|
---|
42 | private CheckedItemCollection<IItemToSolutionMessageConverter> converters;
|
---|
43 | #endregion
|
---|
44 | [Storable]
|
---|
45 | private CheckedItemList<IItemToSolutionMessageConverter> convertersList;
|
---|
46 | public CheckedItemList<IItemToSolutionMessageConverter> Converters {
|
---|
47 | get { return convertersList; }
|
---|
48 | }
|
---|
49 |
|
---|
50 | [StorableConstructor]
|
---|
51 | protected SolutionMessageBuilder(bool deserializing) : base(deserializing) { }
|
---|
52 | [StorableHook(HookType.AfterDeserialization)]
|
---|
53 | private void AfterDeserialization() {
|
---|
54 | AttachEventHandlers();
|
---|
55 | }
|
---|
56 |
|
---|
57 | protected SolutionMessageBuilder(SolutionMessageBuilder original, Cloner cloner)
|
---|
58 | : base(original, cloner) {
|
---|
59 | convertersList = cloner.Clone(original.convertersList);
|
---|
60 | AttachEventHandlers();
|
---|
61 | }
|
---|
62 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
63 | return new SolutionMessageBuilder(this, cloner);
|
---|
64 | }
|
---|
65 | public SolutionMessageBuilder()
|
---|
66 | : base() {
|
---|
67 | name = ItemName;
|
---|
68 | description = ItemDescription;
|
---|
69 | convertersList = new CheckedItemList<IItemToSolutionMessageConverter>();
|
---|
70 | convertersList.Add(new BoolConverter());
|
---|
71 | convertersList.Add(new DateTimeValueConverter());
|
---|
72 | convertersList.Add(new DoubleConverter());
|
---|
73 | convertersList.Add(new IntegerConverter());
|
---|
74 | convertersList.Add(new StringConverter());
|
---|
75 | convertersList.Add(new TimeSpanValueConverter());
|
---|
76 |
|
---|
77 | AttachEventHandlers();
|
---|
78 | }
|
---|
79 |
|
---|
80 | public void AddToMessage(IItem item, string name, SolutionMessage.Builder builder) {
|
---|
81 | if (dispatcher == null) BuildDispatcher();
|
---|
82 | Type itemType = item.GetType();
|
---|
83 | while (!dispatcher.ContainsKey(itemType)) {
|
---|
84 | if (itemType.BaseType != null) itemType = itemType.BaseType;
|
---|
85 | else break;
|
---|
86 | }
|
---|
87 | if (itemType.BaseType == null && !dispatcher.ContainsKey(itemType)) {
|
---|
88 | IEnumerable<Type> interfaces = item.GetType().GetInterfaces().Where(x => dispatcher.ContainsKey(x));
|
---|
89 | if (interfaces.Count() != 1) throw new ArgumentException(Name + ": No converter for type " + itemType.FullName + " defined.", "item");
|
---|
90 | else itemType = interfaces.Single();
|
---|
91 | }
|
---|
92 | dispatcher[itemType](item, name, builder);
|
---|
93 | }
|
---|
94 |
|
---|
95 | private void AttachEventHandlers() {
|
---|
96 | // BackwardsCompatibility3.3
|
---|
97 | #region Backwards compatible code, remove with 3.4
|
---|
98 | #pragma warning disable 0612
|
---|
99 | if (converters != null) {
|
---|
100 | if (convertersList == null) convertersList = new CheckedItemList<IItemToSolutionMessageConverter>();
|
---|
101 | foreach (IItemToSolutionMessageConverter c in converters)
|
---|
102 | convertersList.Add(c);
|
---|
103 | converters.Clear();
|
---|
104 | converters = null;
|
---|
105 | }
|
---|
106 | #pragma warning restore 0612
|
---|
107 | #endregion
|
---|
108 | convertersList.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<IItemToSolutionMessageConverter>>(convertersList_Changed);
|
---|
109 | convertersList.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<IItemToSolutionMessageConverter>>(convertersList_Changed);
|
---|
110 | convertersList.CheckedItemsChanged += new CollectionItemsChangedEventHandler<IndexedItem<IItemToSolutionMessageConverter>>(convertersList_Changed);
|
---|
111 | convertersList.ItemsMoved += new CollectionItemsChangedEventHandler<IndexedItem<IItemToSolutionMessageConverter>>(convertersList_Changed);
|
---|
112 | convertersList.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<IItemToSolutionMessageConverter>>(convertersList_Changed);
|
---|
113 | }
|
---|
114 |
|
---|
115 | private void convertersList_Changed(object sender, CollectionItemsChangedEventArgs<IndexedItem<IItemToSolutionMessageConverter>> e) {
|
---|
116 | BuildDispatcher();
|
---|
117 | }
|
---|
118 |
|
---|
119 | private void BuildDispatcher() {
|
---|
120 | dispatcher = new Dictionary<Type, Action<IItem, string, SolutionMessage.Builder>>();
|
---|
121 | foreach (IItemToSolutionMessageConverter c in convertersList.CheckedItems.OrderBy(x => x.Index).Select(x => x.Value)) {
|
---|
122 | Type[] types = c.ItemTypes;
|
---|
123 | foreach (Type t in types) {
|
---|
124 | if (!dispatcher.ContainsKey(t))
|
---|
125 | dispatcher.Add(t, new Action<IItem, string, SolutionMessage.Builder>(c.AddItemToBuilder));
|
---|
126 | }
|
---|
127 | }
|
---|
128 | }
|
---|
129 | }
|
---|
130 | }
|
---|