#region License Information
/* HeuristicLab
* Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Linq;
using HeuristicLab.Collections;
using HeuristicLab.Core;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.Operators {
///
/// A base class for operators which apply multiple user-defined operators.
///
[Item("MultipleCallsOperator", "A base class for operators which apply multiple user-defined operators.")]
[StorableClass]
public abstract class MultipleCallsOperator : SingleSuccessorOperator {
protected IValueParameter[] OperatorParameters {
get {
return (from p in Parameters
where p is IValueParameter
where Operators.Contains(((IValueParameter)p).Value)
orderby p.Name ascending
select (IValueParameter)p).ToArray();
}
}
private OperatorList operators;
[Storable]
public OperatorList Operators {
get { return operators; }
private set {
operators = value;
operators.ItemsAdded += new CollectionItemsChangedEventHandler>(operators_ItemsAdded);
operators.ItemsRemoved += new CollectionItemsChangedEventHandler>(operators_ItemsRemoved);
operators.ItemsReplaced += new CollectionItemsChangedEventHandler>(operators_ItemsReplaced);
operators.ItemsMoved += new CollectionItemsChangedEventHandler>(operators_ItemsMoved);
operators.CollectionReset += new CollectionItemsChangedEventHandler>(operators_CollectionReset);
var opParams = OperatorParameters;
foreach (IValueParameter opParam in opParams)
opParam.ValueChanged += new EventHandler(opParam_ValueChanged);
}
}
public MultipleCallsOperator()
: base() {
Operators = new OperatorList();
}
public override IDeepCloneable Clone(Cloner cloner) {
MultipleCallsOperator clone = (MultipleCallsOperator)base.Clone(cloner);
clone.Operators = (OperatorList)cloner.Clone(operators);
return clone;
}
private void UpdateOperatorParameters() {
var opParams = OperatorParameters;
foreach (IValueParameter opParam in opParams) {
opParam.ValueChanged -= new EventHandler(opParam_ValueChanged);
Parameters.Remove(opParam.Name);
}
for (int i = 0; i < Operators.Count; i++) {
IValueParameter opParam = new OperatorParameter(i.ToString(), string.Empty, Operators[i]);
opParam.ValueChanged += new EventHandler(opParam_ValueChanged);
Parameters.Add(opParam);
}
}
private void operators_ItemsAdded(object sender, CollectionItemsChangedEventArgs> e) {
UpdateOperatorParameters();
}
private void operators_ItemsRemoved(object sender, CollectionItemsChangedEventArgs> e) {
UpdateOperatorParameters();
}
private void operators_ItemsReplaced(object sender, CollectionItemsChangedEventArgs> e) {
foreach (IndexedItem item in e.Items)
((IValueParameter)Parameters[item.Index.ToString()]).Value = item.Value;
}
private void operators_ItemsMoved(object sender, CollectionItemsChangedEventArgs> e) {
foreach (IndexedItem item in e.Items)
((IValueParameter)Parameters[item.Index.ToString()]).Value = item.Value;
}
private void operators_CollectionReset(object sender, CollectionItemsChangedEventArgs> e) {
UpdateOperatorParameters();
}
private void opParam_ValueChanged(object sender, EventArgs e) {
IValueParameter opParam = (IValueParameter)sender;
int index = int.Parse(opParam.Name);
operators[index] = opParam.Value;
}
}
}