Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Operators/3.3/MultiOperator.cs @ 4143

Last change on this file since 4143 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

File size: 5.9 KB
RevLine 
[2773]1#region License Information
2/* HeuristicLab
[2790]3 * Copyright (C) 2002-2010 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 System;
[3211]23using System.Collections.Generic;
[2773]24using HeuristicLab.Collections;
[3376]25using HeuristicLab.Common;
[2773]26using HeuristicLab.Core;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
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]
[3407]36  public abstract class MultiOperator<T> : SingleSuccessorOperator where T : class, IOperator {
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();
49        }
50      }
[2773]51    }
52
[3407]53    public MultiOperator()
[2773]54      : base() {
[3591]55      this.operators = new ItemList<T>();
[3211]56      Initialize();
[2773]57    }
[3591]58
[3317]59    [StorableConstructor]
[3407]60    protected MultiOperator(bool deserializing) : base(deserializing) { }
[2773]61
[3211]62    [StorableHook(HookType.AfterDeserialization)]
63    private void Initialize() {
[3317]64      if (operators != null) RegisterOperatorsEvents();
[3407]65      operatorParameters = new List<IValueParameter<T>>();
[3211]66      for (int i = 0; i < Operators.Count; i++) {
[3407]67        IValueParameter<T> opParam = (IValueParameter<T>)Parameters[i.ToString()];
[3211]68        operatorParameters.Add(opParam);
69        opParam.ValueChanged += new EventHandler(opParam_ValueChanged);
70      }
71    }
72
[2773]73    public override IDeepCloneable Clone(Cloner cloner) {
[3407]74      MultiOperator<T> clone = (MultiOperator<T>)base.Clone(cloner);
[3591]75      clone.operators = (IItemList<T>)cloner.Clone(operators);
[3211]76      clone.Initialize();
[2773]77      return clone;
78    }
79
80    private void UpdateOperatorParameters() {
[3407]81      foreach (IValueParameter<T> opParam in operatorParameters) {
[2773]82        opParam.ValueChanged -= new EventHandler(opParam_ValueChanged);
83        Parameters.Remove(opParam.Name);
84      }
[3211]85      operatorParameters.Clear();
[2773]86      for (int i = 0; i < Operators.Count; i++) {
[3729]87        IValueParameter<T> opParam = new OptionalValueParameter<T>(i.ToString(), string.Empty, Operators[i]);
[2773]88        opParam.ValueChanged += new EventHandler(opParam_ValueChanged);
89        Parameters.Add(opParam);
[3211]90        operatorParameters.Add(opParam);
[2773]91      }
92    }
[3211]93
94    #region Events
95    private void RegisterOperatorsEvents() {
[3445]96      operators.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Operators_ItemsAdded);
97      operators.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Operators_ItemsRemoved);
98      operators.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Operators_ItemsReplaced);
99      operators.ItemsMoved += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Operators_ItemsMoved);
100      operators.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Operators_CollectionReset);
[3211]101    }
102    private void DeregisterOperatorsEvents() {
[3445]103      operators.ItemsAdded -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Operators_ItemsAdded);
104      operators.ItemsRemoved -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Operators_ItemsRemoved);
105      operators.ItemsReplaced -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Operators_ItemsReplaced);
106      operators.ItemsMoved -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Operators_ItemsMoved);
107      operators.CollectionReset -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Operators_CollectionReset);
[3211]108    }
[3445]109    protected virtual void Operators_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
[2773]110      UpdateOperatorParameters();
111    }
[3445]112    protected virtual void Operators_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
[2773]113      UpdateOperatorParameters();
114    }
[3445]115    protected virtual void Operators_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
[3407]116      foreach (IndexedItem<T> item in e.Items)
[3211]117        operatorParameters[item.Index].Value = item.Value;
[2773]118    }
[3445]119    protected virtual void Operators_ItemsMoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
[3407]120      foreach (IndexedItem<T> item in e.Items)
[3211]121        operatorParameters[item.Index].Value = item.Value;
[2773]122    }
[3445]123    protected virtual void Operators_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
[2773]124      UpdateOperatorParameters();
125    }
126    private void opParam_ValueChanged(object sender, EventArgs e) {
[3407]127      IValueParameter<T> opParam = (IValueParameter<T>)sender;
[3729]128      if (opParam.Value == null)
129        operators.RemoveAt(operatorParameters.IndexOf(opParam));
130      else
131        operators[operatorParameters.IndexOf(opParam)] = opParam.Value;
[2773]132    }
[3211]133    #endregion
[2773]134  }
135}
Note: See TracBrowser for help on using the repository browser.