Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 8075 was 7351, checked in by abeham, 13 years ago

#1722

  • fixed some problems that were identified with the first existing FxCop rules (duplicate storable hook in ExternalEvaluationProblem, multiple wrong names)
  • generally renamed AttachEventHandlers to RegisterEventHandlers to be consistent
  • fixed some backwards compatible regions to use the format from the snippet and the comment
File size: 5.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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    [StorableHook(HookType.AfterDeserialization)]
53    private void AfterDeserialization() {
54      // BackwardsCompatibility3.3
55      #region Backwards compatible code, remove with 3.4
56#pragma warning disable 0612
57      if (converters != null) {
58        if (convertersList == null) convertersList = new CheckedItemList<IItemToSolutionMessageConverter>();
59        foreach (IItemToSolutionMessageConverter c in converters)
60          convertersList.Add(c);
61        converters.Clear();
62        converters = null;
63      }
64#pragma warning restore 0612
65      #endregion
66      RegisterEventHandlers();
67    }
68
69    protected SolutionMessageBuilder(SolutionMessageBuilder original, Cloner cloner)
70      : base(original, cloner) {
71      convertersList = cloner.Clone(original.convertersList);
72      RegisterEventHandlers();
73    }
74    public override IDeepCloneable Clone(Cloner cloner) {
75      return new SolutionMessageBuilder(this, cloner);
76    }
77    public SolutionMessageBuilder()
78      : base() {
79      name = ItemName;
80      description = ItemDescription;
81      convertersList = new CheckedItemList<IItemToSolutionMessageConverter>();
82      convertersList.Add(new BoolConverter());
83      convertersList.Add(new DateTimeValueConverter());
84      convertersList.Add(new DoubleConverter());
85      convertersList.Add(new IntegerConverter());
86      convertersList.Add(new StringConverter());
87      convertersList.Add(new TimeSpanValueConverter());
88
89      RegisterEventHandlers();
90    }
91
92    public void AddToMessage(IItem item, string name, SolutionMessage.Builder builder) {
93      if (dispatcher == null) BuildDispatcher();
94      Type itemType = item.GetType();
95      while (!dispatcher.ContainsKey(itemType)) {
96        if (itemType.BaseType != null) itemType = itemType.BaseType;
97        else break;
98      }
99      if (itemType.BaseType == null && !dispatcher.ContainsKey(itemType)) {
100        IEnumerable<Type> interfaces = item.GetType().GetInterfaces().Where(x => dispatcher.ContainsKey(x));
101        if (interfaces.Count() != 1) throw new ArgumentException(Name + ": No converter for type " + itemType.FullName + " defined.", "item");
102        else itemType = interfaces.Single();
103      }
104      dispatcher[itemType](item, name, builder);
105    }
106
107    private void RegisterEventHandlers() {
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}
Note: See TracBrowser for help on using the repository browser.