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