Free cookie consent management tool by TermsFeed Policy Generator

source: branches/1888_OaaS/HeuristicLab.Operators/3.3/MultiOperator.cs @ 16189

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

#1888:

  • Merged revisions from trunk
File size: 6.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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 HeuristicLab.Collections;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Parameters;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27using System;
28using System.Collections.Generic;
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          UpdateOperatorParameters();
50        }
51      }
52    }
53
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    }
61    public MultiOperator()
62      : base() {
63      this.operators = new ItemList<T>();
64      Initialize();
65    }
66
67    [StorableHook(HookType.AfterDeserialization)]
68    private void AfterDeserialization() {
69      Initialize();
70    }
71
72    private void Initialize() {
73      if (operators != null) RegisterOperatorsEvents();
74      operatorParameters = new List<IValueParameter<T>>();
75      for (int i = 0; i < Operators.Count; i++) {
76        IValueParameter<T> opParam = (IValueParameter<T>)Parameters[i.ToString()];
77        operatorParameters.Add(opParam);
78        opParam.ValueChanged += new EventHandler(opParam_ValueChanged);
79      }
80    }
81
82    private void UpdateOperatorParameters() {
83      foreach (IValueParameter<T> opParam in operatorParameters) {
84        opParam.ValueChanged -= new EventHandler(opParam_ValueChanged);
85        Parameters.Remove(opParam.Name);
86      }
87      operatorParameters.Clear();
88      for (int i = 0; i < Operators.Count; i++) {
89        IValueParameter<T> opParam = new OptionalValueParameter<T>(i.ToString(), string.Empty, Operators[i]);
90        opParam.Hidden = true;
91        opParam.ValueChanged += new EventHandler(opParam_ValueChanged);
92        Parameters.Add(opParam);
93        operatorParameters.Add(opParam);
94      }
95    }
96
97    #region Events
98    private void RegisterOperatorsEvents() {
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);
104    }
105    private void DeregisterOperatorsEvents() {
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);
111    }
112    protected virtual void Operators_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
113      UpdateOperatorParameters();
114    }
115    protected virtual void Operators_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
116      UpdateOperatorParameters();
117    }
118    protected virtual void Operators_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
119      foreach (IndexedItem<T> item in e.Items)
120        operatorParameters[item.Index].Value = item.Value;
121    }
122    protected virtual void Operators_ItemsMoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
123      foreach (IndexedItem<T> item in e.Items)
124        operatorParameters[item.Index].Value = item.Value;
125    }
126    protected virtual void Operators_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
127      UpdateOperatorParameters();
128    }
129    private void opParam_ValueChanged(object sender, EventArgs e) {
130      IValueParameter<T> opParam = (IValueParameter<T>)sender;
131      if (opParam.Value == null)
132        operators.RemoveAt(operatorParameters.IndexOf(opParam));
133      else
134        operators[operatorParameters.IndexOf(opParam)] = opParam.Value;
135    }
136    #endregion
137  }
138}
Note: See TracBrowser for help on using the repository browser.