[2] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[14185] | 3 | * Copyright (C) 2002-2016 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Threading;
|
---|
[3376] | 25 | using HeuristicLab.Common;
|
---|
[1823] | 26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[2] | 27 |
|
---|
| 28 | namespace 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 |
|
---|
[5193] | 44 | #region Variables for communication between threads
|
---|
| 45 | private CancellationTokenSource cancellationTokenSource;
|
---|
| 46 | private bool stopPending;
|
---|
[3262] | 47 | private DateTime lastUpdateTime;
|
---|
[5193] | 48 | #endregion
|
---|
[776] | 49 |
|
---|
[4722] | 50 | [StorableConstructor]
|
---|
[5193] | 51 | protected Engine(bool deserializing) : base(deserializing) { }
|
---|
[4722] | 52 | protected Engine(Engine original, Cloner cloner)
|
---|
| 53 | : base(original, cloner) {
|
---|
| 54 | log = cloner.Clone(original.log);
|
---|
| 55 | executionStack = new Stack<IOperation>();
|
---|
| 56 | IOperation[] contexts = original.executionStack.ToArray();
|
---|
| 57 | for (int i = contexts.Length - 1; i >= 0; i--)
|
---|
| 58 | executionStack.Push(cloner.Clone(contexts[i]));
|
---|
| 59 | }
|
---|
| 60 | protected Engine()
|
---|
| 61 | : base() {
|
---|
| 62 | log = new Log();
|
---|
| 63 | executionStack = new Stack<IOperation>();
|
---|
[3289] | 64 | }
|
---|
[2] | 65 |
|
---|
[3262] | 66 | public sealed override void Prepare() {
|
---|
| 67 | base.Prepare();
|
---|
| 68 | executionStack.Clear();
|
---|
| 69 | OnPrepared();
|
---|
| 70 | }
|
---|
[2834] | 71 | public void Prepare(IOperation initialOperation) {
|
---|
[3262] | 72 | base.Prepare();
|
---|
[2653] | 73 | executionStack.Clear();
|
---|
[2834] | 74 | if (initialOperation != null)
|
---|
| 75 | executionStack.Push(initialOperation);
|
---|
[2790] | 76 | OnPrepared();
|
---|
[2] | 77 | }
|
---|
[3289] | 78 | protected override void OnPrepared() {
|
---|
| 79 | Log.LogMessage("Engine prepared");
|
---|
| 80 | base.OnPrepared();
|
---|
| 81 | }
|
---|
| 82 |
|
---|
[15287] | 83 | public override void Start(CancellationToken cancellationToken) {
|
---|
| 84 | base.Start(cancellationToken);
|
---|
| 85 | cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
|
---|
[5193] | 86 | stopPending = false;
|
---|
[15287] | 87 |
|
---|
| 88 | try {
|
---|
| 89 | Run((object)cancellationTokenSource.Token);
|
---|
| 90 | } catch (OperationCanceledException) {
|
---|
| 91 | } catch (AggregateException ae) {
|
---|
[15367] | 92 | ae.FlattenAndHandle(new[] { typeof(OperationCanceledException) }, e => OnExceptionOccurred(e));
|
---|
[15287] | 93 | } catch (Exception e) {
|
---|
| 94 | OnExceptionOccurred(e);
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | cancellationTokenSource.Dispose();
|
---|
| 98 | cancellationTokenSource = null;
|
---|
| 99 | if (stopPending) executionStack.Clear();
|
---|
| 100 | if (executionStack.Count == 0) OnStopped();
|
---|
| 101 | else OnPaused();
|
---|
[2] | 102 | }
|
---|
[3289] | 103 | protected override void OnStarted() {
|
---|
| 104 | Log.LogMessage("Engine started");
|
---|
| 105 | base.OnStarted();
|
---|
| 106 | }
|
---|
| 107 |
|
---|
[3262] | 108 | public override void Pause() {
|
---|
| 109 | base.Pause();
|
---|
[5193] | 110 | cancellationTokenSource.Cancel();
|
---|
[2] | 111 | }
|
---|
[3289] | 112 | protected override void OnPaused() {
|
---|
| 113 | Log.LogMessage("Engine paused");
|
---|
| 114 | base.OnPaused();
|
---|
| 115 | }
|
---|
| 116 |
|
---|
[3262] | 117 | public override void Stop() {
|
---|
| 118 | base.Stop();
|
---|
[5193] | 119 | if (ExecutionState == ExecutionState.Paused) {
|
---|
| 120 | executionStack.Clear();
|
---|
| 121 | OnStopped();
|
---|
| 122 | } else {
|
---|
| 123 | stopPending = true;
|
---|
| 124 | cancellationTokenSource.Cancel();
|
---|
| 125 | }
|
---|
[2] | 126 | }
|
---|
[3289] | 127 | protected override void OnStopped() {
|
---|
| 128 | Log.LogMessage("Engine stopped");
|
---|
| 129 | base.OnStopped();
|
---|
| 130 | }
|
---|
[2] | 131 |
|
---|
[3289] | 132 | protected override void OnExceptionOccurred(Exception exception) {
|
---|
| 133 | Log.LogException(exception);
|
---|
| 134 | base.OnExceptionOccurred(exception);
|
---|
| 135 | }
|
---|
| 136 |
|
---|
[2] | 137 | private void Run(object state) {
|
---|
[5193] | 138 | CancellationToken cancellationToken = (CancellationToken)state;
|
---|
| 139 |
|
---|
[2653] | 140 | OnStarted();
|
---|
[9343] | 141 | lastUpdateTime = DateTime.UtcNow;
|
---|
[5240] | 142 | System.Timers.Timer timer = new System.Timers.Timer(250);
|
---|
[5193] | 143 | timer.AutoReset = true;
|
---|
| 144 | timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
|
---|
[3262] | 145 | timer.Start();
|
---|
[5193] | 146 | try {
|
---|
| 147 | Run(cancellationToken);
|
---|
[15287] | 148 | } finally {
|
---|
[5444] | 149 | timer.Elapsed -= new System.Timers.ElapsedEventHandler(timer_Elapsed);
|
---|
[5193] | 150 | timer.Stop();
|
---|
[9343] | 151 | ExecutionTime += DateTime.UtcNow - lastUpdateTime;
|
---|
[5193] | 152 | }
|
---|
[3262] | 153 |
|
---|
[5193] | 154 | cancellationToken.ThrowIfCancellationRequested();
|
---|
[2] | 155 | }
|
---|
[5193] | 156 | protected abstract void Run(CancellationToken cancellationToken);
|
---|
[2] | 157 |
|
---|
[3262] | 158 | private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) {
|
---|
[5240] | 159 | System.Timers.Timer timer = (System.Timers.Timer)sender;
|
---|
| 160 | timer.Enabled = false;
|
---|
[9343] | 161 | DateTime now = DateTime.UtcNow;
|
---|
[3262] | 162 | ExecutionTime += now - lastUpdateTime;
|
---|
| 163 | lastUpdateTime = now;
|
---|
[5240] | 164 | timer.Enabled = true;
|
---|
[3226] | 165 | }
|
---|
[2] | 166 | }
|
---|
| 167 | }
|
---|