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.Collections.Generic;
|
---|
24 | using System.Threading;
|
---|
25 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
26 |
|
---|
27 | namespace HeuristicLab.Core {
|
---|
28 | [Item("Engine", "A base class for engines.")]
|
---|
29 | [StorableClass]
|
---|
30 | public abstract class Engine : Executable, IEngine {
|
---|
31 | [Storable]
|
---|
32 | private Stack<IOperation> executionStack;
|
---|
33 | protected Stack<IOperation> ExecutionStack {
|
---|
34 | get { return executionStack; }
|
---|
35 | }
|
---|
36 |
|
---|
37 | private bool pausePending, stopPending;
|
---|
38 | private DateTime lastUpdateTime;
|
---|
39 | private System.Timers.Timer timer;
|
---|
40 |
|
---|
41 | protected Engine()
|
---|
42 | : base() {
|
---|
43 | executionStack = new Stack<IOperation>();
|
---|
44 | pausePending = stopPending = false;
|
---|
45 | timer = new System.Timers.Timer(100);
|
---|
46 | timer.AutoReset = true;
|
---|
47 | timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
|
---|
48 | }
|
---|
49 |
|
---|
50 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
51 | Engine clone = (Engine)base.Clone(cloner);
|
---|
52 | IOperation[] contexts = executionStack.ToArray();
|
---|
53 | for (int i = contexts.Length - 1; i >= 0; i--)
|
---|
54 | clone.executionStack.Push((IOperation)cloner.Clone(contexts[i]));
|
---|
55 | clone.pausePending = pausePending;
|
---|
56 | clone.stopPending = stopPending;
|
---|
57 | return clone;
|
---|
58 | }
|
---|
59 |
|
---|
60 | public sealed override void Prepare() {
|
---|
61 | base.Prepare();
|
---|
62 | executionStack.Clear();
|
---|
63 | OnPrepared();
|
---|
64 | }
|
---|
65 | public void Prepare(IOperation initialOperation) {
|
---|
66 | base.Prepare();
|
---|
67 | executionStack.Clear();
|
---|
68 | if (initialOperation != null)
|
---|
69 | executionStack.Push(initialOperation);
|
---|
70 | OnPrepared();
|
---|
71 | }
|
---|
72 | public override void Start() {
|
---|
73 | base.Start();
|
---|
74 | ThreadPool.QueueUserWorkItem(new WaitCallback(Run), null);
|
---|
75 | }
|
---|
76 | public override void Pause() {
|
---|
77 | base.Pause();
|
---|
78 | pausePending = true;
|
---|
79 | }
|
---|
80 | public override void Stop() {
|
---|
81 | base.Stop();
|
---|
82 | stopPending = true;
|
---|
83 | if (ExecutionState == ExecutionState.Paused) OnStopped();
|
---|
84 | }
|
---|
85 |
|
---|
86 | private void Run(object state) {
|
---|
87 | OnStarted();
|
---|
88 | pausePending = stopPending = false;
|
---|
89 |
|
---|
90 | lastUpdateTime = DateTime.Now;
|
---|
91 | timer.Start();
|
---|
92 | while (!pausePending && !stopPending && (executionStack.Count > 0)) {
|
---|
93 | ProcessNextOperator();
|
---|
94 | }
|
---|
95 | timer.Stop();
|
---|
96 | ExecutionTime += DateTime.Now - lastUpdateTime;
|
---|
97 |
|
---|
98 | if (pausePending) OnPaused();
|
---|
99 | else OnStopped();
|
---|
100 | }
|
---|
101 |
|
---|
102 | protected abstract void ProcessNextOperator();
|
---|
103 |
|
---|
104 | private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) {
|
---|
105 | DateTime now = DateTime.Now;
|
---|
106 | ExecutionTime += now - lastUpdateTime;
|
---|
107 | lastUpdateTime = now;
|
---|
108 | }
|
---|
109 | }
|
---|
110 | }
|
---|