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.ComponentModel;
|
---|
25 | using System.Drawing;
|
---|
26 | using System.Data;
|
---|
27 | using System.Text;
|
---|
28 | using System.Windows.Forms;
|
---|
29 | using HeuristicLab.Core;
|
---|
30 | using HeuristicLab.Common;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Core {
|
---|
33 | /// <summary>
|
---|
34 | /// Base class for editors of engines.
|
---|
35 | /// </summary>
|
---|
36 | public partial class EngineBaseEditor : EditorBase {
|
---|
37 | private int executionTimeCounter;
|
---|
38 |
|
---|
39 | /// <summary>
|
---|
40 | /// Gets or sets the current engine.
|
---|
41 | /// </summary>
|
---|
42 | /// <remarks>Uses property <see cref="ViewBase.Item"/> of base class <see cref="EditorBase"/>.</remarks>
|
---|
43 | public IEngine Engine {
|
---|
44 | get { return (IEngine)Item; }
|
---|
45 | set { base.Item = value; }
|
---|
46 | }
|
---|
47 |
|
---|
48 | /// <summary>
|
---|
49 | /// Initializes a new instance of <see cref="EngineBaseEditor"/>.
|
---|
50 | /// </summary>
|
---|
51 | public EngineBaseEditor() {
|
---|
52 | InitializeComponent();
|
---|
53 | }
|
---|
54 |
|
---|
55 | /// <summary>
|
---|
56 | /// Removes the event handlers from the underlying <see cref="IEngine"/>.
|
---|
57 | /// </summary>
|
---|
58 | /// <remarks>Calls <see cref="ViewBase.RemoveItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
|
---|
59 | protected override void RemoveItemEvents() {
|
---|
60 | Engine.Initialized -= new EventHandler(Engine_Initialized);
|
---|
61 | Engine.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Engine_ExceptionOccurred);
|
---|
62 | Engine.ExecutionTimeChanged -= new EventHandler(Engine_ExecutionTimeChanged);
|
---|
63 | Engine.Finished -= new EventHandler(Engine_Finished);
|
---|
64 | operatorGraphView.OperatorGraph = null;
|
---|
65 | scopeView.Scope = null;
|
---|
66 | base.RemoveItemEvents();
|
---|
67 | }
|
---|
68 | /// <summary>
|
---|
69 | /// Adds event handlers to the underlying <see cref="IEngine"/>.
|
---|
70 | /// </summary>
|
---|
71 | /// <remarks>Calls <see cref="ViewBase.AddItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
|
---|
72 | protected override void AddItemEvents() {
|
---|
73 | base.AddItemEvents();
|
---|
74 | Engine.Initialized += new EventHandler(Engine_Initialized);
|
---|
75 | Engine.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Engine_ExceptionOccurred);
|
---|
76 | Engine.ExecutionTimeChanged += new EventHandler(Engine_ExecutionTimeChanged);
|
---|
77 | Engine.Finished += new EventHandler(Engine_Finished);
|
---|
78 | operatorGraphView.OperatorGraph = Engine.OperatorGraph;
|
---|
79 | scopeView.Scope = Engine.GlobalScope;
|
---|
80 | }
|
---|
81 |
|
---|
82 | /// <summary>
|
---|
83 | /// Updates all controls with the latest data of the model.
|
---|
84 | /// </summary>
|
---|
85 | /// <remarks>Calls <see cref="EditorBase.UpdateControls"/> of base class <see cref="EditorBase"/>.</remarks>
|
---|
86 | protected override void UpdateControls() {
|
---|
87 | base.UpdateControls();
|
---|
88 | abortButton.Enabled = false;
|
---|
89 | if (Engine == null) {
|
---|
90 | executeButton.Enabled = false;
|
---|
91 | resetButton.Enabled = false;
|
---|
92 | executionTimeTextBox.Enabled = false;
|
---|
93 | } else {
|
---|
94 | executeButton.Enabled = true;
|
---|
95 | resetButton.Enabled = true;
|
---|
96 | executionTimeCounter = 0;
|
---|
97 | executionTimeTextBox.Text = Engine.ExecutionTime.ToString();
|
---|
98 | executionTimeTextBox.Enabled = true;
|
---|
99 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 | #region Engine Events
|
---|
103 | private void Engine_Initialized(object sender, EventArgs e) {
|
---|
104 | Refresh();
|
---|
105 | }
|
---|
106 | private delegate void OnExceptionEventDelegate(object sender, EventArgs<Exception> e);
|
---|
107 | private void Engine_ExceptionOccurred(object sender, EventArgs<Exception> e) {
|
---|
108 | if (InvokeRequired)
|
---|
109 | Invoke(new OnExceptionEventDelegate(Engine_ExceptionOccurred), sender, e);
|
---|
110 | else
|
---|
111 | Auxiliary.ShowErrorMessageBox(e.Value);
|
---|
112 | }
|
---|
113 | private void Engine_ExecutionTimeChanged(object sender, EventArgs e) {
|
---|
114 | executionTimeCounter++;
|
---|
115 | if (executionTimeCounter == 1000)
|
---|
116 | UpdateExecutionTimeTextBox();
|
---|
117 | }
|
---|
118 | private void Engine_Finished(object sender, EventArgs e) {
|
---|
119 | UpdateExecutionTimeTextBox();
|
---|
120 | if (globalScopeGroupBox.Controls.Count > 0) {
|
---|
121 | ScopeView scopeEditor = (ScopeView)globalScopeGroupBox.Controls[0];
|
---|
122 | if (!scopeEditor.AutomaticUpdating)
|
---|
123 | scopeEditor.Refresh();
|
---|
124 | }
|
---|
125 | EnableDisableControls();
|
---|
126 | }
|
---|
127 | private void UpdateExecutionTimeTextBox() {
|
---|
128 | if (InvokeRequired)
|
---|
129 | Invoke(new MethodInvoker(UpdateExecutionTimeTextBox));
|
---|
130 | else {
|
---|
131 | executionTimeCounter = 0;
|
---|
132 | executionTimeTextBox.Text = Engine.ExecutionTime.ToString();
|
---|
133 | }
|
---|
134 | }
|
---|
135 | private void EnableDisableControls() {
|
---|
136 | if (InvokeRequired)
|
---|
137 | Invoke(new MethodInvoker(EnableDisableControls));
|
---|
138 | else {
|
---|
139 | executeButton.Enabled = true;
|
---|
140 | abortButton.Enabled = false;
|
---|
141 | resetButton.Enabled = true;
|
---|
142 | operatorGraphGroupBox.Enabled = true;
|
---|
143 | globalScopeGroupBox.Enabled = true;
|
---|
144 | }
|
---|
145 | }
|
---|
146 | #endregion
|
---|
147 |
|
---|
148 | #region Button events
|
---|
149 | private void executeButton_Click(object sender, EventArgs e) {
|
---|
150 | executeButton.Enabled = false;
|
---|
151 | abortButton.Enabled = true;
|
---|
152 | resetButton.Enabled = false;
|
---|
153 | operatorGraphGroupBox.Enabled = false;
|
---|
154 | globalScopeGroupBox.Enabled = false;
|
---|
155 | Engine.Execute();
|
---|
156 | }
|
---|
157 | private void abortButton_Click(object sender, EventArgs e) {
|
---|
158 | Engine.Abort();
|
---|
159 | }
|
---|
160 | private void resetButton_Click(object sender, EventArgs e) {
|
---|
161 | Engine.Reset();
|
---|
162 | UpdateExecutionTimeTextBox();
|
---|
163 | if (globalScopeGroupBox.Controls.Count > 0) {
|
---|
164 | ScopeView scopeEditor = (ScopeView)globalScopeGroupBox.Controls[0];
|
---|
165 | if (!scopeEditor.AutomaticUpdating)
|
---|
166 | scopeEditor.Refresh();
|
---|
167 | }
|
---|
168 | }
|
---|
169 | #endregion
|
---|
170 | }
|
---|
171 | }
|
---|