Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 9651 was 9456, checked in by swagner, 12 years ago

Updated copyright year and added some missing license headers (#1889)

File size: 5.8 KB
RevLine 
[3881]1#region License Information
2/* HeuristicLab
[9456]3 * Copyright (C) 2002-2013 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() {
[7351]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();
[4722]67    }
68
69    protected SolutionMessageBuilder(SolutionMessageBuilder original, Cloner cloner)
70      : base(original, cloner) {
71      convertersList = cloner.Clone(original.convertersList);
[7351]72      RegisterEventHandlers();
[4722]73    }
74    public override IDeepCloneable Clone(Cloner cloner) {
75      return new SolutionMessageBuilder(this, cloner);
76    }
[3881]77    public SolutionMessageBuilder()
78      : base() {
79      name = ItemName;
80      description = ItemDescription;
[4108]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
[7351]89      RegisterEventHandlers();
[4108]90    }
[3881]91
92    public void AddToMessage(IItem item, string name, SolutionMessage.Builder builder) {
[4108]93      if (dispatcher == null) BuildDispatcher();
[3881]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));
[4643]101        if (interfaces.Count() != 1) throw new ArgumentException(Name + ": No converter for type " + itemType.FullName + " defined.", "item");
[3881]102        else itemType = interfaces.Single();
103      }
104      dispatcher[itemType](item, name, builder);
105    }
106
[7351]107    private void RegisterEventHandlers() {
[4108]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.