1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2010 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.Drawing;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
26 |
|
---|
27 | namespace HeuristicLab.Operators {
|
---|
28 | /// <summary>
|
---|
29 | /// The base class for all operators.
|
---|
30 | /// </summary>
|
---|
31 | [Item("Operator", "Base class for operators.")]
|
---|
32 | [StorableClass]
|
---|
33 | public abstract class Operator : ParameterizedNamedItem, IOperator {
|
---|
34 | public override Image ItemImage {
|
---|
35 | get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Method; }
|
---|
36 | }
|
---|
37 | public override bool CanChangeDescription {
|
---|
38 | get { return false; }
|
---|
39 | }
|
---|
40 |
|
---|
41 | [Storable]
|
---|
42 | private IExecutionContext executionContext;
|
---|
43 | protected IExecutionContext ExecutionContext {
|
---|
44 | get { return executionContext; }
|
---|
45 | private set {
|
---|
46 | if (value != executionContext) {
|
---|
47 | executionContext = value;
|
---|
48 | OnExecutionContextChanged();
|
---|
49 | }
|
---|
50 | }
|
---|
51 | }
|
---|
52 |
|
---|
53 | /// <summary>
|
---|
54 | /// Flag whether the current instance has been canceled.
|
---|
55 | /// </summary>
|
---|
56 | private bool canceled;
|
---|
57 | /// <inheritdoc/>
|
---|
58 | protected bool Canceled {
|
---|
59 | get { return canceled; }
|
---|
60 | private set {
|
---|
61 | if (value != canceled) {
|
---|
62 | canceled = value;
|
---|
63 | OnCanceledChanged();
|
---|
64 | }
|
---|
65 | }
|
---|
66 | }
|
---|
67 |
|
---|
68 | [Storable]
|
---|
69 | private bool breakpoint;
|
---|
70 | /// <inheritdoc/>
|
---|
71 | /// <remarks>Calls <see cref="OnBreakpointChanged"/> in the setter.</remarks>
|
---|
72 | public bool Breakpoint {
|
---|
73 | get { return breakpoint; }
|
---|
74 | set {
|
---|
75 | if (value != breakpoint) {
|
---|
76 | breakpoint = value;
|
---|
77 | OnBreakpointChanged();
|
---|
78 | }
|
---|
79 | }
|
---|
80 | }
|
---|
81 |
|
---|
82 | /// <summary>
|
---|
83 | /// Initializes a new instance of <see cref="OperatorBase"/> setting the breakpoint flag and
|
---|
84 | /// the canceled flag to <c>false</c> and the name of the operator to the type name.
|
---|
85 | /// </summary>
|
---|
86 | protected Operator()
|
---|
87 | : base() {
|
---|
88 | canceled = false;
|
---|
89 | breakpoint = false;
|
---|
90 | }
|
---|
91 | protected Operator(string name)
|
---|
92 | : base(name) {
|
---|
93 | canceled = false;
|
---|
94 | breakpoint = false;
|
---|
95 | }
|
---|
96 | protected Operator(string name, ParameterCollection parameters)
|
---|
97 | : base(name, parameters) {
|
---|
98 | canceled = false;
|
---|
99 | breakpoint = false;
|
---|
100 | }
|
---|
101 | protected Operator(string name, string description)
|
---|
102 | : base(name, description) {
|
---|
103 | canceled = false;
|
---|
104 | breakpoint = false;
|
---|
105 | }
|
---|
106 | protected Operator(string name, string description, ParameterCollection parameters)
|
---|
107 | : base(name, description, parameters) {
|
---|
108 | canceled = false;
|
---|
109 | breakpoint = false;
|
---|
110 | }
|
---|
111 |
|
---|
112 | /// <summary>
|
---|
113 | /// Clones the current instance (deep clone).
|
---|
114 | /// </summary>
|
---|
115 | /// <remarks>Clones also sub operators, variables and variable infos.</remarks>
|
---|
116 | /// <param name="clonedObjects">Dictionary of all already cloned objects. (Needed to avoid cycles.)</param>
|
---|
117 | /// <returns>The cloned object as <see cref="OperatorBase"/>.</returns>
|
---|
118 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
119 | Operator clone = (Operator)base.Clone(cloner);
|
---|
120 | clone.canceled = canceled;
|
---|
121 | clone.breakpoint = breakpoint;
|
---|
122 | clone.executionContext = (IExecutionContext)cloner.Clone(executionContext);
|
---|
123 | return clone;
|
---|
124 | }
|
---|
125 |
|
---|
126 | /// <inheritdoc/>
|
---|
127 | public virtual IOperation Execute(IExecutionContext context) {
|
---|
128 | try {
|
---|
129 | Canceled = false;
|
---|
130 | ExecutionContext = context;
|
---|
131 | foreach (IParameter param in Parameters)
|
---|
132 | param.ExecutionContext = context;
|
---|
133 | IOperation next = Apply();
|
---|
134 | OnExecuted();
|
---|
135 | return next;
|
---|
136 | }
|
---|
137 | finally {
|
---|
138 | foreach (IParameter param in Parameters)
|
---|
139 | param.ExecutionContext = null;
|
---|
140 | ExecutionContext = null;
|
---|
141 | }
|
---|
142 | }
|
---|
143 | /// <inheritdoc/>
|
---|
144 | /// <remarks>Sets property <see cref="Canceled"/> to <c>true</c>.</remarks>
|
---|
145 | public void Abort() {
|
---|
146 | Canceled = true;
|
---|
147 | }
|
---|
148 | /// <summary>
|
---|
149 | /// Performs the current operator on the specified <paramref name="scope"/>.
|
---|
150 | /// </summary>
|
---|
151 | /// <param name="scope">The scope where to execute the operator</param>
|
---|
152 | /// <returns><c>null</c>.</returns>
|
---|
153 | public abstract IOperation Apply();
|
---|
154 |
|
---|
155 | protected virtual void OnExecutionContextChanged() { }
|
---|
156 | protected virtual void OnCanceledChanged() { }
|
---|
157 | /// <inheritdoc/>
|
---|
158 | public event EventHandler BreakpointChanged;
|
---|
159 | /// <summary>
|
---|
160 | /// Fires a new <c>BreakpointChanged</c> event.
|
---|
161 | /// </summary>
|
---|
162 | protected virtual void OnBreakpointChanged() {
|
---|
163 | if (BreakpointChanged != null) {
|
---|
164 | BreakpointChanged(this, EventArgs.Empty);
|
---|
165 | }
|
---|
166 | }
|
---|
167 | /// <inheritdoc/>
|
---|
168 | public event EventHandler Executed;
|
---|
169 | /// <summary>
|
---|
170 | /// Fires a new <c>Executed</c> event.
|
---|
171 | /// </summary>
|
---|
172 | protected virtual void OnExecuted() {
|
---|
173 | if (Executed != null) {
|
---|
174 | Executed(this, EventArgs.Empty);
|
---|
175 | }
|
---|
176 | }
|
---|
177 | }
|
---|
178 | }
|
---|