Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.ExternalEvaluation/3.3/SolutionMessageBuilder.cs @ 4555

Last change on this file since 4555 was 4108, checked in by abeham, 14 years ago

#1033

  • Changed collection to list
  • Rebuilding dispatcher after every change to the list

#1110

  • Added clone method
File size: 5.7 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Collections;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace 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    public SolutionMessageBuilder()
53      : base() {
54      name = ItemName;
55      description = ItemDescription;
56      convertersList = new CheckedItemList<IItemToSolutionMessageConverter>();
57      convertersList.Add(new BoolConverter());
58      convertersList.Add(new DateTimeValueConverter());
59      convertersList.Add(new DoubleConverter());
60      convertersList.Add(new IntegerConverter());
61      convertersList.Add(new StringConverter());
62      convertersList.Add(new TimeSpanValueConverter());
63
64      AttachEventHandlers();
65    }
66
67    public override IDeepCloneable Clone(Cloner cloner) {
68      SolutionMessageBuilder clone = (SolutionMessageBuilder)base.Clone(cloner);
69      clone.convertersList = (CheckedItemList<IItemToSolutionMessageConverter>)cloner.Clone(convertersList);
70      clone.AttachEventHandlers();
71      return clone;
72    }
73
74    public void AddToMessage(IItem item, string name, SolutionMessage.Builder builder) {
75      if (dispatcher == null) BuildDispatcher();
76      Type itemType = item.GetType();
77      while (!dispatcher.ContainsKey(itemType)) {
78        if (itemType.BaseType != null) itemType = itemType.BaseType;
79        else break;
80      }
81      if (itemType.BaseType == null && !dispatcher.ContainsKey(itemType)) {
82        IEnumerable<Type> interfaces = item.GetType().GetInterfaces().Where(x => dispatcher.ContainsKey(x));
83        if (interfaces.Count() != 1) throw new ArgumentException(Name + ": No converter for type + " + itemType.FullName + " defined.", "item");
84        else itemType = interfaces.Single();
85      }
86      dispatcher[itemType](item, name, builder);
87    }
88
89    [StorableHook(HookType.AfterDeserialization)]
90    private void AttachEventHandlers() {
91      // BackwardsCompatibility3.3
92      #region Backwards compatible code, remove with 3.4
93      #pragma warning disable 0612
94      if (converters != null) {
95        if (convertersList == null) convertersList = new CheckedItemList<IItemToSolutionMessageConverter>();
96        foreach (IItemToSolutionMessageConverter c in converters)
97          convertersList.Add(c);
98        converters.Clear();
99        converters = null;
100      }
101      #pragma warning restore 0612
102      #endregion
103      convertersList.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<IItemToSolutionMessageConverter>>(convertersList_Changed);
104      convertersList.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<IItemToSolutionMessageConverter>>(convertersList_Changed);
105      convertersList.CheckedItemsChanged += new CollectionItemsChangedEventHandler<IndexedItem<IItemToSolutionMessageConverter>>(convertersList_Changed);
106      convertersList.ItemsMoved += new CollectionItemsChangedEventHandler<IndexedItem<IItemToSolutionMessageConverter>>(convertersList_Changed);
107      convertersList.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<IItemToSolutionMessageConverter>>(convertersList_Changed);
108    }
109
110    private void convertersList_Changed(object sender, CollectionItemsChangedEventArgs<IndexedItem<IItemToSolutionMessageConverter>> e) {
111      BuildDispatcher();
112    }
113
114    private void BuildDispatcher() {
115      dispatcher = new Dictionary<Type, Action<IItem, string, SolutionMessage.Builder>>();
116      foreach (IItemToSolutionMessageConverter c in convertersList.CheckedItems.OrderBy(x => x.Index).Select(x => x.Value)) {
117        Type[] types = c.ItemTypes;
118        foreach (Type t in types) {
119          if (!dispatcher.ContainsKey(t))
120            dispatcher.Add(t, new Action<IItem, string, SolutionMessage.Builder>(c.AddItemToBuilder));
121        }
122      }
123    }
124  }
125}
Note: See TracBrowser for help on using the repository browser.