#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.Persistence.Default.Decomposers.Storable; namespace HeuristicLab.Core { /// /// The base class for all operators. /// public abstract class OperatorBase : ConstrainedItemBase, IOperator { [Storable] 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; } } [Storable] private bool myBreakpoint; /// /// Calls in the setter. public bool Breakpoint { get { return myBreakpoint; } set { if (value != myBreakpoint) { myBreakpoint = value; OnBreakpointChanged(); } } } [Storable] private List mySubOperators; /// /// Gets a list of all suboperators. /// Returns the suboperators read-only! /// public virtual IList SubOperators { get { return mySubOperators.AsReadOnly(); } } [Storable] private Dictionary myVariableInfos; /// public virtual ICollection VariableInfos { get { return myVariableInfos.Values; } } private Dictionary myVariables; /// public virtual ICollection Variables { get { return myVariables.Values; } } [Storable(Name="Variables")] private List VariablePersistence { get { return new List(myVariables.Values); } set { foreach (IVariable var in value) { AddVariable(var); } } } /// /// 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(); myVariableInfos = new Dictionary(); myVariables = 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.myVariableInfos.Clear(); foreach (IVariableInfo variableInfo in myVariableInfos.Values) clone.AddVariableInfo((IVariableInfo)Auxiliary.Clone(variableInfo, clonedObjects)); clone.myVariables.Clear(); foreach (IVariable variable in myVariables.Values) clone.AddVariable((IVariable)Auxiliary.Clone(variable, 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 bool TryAddSubOperator(IOperator subOperator) { mySubOperators.Add(subOperator); if (IsValid()) { OnSubOperatorAdded(subOperator, mySubOperators.Count - 1); return true; } else { mySubOperators.RemoveAt(mySubOperators.Count - 1); return false; } } /// /// The sub operator to add. /// Calls . public virtual bool TryAddSubOperator(IOperator subOperator, out ICollection violatedConstraints) { mySubOperators.Add(subOperator); if (IsValid(out violatedConstraints)) { OnSubOperatorAdded(subOperator, mySubOperators.Count - 1); return true; } else { mySubOperators.RemoveAt(mySubOperators.Count - 1); return false; } } /// /// The sub operator to add. /// Calls . public virtual void AddSubOperator(IOperator subOperator, int index) { mySubOperators.Insert(index, subOperator); OnSubOperatorAdded(subOperator, index); } /// /// The sub operator to add. /// Calls . public virtual bool TryAddSubOperator(IOperator subOperator, int index) { mySubOperators.Insert(index, subOperator); if (IsValid()) { OnSubOperatorAdded(subOperator, index); return true; } else { mySubOperators.RemoveAt(index); return false; } } /// /// The sub operator to add. /// Calls . public virtual bool TryAddSubOperator(IOperator subOperator, int index, out ICollection violatedConstraints) { mySubOperators.Insert(index, subOperator); if (IsValid(out violatedConstraints)) { OnSubOperatorAdded(subOperator, index); return true; } else { mySubOperators.RemoveAt(index); return false; } } /// /// Calls . public virtual void RemoveSubOperator(int index) { IOperator op = mySubOperators[index]; mySubOperators.RemoveAt(index); OnSubOperatorRemoved(op, index); } /// /// Calls . public virtual bool TryRemoveSubOperator(int index) { IOperator op = mySubOperators[index]; mySubOperators.RemoveAt(index); if (IsValid()) { OnSubOperatorRemoved(op, index); return true; } else { mySubOperators.Insert(index, op); return false; } } /// /// Calls . public virtual bool TryRemoveSubOperator(int index, out ICollection violatedConstraints) { IOperator op = mySubOperators[index]; mySubOperators.RemoveAt(index); if (IsValid(out violatedConstraints)) { OnSubOperatorRemoved(op, index); return true; } else { mySubOperators.Insert(index, op); return false; } } #endregion #region VariableInfo Methods /// public virtual IVariableInfo GetVariableInfo(string formalName) { IVariableInfo info; if (myVariableInfos.TryGetValue(formalName, out info)) return info; else return null; } /// /// Calls . public virtual void AddVariableInfo(IVariableInfo variableInfo) { myVariableInfos.Add(variableInfo.FormalName, variableInfo); OnVariableInfoAdded(variableInfo); } /// /// Calls . public virtual bool TryAddVariableInfo(IVariableInfo variableInfo) { myVariableInfos.Add(variableInfo.FormalName, variableInfo); if (IsValid()) { OnVariableInfoAdded(variableInfo); return true; } else { myVariableInfos.Remove(variableInfo.FormalName); return false; } } /// /// Calls . public virtual bool TryAddVariableInfo(IVariableInfo variableInfo, out ICollection violatedConstraints) { myVariableInfos.Add(variableInfo.FormalName, variableInfo); if (IsValid(out violatedConstraints)) { OnVariableInfoAdded(variableInfo); return true; } else { myVariableInfos.Remove(variableInfo.FormalName); return false; } } /// /// Calls . public virtual void RemoveVariableInfo(string formalName) { IVariableInfo variableInfo; if (myVariableInfos.TryGetValue(formalName, out variableInfo)) { myVariableInfos.Remove(formalName); OnVariableInfoRemoved(variableInfo); } } /// /// Calls . public virtual bool TryRemoveVariableInfo(string formalName) { IVariableInfo variableInfo; if (myVariableInfos.TryGetValue(formalName, out variableInfo)) { myVariableInfos.Remove(formalName); if (IsValid()) { OnVariableInfoRemoved(variableInfo); return true; } else { myVariableInfos.Add(formalName, variableInfo); return false; } } return true; } /// /// Calls . public virtual bool TryRemoveVariableInfo(string formalName, out ICollection violatedConstraints) { IVariableInfo variableInfo; if (myVariableInfos.TryGetValue(formalName, out variableInfo)) { myVariableInfos.Remove(formalName); if (IsValid(out violatedConstraints)) { OnVariableInfoRemoved(variableInfo); return true; } else { myVariableInfos.Add(formalName, variableInfo); return false; } } violatedConstraints = new List(); return true; } #endregion #region Variable Methods /// public virtual IVariable GetVariable(string name) { IVariable variable; if (myVariables.TryGetValue(name, out variable)) return variable; else return null; } /// /// Calls and adds NameChanging and NameChanged /// event handlers. public virtual void AddVariable(IVariable variable) { myVariables.Add(variable.Name, variable); variable.NameChanging += new EventHandler(Variable_NameChanging); variable.NameChanged += new EventHandler(Variable_NameChanged); OnVariableAdded(variable); } /// /// Calls and adds NameChanging and NameChanged /// event handlers. public virtual bool TryAddVariable(IVariable variable) { myVariables.Add(variable.Name, variable); if (IsValid()) { variable.NameChanging += new EventHandler(Variable_NameChanging); variable.NameChanged += new EventHandler(Variable_NameChanged); OnVariableAdded(variable); return true; } else { myVariableInfos.Remove(variable.Name); return false; } } /// /// Calls and adds NameChanging and NameChanged /// event handlers. public virtual bool TryAddVariable(IVariable variable, out ICollection violatedConstraints) { myVariables.Add(variable.Name, variable); if (IsValid(out violatedConstraints)) { variable.NameChanging += new EventHandler(Variable_NameChanging); variable.NameChanged += new EventHandler(Variable_NameChanged); OnVariableAdded(variable); return true; } else { myVariableInfos.Remove(variable.Name); return false; } } /// /// Calls and removes NameChanging and NameChanged /// event handlers. public virtual void RemoveVariable(string name) { IVariable variable; if (myVariables.TryGetValue(name, out variable)) { variable.NameChanging -= new EventHandler(Variable_NameChanging); variable.NameChanged -= new EventHandler(Variable_NameChanged); myVariables.Remove(name); OnVariableRemoved(variable); } } /// /// Calls and removes NameChanging and NameChanged /// event handlers. public virtual bool TryRemoveVariable(string name) { IVariable variable; if (myVariables.TryGetValue(name, out variable)) { myVariables.Remove(name); if (IsValid()) { variable.NameChanging -= new EventHandler(Variable_NameChanging); variable.NameChanged -= new EventHandler(Variable_NameChanged); OnVariableRemoved(variable); return true; } else { myVariables.Add(name, variable); return false; } } return true; } /// /// Calls and removes NameChanging and NameChanged /// event handlers. public virtual bool TryRemoveVariable(string name, out ICollection violatedConstraints) { IVariable variable; if (myVariables.TryGetValue(name, out variable)) { myVariables.Remove(name); if (IsValid(out violatedConstraints)) { variable.NameChanging -= new EventHandler(Variable_NameChanging); variable.NameChanged -= new EventHandler(Variable_NameChanged); OnVariableRemoved(variable); return true; } else { myVariables.Add(name, variable); return false; } } violatedConstraints = new List(); return true; } private void Variable_NameChanging(object sender, NameChangingEventArgs e) { e.Cancel = myVariables.ContainsKey(e.Name); } private void Variable_NameChanged(object sender, EventArgs e) { IVariable variable = (IVariable)sender; string oldName = null; foreach (KeyValuePair element in myVariables) { if (element.Value == variable) oldName = element.Key; } myVariables.Remove(oldName); myVariables.Add(variable.Name, variable); } /// /// Calls /// with throwOnError set to false. public T GetVariableValue(string formalName, IScope scope, bool recursiveLookup) where T : class, IItem { return GetVariableValue(formalName, scope, recursiveLookup, true); } /// /// Calls /// . public T GetVariableValue(string formalName, IScope scope, bool recursiveLookup, bool throwOnError) where T : class, IItem { return (T)GetVariableValue(formalName, scope, recursiveLookup, throwOnError); } /// /// Calls /// with throwOnError set to false. public IItem GetVariableValue(string formalName, IScope scope, bool recursiveLookup) { return GetVariableValue(formalName, scope, recursiveLookup, true); } /// public virtual IItem GetVariableValue(string formalName, IScope scope, bool recursiveLookup, bool throwOnError) { IVariableInfo info = GetVariableInfo(formalName); if (info.Local) { IVariable variable; if (myVariables.TryGetValue(info.ActualName, out variable)) return variable.Value; else { if (throwOnError) throw new ArgumentException("Variable " + info.ActualName + " not found"); else return null; } } else { return scope.GetVariableValue(formalName, recursiveLookup, throwOnError); } } #endregion /// public virtual IOperation Execute(IScope scope) { myCanceled = false; foreach (IVariableInfo variableInfo in VariableInfos) scope.AddAlias(variableInfo.FormalName, variableInfo.ActualName); IOperation next = Apply(scope); foreach (IVariableInfo variableInfo in VariableInfos) scope.RemoveAlias(variableInfo.FormalName); 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(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 VariableInfoAdded; /// /// Fires a new VariableInfoAdded event. /// /// The variable info that has been added. protected virtual void OnVariableInfoAdded(IVariableInfo variableInfo) { if (VariableInfoAdded != null) VariableInfoAdded(this, new VariableInfoEventArgs(variableInfo)); } /// public event EventHandler VariableInfoRemoved; /// /// Fires a new VariableInfoRemoved event. /// /// The variable info that has been removed. protected virtual void OnVariableInfoRemoved(IVariableInfo variableInfo) { if (VariableInfoRemoved != null) VariableInfoRemoved(this, new VariableInfoEventArgs(variableInfo)); } /// public event EventHandler VariableAdded; /// /// Fires a new VariableAdded event. /// /// The variable that has been added. protected virtual void OnVariableAdded(IVariable variable) { if (VariableAdded != null) VariableAdded(this, new VariableEventArgs(variable)); } /// public event EventHandler VariableRemoved; /// /// Fires a new VariableRemoved event. /// /// The variable that has been removed protected virtual void OnVariableRemoved(IVariable variable) { if (VariableRemoved != null) VariableRemoved(this, new VariableEventArgs(variable)); } /// public event EventHandler Executed; /// /// Fires a new Executed event. /// protected virtual void OnExecuted() { if (Executed != null) { Executed(this, new EventArgs()); } } } }