Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Grid/ProcessingEngine.cs @ 860

Last change on this file since 860 was 414, checked in by gkronber, 16 years ago

implemented #216 (ProcessingEngine should terminate on breakpoints)

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