1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
6 | using HeuristicLab.Core;
|
---|
7 | using HeuristicLab.Common;
|
---|
8 |
|
---|
9 | namespace HeuristicLab.HiveParallelEngine {
|
---|
10 | /// <summary>
|
---|
11 | /// Represents an engine that executes operations which can be executed in parallel on the hive
|
---|
12 | /// </summary>
|
---|
13 | [StorableClass]
|
---|
14 | [Item("Hive Parallel Engine", "Engine for parallel execution on the hive.")]
|
---|
15 | public class HiveEngine : Engine {
|
---|
16 | private IOperator currentOperator;
|
---|
17 |
|
---|
18 | #region constructors and cloning
|
---|
19 | public HiveEngine() { }
|
---|
20 | [StorableConstructor]
|
---|
21 | protected HiveEngine(bool deserializing) : base(deserializing) { }
|
---|
22 | protected HiveEngine(HiveEngine original, Cloner cloner) : base(original, cloner) {
|
---|
23 | }
|
---|
24 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
25 | return new HiveEngine(this, cloner);
|
---|
26 | }
|
---|
27 | #endregion
|
---|
28 |
|
---|
29 | protected override void ProcessNextOperation() {
|
---|
30 | currentOperator = null;
|
---|
31 | IOperation next = ExecutionStack.Pop();
|
---|
32 | OperationCollection coll = next as OperationCollection;
|
---|
33 |
|
---|
34 | if (coll.Parallel) {
|
---|
35 |
|
---|
36 | } else {
|
---|
37 | while (coll != null) {
|
---|
38 | for (int i = coll.Count - 1; i >= 0; i--)
|
---|
39 | ExecutionStack.Push(coll[i]);
|
---|
40 | next = ExecutionStack.Count > 0 ? ExecutionStack.Pop() : null;
|
---|
41 | coll = next as OperationCollection;
|
---|
42 | }
|
---|
43 | }
|
---|
44 |
|
---|
45 | IAtomicOperation operation = next as IAtomicOperation;
|
---|
46 | if (operation != null) {
|
---|
47 | try {
|
---|
48 | currentOperator = operation.Operator;
|
---|
49 | ExecutionStack.Push(operation.Operator.Execute((IExecutionContext)operation));
|
---|
50 | }
|
---|
51 | catch (Exception ex) {
|
---|
52 | ExecutionStack.Push(operation);
|
---|
53 | OnExceptionOccurred(new OperatorExecutionException(operation.Operator, ex));
|
---|
54 | Pause();
|
---|
55 | }
|
---|
56 | if (operation.Operator.Breakpoint) {
|
---|
57 | Log.LogMessage(string.Format("Breakpoint: {0}", operation.Operator.Name != string.Empty ? operation.Operator.Name : operation.Operator.ItemName));
|
---|
58 | Pause();
|
---|
59 | }
|
---|
60 | }
|
---|
61 | }
|
---|
62 |
|
---|
63 | public override void Pause() {
|
---|
64 | base.Pause();
|
---|
65 | if (currentOperator != null) currentOperator.Abort();
|
---|
66 | }
|
---|
67 | public override void Stop() {
|
---|
68 | base.Stop();
|
---|
69 | if (currentOperator != null) currentOperator.Abort();
|
---|
70 | }
|
---|
71 | }
|
---|
72 | }
|
---|