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