[2] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
| 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Text;
|
---|
| 25 | using System.Xml;
|
---|
[2653] | 26 | using System.Drawing;
|
---|
[1823] | 27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[2663] | 28 | using HeuristicLab.Core;
|
---|
[2653] | 29 | using HeuristicLab.Collections;
|
---|
[2] | 30 |
|
---|
[2663] | 31 | namespace HeuristicLab.Operators {
|
---|
[776] | 32 | /// <summary>
|
---|
| 33 | /// The base class for all operators.
|
---|
| 34 | /// </summary>
|
---|
[2664] | 35 | [Item("Operator", "Base class for operators.")]
|
---|
| 36 | public abstract class Operator : NamedItem, IOperator {
|
---|
[2653] | 37 | public override Image ItemImage {
|
---|
| 38 | get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Method; }
|
---|
| 39 | }
|
---|
[2684] | 40 | public override bool CanChangeDescription {
|
---|
| 41 | get { return false; }
|
---|
| 42 | }
|
---|
[1667] | 43 |
|
---|
[2653] | 44 | private ParameterCollection parameters;
|
---|
[1667] | 45 | [Storable]
|
---|
[2653] | 46 | protected ParameterCollection Parameters {
|
---|
| 47 | get { return parameters;}
|
---|
| 48 | private set {
|
---|
| 49 | if (parameters != null) parameters.Changed -= new ChangedEventHandler(Parameters_Changed);
|
---|
| 50 | parameters = value;
|
---|
| 51 | readOnlyParameters = null;
|
---|
| 52 | if (parameters != null) parameters.Changed += new ChangedEventHandler(Parameters_Changed);
|
---|
[2] | 53 | }
|
---|
| 54 | }
|
---|
[2653] | 55 | private ReadOnlyObservableKeyedCollection<string, IParameter> readOnlyParameters;
|
---|
| 56 | IObservableKeyedCollection<string, IParameter> IOperator.Parameters {
|
---|
| 57 | get {
|
---|
| 58 | if (readOnlyParameters == null) readOnlyParameters = parameters.AsReadOnly();
|
---|
| 59 | return readOnlyParameters;
|
---|
| 60 | }
|
---|
[2] | 61 | }
|
---|
[2653] | 62 |
|
---|
[776] | 63 | /// <summary>
|
---|
| 64 | /// Flag whether the current instance has been canceled.
|
---|
| 65 | /// </summary>
|
---|
[2653] | 66 | private bool canceled;
|
---|
[776] | 67 | /// <inheritdoc/>
|
---|
[2653] | 68 | protected bool Canceled {
|
---|
| 69 | get { return canceled; }
|
---|
[2] | 70 | }
|
---|
[1667] | 71 |
|
---|
| 72 | [Storable]
|
---|
[2653] | 73 | private bool breakpoint;
|
---|
[776] | 74 | /// <inheritdoc/>
|
---|
| 75 | /// <remarks>Calls <see cref="OnBreakpointChanged"/> in the setter.</remarks>
|
---|
[2] | 76 | public bool Breakpoint {
|
---|
[2653] | 77 | get { return breakpoint; }
|
---|
[2] | 78 | set {
|
---|
[2653] | 79 | if (value != breakpoint) {
|
---|
| 80 | breakpoint = value;
|
---|
[2] | 81 | OnBreakpointChanged();
|
---|
| 82 | }
|
---|
| 83 | }
|
---|
| 84 | }
|
---|
| 85 |
|
---|
[776] | 86 | /// <summary>
|
---|
| 87 | /// Initializes a new instance of <see cref="OperatorBase"/> setting the breakpoint flag and
|
---|
| 88 | /// the canceled flag to <c>false</c> and the name of the operator to the type name.
|
---|
| 89 | /// </summary>
|
---|
[2664] | 90 | protected Operator() {
|
---|
[2653] | 91 | name = ItemName;
|
---|
| 92 | Parameters = new ParameterCollection();
|
---|
| 93 | readOnlyParameters = null;
|
---|
| 94 | canceled = false;
|
---|
| 95 | breakpoint = false;
|
---|
[2] | 96 | }
|
---|
| 97 |
|
---|
[776] | 98 | /// <summary>
|
---|
| 99 | /// Clones the current instance (deep clone).
|
---|
| 100 | /// </summary>
|
---|
| 101 | /// <remarks>Clones also sub operators, variables and variable infos.</remarks>
|
---|
| 102 | /// <param name="clonedObjects">Dictionary of all already cloned objects. (Needed to avoid cycles.)</param>
|
---|
| 103 | /// <returns>The cloned object as <see cref="OperatorBase"/>.</returns>
|
---|
[2653] | 104 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[2664] | 105 | Operator clone = (Operator)base.Clone(cloner);
|
---|
[2653] | 106 | clone.Parameters = (ParameterCollection)cloner.Clone(parameters);
|
---|
| 107 | clone.canceled = canceled;
|
---|
| 108 | clone.breakpoint = breakpoint;
|
---|
[2] | 109 | return clone;
|
---|
| 110 | }
|
---|
| 111 |
|
---|
[776] | 112 | /// <inheritdoc/>
|
---|
[2653] | 113 | public virtual ExecutionContextCollection Execute(ExecutionContext context) {
|
---|
| 114 | canceled = false;
|
---|
| 115 | ExecutionContextCollection next = Apply(context);
|
---|
[2] | 116 | OnExecuted();
|
---|
| 117 | return next;
|
---|
| 118 | }
|
---|
[776] | 119 | /// <inheritdoc/>
|
---|
| 120 | /// <remarks>Sets property <see cref="Canceled"/> to <c>true</c>.</remarks>
|
---|
[2] | 121 | public virtual void Abort() {
|
---|
[2653] | 122 | canceled = true;
|
---|
[2] | 123 | }
|
---|
[776] | 124 | /// <summary>
|
---|
| 125 | /// Performs the current operator on the specified <paramref name="scope"/>.
|
---|
| 126 | /// </summary>
|
---|
| 127 | /// <param name="scope">The scope where to execute the operator</param>
|
---|
| 128 | /// <returns><c>null</c>.</returns>
|
---|
[2653] | 129 | public virtual ExecutionContextCollection Apply(ExecutionContext context) {
|
---|
| 130 | return new ExecutionContextCollection();
|
---|
[2] | 131 | }
|
---|
[2653] | 132 |
|
---|
[776] | 133 | /// <inheritdoc/>
|
---|
[2] | 134 | public event EventHandler BreakpointChanged;
|
---|
[776] | 135 | /// <summary>
|
---|
| 136 | /// Fires a new <c>BreakpointChanged</c> event.
|
---|
| 137 | /// </summary>
|
---|
[2] | 138 | protected virtual void OnBreakpointChanged() {
|
---|
| 139 | if (BreakpointChanged != null) {
|
---|
| 140 | BreakpointChanged(this, new EventArgs());
|
---|
| 141 | }
|
---|
[2653] | 142 | OnChanged();
|
---|
[2] | 143 | }
|
---|
[776] | 144 | /// <inheritdoc/>
|
---|
[2] | 145 | public event EventHandler Executed;
|
---|
[776] | 146 | /// <summary>
|
---|
| 147 | /// Fires a new <c>Executed</c> event.
|
---|
| 148 | /// </summary>
|
---|
[2] | 149 | protected virtual void OnExecuted() {
|
---|
| 150 | if (Executed != null) {
|
---|
| 151 | Executed(this, new EventArgs());
|
---|
| 152 | }
|
---|
| 153 | }
|
---|
[2653] | 154 |
|
---|
| 155 | private void Parameters_Changed(object sender, ChangedEventArgs e) {
|
---|
| 156 | OnChanged(e);
|
---|
| 157 | }
|
---|
[2] | 158 | }
|
---|
| 159 | }
|
---|