Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5668 was 5445, checked in by swagner, 14 years ago

Updated year of copyrights (#1406)

File size: 5.8 KB
RevLine 
[3881]1#region License Information
2/* HeuristicLab
[5445]3 * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[3881]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;
[4068]25using HeuristicLab.Collections;
[4108]26using HeuristicLab.Common;
[3881]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
[4108]38    // BackwardsCompatibility3.3
39    #region Backwards compatible code, remove with 3.4
[3881]40    [Storable]
[4108]41    [Obsolete]
[3881]42    private CheckedItemCollection<IItemToSolutionMessageConverter> converters;
[4108]43    #endregion
44    [Storable]
45    private CheckedItemList<IItemToSolutionMessageConverter> convertersList;
46    public CheckedItemList<IItemToSolutionMessageConverter> Converters {
47      get { return convertersList; }
[3881]48    }
49
50    [StorableConstructor]
51    protected SolutionMessageBuilder(bool deserializing) : base(deserializing) { }
[4722]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    }
[3881]65    public SolutionMessageBuilder()
66      : base() {
67      name = ItemName;
68      description = ItemDescription;
[4108]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
[3881]77      AttachEventHandlers();
[4108]78    }
[3881]79
80    public void AddToMessage(IItem item, string name, SolutionMessage.Builder builder) {
[4108]81      if (dispatcher == null) BuildDispatcher();
[3881]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));
[4643]89        if (interfaces.Count() != 1) throw new ArgumentException(Name + ": No converter for type " + itemType.FullName + " defined.", "item");
[3881]90        else itemType = interfaces.Single();
91      }
92      dispatcher[itemType](item, name, builder);
93    }
94
95    private void AttachEventHandlers() {
[4108]96      // BackwardsCompatibility3.3
97      #region Backwards compatible code, remove with 3.4
[4722]98#pragma warning disable 0612
[4108]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      }
[4722]106#pragma warning restore 0612
[4108]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);
[3881]113    }
114
[4108]115    private void convertersList_Changed(object sender, CollectionItemsChangedEventArgs<IndexedItem<IItemToSolutionMessageConverter>> e) {
116      BuildDispatcher();
[3881]117    }
118
[4108]119    private void BuildDispatcher() {
[3881]120      dispatcher = new Dictionary<Type, Action<IItem, string, SolutionMessage.Builder>>();
[4108]121      foreach (IItemToSolutionMessageConverter c in convertersList.CheckedItems.OrderBy(x => x.Index).Select(x => x.Value)) {
[3881]122        Type[] types = c.ItemTypes;
123        foreach (Type t in types) {
[4108]124          if (!dispatcher.ContainsKey(t))
125            dispatcher.Add(t, new Action<IItem, string, SolutionMessage.Builder>(c.AddItemToBuilder));
[3881]126        }
127      }
128    }
129  }
130}
Note: See TracBrowser for help on using the repository browser.