Free cookie consent management tool by TermsFeed Policy Generator

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

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