Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OaaS/HeuristicLab.Operators/3.3/MultiOperator.cs @ 14871

Last change on this file since 14871 was 9363, checked in by spimming, 12 years ago

#1888:

  • Merged revisions from trunk
File size: 6.0 KB
RevLine 
[2773]1#region License Information
2/* HeuristicLab
[7259]3 * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2773]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 HeuristicLab.Collections;
[3376]23using HeuristicLab.Common;
[2773]24using HeuristicLab.Core;
25using HeuristicLab.Parameters;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[9363]27using System;
28using System.Collections.Generic;
[2773]29
30namespace HeuristicLab.Operators {
31  /// <summary>
[3407]32  /// A base class for operators which apply arbitrary many other operators of a specific type.
[2773]33  /// </summary>
[3822]34  [Item("MultiOperator", "A base class for operators which apply arbitrary many other operators of a specific type.")]
[3017]35  [StorableClass]
[5080]36  public abstract class MultiOperator<T> : SingleSuccessorOperator, IMultiOperator<T> where T : class, IOperator {
[3407]37    private List<IValueParameter<T>> operatorParameters;
[2773]38
[3317]39    [Storable]
[3591]40    private IItemList<T> operators;
41    public IItemList<T> Operators {
[2773]42      get { return operators; }
[3591]43      protected set {
44        if (operators != value) {
45          if (value == null) throw new ArgumentException();
46          DeregisterOperatorsEvents();
47          operators = value;
48          RegisterOperatorsEvents();
[9363]49          UpdateOperatorParameters();
[3591]50        }
51      }
[2773]52    }
53
[4722]54    [StorableConstructor]
55    protected MultiOperator(bool deserializing) : base(deserializing) { }
56    protected MultiOperator(MultiOperator<T> original, Cloner cloner)
57      : base(original, cloner) {
58      this.operators = cloner.Clone<IItemList<T>>(original.operators);
59      Initialize();
60    }
[3407]61    public MultiOperator()
[2773]62      : base() {
[3591]63      this.operators = new ItemList<T>();
[3211]64      Initialize();
[2773]65    }
[3591]66
[3211]67    [StorableHook(HookType.AfterDeserialization)]
[4722]68    private void AfterDeserialization() {
69      Initialize();
70    }
[5080]71
[3211]72    private void Initialize() {
[3317]73      if (operators != null) RegisterOperatorsEvents();
[3407]74      operatorParameters = new List<IValueParameter<T>>();
[3211]75      for (int i = 0; i < Operators.Count; i++) {
[3407]76        IValueParameter<T> opParam = (IValueParameter<T>)Parameters[i.ToString()];
[3211]77        operatorParameters.Add(opParam);
78        opParam.ValueChanged += new EventHandler(opParam_ValueChanged);
79      }
80    }
81
[2773]82    private void UpdateOperatorParameters() {
[3407]83      foreach (IValueParameter<T> opParam in operatorParameters) {
[2773]84        opParam.ValueChanged -= new EventHandler(opParam_ValueChanged);
85        Parameters.Remove(opParam.Name);
86      }
[3211]87      operatorParameters.Clear();
[2773]88      for (int i = 0; i < Operators.Count; i++) {
[3729]89        IValueParameter<T> opParam = new OptionalValueParameter<T>(i.ToString(), string.Empty, Operators[i]);
[6051]90        opParam.Hidden = true;
[2773]91        opParam.ValueChanged += new EventHandler(opParam_ValueChanged);
92        Parameters.Add(opParam);
[3211]93        operatorParameters.Add(opParam);
[2773]94      }
95    }
[3211]96
97    #region Events
98    private void RegisterOperatorsEvents() {
[3445]99      operators.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Operators_ItemsAdded);
100      operators.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Operators_ItemsRemoved);
101      operators.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Operators_ItemsReplaced);
102      operators.ItemsMoved += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Operators_ItemsMoved);
103      operators.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Operators_CollectionReset);
[3211]104    }
105    private void DeregisterOperatorsEvents() {
[3445]106      operators.ItemsAdded -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Operators_ItemsAdded);
107      operators.ItemsRemoved -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Operators_ItemsRemoved);
108      operators.ItemsReplaced -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Operators_ItemsReplaced);
109      operators.ItemsMoved -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Operators_ItemsMoved);
110      operators.CollectionReset -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Operators_CollectionReset);
[3211]111    }
[3445]112    protected virtual void Operators_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
[2773]113      UpdateOperatorParameters();
114    }
[3445]115    protected virtual void Operators_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
[2773]116      UpdateOperatorParameters();
117    }
[3445]118    protected virtual void Operators_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
[3407]119      foreach (IndexedItem<T> item in e.Items)
[3211]120        operatorParameters[item.Index].Value = item.Value;
[2773]121    }
[3445]122    protected virtual void Operators_ItemsMoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
[3407]123      foreach (IndexedItem<T> item in e.Items)
[3211]124        operatorParameters[item.Index].Value = item.Value;
[2773]125    }
[3445]126    protected virtual void Operators_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
[2773]127      UpdateOperatorParameters();
128    }
129    private void opParam_ValueChanged(object sender, EventArgs e) {
[3407]130      IValueParameter<T> opParam = (IValueParameter<T>)sender;
[3729]131      if (opParam.Value == null)
132        operators.RemoveAt(operatorParameters.IndexOf(opParam));
133      else
134        operators[operatorParameters.IndexOf(opParam)] = opParam.Value;
[2773]135    }
[3211]136    #endregion
[2773]137  }
138}
Note: See TracBrowser for help on using the repository browser.