Free cookie consent management tool by TermsFeed Policy Generator

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

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

Implemented logs of engines (#963).

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