Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Scheduling/HeuristicLab.Operators/3.3/MultiOperator.cs @ 6307

Last change on this file since 6307 was 5445, checked in by swagner, 14 years ago

Updated year of copyrights (#1406)

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