Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3838 was 3376, checked in by swagner, 15 years ago

Moved interfaces and classes for deep cloning from HeuristicLab.Core to HeuristicLab.Common (#975).

File size: 4.8 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
[3262]48    protected Engine()
49      : base() {
[3289]50      log = new Log();
[2834]51      executionStack = new Stack<IOperation>();
[3262]52      pausePending = stopPending = false;
53      timer = new System.Timers.Timer(100);
54      timer.AutoReset = true;
55      timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
[2]56    }
[3289]57    [StorableConstructor]
58    protected Engine(bool deserializing)
59      : base(deserializing) {
60      pausePending = stopPending = false;
61      timer = new System.Timers.Timer(100);
62      timer.AutoReset = true;
63      timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
64    }
[2]65
[2653]66    public override IDeepCloneable Clone(Cloner cloner) {
[3280]67      if (ExecutionState == ExecutionState.Started) throw new InvalidOperationException(string.Format("Clone not allowed in execution state \"{0}\".", ExecutionState));
[2664]68      Engine clone = (Engine)base.Clone(cloner);
[3289]69      clone.log = (ILog)cloner.Clone(log);
[2834]70      IOperation[] contexts = executionStack.ToArray();
[2653]71      for (int i = contexts.Length - 1; i >= 0; i--)
[2834]72        clone.executionStack.Push((IOperation)cloner.Clone(contexts[i]));
[3262]73      clone.pausePending = pausePending;
74      clone.stopPending = stopPending;
[2]75      return clone;
76    }
77
[3262]78    public sealed override void Prepare() {
79      base.Prepare();
80      executionStack.Clear();
81      OnPrepared();
82    }
[2834]83    public void Prepare(IOperation initialOperation) {
[3262]84      base.Prepare();
[2653]85      executionStack.Clear();
[2834]86      if (initialOperation != null)
87        executionStack.Push(initialOperation);
[2790]88      OnPrepared();
[2]89    }
[3289]90    protected override void OnPrepared() {
91      Log.LogMessage("Engine prepared");
92      base.OnPrepared();
93    }
94
[3262]95    public override void Start() {
96      base.Start();
[2653]97      ThreadPool.QueueUserWorkItem(new WaitCallback(Run), null);
[2]98    }
[3289]99    protected override void OnStarted() {
100      Log.LogMessage("Engine started");
101      base.OnStarted();
102    }
103
[3262]104    public override void Pause() {
105      base.Pause();
106      pausePending = true;
[2]107    }
[3289]108    protected override void OnPaused() {
109      Log.LogMessage("Engine paused");
110      base.OnPaused();
111    }
112
[3262]113    public override void Stop() {
114      base.Stop();
115      stopPending = true;
116      if (ExecutionState == ExecutionState.Paused) OnStopped();
[2]117    }
[3289]118    protected override void OnStopped() {
119      Log.LogMessage("Engine stopped");
120      base.OnStopped();
121    }
[2]122
[3289]123    protected override void OnExceptionOccurred(Exception exception) {
124      Log.LogException(exception);
125      base.OnExceptionOccurred(exception);
126    }
127
[2]128    private void Run(object state) {
[2653]129      OnStarted();
[3262]130      pausePending = stopPending = false;
131
132      lastUpdateTime = DateTime.Now;
133      timer.Start();
134      while (!pausePending && !stopPending && (executionStack.Count > 0)) {
[3288]135        ProcessNextOperation();
[2]136      }
[3262]137      timer.Stop();
138      ExecutionTime += DateTime.Now - lastUpdateTime;
139
140      if (pausePending) OnPaused();
141      else OnStopped();
[2]142    }
143
[3288]144    protected abstract void ProcessNextOperation();
[2]145
[3262]146    private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) {
147      DateTime now = DateTime.Now;
148      ExecutionTime += now - lastUpdateTime;
149      lastUpdateTime = now;
[3226]150    }
[2]151  }
152}
Note: See TracBrowser for help on using the repository browser.