#region License Information /* HeuristicLab * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Linq; using System.Collections.Generic; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.Collections; using System.Threading; namespace HeuristicLab.DebugEngine { [StorableClass] [Item("Debug Engine", "Engine for debugging algorithms.")] public class DebugEngine : Executable, IEngine { #region Construction and Cloning [StorableConstructor] protected DebugEngine(bool deserializing) : base(deserializing) { pausePending = stopPending = false; timer = new System.Timers.Timer(100); timer.AutoReset = true; timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed); } protected DebugEngine(DebugEngine original, Cloner cloner) : base(original, cloner) { if (original.ExecutionState == ExecutionState.Started) throw new InvalidOperationException(string.Format("Clone not allowed in execution state \"{0}\".", ExecutionState)); Log = cloner.Clone(original.Log); ExecutionStack = cloner.Clone(original.ExecutionStack); pausePending = original.pausePending; stopPending = original.stopPending; timer = new System.Timers.Timer(100); timer.AutoReset = true; timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed); this.currentOperation = cloner.Clone(original.currentOperation); this.currentOperator = cloner.Clone(original.currentOperator); } public DebugEngine() : base() { Log = new Log(); ExecutionStack = new ExecutionStack(); pausePending = stopPending = false; timer = new System.Timers.Timer(100); timer.AutoReset = true; timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed); } public override IDeepCloneable Clone(Cloner cloner) { return new DebugEngine(this, cloner); } #endregion #region Fields and Properties [Storable] public ILog Log { get; protected set; } [Storable] public ExecutionStack ExecutionStack { get; protected set; } private bool pausePending, stopPending; private DateTime lastUpdateTime; private System.Timers.Timer timer; [Storable] private IOperator currentOperator; [Storable] private bool ignoreNextBreakpoint; [Storable] private IOperation currentOperation; public virtual IOperation CurrentOperation { get { return currentOperation; } private set { if (value == currentOperation) return; currentOperation = value; OnOperationChanged(value); } } public virtual IAtomicOperation CurrentAtomicOperation { get { return CurrentOperation as IAtomicOperation; } } public virtual IExecutionContext CurrentExecutionContext { get { return CurrentOperation as IExecutionContext; } } #endregion #region Events public event EventHandler CurrentOperationChanged; protected virtual void OnOperationChanged(IOperation newOperation) { EventHandler handler = CurrentOperationChanged; if (handler != null) { handler(this, new OperationChangedEventArgs(newOperation)); } } #endregion #region Std Methods public sealed override void Prepare() { base.Prepare(); ExecutionStack.Clear(); ignoreNextBreakpoint = false; CurrentOperation = null; OnPrepared(); } public void Prepare(IOperation initialOperation) { base.Prepare(); ExecutionStack.Clear(); if (initialOperation != null) ExecutionStack.Add(initialOperation); ignoreNextBreakpoint = false; CurrentOperation = null; OnPrepared(); } protected override void OnPrepared() { Log.LogMessage("Engine prepared"); base.OnPrepared(); } public virtual void Step() { OnStarted(); lastUpdateTime = DateTime.Now; ignoreNextBreakpoint = true; timer.Start(); ProcessNextOperation(); timer.Stop(); ExecutionTime += DateTime.Now - lastUpdateTime; ignoreNextBreakpoint = false; OnPaused(); } public override void Start() { base.Start(); CurrentOperation = null; ThreadPool.QueueUserWorkItem(new WaitCallback(Run), null); } protected override void OnStarted() { Log.LogMessage("Engine started"); base.OnStarted(); } public override void Pause() { base.Pause(); pausePending = true; if (currentOperator != null) currentOperator.Abort(); } protected override void OnPaused() { Log.LogMessage("Engine paused"); base.OnPaused(); } public override void Stop() { CurrentOperation = null; base.Stop(); stopPending = true; if (currentOperator != null) currentOperator.Abort(); ignoreNextBreakpoint = false; if (ExecutionState == ExecutionState.Paused) OnStopped(); } protected override void OnStopped() { Log.LogMessage("Engine stopped"); base.OnStopped(); } protected override void OnExceptionOccurred(Exception exception) { Log.LogException(exception); base.OnExceptionOccurred(exception); } private void Run(object state) { OnStarted(); pausePending = stopPending = false; lastUpdateTime = DateTime.Now; timer.Start(); while (!pausePending && !stopPending && CanContinue) { ProcessNextOperation(); } timer.Stop(); ExecutionTime += DateTime.Now - lastUpdateTime; if (pausePending) OnPaused(); else OnStopped(); } private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { DateTime now = DateTime.Now; ExecutionTime += now - lastUpdateTime; lastUpdateTime = now; } #endregion #region Methods public virtual bool CanContinue { get { return CurrentOperation != null || ExecutionStack.Count > 0; } } /// /// Deals with the next operation, if it is an it is executed, /// if it is a its single operations are pushed on the execution stack. /// /// If an error occurs during the execution the operation is aborted and the operation /// is pushed on the stack again.
/// If the execution was successful is called.
protected virtual void ProcessNextOperation() { try { IAtomicOperation atomicOperation = CurrentOperation as IAtomicOperation; OperationCollection operations = CurrentOperation as OperationCollection; if (atomicOperation != null && operations != null) throw new InvalidOperationException("Current operation is both atomic and an operation collection"); if (atomicOperation != null) { Log.LogMessage(string.Format("Performing atomic operation {0}", Name(atomicOperation))); PerformAtomicOperation(atomicOperation); } else if (operations != null) { Log.LogMessage("Expanding operation collection"); ExpandOperationCollection(operations); } else if (ExecutionStack.Count > 0) { Log.LogMessage("Poping execution stack"); CurrentOperation = ExecutionStack.Last(); ExecutionStack.RemoveAt(ExecutionStack.Count - 1); } else { Log.LogMessage("Nothing to do"); } } catch (Exception x) { OnExceptionOccurred(x); } } protected virtual void PerformAtomicOperation(IAtomicOperation operation) { if (operation != null) { if (operation.Operator.Breakpoint) { if (ignoreNextBreakpoint) { ignoreNextBreakpoint = false; } else { ignoreNextBreakpoint = true; Log.LogMessage(string.Format("Breaking before: {0}", Name(operation))); Pause(); return; } } try { currentOperator = operation.Operator; IOperation successor = operation.Operator.Execute((IExecutionContext)operation); CurrentOperation = null; currentOperator = null; if (successor != null) { ExecutionStack.Add(successor); } } catch (Exception ex) { OnExceptionOccurred(new OperatorExecutionException(operation.Operator, ex)); Pause(); } } } protected virtual void ExpandOperationCollection(OperationCollection operations) { ExecutionStack.AddRange(operations.Reverse()); CurrentOperation = null; } protected virtual string Name(IAtomicOperation operation) { return string.IsNullOrEmpty(operation.Operator.Name) ? operation.Operator.ItemName : operation.Operator.Name; } #endregion } }