[4747] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17181] | 3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[4747] | 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;
|
---|
[4871] | 23 | using System.Linq;
|
---|
[4903] | 24 | using System.Threading;
|
---|
[4743] | 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
[17097] | 27 | using HEAL.Attic;
|
---|
[4743] | 28 |
|
---|
| 29 | namespace HeuristicLab.DebugEngine {
|
---|
| 30 |
|
---|
[17097] | 31 | [StorableType("BC1A9855-D280-4314-A272-CD2120679F12")]
|
---|
[4743] | 32 | [Item("Debug Engine", "Engine for debugging algorithms.")]
|
---|
[4871] | 33 | public class DebugEngine : Executable, IEngine {
|
---|
[4743] | 34 |
|
---|
[4903] | 35 | #region Construction and Cloning
|
---|
[4871] | 36 |
|
---|
[4743] | 37 | [StorableConstructor]
|
---|
[17097] | 38 | protected DebugEngine(StorableConstructorFlag _) : base(_) {
|
---|
[4946] | 39 | InitializeTimer();
|
---|
[4871] | 40 | }
|
---|
[4946] | 41 |
|
---|
[4903] | 42 | protected DebugEngine(DebugEngine original, Cloner cloner)
|
---|
| 43 | : base(original, cloner) {
|
---|
[4871] | 44 | if (original.ExecutionState == ExecutionState.Started) throw new InvalidOperationException(string.Format("Clone not allowed in execution state \"{0}\".", ExecutionState));
|
---|
| 45 | Log = cloner.Clone(original.Log);
|
---|
| 46 | ExecutionStack = cloner.Clone(original.ExecutionStack);
|
---|
[4947] | 47 | OperatorTrace = cloner.Clone(original.OperatorTrace);
|
---|
[4946] | 48 | InitializeTimer();
|
---|
| 49 | currentOperation = cloner.Clone(original.currentOperation);
|
---|
[4871] | 50 | }
|
---|
[4743] | 51 | public DebugEngine()
|
---|
| 52 | : base() {
|
---|
[4871] | 53 | Log = new Log();
|
---|
| 54 | ExecutionStack = new ExecutionStack();
|
---|
[4993] | 55 | OperatorTrace = new OperatorTrace();
|
---|
[4946] | 56 | InitializeTimer();
|
---|
[4743] | 57 | }
|
---|
| 58 |
|
---|
[4871] | 59 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 60 | return new DebugEngine(this, cloner);
|
---|
[4743] | 61 | }
|
---|
| 62 |
|
---|
[4946] | 63 | private void InitializeTimer() {
|
---|
[5240] | 64 | timer = new System.Timers.Timer(250);
|
---|
[4946] | 65 | timer.AutoReset = true;
|
---|
| 66 | timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
|
---|
| 67 | }
|
---|
| 68 |
|
---|
[4871] | 69 | #endregion
|
---|
[4743] | 70 |
|
---|
[4871] | 71 | #region Fields and Properties
|
---|
| 72 |
|
---|
| 73 | [Storable]
|
---|
[5001] | 74 | public ILog Log { get; private set; }
|
---|
[4871] | 75 |
|
---|
| 76 | [Storable]
|
---|
[5001] | 77 | public ExecutionStack ExecutionStack { get; private set; }
|
---|
[4871] | 78 |
|
---|
[4993] | 79 | [Storable]
|
---|
| 80 | public OperatorTrace OperatorTrace { get; private set; }
|
---|
| 81 |
|
---|
[5193] | 82 | private CancellationTokenSource cancellationTokenSource;
|
---|
| 83 | private bool stopPending;
|
---|
[4871] | 84 | private DateTime lastUpdateTime;
|
---|
| 85 | private System.Timers.Timer timer;
|
---|
[4903] | 86 |
|
---|
[4871] | 87 | [Storable]
|
---|
| 88 | private IOperation currentOperation;
|
---|
[5002] | 89 | public IOperation CurrentOperation {
|
---|
[4871] | 90 | get { return currentOperation; }
|
---|
| 91 | private set {
|
---|
[4946] | 92 | if (value != currentOperation) {
|
---|
| 93 | currentOperation = value;
|
---|
| 94 | OnOperationChanged(value);
|
---|
| 95 | }
|
---|
[4871] | 96 | }
|
---|
[4743] | 97 | }
|
---|
| 98 |
|
---|
[4871] | 99 | public virtual IAtomicOperation CurrentAtomicOperation {
|
---|
| 100 | get { return CurrentOperation as IAtomicOperation; }
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | public virtual IExecutionContext CurrentExecutionContext {
|
---|
[4903] | 104 | get { return CurrentOperation as IExecutionContext; }
|
---|
[4871] | 105 | }
|
---|
| 106 |
|
---|
[4947] | 107 | public virtual bool CanContinue {
|
---|
| 108 | get { return CurrentOperation != null || ExecutionStack.Count > 0; }
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | public virtual bool IsAtBreakpoint {
|
---|
| 112 | get { return CurrentAtomicOperation != null && CurrentAtomicOperation.Operator != null && CurrentAtomicOperation.Operator.Breakpoint; }
|
---|
| 113 | }
|
---|
| 114 |
|
---|
[4871] | 115 | #endregion
|
---|
| 116 |
|
---|
| 117 | #region Events
|
---|
| 118 |
|
---|
| 119 | public event EventHandler<OperationChangedEventArgs> CurrentOperationChanged;
|
---|
| 120 | protected virtual void OnOperationChanged(IOperation newOperation) {
|
---|
| 121 | EventHandler<OperationChangedEventArgs> handler = CurrentOperationChanged;
|
---|
| 122 | if (handler != null) {
|
---|
| 123 | handler(this, new OperationChangedEventArgs(newOperation));
|
---|
| 124 | }
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | #endregion
|
---|
| 128 |
|
---|
| 129 | #region Std Methods
|
---|
| 130 | public sealed override void Prepare() {
|
---|
| 131 | base.Prepare();
|
---|
| 132 | ExecutionStack.Clear();
|
---|
| 133 | CurrentOperation = null;
|
---|
[4996] | 134 | OperatorTrace.Reset();
|
---|
[4871] | 135 | OnPrepared();
|
---|
| 136 | }
|
---|
| 137 | public void Prepare(IOperation initialOperation) {
|
---|
| 138 | base.Prepare();
|
---|
| 139 | ExecutionStack.Clear();
|
---|
| 140 | if (initialOperation != null)
|
---|
| 141 | ExecutionStack.Add(initialOperation);
|
---|
| 142 | CurrentOperation = null;
|
---|
[4996] | 143 | OperatorTrace.Reset();
|
---|
[4871] | 144 | OnPrepared();
|
---|
| 145 | }
|
---|
| 146 | protected override void OnPrepared() {
|
---|
| 147 | Log.LogMessage("Engine prepared");
|
---|
| 148 | base.OnPrepared();
|
---|
| 149 | }
|
---|
| 150 |
|
---|
[4909] | 151 | public virtual void Step(bool skipStackOperations) {
|
---|
[4871] | 152 | OnStarted();
|
---|
[5193] | 153 | cancellationTokenSource = new CancellationTokenSource();
|
---|
| 154 | stopPending = false;
|
---|
[9343] | 155 | lastUpdateTime = DateTime.UtcNow;
|
---|
[4871] | 156 | timer.Start();
|
---|
[5193] | 157 | try {
|
---|
| 158 | ProcessNextOperation(true, cancellationTokenSource.Token);
|
---|
| 159 | while (skipStackOperations && !(CurrentOperation is IAtomicOperation) && CanContinue)
|
---|
| 160 | ProcessNextOperation(true, cancellationTokenSource.Token);
|
---|
[15292] | 161 | } catch (Exception ex) {
|
---|
[5193] | 162 | OnExceptionOccurred(ex);
|
---|
| 163 | }
|
---|
[4871] | 164 | timer.Stop();
|
---|
[9343] | 165 | ExecutionTime += DateTime.UtcNow - lastUpdateTime;
|
---|
[5193] | 166 | cancellationTokenSource.Dispose();
|
---|
| 167 | cancellationTokenSource = null;
|
---|
| 168 | if (stopPending) ExecutionStack.Clear();
|
---|
| 169 | if (stopPending || !CanContinue) OnStopped();
|
---|
| 170 | else OnPaused();
|
---|
[4871] | 171 | }
|
---|
| 172 |
|
---|
[15292] | 173 | public override void Start(CancellationToken cancellationToken) {
|
---|
| 174 | base.Start(cancellationToken);
|
---|
| 175 | cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
|
---|
[5193] | 176 | stopPending = false;
|
---|
| 177 |
|
---|
[15292] | 178 | try {
|
---|
| 179 | Run(cancellationTokenSource.Token);
|
---|
| 180 | } catch (OperationCanceledException) {
|
---|
| 181 | } catch (AggregateException ae) {
|
---|
[15384] | 182 | ae.FlattenAndHandle(new[] { typeof(OperationCanceledException) }, e => OnExceptionOccurred(e));
|
---|
[15292] | 183 | } catch (Exception e) {
|
---|
| 184 | OnExceptionOccurred(e);
|
---|
| 185 | }
|
---|
| 186 |
|
---|
| 187 | cancellationTokenSource.Dispose();
|
---|
| 188 | cancellationTokenSource = null;
|
---|
| 189 | if (stopPending) ExecutionStack.Clear();
|
---|
| 190 | if (stopPending || !CanContinue) OnStopped();
|
---|
| 191 | else OnPaused();
|
---|
[4871] | 192 | }
|
---|
| 193 | protected override void OnStarted() {
|
---|
| 194 | Log.LogMessage("Engine started");
|
---|
| 195 | base.OnStarted();
|
---|
| 196 | }
|
---|
| 197 |
|
---|
| 198 | public override void Pause() {
|
---|
| 199 | base.Pause();
|
---|
[5193] | 200 | cancellationTokenSource.Cancel();
|
---|
[4871] | 201 | }
|
---|
| 202 |
|
---|
| 203 | protected override void OnPaused() {
|
---|
| 204 | Log.LogMessage("Engine paused");
|
---|
| 205 | base.OnPaused();
|
---|
| 206 | }
|
---|
| 207 |
|
---|
| 208 | public override void Stop() {
|
---|
| 209 | CurrentOperation = null;
|
---|
| 210 | base.Stop();
|
---|
[5193] | 211 | if (ExecutionState == ExecutionState.Paused) {
|
---|
| 212 | ExecutionStack.Clear();
|
---|
| 213 | OnStopped();
|
---|
| 214 | } else {
|
---|
| 215 | stopPending = true;
|
---|
| 216 | cancellationTokenSource.Cancel();
|
---|
| 217 | }
|
---|
[4871] | 218 | }
|
---|
[4903] | 219 |
|
---|
[4871] | 220 | protected override void OnStopped() {
|
---|
| 221 | Log.LogMessage("Engine stopped");
|
---|
| 222 | base.OnStopped();
|
---|
| 223 | }
|
---|
| 224 |
|
---|
| 225 | protected override void OnExceptionOccurred(Exception exception) {
|
---|
| 226 | Log.LogException(exception);
|
---|
| 227 | base.OnExceptionOccurred(exception);
|
---|
| 228 | }
|
---|
| 229 |
|
---|
| 230 | private void Run(object state) {
|
---|
[5193] | 231 | CancellationToken cancellationToken = (CancellationToken)state;
|
---|
| 232 |
|
---|
[4871] | 233 | OnStarted();
|
---|
[9343] | 234 | lastUpdateTime = DateTime.UtcNow;
|
---|
[4871] | 235 | timer.Start();
|
---|
[5193] | 236 | try {
|
---|
| 237 | if (!cancellationToken.IsCancellationRequested && CanContinue)
|
---|
| 238 | ProcessNextOperation(false, cancellationToken);
|
---|
| 239 | while (!cancellationToken.IsCancellationRequested && CanContinue && !IsAtBreakpoint)
|
---|
| 240 | ProcessNextOperation(false, cancellationToken);
|
---|
| 241 | cancellationToken.ThrowIfCancellationRequested();
|
---|
[15292] | 242 | } finally {
|
---|
[5193] | 243 | timer.Stop();
|
---|
[9343] | 244 | ExecutionTime += DateTime.UtcNow - lastUpdateTime;
|
---|
[4871] | 245 |
|
---|
[5193] | 246 | if (IsAtBreakpoint)
|
---|
| 247 | Log.LogMessage(string.Format("Breaking before: {0}", CurrentAtomicOperation.Operator.Name));
|
---|
| 248 | }
|
---|
[4871] | 249 | }
|
---|
| 250 |
|
---|
| 251 | private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) {
|
---|
[5240] | 252 | System.Timers.Timer timer = (System.Timers.Timer)sender;
|
---|
| 253 | timer.Enabled = false;
|
---|
[9343] | 254 | DateTime now = DateTime.UtcNow;
|
---|
[4871] | 255 | ExecutionTime += now - lastUpdateTime;
|
---|
| 256 | lastUpdateTime = now;
|
---|
[5240] | 257 | timer.Enabled = true;
|
---|
[4871] | 258 | }
|
---|
| 259 | #endregion
|
---|
| 260 |
|
---|
| 261 | #region Methods
|
---|
| 262 |
|
---|
| 263 |
|
---|
[4947] | 264 |
|
---|
[4743] | 265 | /// <summary>
|
---|
| 266 | /// Deals with the next operation, if it is an <see cref="AtomicOperation"/> it is executed,
|
---|
| 267 | /// if it is a <see cref="CompositeOperation"/> its single operations are pushed on the execution stack.
|
---|
| 268 | /// </summary>
|
---|
| 269 | /// <remarks>If an error occurs during the execution the operation is aborted and the operation
|
---|
| 270 | /// is pushed on the stack again.<br/>
|
---|
| 271 | /// If the execution was successful <see cref="EngineBase.OnOperationExecuted"/> is called.</remarks>
|
---|
[5193] | 272 | protected virtual void ProcessNextOperation(bool logOperations, CancellationToken cancellationToken) {
|
---|
| 273 | IAtomicOperation atomicOperation = CurrentOperation as IAtomicOperation;
|
---|
| 274 | OperationCollection operations = CurrentOperation as OperationCollection;
|
---|
| 275 | if (atomicOperation != null && operations != null)
|
---|
| 276 | throw new InvalidOperationException("Current operation is both atomic and an operation collection");
|
---|
[4871] | 277 |
|
---|
[5193] | 278 | if (atomicOperation != null) {
|
---|
| 279 | if (logOperations)
|
---|
| 280 | Log.LogMessage(string.Format("Performing atomic operation {0}", Utils.Name(atomicOperation)));
|
---|
| 281 | PerformAtomicOperation(atomicOperation, cancellationToken);
|
---|
| 282 | } else if (operations != null) {
|
---|
| 283 | if (logOperations)
|
---|
| 284 | Log.LogMessage("Expanding operation collection");
|
---|
| 285 | ExecutionStack.AddRange(operations.Reverse());
|
---|
| 286 | CurrentOperation = null;
|
---|
| 287 | } else if (ExecutionStack.Count > 0) {
|
---|
| 288 | if (logOperations)
|
---|
| 289 | Log.LogMessage("Popping execution stack");
|
---|
| 290 | CurrentOperation = ExecutionStack.Last();
|
---|
| 291 | ExecutionStack.RemoveAt(ExecutionStack.Count - 1);
|
---|
| 292 | } else {
|
---|
| 293 | if (logOperations)
|
---|
| 294 | Log.LogMessage("Nothing to do");
|
---|
[4743] | 295 | }
|
---|
[5193] | 296 | OperatorTrace.Regenerate(CurrentAtomicOperation);
|
---|
[4871] | 297 | }
|
---|
| 298 |
|
---|
[5193] | 299 | protected virtual void PerformAtomicOperation(IAtomicOperation operation, CancellationToken cancellationToken) {
|
---|
[4743] | 300 | if (operation != null) {
|
---|
| 301 | try {
|
---|
[5193] | 302 | IOperation successor = operation.Operator.Execute((IExecutionContext)operation, cancellationToken);
|
---|
[4871] | 303 | if (successor != null) {
|
---|
[4996] | 304 | OperatorTrace.RegisterParenthood(operation, successor);
|
---|
[4871] | 305 | ExecutionStack.Add(successor);
|
---|
[4743] | 306 | }
|
---|
[4904] | 307 | CurrentOperation = null;
|
---|
[15292] | 308 | } catch (Exception ex) {
|
---|
[15393] | 309 | if (ex is OperationCanceledException) throw;
|
---|
[5193] | 310 | else throw new OperatorExecutionException(operation.Operator, ex);
|
---|
| 311 | }
|
---|
[4743] | 312 | }
|
---|
| 313 | }
|
---|
| 314 |
|
---|
[4871] | 315 | #endregion
|
---|
[4743] | 316 | }
|
---|
| 317 | }
|
---|