#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;
namespace HeuristicLab.Core {
///
/// The base class for all operators.
///
public abstract class OperatorBase : ItemBase, IOperator {
private string myName;
///
/// Gets or sets the name of the operator.
///
/// Calls in the setter.
public string Name {
get { return myName; }
set {
if (myName != value) {
myName = value;
OnNameChanged();
}
}
}
///
/// Gets the description of the current operator.
///
/// Returns "No operator description available" if the method is not overriden.
public virtual string Description {
get { return "No operator description available."; }
}
///
/// Flag whether the current instance has been canceled.
///
protected bool myCanceled;
///
public bool Canceled {
get { return myCanceled; }
}
private bool myBreakpoint;
///
/// Calls in the setter.
public bool Breakpoint {
get { return myBreakpoint; }
set {
if (value != myBreakpoint) {
myBreakpoint = value;
OnBreakpointChanged();
}
}
}
private List mySubOperators;
///
/// Gets a list of all suboperators.
/// Returns the suboperators read-only!
///
public virtual IList SubOperators {
get { return mySubOperators.AsReadOnly(); }
}
private Dictionary myParameters;
///
public virtual ICollection Parameters {
get { return myParameters.Values; }
}
///
/// Initializes a new instance of setting the breakpoint flag and
/// the canceled flag to false and the name of the operator to the type name.
///
protected OperatorBase() {
myName = this.GetType().Name;
myCanceled = false;
myBreakpoint = false;
mySubOperators = new List();
myParameters = new Dictionary();
}
///
/// Clones the current instance (deep clone).
///
/// Clones also sub operators, variables and variable infos.
/// Dictionary of all already cloned objects. (Needed to avoid cycles.)
/// The cloned object as .
public override object Clone(IDictionary clonedObjects) {
OperatorBase clone = (OperatorBase)base.Clone(clonedObjects);
clone.myName = Name;
clone.mySubOperators.Clear();
for (int i = 0; i < SubOperators.Count; i++)
clone.AddSubOperator((IOperator)Auxiliary.Clone(SubOperators[i], clonedObjects));
clone.myParameters.Clear();
foreach (IParameter parameter in myParameters.Values)
clone.AddParameter((IParameter)Auxiliary.Clone(parameter, clonedObjects));
return clone;
}
///
/// Creates a new instance of to represent the current operator
/// visually.
///
/// The created view as .
public override IView CreateView() {
return new OperatorBaseView(this);
}
#region SubOperator Methods
///
/// The sub operator to add.
/// Calls .
public virtual void AddSubOperator(IOperator subOperator) {
mySubOperators.Add(subOperator);
OnSubOperatorAdded(subOperator, mySubOperators.Count - 1);
}
///
/// The sub operator to add.
/// Calls .
public virtual void AddSubOperator(IOperator subOperator, int index) {
mySubOperators.Insert(index, subOperator);
OnSubOperatorAdded(subOperator, index);
}
///
/// Calls .
public virtual void RemoveSubOperator(int index) {
IOperator op = mySubOperators[index];
mySubOperators.RemoveAt(index);
OnSubOperatorRemoved(op, index);
}
#endregion
#region Parameter Methods
///
public virtual IParameter GetParameter(string name) {
IParameter info;
if (myParameters.TryGetValue(name, out info))
return info;
else
return null;
}
///
/// Calls .
public virtual void AddParameter(IParameter parameter) {
myParameters.Add(parameter.Name, parameter);
OnParameterAdded(parameter);
}
///
/// Calls .
public virtual void RemoveParameter(string name) {
IParameter parameter;
if (myParameters.TryGetValue(name, out parameter)) {
myParameters.Remove(name);
OnParameterRemoved(parameter);
}
}
#endregion
///
public virtual IOperation Execute(IEnvironment env, IScope scope) {
myCanceled = false;
IOperation next = Apply(env, scope);
OnExecuted();
return next;
}
///
/// Sets property to true.
public virtual void Abort() {
myCanceled = true;
}
///
/// Performs the current operator on the specified .
///
/// The scope where to execute the operator
/// null.
public virtual IOperation Apply(IEnvironment env, IScope scope) {
return null;
}
///
public event EventHandler NameChanged;
///
/// Fires a new NameChanged event.
///
protected virtual void OnNameChanged() {
if (NameChanged != null) {
NameChanged(this, new EventArgs());
}
}
///
public event EventHandler BreakpointChanged;
///
/// Fires a new BreakpointChanged event.
///
protected virtual void OnBreakpointChanged() {
if (BreakpointChanged != null) {
BreakpointChanged(this, new EventArgs());
}
}
///
public event EventHandler SubOperatorAdded;
///
/// Fires a new SubOperatorAdded event.
///
/// The sub operator that has been added.
/// The position where the operator has been added.
protected virtual void OnSubOperatorAdded(IOperator subOperator, int index) {
if (SubOperatorAdded != null)
SubOperatorAdded(this, new OperatorIndexEventArgs(subOperator, index));
}
///
public event EventHandler SubOperatorRemoved;
///
/// Fires a new SubOperatorRemoved event.
///
/// The sub operator that has been removed.
/// The position where the operator has been removed.
protected virtual void OnSubOperatorRemoved(IOperator subOperator, int index) {
if (SubOperatorRemoved != null)
SubOperatorRemoved(this, new OperatorIndexEventArgs(subOperator, index));
}
///
public event EventHandler ParameterAdded;
///
/// Fires a new ParameterAdded event.
///
/// The parameter that has been added.
protected virtual void OnParameterAdded(IParameter parameter) {
if (ParameterAdded != null)
ParameterAdded(this, new ParameterEventArgs(parameter));
}
///
public event EventHandler ParameterRemoved;
///
/// Fires a new ParameterRemoved event.
///
/// The parameter that has been removed.
protected virtual void OnParameterRemoved(IParameter parameter) {
if (ParameterRemoved != null)
ParameterRemoved(this, new ParameterEventArgs(parameter));
}
///
public event EventHandler Executed;
///
/// Fires a new Executed event.
///
protected virtual void OnExecuted() {
if (Executed != null) {
Executed(this, new EventArgs());
}
}
#region Persistence Methods
///
/// Saves the current instance as in the specified .
///
///
/// Calls of base class .
///
A quick overview how the single elements of the current instance are saved:
///
/// -
/// Name:
/// Saved as an with the name Name.
///
/// -
/// Breakpoint:
/// Is only saved if it set to true.
/// Saved as an with the name Breakpoint.
///
/// -
/// Sub operators:
/// Saved as child node with tag name SubOperators. All sub operators are themselves
/// saved as child nodes.
///
/// -
/// Variable infos:
/// Saved as child node with tag name VariableInfos. All variable infos are themselves
/// saved as child nodes.
///
/// -
/// Variables:
/// Saved as child node with tag name Variables. All variables are themselves
/// saved as child nodes.
///
///
///
/// The (tag)name of the .
/// The where to save the data.
/// The dictionary of all already persisted objects. (Needed to avoid cycles.)
/// The saved .
public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary persistedObjects) {
XmlNode node = base.GetXmlNode(name, document, persistedObjects);
XmlAttribute nameAttribute = document.CreateAttribute("Name");
nameAttribute.Value = Name;
node.Attributes.Append(nameAttribute);
if (Breakpoint) {
XmlAttribute breakpointAttribute = document.CreateAttribute("Breakpoint");
breakpointAttribute.Value = Breakpoint.ToString();
node.Attributes.Append(breakpointAttribute);
}
XmlNode subOperatorsNode = document.CreateNode(XmlNodeType.Element, "SubOperators", null);
for (int i = 0; i < SubOperators.Count; i++)
subOperatorsNode.AppendChild(PersistenceManager.Persist(SubOperators[i], document, persistedObjects));
node.AppendChild(subOperatorsNode);
XmlNode parametersNode = document.CreateNode(XmlNodeType.Element, "Parameters", null);
foreach (IParameter parameter in myParameters.Values)
parametersNode.AppendChild(PersistenceManager.Persist(parameter, document, persistedObjects));
node.AppendChild(parametersNode);
return node;
}
///
/// Loads the persisted operation from the specified .
///
/// Calls of base class
/// .
/// For informations how the different elements must be saved please see .
/// The where the operation is saved.
/// A dictionary of all already restored objects. (Needed to avoid cycles.)
public override void Populate(XmlNode node, IDictionary restoredObjects) {
base.Populate(node, restoredObjects);
myName = node.Attributes["Name"].Value;
if (node.Attributes["Breakpoint"] != null)
myBreakpoint = bool.Parse(node.Attributes["Breakpoint"].Value);
XmlNode subOperatorsNode = node.SelectSingleNode("SubOperators");
for (int i = 0; i < subOperatorsNode.ChildNodes.Count; i++)
AddSubOperator((IOperator)PersistenceManager.Restore(subOperatorsNode.ChildNodes[i], restoredObjects));
XmlNode parametersNode = node.SelectSingleNode("Parameters");
myParameters.Clear();
foreach (XmlNode parameterNode in parametersNode.ChildNodes)
AddParameter((IParameter)PersistenceManager.Restore(parameterNode, restoredObjects));
}
#endregion
}
}