[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;
|
---|
[5193] | 25 | using System.Threading.Tasks;
|
---|
[3376] | 26 | using HeuristicLab.Common;
|
---|
[14927] | 27 | using HeuristicLab.Persistence;
|
---|
[2] | 28 |
|
---|
| 29 | namespace HeuristicLab.Core {
|
---|
[2664] | 30 | [Item("Engine", "A base class for engines.")]
|
---|
[14927] | 31 | [StorableType("fd2385ca-79e8-496e-bd12-4925d4b3cd1a")]
|
---|
[3262] | 32 | public abstract class Engine : Executable, IEngine {
|
---|
[2653] | 33 | [Storable]
|
---|
[3289] | 34 | protected ILog log;
|
---|
| 35 | public ILog Log {
|
---|
| 36 | get { return log; }
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | [Storable]
|
---|
[2834] | 40 | private Stack<IOperation> executionStack;
|
---|
| 41 | protected Stack<IOperation> ExecutionStack {
|
---|
[2653] | 42 | get { return executionStack; }
|
---|
[2] | 43 | }
|
---|
[1667] | 44 |
|
---|
[5193] | 45 | #region Variables for communication between threads
|
---|
| 46 | private CancellationTokenSource cancellationTokenSource;
|
---|
| 47 | private bool stopPending;
|
---|
[3262] | 48 | private DateTime lastUpdateTime;
|
---|
[5193] | 49 | #endregion
|
---|
[776] | 50 |
|
---|
[4722] | 51 | [StorableConstructor]
|
---|
[15018] | 52 | protected Engine(StorableConstructorFlag deserializing) : base(deserializing) { }
|
---|
[4722] | 53 | protected Engine(Engine original, Cloner cloner)
|
---|
| 54 | : base(original, cloner) {
|
---|
| 55 | log = cloner.Clone(original.log);
|
---|
| 56 | executionStack = new Stack<IOperation>();
|
---|
| 57 | IOperation[] contexts = original.executionStack.ToArray();
|
---|
| 58 | for (int i = contexts.Length - 1; i >= 0; i--)
|
---|
| 59 | executionStack.Push(cloner.Clone(contexts[i]));
|
---|
| 60 | }
|
---|
| 61 | protected Engine()
|
---|
| 62 | : base() {
|
---|
| 63 | log = new Log();
|
---|
| 64 | executionStack = new Stack<IOperation>();
|
---|
[3289] | 65 | }
|
---|
[2] | 66 |
|
---|
[3262] | 67 | public sealed override void Prepare() {
|
---|
| 68 | base.Prepare();
|
---|
| 69 | executionStack.Clear();
|
---|
| 70 | OnPrepared();
|
---|
| 71 | }
|
---|
[2834] | 72 | public void Prepare(IOperation initialOperation) {
|
---|
[3262] | 73 | base.Prepare();
|
---|
[2653] | 74 | executionStack.Clear();
|
---|
[2834] | 75 | if (initialOperation != null)
|
---|
| 76 | executionStack.Push(initialOperation);
|
---|
[2790] | 77 | OnPrepared();
|
---|
[2] | 78 | }
|
---|
[3289] | 79 | protected override void OnPrepared() {
|
---|
| 80 | Log.LogMessage("Engine prepared");
|
---|
| 81 | base.OnPrepared();
|
---|
| 82 | }
|
---|
| 83 |
|
---|
[3262] | 84 | public override void Start() {
|
---|
| 85 | base.Start();
|
---|
[5193] | 86 | cancellationTokenSource = new CancellationTokenSource();
|
---|
| 87 | stopPending = false;
|
---|
| 88 | Task task = Task.Factory.StartNew(Run, cancellationTokenSource.Token, cancellationTokenSource.Token);
|
---|
| 89 | task.ContinueWith(t => {
|
---|
| 90 | try {
|
---|
| 91 | t.Wait();
|
---|
[14927] | 92 | } catch (AggregateException ex) {
|
---|
[5193] | 93 | try {
|
---|
| 94 | ex.Flatten().Handle(x => x is OperationCanceledException);
|
---|
[14927] | 95 | } catch (AggregateException remaining) {
|
---|
[5193] | 96 | if (remaining.InnerExceptions.Count == 1) OnExceptionOccurred(remaining.InnerExceptions[0]);
|
---|
| 97 | else OnExceptionOccurred(remaining);
|
---|
| 98 | }
|
---|
| 99 | }
|
---|
| 100 | cancellationTokenSource.Dispose();
|
---|
| 101 | cancellationTokenSource = null;
|
---|
| 102 | if (stopPending) executionStack.Clear();
|
---|
| 103 | if (executionStack.Count == 0) OnStopped();
|
---|
| 104 | else OnPaused();
|
---|
| 105 | });
|
---|
[2] | 106 | }
|
---|
[3289] | 107 | protected override void OnStarted() {
|
---|
| 108 | Log.LogMessage("Engine started");
|
---|
| 109 | base.OnStarted();
|
---|
| 110 | }
|
---|
| 111 |
|
---|
[3262] | 112 | public override void Pause() {
|
---|
| 113 | base.Pause();
|
---|
[5193] | 114 | cancellationTokenSource.Cancel();
|
---|
[2] | 115 | }
|
---|
[3289] | 116 | protected override void OnPaused() {
|
---|
| 117 | Log.LogMessage("Engine paused");
|
---|
| 118 | base.OnPaused();
|
---|
| 119 | }
|
---|
| 120 |
|
---|
[3262] | 121 | public override void Stop() {
|
---|
| 122 | base.Stop();
|
---|
[5193] | 123 | if (ExecutionState == ExecutionState.Paused) {
|
---|
| 124 | executionStack.Clear();
|
---|
| 125 | OnStopped();
|
---|
| 126 | } else {
|
---|
| 127 | stopPending = true;
|
---|
| 128 | cancellationTokenSource.Cancel();
|
---|
| 129 | }
|
---|
[2] | 130 | }
|
---|
[3289] | 131 | protected override void OnStopped() {
|
---|
| 132 | Log.LogMessage("Engine stopped");
|
---|
| 133 | base.OnStopped();
|
---|
| 134 | }
|
---|
[2] | 135 |
|
---|
[3289] | 136 | protected override void OnExceptionOccurred(Exception exception) {
|
---|
| 137 | Log.LogException(exception);
|
---|
| 138 | base.OnExceptionOccurred(exception);
|
---|
| 139 | }
|
---|
| 140 |
|
---|
[2] | 141 | private void Run(object state) {
|
---|
[5193] | 142 | CancellationToken cancellationToken = (CancellationToken)state;
|
---|
| 143 |
|
---|
[2653] | 144 | OnStarted();
|
---|
[9343] | 145 | lastUpdateTime = DateTime.UtcNow;
|
---|
[5240] | 146 | System.Timers.Timer timer = new System.Timers.Timer(250);
|
---|
[5193] | 147 | timer.AutoReset = true;
|
---|
| 148 | timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
|
---|
[3262] | 149 | timer.Start();
|
---|
[5193] | 150 | try {
|
---|
| 151 | Run(cancellationToken);
|
---|
[14927] | 152 | } finally {
|
---|
[5444] | 153 | timer.Elapsed -= new System.Timers.ElapsedEventHandler(timer_Elapsed);
|
---|
[5193] | 154 | timer.Stop();
|
---|
[9343] | 155 | ExecutionTime += DateTime.UtcNow - lastUpdateTime;
|
---|
[5193] | 156 | }
|
---|
[3262] | 157 |
|
---|
[5193] | 158 | cancellationToken.ThrowIfCancellationRequested();
|
---|
[2] | 159 | }
|
---|
[5193] | 160 | protected abstract void Run(CancellationToken cancellationToken);
|
---|
[2] | 161 |
|
---|
[3262] | 162 | private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) {
|
---|
[5240] | 163 | System.Timers.Timer timer = (System.Timers.Timer)sender;
|
---|
| 164 | timer.Enabled = false;
|
---|
[9343] | 165 | DateTime now = DateTime.UtcNow;
|
---|
[3262] | 166 | ExecutionTime += now - lastUpdateTime;
|
---|
| 167 | lastUpdateTime = now;
|
---|
[5240] | 168 | timer.Enabled = true;
|
---|
[3226] | 169 | }
|
---|
[2] | 170 | }
|
---|
| 171 | }
|
---|