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