[2] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using System.Text;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
| 27 | using System.Xml;
|
---|
[27] | 28 | using System.Threading;
|
---|
[2] | 29 |
|
---|
| 30 | namespace HeuristicLab.Grid {
|
---|
[27] | 31 | public class ProcessingEngine : EngineBase {
|
---|
[2] | 32 | private AtomicOperation initialOperation;
|
---|
| 33 | public AtomicOperation InitialOperation {
|
---|
| 34 | get { return initialOperation; }
|
---|
[414] | 35 | set { initialOperation = value; }
|
---|
[2] | 36 | }
|
---|
| 37 |
|
---|
[413] | 38 | private string errorMessage;
|
---|
| 39 | public string ErrorMessage {
|
---|
| 40 | get { return errorMessage; }
|
---|
| 41 | }
|
---|
| 42 |
|
---|
[414] | 43 | private bool suspended;
|
---|
| 44 | public bool Suspended {
|
---|
| 45 | get { return suspended; }
|
---|
| 46 | }
|
---|
| 47 |
|
---|
[2] | 48 | public ProcessingEngine()
|
---|
| 49 | : base() {
|
---|
| 50 | }
|
---|
| 51 |
|
---|
[268] | 52 | public ProcessingEngine(IScope globalScope, AtomicOperation initialOperation)
|
---|
[2] | 53 | : base() {
|
---|
| 54 | this.initialOperation = initialOperation;
|
---|
| 55 | myGlobalScope = globalScope;
|
---|
| 56 | myExecutionStack.Push(initialOperation);
|
---|
| 57 | }
|
---|
[414] | 58 |
|
---|
| 59 | private void Suspend() {
|
---|
| 60 | Abort();
|
---|
| 61 | suspended = true;
|
---|
[2] | 62 | }
|
---|
| 63 |
|
---|
[414] | 64 | public override void Execute() {
|
---|
| 65 | suspended = false;
|
---|
| 66 | base.Execute();
|
---|
[2] | 67 | }
|
---|
[27] | 68 |
|
---|
[414] | 69 | public override void ExecuteSteps(int steps) {
|
---|
| 70 | suspended = false;
|
---|
| 71 | base.ExecuteSteps(steps);
|
---|
| 72 | }
|
---|
| 73 |
|
---|
[27] | 74 | protected override void ProcessNextOperation() {
|
---|
| 75 | IOperation operation = myExecutionStack.Pop();
|
---|
| 76 | if(operation is AtomicOperation) {
|
---|
| 77 | AtomicOperation atomicOperation = (AtomicOperation)operation;
|
---|
| 78 | IOperation next = null;
|
---|
| 79 | try {
|
---|
| 80 | next = atomicOperation.Operator.Execute(atomicOperation.Scope);
|
---|
[411] | 81 | } catch(Exception ex) {
|
---|
[413] | 82 | errorMessage = CreateErrorMessage(ex);
|
---|
[2073] | 83 | HeuristicLab.Tracing.HiveLogger.Error(errorMessage);
|
---|
[27] | 84 | // push operation on stack again
|
---|
| 85 | myExecutionStack.Push(atomicOperation);
|
---|
| 86 | Abort();
|
---|
| 87 | }
|
---|
| 88 | if(next != null)
|
---|
| 89 | myExecutionStack.Push(next);
|
---|
[414] | 90 | if(atomicOperation.Operator.Breakpoint) Suspend();
|
---|
[27] | 91 | } else if(operation is CompositeOperation) {
|
---|
| 92 | CompositeOperation compositeOperation = (CompositeOperation)operation;
|
---|
| 93 | for(int i = compositeOperation.Operations.Count - 1; i >= 0; i--)
|
---|
| 94 | myExecutionStack.Push(compositeOperation.Operations[i]);
|
---|
| 95 | }
|
---|
| 96 | }
|
---|
[413] | 97 |
|
---|
[414] | 98 | #region persistence
|
---|
| 99 | public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
|
---|
| 100 | XmlNode node = base.GetXmlNode(name, document, persistedObjects);
|
---|
| 101 | XmlAttribute canceledAttr = document.CreateAttribute("Canceled");
|
---|
| 102 | canceledAttr.Value = Canceled.ToString();
|
---|
| 103 | node.Attributes.Append(canceledAttr);
|
---|
| 104 | XmlAttribute suspendedAttr = document.CreateAttribute("Suspended");
|
---|
| 105 | suspendedAttr.Value = Suspended.ToString();
|
---|
| 106 | node.Attributes.Append(suspendedAttr);
|
---|
| 107 | if(errorMessage != null) {
|
---|
| 108 | XmlAttribute errorMessageAttr = document.CreateAttribute("ErrorMessage");
|
---|
| 109 | errorMessageAttr.Value = ErrorMessage;
|
---|
| 110 | node.Attributes.Append(errorMessageAttr);
|
---|
| 111 | }
|
---|
| 112 | node.AppendChild(PersistenceManager.Persist("InitialOperation", initialOperation, document, persistedObjects));
|
---|
| 113 | return node;
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
|
---|
| 117 | base.Populate(node, restoredObjects);
|
---|
| 118 | myCanceled = bool.Parse(node.Attributes["Canceled"].Value);
|
---|
| 119 | suspended = bool.Parse(node.Attributes["Suspended"].Value);
|
---|
| 120 | if(node.Attributes["ErrorMessage"] != null) errorMessage = node.Attributes["ErrorMessage"].Value;
|
---|
| 121 | initialOperation = (AtomicOperation)PersistenceManager.Restore(node.SelectSingleNode("InitialOperation"), restoredObjects);
|
---|
| 122 | }
|
---|
| 123 | #endregion
|
---|
| 124 |
|
---|
[413] | 125 | private string CreateErrorMessage(Exception ex) {
|
---|
| 126 | StringBuilder sb = new StringBuilder();
|
---|
| 127 | sb.Append("Sorry, but something went wrong!\n\n" + ex.Message + "\n\n" + ex.StackTrace);
|
---|
| 128 |
|
---|
| 129 | while(ex.InnerException != null) {
|
---|
| 130 | ex = ex.InnerException;
|
---|
| 131 | sb.Append("\n\n-----\n\n" + ex.Message + "\n\n" + ex.StackTrace);
|
---|
| 132 | }
|
---|
| 133 | return sb.ToString();
|
---|
| 134 | }
|
---|
[2] | 135 | }
|
---|
| 136 | }
|
---|