Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.SequentialEngine/3.3/SequentialEngine.cs @ 2520

Last change on this file since 2520 was 2520, checked in by swagner, 14 years ago

Implemented first draft of MainForm support in HeuristicLab.Core/HeuristicLab.Core.Views and all other depending plugins (#770)

File size: 3.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
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
22using System;
23using System.Collections.Generic;
24using System.Text;
25using HeuristicLab.Core;
26using System.Threading;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.SequentialEngine {
30  /// <summary>
31  /// Represents an engine that executes its steps sequentially, also if they could be executed
32  /// in parallel.
33  /// </summary>
34  [EmptyStorableClass]
35  public class SequentialEngine : EngineBase, IEditable {
36    private IOperator currentOperator;
37
38    /// <summary>
39    /// Aborts the engine execution.
40    /// </summary>
41    /// <remarks>Calls <see cref="EngineBase.Abort"/> of base class <see cref="EngineBase"/> and
42    /// <see cref="IOperator.Abort"/> of the current <see cref="IOperator"/>.</remarks>
43    public override void Abort() {
44      base.Abort();
45      if (currentOperator != null)
46        currentOperator.Abort();
47    }
48
49    /// <summary>
50    /// Deals with the next operation, if it is an <see cref="AtomicOperation"/> it is executed,
51    /// if it is a <see cref="CompositeOperation"/> its single operations are pushed on the execution stack.
52    /// </summary>
53    /// <remarks>If an error occurs during the execution the operation is aborted and the operation
54    /// is pushed on the stack again.<br/>
55    /// If the execution was successful <see cref="EngineBase.OnOperationExecuted"/> is called.</remarks>
56    protected override void ProcessNextOperation() {
57      IOperation operation = myExecutionStack.Pop();
58      if (operation is AtomicOperation) {
59        AtomicOperation atomicOperation = (AtomicOperation)operation;
60        IOperation next = null;
61        try {
62          currentOperator = atomicOperation.Operator;
63          next = atomicOperation.Operator.Execute(atomicOperation.Scope);
64        }
65        catch (Exception ex) {
66          // push operation on stack again
67          myExecutionStack.Push(atomicOperation);
68          Abort();
69          ThreadPool.QueueUserWorkItem(delegate(object state) { OnExceptionOccurred(ex);});
70        }
71        if (next != null)
72          myExecutionStack.Push(next);
73        OnOperationExecuted(atomicOperation);
74        if (atomicOperation.Operator.Breakpoint) Abort();
75      } else if (operation is CompositeOperation) {
76        CompositeOperation compositeOperation = (CompositeOperation)operation;
77        for (int i = compositeOperation.Operations.Count - 1; i >= 0; i--)
78          myExecutionStack.Push(compositeOperation.Operations[i]);
79      }
80    }
81  }
82}
Note: See TracBrowser for help on using the repository browser.