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 | if (ExecutionState == ExecutionState.Started) throw new InvalidOperationException(string.Format("Clone not allowed in execution state \"{0}\".", ExecutionState));
|
---|
52 | Engine clone = (Engine)base.Clone(cloner);
|
---|
53 | IOperation[] contexts = executionStack.ToArray();
|
---|
54 | for (int i = contexts.Length - 1; i >= 0; i--)
|
---|
55 | clone.executionStack.Push((IOperation)cloner.Clone(contexts[i]));
|
---|
56 | clone.pausePending = pausePending;
|
---|
57 | clone.stopPending = stopPending;
|
---|
58 | return clone;
|
---|
59 | }
|
---|
60 |
|
---|
61 | public sealed override void Prepare() {
|
---|
62 | base.Prepare();
|
---|
63 | executionStack.Clear();
|
---|
64 | OnPrepared();
|
---|
65 | }
|
---|
66 | public void Prepare(IOperation initialOperation) {
|
---|
67 | base.Prepare();
|
---|
68 | executionStack.Clear();
|
---|
69 | if (initialOperation != null)
|
---|
70 | executionStack.Push(initialOperation);
|
---|
71 | OnPrepared();
|
---|
72 | }
|
---|
73 | public override void Start() {
|
---|
74 | base.Start();
|
---|
75 | ThreadPool.QueueUserWorkItem(new WaitCallback(Run), null);
|
---|
76 | }
|
---|
77 | public override void Pause() {
|
---|
78 | base.Pause();
|
---|
79 | pausePending = true;
|
---|
80 | }
|
---|
81 | public override void Stop() {
|
---|
82 | base.Stop();
|
---|
83 | stopPending = true;
|
---|
84 | if (ExecutionState == ExecutionState.Paused) OnStopped();
|
---|
85 | }
|
---|
86 |
|
---|
87 | private void Run(object state) {
|
---|
88 | OnStarted();
|
---|
89 | pausePending = stopPending = false;
|
---|
90 |
|
---|
91 | lastUpdateTime = DateTime.Now;
|
---|
92 | timer.Start();
|
---|
93 | while (!pausePending && !stopPending && (executionStack.Count > 0)) {
|
---|
94 | ProcessNextOperator();
|
---|
95 | }
|
---|
96 | timer.Stop();
|
---|
97 | ExecutionTime += DateTime.Now - lastUpdateTime;
|
---|
98 |
|
---|
99 | if (pausePending) OnPaused();
|
---|
100 | else OnStopped();
|
---|
101 | }
|
---|
102 |
|
---|
103 | protected abstract void ProcessNextOperator();
|
---|
104 |
|
---|
105 | private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) {
|
---|
106 | DateTime now = DateTime.Now;
|
---|
107 | ExecutionTime += now - lastUpdateTime;
|
---|
108 | lastUpdateTime = now;
|
---|
109 | }
|
---|
110 | }
|
---|
111 | }
|
---|