#region License Information
/* HeuristicLab
* Copyright (C) 2002-2008 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.Collections.Generic;
using System.Text;
using System.Xml;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.Operators {
///
/// Contains an operator graph and automatically injects its sub-operators into the scope it is
/// applied on (useful for modularization to assemble complex operators out of simpler ones).
///
public class CombinedOperator : DelegatingOperator {
[Storable]
private string myDescription;
///
/// Gets the description of the current instance.
///
public override string Description {
get { return myDescription; }
}
[Storable]
private IOperatorGraph myOperatorGraph;
///
/// Gets the operator graph of the current instance.
///
public IOperatorGraph OperatorGraph {
get { return myOperatorGraph; }
}
///
/// Initializes a new instance of .
///
public CombinedOperator()
: base() {
myDescription =
@"A combined operator contains a whole operator graph. It is useful for modularization to assemble complex operators out of simpler ones.
A combined operator automatically inject its sub-operators into the scope it is applied on. Thereby the names of the sub-operators are used as variable names. Those operators can be extracted again in the contained operator graph by using an OperatorExtractor. So it is possible to parameterize a combined operator with custom operators.";
myOperatorGraph = new OperatorGraph();
}
///
/// Sets the description of the current instance.
///
/// Calls .
/// Thrown when is null.
/// The description to set.
public void SetDescription(string description) {
if (description == null)
throw new NullReferenceException("description must not be null");
if (description != myDescription) {
myDescription = description;
OnDescriptionChanged();
}
}
///
/// Clones the current instance (deep clone).
///
/// Calls
/// of base class .
/// Deep clone through method of helper class
/// .
/// Dictionary of all already cloned objects. (Needed to avoid cycles.)
/// The cloned object as .
public override object Clone(IDictionary clonedObjects) {
CombinedOperator clone = (CombinedOperator)base.Clone(clonedObjects);
clone.myDescription = Description;
clone.myOperatorGraph = (IOperatorGraph)Auxiliary.Clone(OperatorGraph, clonedObjects);
return clone;
}
///
/// Adds all sub operators to the specified .
///
/// The scope where to inject the sub operators.
/// null if the initial operator is nulll, else a new
/// with the initial operator and the given .
public override IOperation Apply(IScope scope) {
if (OperatorGraph.InitialOperator != null) {
for (int i = 0; i < SubOperators.Count; i++) {
if (scope.GetVariable(SubOperators[i].Name) != null)
scope.RemoveVariable(SubOperators[i].Name);
scope.AddVariable(new Variable(SubOperators[i].Name, SubOperators[i]));
}
return new AtomicOperation(OperatorGraph.InitialOperator, scope);
} else {
return null;
}
}
///
/// Creates a new instance of to display the current instance.
///
/// The created view as .
public override IView CreateView() {
return new CombinedOperatorView(this);
}
///
/// Occurs when the description of the current instance has been changed.
///
public event EventHandler DescriptionChanged;
///
/// Fires a new DescriptionChanged event.
///
protected virtual void OnDescriptionChanged() {
if (DescriptionChanged != null)
DescriptionChanged(this, new EventArgs());
}
}
}