Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 13820 was 12012, checked in by ascheibe, 10 years ago

#2212 merged r12008, r12009, r12010 back into trunk

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