Free cookie consent management tool by TermsFeed Policy Generator

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

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

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

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