Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.ExternalEvaluation/3.4/SolutionMessageBuilder.cs @ 15325

Last change on this file since 15325 was 14186, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

File size: 4.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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    [Storable]
39    private CheckedItemList<IItemToSolutionMessageConverter> convertersList;
40    public CheckedItemList<IItemToSolutionMessageConverter> Converters {
41      get { return convertersList; }
42    }
43
44    [StorableConstructor]
45    protected SolutionMessageBuilder(bool deserializing) : base(deserializing) { }
46    [StorableHook(HookType.AfterDeserialization)]
47    private void AfterDeserialization() {
48      RegisterEventHandlers();
49    }
50
51    protected SolutionMessageBuilder(SolutionMessageBuilder original, Cloner cloner)
52      : base(original, cloner) {
53      convertersList = cloner.Clone(original.convertersList);
54      RegisterEventHandlers();
55    }
56    public override IDeepCloneable Clone(Cloner cloner) {
57      return new SolutionMessageBuilder(this, cloner);
58    }
59    public SolutionMessageBuilder()
60      : base() {
61      name = ItemName;
62      description = ItemDescription;
63      convertersList = new CheckedItemList<IItemToSolutionMessageConverter>();
64      convertersList.Add(new BoolConverter());
65      convertersList.Add(new DateTimeValueConverter());
66      convertersList.Add(new DoubleConverter());
67      convertersList.Add(new IntegerConverter());
68      convertersList.Add(new StringConverter());
69      convertersList.Add(new TimeSpanValueConverter());
70
71      RegisterEventHandlers();
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    private void RegisterEventHandlers() {
90      convertersList.ItemsAdded += convertersList_Changed;
91      convertersList.ItemsRemoved += convertersList_Changed;
92      convertersList.CheckedItemsChanged += convertersList_Changed;
93      convertersList.ItemsMoved += convertersList_Changed;
94      convertersList.ItemsReplaced += convertersList_Changed;
95    }
96
97    private void convertersList_Changed(object sender, CollectionItemsChangedEventArgs<IndexedItem<IItemToSolutionMessageConverter>> e) {
98      BuildDispatcher();
99    }
100
101    private void BuildDispatcher() {
102      dispatcher = new Dictionary<Type, Action<IItem, string, SolutionMessage.Builder>>();
103      foreach (var c in convertersList.CheckedItems.OrderBy(x => x.Index).Select(x => x.Value)) {
104        var types = c.ItemTypes;
105        foreach (var t in types) {
106          if (!dispatcher.ContainsKey(t))
107            dispatcher.Add(t, c.AddItemToBuilder);
108        }
109      }
110    }
111  }
112}
Note: See TracBrowser for help on using the repository browser.