Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/27/10 00:07:36 (14 years ago)
Author:
abeham
Message:

#1033

  • Changed collection to list
  • Rebuilding dispatcher after every change to the list

#1110

  • Added clone method
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.ExternalEvaluation/3.3/SolutionMessageBuilder.cs

    r4068 r4108  
    2424using System.Linq;
    2525using HeuristicLab.Collections;
     26using HeuristicLab.Common;
    2627using HeuristicLab.Core;
    2728using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     
    3536    private Dictionary<Type, Action<IItem, string, SolutionMessage.Builder>> dispatcher;
    3637
     38    // BackwardsCompatibility3.3
     39    #region Backwards compatible code, remove with 3.4
    3740    [Storable]
     41    [Obsolete]
    3842    private CheckedItemCollection<IItemToSolutionMessageConverter> converters;
    39     public CheckedItemCollection<IItemToSolutionMessageConverter> Converters {
    40       get { return converters; }
     43    #endregion
     44    [Storable]
     45    private CheckedItemList<IItemToSolutionMessageConverter> convertersList;
     46    public CheckedItemList<IItemToSolutionMessageConverter> Converters {
     47      get { return convertersList; }
    4148    }
    4249
     
    4754      name = ItemName;
    4855      description = ItemDescription;
    49       converters = new CheckedItemCollection<IItemToSolutionMessageConverter>();
     56      convertersList = new CheckedItemList<IItemToSolutionMessageConverter>();
     57      convertersList.Add(new BoolConverter());
     58      convertersList.Add(new DateTimeValueConverter());
     59      convertersList.Add(new DoubleConverter());
     60      convertersList.Add(new IntegerConverter());
     61      convertersList.Add(new StringConverter());
     62      convertersList.Add(new TimeSpanValueConverter());
     63
    5064      AttachEventHandlers();
     65    }
    5166
    52       converters.Add(new BoolConverter());
    53       converters.Add(new DateTimeValueConverter());
    54       converters.Add(new DoubleConverter());
    55       converters.Add(new IntegerConverter());
    56       converters.Add(new StringConverter());
    57       converters.Add(new TimeSpanValueConverter());
     67    public override IDeepCloneable Clone(Cloner cloner) {
     68      SolutionMessageBuilder clone = (SolutionMessageBuilder)base.Clone(cloner);
     69      clone.convertersList = (CheckedItemList<IItemToSolutionMessageConverter>)cloner.Clone(convertersList);
     70      clone.AttachEventHandlers();
     71      return clone;
    5872    }
    5973
    6074    public void AddToMessage(IItem item, string name, SolutionMessage.Builder builder) {
    61       if (dispatcher == null) FillDispatcher();
     75      if (dispatcher == null) BuildDispatcher();
    6276      Type itemType = item.GetType();
    6377      while (!dispatcher.ContainsKey(itemType)) {
     
    7589    [StorableHook(HookType.AfterDeserialization)]
    7690    private void AttachEventHandlers() {
    77       converters.ItemsAdded += new CollectionItemsChangedEventHandler<IItemToSolutionMessageConverter>(converters_ItemsAdded);
    78       converters.ItemsRemoved += new CollectionItemsChangedEventHandler<IItemToSolutionMessageConverter>(converters_ItemsRemoved);
    79       converters.CheckedItemsChanged += new CollectionItemsChangedEventHandler<IItemToSolutionMessageConverter>(converters_CheckedItemsChanged);
     91      // BackwardsCompatibility3.3
     92      #region Backwards compatible code, remove with 3.4
     93      #pragma warning disable 0612
     94      if (converters != null) {
     95        if (convertersList == null) convertersList = new CheckedItemList<IItemToSolutionMessageConverter>();
     96        foreach (IItemToSolutionMessageConverter c in converters)
     97          convertersList.Add(c);
     98        converters.Clear();
     99        converters = null;
     100      }
     101      #pragma warning restore 0612
     102      #endregion
     103      convertersList.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<IItemToSolutionMessageConverter>>(convertersList_Changed);
     104      convertersList.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<IItemToSolutionMessageConverter>>(convertersList_Changed);
     105      convertersList.CheckedItemsChanged += new CollectionItemsChangedEventHandler<IndexedItem<IItemToSolutionMessageConverter>>(convertersList_Changed);
     106      convertersList.ItemsMoved += new CollectionItemsChangedEventHandler<IndexedItem<IItemToSolutionMessageConverter>>(convertersList_Changed);
     107      convertersList.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<IItemToSolutionMessageConverter>>(convertersList_Changed);
    80108    }
    81109
    82     private void converters_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IItemToSolutionMessageConverter> e) {
    83       AddToDispatcher(e.Items);
     110    private void convertersList_Changed(object sender, CollectionItemsChangedEventArgs<IndexedItem<IItemToSolutionMessageConverter>> e) {
     111      BuildDispatcher();
    84112    }
    85113
    86     private void converters_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IItemToSolutionMessageConverter> e) {
    87       RemoveFromDispatcher(e.Items);
    88     }
    89 
    90     private void converters_CheckedItemsChanged(object sender, CollectionItemsChangedEventArgs<IItemToSolutionMessageConverter> e) {
    91       FillDispatcher();
    92     }
    93 
    94     private void FillDispatcher() {
     114    private void BuildDispatcher() {
    95115      dispatcher = new Dictionary<Type, Action<IItem, string, SolutionMessage.Builder>>();
    96       foreach (IItemToSolutionMessageConverter c in converters.CheckedItems) {
     116      foreach (IItemToSolutionMessageConverter c in convertersList.CheckedItems.OrderBy(x => x.Index).Select(x => x.Value)) {
    97117        Type[] types = c.ItemTypes;
    98118        foreach (Type t in types) {
    99           if (dispatcher.ContainsKey(t)) dispatcher.Remove(t);
    100           dispatcher.Add(t, new Action<IItem, string, SolutionMessage.Builder>(c.AddItemToBuilder));
    101         }
    102       }
    103     }
    104 
    105     private void AddToDispatcher(IEnumerable<IItemToSolutionMessageConverter> items) {
    106       if (dispatcher == null) FillDispatcher();
    107       foreach (IItemToSolutionMessageConverter c in items) {
    108         Type[] types = c.ItemTypes;
    109         foreach (Type t in types) {
    110           if (dispatcher.ContainsKey(t)) dispatcher.Remove(t);
    111           dispatcher.Add(t, new Action<IItem, string, SolutionMessage.Builder>(c.AddItemToBuilder));
    112         }
    113       }
    114     }
    115 
    116     private void RemoveFromDispatcher(IEnumerable<IItemToSolutionMessageConverter> items) {
    117       if (dispatcher == null) FillDispatcher();
    118       foreach (IItemToSolutionMessageConverter c in items) {
    119         Type[] types = c.ItemTypes;
    120         foreach (Type t in types) {
    121           if (dispatcher.ContainsKey(t)) dispatcher.Remove(t);
     119          if (!dispatcher.ContainsKey(t))
     120            dispatcher.Add(t, new Action<IItem, string, SolutionMessage.Builder>(c.AddItemToBuilder));
    122121        }
    123122      }
Note: See TracChangeset for help on using the changeset viewer.