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 | /// Base class for 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 | private bool canceled;
|
---|
46 | protected bool Canceled {
|
---|
47 | get { return canceled; }
|
---|
48 | private set {
|
---|
49 | if (value != canceled) {
|
---|
50 | canceled = value;
|
---|
51 | OnCanceledChanged();
|
---|
52 | }
|
---|
53 | }
|
---|
54 | }
|
---|
55 |
|
---|
56 | [Storable]
|
---|
57 | private bool breakpoint;
|
---|
58 | public bool Breakpoint {
|
---|
59 | get { return breakpoint; }
|
---|
60 | set {
|
---|
61 | if (value != breakpoint) {
|
---|
62 | breakpoint = value;
|
---|
63 | OnBreakpointChanged();
|
---|
64 | OnItemImageChanged();
|
---|
65 | }
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 | [StorableConstructor]
|
---|
70 | protected Operator(bool deserializing) : base(deserializing) { }
|
---|
71 | protected Operator(Operator original, Cloner cloner)
|
---|
72 | : base(original, cloner) {
|
---|
73 | this.canceled = original.canceled;
|
---|
74 | this.breakpoint = original.breakpoint;
|
---|
75 | }
|
---|
76 | protected Operator()
|
---|
77 | : base() {
|
---|
78 | canceled = false;
|
---|
79 | breakpoint = false;
|
---|
80 | }
|
---|
81 | protected Operator(string name)
|
---|
82 | : base(name) {
|
---|
83 | canceled = false;
|
---|
84 | breakpoint = false;
|
---|
85 | }
|
---|
86 | protected Operator(string name, ParameterCollection parameters)
|
---|
87 | : base(name, parameters) {
|
---|
88 | canceled = false;
|
---|
89 | breakpoint = false;
|
---|
90 | }
|
---|
91 | protected Operator(string name, string description)
|
---|
92 | : base(name, description) {
|
---|
93 | canceled = false;
|
---|
94 | breakpoint = false;
|
---|
95 | }
|
---|
96 | protected Operator(string name, string description, ParameterCollection parameters)
|
---|
97 | : base(name, description, parameters) {
|
---|
98 | canceled = false;
|
---|
99 | breakpoint = false;
|
---|
100 | }
|
---|
101 |
|
---|
102 | public virtual IOperation Execute(IExecutionContext context) {
|
---|
103 | try {
|
---|
104 | Canceled = false;
|
---|
105 | foreach (IParameter param in Parameters)
|
---|
106 | param.ExecutionContext = context;
|
---|
107 | IOperation next = Apply(context);
|
---|
108 | OnExecuted();
|
---|
109 | return next;
|
---|
110 | }
|
---|
111 | finally {
|
---|
112 | foreach (IParameter param in Parameters)
|
---|
113 | param.ExecutionContext = null;
|
---|
114 | }
|
---|
115 | }
|
---|
116 | public void Abort() {
|
---|
117 | Canceled = true;
|
---|
118 | }
|
---|
119 | public abstract IOperation Apply(IExecutionContext context);
|
---|
120 |
|
---|
121 | protected virtual void OnCanceledChanged() { }
|
---|
122 | public event EventHandler BreakpointChanged;
|
---|
123 | protected virtual void OnBreakpointChanged() {
|
---|
124 | if (BreakpointChanged != null) {
|
---|
125 | BreakpointChanged(this, EventArgs.Empty);
|
---|
126 | }
|
---|
127 | }
|
---|
128 | public event EventHandler Executed;
|
---|
129 | protected virtual void OnExecuted() {
|
---|
130 | if (Executed != null) {
|
---|
131 | Executed(this, EventArgs.Empty);
|
---|
132 | }
|
---|
133 | }
|
---|
134 | }
|
---|
135 | }
|
---|