Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Grid/3.2/ProcessingEngine.cs @ 2651

Last change on this file since 2651 was 2073, checked in by gkronber, 15 years ago

Fixed bugs in preparation of engines for execution on hive. Used HL.Tracing instead of trace statements. #642 (Hive backend for CEDMA)

File size: 4.9 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.Linq;
25using System.Text;
26using HeuristicLab.Core;
27using System.Xml;
28using System.Threading;
29
30namespace HeuristicLab.Grid {
31  public class ProcessingEngine : EngineBase {
32    private AtomicOperation initialOperation;
33    public AtomicOperation InitialOperation {
34      get { return initialOperation; }
35      set { initialOperation = value; }
36    }
37
38    private string errorMessage;
39    public string ErrorMessage {
40      get { return errorMessage; }
41    }
42
43    private bool suspended;
44    public bool Suspended {
45      get { return suspended; }
46    }
47
48    public ProcessingEngine()
49      : base() {
50    }
51
52    public ProcessingEngine(IScope globalScope, AtomicOperation initialOperation)
53      : base() {
54      this.initialOperation = initialOperation;
55      myGlobalScope = globalScope;
56      myExecutionStack.Push(initialOperation);
57    }
58
59    private void Suspend() {
60      Abort();
61      suspended = true;
62    }
63
64    public override void Execute() {
65      suspended = false;
66      base.Execute();
67    }
68
69    public override void ExecuteSteps(int steps) {
70      suspended = false;
71      base.ExecuteSteps(steps);
72    }
73
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);
81        } catch(Exception ex) {
82          errorMessage = CreateErrorMessage(ex);
83          HeuristicLab.Tracing.HiveLogger.Error(errorMessage);
84          // push operation on stack again
85          myExecutionStack.Push(atomicOperation);
86          Abort();
87        }
88        if(next != null)
89          myExecutionStack.Push(next);
90        if(atomicOperation.Operator.Breakpoint) Suspend();
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    }
97
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
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    }
135  }
136}
Note: See TracBrowser for help on using the repository browser.