Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Problems.ExternalEvaluation/3.4/SolutionMessageBuilder.cs @ 16793

Last change on this file since 16793 was 16565, checked in by gkronber, 6 years ago

#2520: merged changes from PersistenceOverhaul branch (r16451:16564) into trunk

File size: 4.6 KB
RevLine 
[3881]1#region License Information
2/* HeuristicLab
[16565]3 * Copyright (C) 2002-2019 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;
[16565]28using HEAL.Attic;
[3881]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.")]
[16565]32  [StorableType("8F406464-C1F6-4BBD-8791-C836846B473B")]
[3881]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]
[4108]39    private CheckedItemList<IItemToSolutionMessageConverter> convertersList;
40    public CheckedItemList<IItemToSolutionMessageConverter> Converters {
41      get { return convertersList; }
[3881]42    }
43
44    [StorableConstructor]
[16565]45    protected SolutionMessageBuilder(StorableConstructorFlag _) : base(_) { }
[4722]46    [StorableHook(HookType.AfterDeserialization)]
47    private void AfterDeserialization() {
[7351]48      RegisterEventHandlers();
[4722]49    }
50
51    protected SolutionMessageBuilder(SolutionMessageBuilder original, Cloner cloner)
52      : base(original, cloner) {
53      convertersList = cloner.Clone(original.convertersList);
[7351]54      RegisterEventHandlers();
[4722]55    }
56    public override IDeepCloneable Clone(Cloner cloner) {
57      return new SolutionMessageBuilder(this, cloner);
58    }
[3881]59    public SolutionMessageBuilder()
60      : base() {
61      name = ItemName;
62      description = ItemDescription;
[4108]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
[7351]71      RegisterEventHandlers();
[4108]72    }
[3881]73
74    public void AddToMessage(IItem item, string name, SolutionMessage.Builder builder) {
[4108]75      if (dispatcher == null) BuildDispatcher();
[3881]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));
[4643]83        if (interfaces.Count() != 1) throw new ArgumentException(Name + ": No converter for type " + itemType.FullName + " defined.", "item");
[3881]84        else itemType = interfaces.Single();
85      }
86      dispatcher[itemType](item, name, builder);
87    }
88
[7351]89    private void RegisterEventHandlers() {
[11892]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;
[3881]95    }
96
[4108]97    private void convertersList_Changed(object sender, CollectionItemsChangedEventArgs<IndexedItem<IItemToSolutionMessageConverter>> e) {
98      BuildDispatcher();
[3881]99    }
100
[4108]101    private void BuildDispatcher() {
[3881]102      dispatcher = new Dictionary<Type, Action<IItem, string, SolutionMessage.Builder>>();
[11892]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) {
[4108]106          if (!dispatcher.ContainsKey(t))
[11892]107            dispatcher.Add(t, c.AddItemToBuilder);
[3881]108        }
109      }
110    }
111  }
112}
Note: See TracBrowser for help on using the repository browser.