Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2601 was 2546, checked in by swagner, 15 years ago

Continued work on Optimizer and on adapting all views to the new MainForm concept (#770)

File size: 3.4 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  [Creatable("Engines")]
36  [Item("Sequential Engine", "Engine for sequential execution of algorithms.")]
37  public class SequentialEngine : EngineBase {
38    private IOperator currentOperator;
39
40    /// <summary>
41    /// Aborts the engine execution.
42    /// </summary>
43    /// <remarks>Calls <see cref="EngineBase.Abort"/> of base class <see cref="EngineBase"/> and
44    /// <see cref="IOperator.Abort"/> of the current <see cref="IOperator"/>.</remarks>
45    public override void Abort() {
46      base.Abort();
47      if (currentOperator != null)
48        currentOperator.Abort();
49    }
50
51    /// <summary>
52    /// Deals with the next operation, if it is an <see cref="AtomicOperation"/> it is executed,
53    /// if it is a <see cref="CompositeOperation"/> its single operations are pushed on the execution stack.
54    /// </summary>
55    /// <remarks>If an error occurs during the execution the operation is aborted and the operation
56    /// is pushed on the stack again.<br/>
57    /// If the execution was successful <see cref="EngineBase.OnOperationExecuted"/> is called.</remarks>
58    protected override void ProcessNextOperation() {
59      IOperation operation = myExecutionStack.Pop();
60      if (operation is AtomicOperation) {
61        AtomicOperation atomicOperation = (AtomicOperation)operation;
62        IOperation next = null;
63        try {
64          currentOperator = atomicOperation.Operator;
65          next = atomicOperation.Operator.Execute(atomicOperation.Scope);
66        }
67        catch (Exception ex) {
68          // push operation on stack again
69          myExecutionStack.Push(atomicOperation);
70          Abort();
71          ThreadPool.QueueUserWorkItem(delegate(object state) { OnExceptionOccurred(ex);});
72        }
73        if (next != null)
74          myExecutionStack.Push(next);
75        OnOperationExecuted(atomicOperation);
76        if (atomicOperation.Operator.Breakpoint) Abort();
77      } else if (operation is CompositeOperation) {
78        CompositeOperation compositeOperation = (CompositeOperation)operation;
79        for (int i = compositeOperation.Operations.Count - 1; i >= 0; i--)
80          myExecutionStack.Push(compositeOperation.Operations[i]);
81      }
82    }
83  }
84}
Note: See TracBrowser for help on using the repository browser.