Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ParameterBinding/HeuristicLab.Core/3.3/Engine.cs @ 12858

Last change on this file since 12858 was 4722, checked in by swagner, 14 years ago

Merged cloning refactoring branch back into trunk (#922)

File size: 4.9 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Threading;
25using HeuristicLab.Common;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.Core {
29  [Item("Engine", "A base class for engines.")]
30  [StorableClass]
31  public abstract class Engine : Executable, IEngine {
32    [Storable]
33    protected ILog log;
34    public ILog Log {
35      get { return log; }
36    }
37
38    [Storable]
39    private Stack<IOperation> executionStack;
40    protected Stack<IOperation> ExecutionStack {
41      get { return executionStack; }
42    }
43
44    private bool pausePending, stopPending;
45    private DateTime lastUpdateTime;
46    private System.Timers.Timer timer;
47
48    [StorableConstructor]
49    protected Engine(bool deserializing)
50      : base(deserializing) {
51      pausePending = stopPending = false;
52      timer = new System.Timers.Timer(100);
53      timer.AutoReset = true;
54      timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
55    }
56    protected Engine(Engine original, Cloner cloner)
57      : base(original, cloner) {
58      if (original.ExecutionState == ExecutionState.Started) throw new InvalidOperationException(string.Format("Clone not allowed in execution state \"{0}\".", ExecutionState));
59      log = cloner.Clone(original.log);
60      executionStack = new Stack<IOperation>();
61      IOperation[] contexts = original.executionStack.ToArray();
62      for (int i = contexts.Length - 1; i >= 0; i--)
63        executionStack.Push(cloner.Clone(contexts[i]));
64      pausePending = original.pausePending;
65      stopPending = original.stopPending;
66      timer = new System.Timers.Timer(100);
67      timer.AutoReset = true;
68      timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
69    }
70    protected Engine()
71      : base() {
72      log = new Log();
73      executionStack = new Stack<IOperation>();
74      pausePending = stopPending = false;
75      timer = new System.Timers.Timer(100);
76      timer.AutoReset = true;
77      timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
78    }
79
80    public sealed override void Prepare() {
81      base.Prepare();
82      executionStack.Clear();
83      OnPrepared();
84    }
85    public void Prepare(IOperation initialOperation) {
86      base.Prepare();
87      executionStack.Clear();
88      if (initialOperation != null)
89        executionStack.Push(initialOperation);
90      OnPrepared();
91    }
92    protected override void OnPrepared() {
93      Log.LogMessage("Engine prepared");
94      base.OnPrepared();
95    }
96
97    public override void Start() {
98      base.Start();
99      ThreadPool.QueueUserWorkItem(new WaitCallback(Run), null);
100    }
101    protected override void OnStarted() {
102      Log.LogMessage("Engine started");
103      base.OnStarted();
104    }
105
106    public override void Pause() {
107      base.Pause();
108      pausePending = true;
109    }
110    protected override void OnPaused() {
111      Log.LogMessage("Engine paused");
112      base.OnPaused();
113    }
114
115    public override void Stop() {
116      base.Stop();
117      stopPending = true;
118      if (ExecutionState == ExecutionState.Paused) OnStopped();
119    }
120    protected override void OnStopped() {
121      Log.LogMessage("Engine stopped");
122      base.OnStopped();
123    }
124
125    protected override void OnExceptionOccurred(Exception exception) {
126      Log.LogException(exception);
127      base.OnExceptionOccurred(exception);
128    }
129
130    private void Run(object state) {
131      OnStarted();
132      pausePending = stopPending = false;
133
134      lastUpdateTime = DateTime.Now;
135      timer.Start();
136      while (!pausePending && !stopPending && (executionStack.Count > 0)) {
137        ProcessNextOperation();
138      }
139      timer.Stop();
140      ExecutionTime += DateTime.Now - lastUpdateTime;
141
142      if (pausePending) OnPaused();
143      else OnStopped();
144    }
145
146    protected abstract void ProcessNextOperation();
147
148    private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) {
149      DateTime now = DateTime.Now;
150      ExecutionTime += now - lastUpdateTime;
151      lastUpdateTime = now;
152    }
153  }
154}
Note: See TracBrowser for help on using the repository browser.