Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core/3.3/Engine.cs @ 5010

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

Merged cloning refactoring branch back into trunk (#922)

File size: 4.9 KB
RevLine 
[2]1#region License Information
2/* HeuristicLab
[2790]3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2]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;
[3376]25using HeuristicLab.Common;
[1823]26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[2]27
28namespace HeuristicLab.Core {
[2664]29  [Item("Engine", "A base class for engines.")]
[3017]30  [StorableClass]
[3262]31  public abstract class Engine : Executable, IEngine {
[2653]32    [Storable]
[3289]33    protected ILog log;
34    public ILog Log {
35      get { return log; }
36    }
37
38    [Storable]
[2834]39    private Stack<IOperation> executionStack;
40    protected Stack<IOperation> ExecutionStack {
[2653]41      get { return executionStack; }
[2]42    }
[1667]43
[3262]44    private bool pausePending, stopPending;
45    private DateTime lastUpdateTime;
46    private System.Timers.Timer timer;
[776]47
[4722]48    [StorableConstructor]
49    protected Engine(bool deserializing)
50      : base(deserializing) {
[3262]51      pausePending = stopPending = false;
52      timer = new System.Timers.Timer(100);
53      timer.AutoReset = true;
54      timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
[2]55    }
[4722]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>();
[3289]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    }
[2]79
[3262]80    public sealed override void Prepare() {
81      base.Prepare();
82      executionStack.Clear();
83      OnPrepared();
84    }
[2834]85    public void Prepare(IOperation initialOperation) {
[3262]86      base.Prepare();
[2653]87      executionStack.Clear();
[2834]88      if (initialOperation != null)
89        executionStack.Push(initialOperation);
[2790]90      OnPrepared();
[2]91    }
[3289]92    protected override void OnPrepared() {
93      Log.LogMessage("Engine prepared");
94      base.OnPrepared();
95    }
96
[3262]97    public override void Start() {
98      base.Start();
[2653]99      ThreadPool.QueueUserWorkItem(new WaitCallback(Run), null);
[2]100    }
[3289]101    protected override void OnStarted() {
102      Log.LogMessage("Engine started");
103      base.OnStarted();
104    }
105
[3262]106    public override void Pause() {
107      base.Pause();
108      pausePending = true;
[2]109    }
[3289]110    protected override void OnPaused() {
111      Log.LogMessage("Engine paused");
112      base.OnPaused();
113    }
114
[3262]115    public override void Stop() {
116      base.Stop();
117      stopPending = true;
118      if (ExecutionState == ExecutionState.Paused) OnStopped();
[2]119    }
[3289]120    protected override void OnStopped() {
121      Log.LogMessage("Engine stopped");
122      base.OnStopped();
123    }
[2]124
[3289]125    protected override void OnExceptionOccurred(Exception exception) {
126      Log.LogException(exception);
127      base.OnExceptionOccurred(exception);
128    }
129
[2]130    private void Run(object state) {
[2653]131      OnStarted();
[3262]132      pausePending = stopPending = false;
133
134      lastUpdateTime = DateTime.Now;
135      timer.Start();
136      while (!pausePending && !stopPending && (executionStack.Count > 0)) {
[3288]137        ProcessNextOperation();
[2]138      }
[3262]139      timer.Stop();
140      ExecutionTime += DateTime.Now - lastUpdateTime;
141
142      if (pausePending) OnPaused();
143      else OnStopped();
[2]144    }
145
[3288]146    protected abstract void ProcessNextOperation();
[2]147
[3262]148    private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) {
149      DateTime now = DateTime.Now;
150      ExecutionTime += now - lastUpdateTime;
151      lastUpdateTime = now;
[3226]152    }
[2]153  }
154}
Note: See TracBrowser for help on using the repository browser.