#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.Drawing;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.Operators {
///
/// The base class for all operators.
///
[Item("Operator", "Base class for operators.")]
[StorableClass]
public abstract class Operator : ParameterizedNamedItem, IOperator {
public override Image ItemImage {
get {
if (Breakpoint) return HeuristicLab.Common.Resources.VS2008ImageLibrary.BreakpointActive;
else return HeuristicLab.Common.Resources.VS2008ImageLibrary.Method;
}
}
public override bool CanChangeDescription {
get { return false; }
}
[Storable]
private IExecutionContext executionContext;
protected IExecutionContext ExecutionContext {
get { return executionContext; }
private set {
if (value != executionContext) {
executionContext = value;
OnExecutionContextChanged();
}
}
}
///
/// Flag whether the current instance has been canceled.
///
private bool canceled;
///
protected bool Canceled {
get { return canceled; }
private set {
if (value != canceled) {
canceled = value;
OnCanceledChanged();
}
}
}
[Storable]
private bool breakpoint;
///
/// Calls in the setter.
public bool Breakpoint {
get { return breakpoint; }
set {
if (value != breakpoint) {
breakpoint = value;
OnBreakpointChanged();
OnItemImageChanged();
}
}
}
[StorableConstructor]
protected Operator(bool deserializing) : base(deserializing) { }
protected Operator(Operator original, Cloner cloner)
: base(original, cloner) {
this.canceled = original.canceled;
this.breakpoint = original.breakpoint;
this.executionContext = cloner.Clone(original.executionContext);
}
///
/// 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 Operator()
: base() {
canceled = false;
breakpoint = false;
}
protected Operator(string name)
: base(name) {
canceled = false;
breakpoint = false;
}
protected Operator(string name, ParameterCollection parameters)
: base(name, parameters) {
canceled = false;
breakpoint = false;
}
protected Operator(string name, string description)
: base(name, description) {
canceled = false;
breakpoint = false;
}
protected Operator(string name, string description, ParameterCollection parameters)
: base(name, description, parameters) {
canceled = false;
breakpoint = false;
}
///
public virtual IOperation Execute(IExecutionContext context) {
try {
Canceled = false;
ExecutionContext = context;
foreach (IParameter param in Parameters)
param.ExecutionContext = context;
IOperation next = Apply();
OnExecuted();
return next;
}
finally {
foreach (IParameter param in Parameters)
param.ExecutionContext = null;
ExecutionContext = null;
}
}
///
/// Sets property to true.
public void Abort() {
Canceled = true;
}
///
/// Performs the current operator on the specified .
///
/// The scope where to execute the operator
/// null.
public abstract IOperation Apply();
protected virtual void OnExecutionContextChanged() { }
protected virtual void OnCanceledChanged() { }
///
public event EventHandler BreakpointChanged;
///
/// Fires a new BreakpointChanged event.
///
protected virtual void OnBreakpointChanged() {
if (BreakpointChanged != null) {
BreakpointChanged(this, EventArgs.Empty);
}
}
///
public event EventHandler Executed;
///
/// Fires a new Executed event.
///
protected virtual void OnExecuted() {
if (Executed != null) {
Executed(this, EventArgs.Empty);
}
}
}
}