Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.1/sources/HeuristicLab.Core/EngineBase.cs @ 3135

Last change on this file since 3135 was 47, checked in by swagner, 17 years ago

Fixed ticket #49

  • removed operator reset mechanism
File size: 7.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 System.Xml;
26using System.Threading;
27
28namespace HeuristicLab.Core {
29  public abstract class EngineBase : ItemBase, IEngine {
30    protected IOperatorGraph myOperatorGraph;
31    public IOperatorGraph OperatorGraph {
32      get { return myOperatorGraph; }
33    }
34    protected IScope myGlobalScope;
35    public IScope GlobalScope {
36      get { return myGlobalScope; }
37    }
38
39    private TimeSpan myExecutionTime;
40    public TimeSpan ExecutionTime {
41      get { return myExecutionTime; }
42      protected set {
43        myExecutionTime = value;
44        OnExecutionTimeChanged();
45      }
46    }
47
48    protected Stack<IOperation> myExecutionStack;
49    public Stack<IOperation> ExecutionStack {
50      get { return myExecutionStack; }
51    }
52    protected bool myRunning;
53    public bool Running {
54      get { return myRunning; }
55    }
56    protected bool myCanceled;
57    public bool Canceled {
58      get { return myCanceled; }
59    }
60    public virtual bool Terminated {
61      get { return ExecutionStack.Count == 0; }
62    }
63
64    protected EngineBase() {
65      myOperatorGraph = new OperatorGraph();
66      myGlobalScope = new Scope("Global");
67      myExecutionStack = new Stack<IOperation>();
68      Reset();
69    }
70
71    public override object Clone(IDictionary<Guid, object> clonedObjects) {
72      EngineBase clone = (EngineBase)base.Clone(clonedObjects);
73      clone.myOperatorGraph = (IOperatorGraph)Auxiliary.Clone(OperatorGraph, clonedObjects);
74      clone.myGlobalScope = (IScope)Auxiliary.Clone(GlobalScope, clonedObjects);
75      clone.myExecutionTime = ExecutionTime;
76      IOperation[] operations = new IOperation[ExecutionStack.Count];
77      ExecutionStack.CopyTo(operations, 0);
78      for (int i = operations.Length - 1; i >= 0; i--)
79        clone.myExecutionStack.Push((IOperation)Auxiliary.Clone(operations[i], clonedObjects));
80      clone.myRunning = Running;
81      clone.myCanceled = Canceled;
82      return clone;
83     
84    }
85
86    public virtual void Execute() {
87      myRunning = true;
88      myCanceled = false;
89      ThreadPool.QueueUserWorkItem(new WaitCallback(Run), null);
90    }
91    public virtual void ExecuteSteps(int steps) {
92      myRunning = true;
93      myCanceled = false;
94      ThreadPool.QueueUserWorkItem(new WaitCallback(Run), steps);
95    }
96    public void ExecuteStep() {
97      ExecuteSteps(1);
98    }
99    public virtual void Abort() {
100      myCanceled = true;
101    }
102    public virtual void Reset() {
103      myCanceled = false;
104      myRunning = false;
105      GlobalScope.Clear();
106      ExecutionTime = new TimeSpan();
107      myExecutionStack.Clear();
108      if (OperatorGraph.InitialOperator != null)
109        myExecutionStack.Push(new AtomicOperation(OperatorGraph.InitialOperator, GlobalScope));
110      OnInitialized();
111    }
112
113    private void Run(object state) {
114      if (state == null) Run();
115      else RunSteps((int)state);
116      myRunning = false;
117      OnFinished();
118    }
119    private void Run() {
120      DateTime start = DateTime.Now;
121      DateTime end;
122      while ((!Canceled) && (!Terminated)) {
123        ProcessNextOperation();
124        end = DateTime.Now;
125        ExecutionTime += end - start;
126        start = end;
127      }
128      ExecutionTime += DateTime.Now - start;
129    }
130    private void RunSteps(int steps) {
131      DateTime start = DateTime.Now;
132      DateTime end;
133      int step = 0;
134      while ((!Canceled) && (!Terminated) && (step < steps)) {
135        ProcessNextOperation();
136        step++;
137        end = DateTime.Now;
138        ExecutionTime += end - start;
139        start = end;
140      }
141      ExecutionTime += DateTime.Now - start;
142    }
143
144    protected abstract void ProcessNextOperation();
145
146    public event EventHandler Initialized;
147    protected virtual void OnInitialized() {
148      if (Initialized != null)
149        Initialized(this, new EventArgs());
150    }
151    public event EventHandler<OperationEventArgs> OperationExecuted;
152    protected virtual void OnOperationExecuted(IOperation operation) {
153      if (OperationExecuted != null)
154        OperationExecuted(this, new OperationEventArgs(operation));
155    }
156    public event EventHandler<ExceptionEventArgs> ExceptionOccurred;
157    protected virtual void OnExceptionOccurred(Exception exception) {
158      Abort();
159      if (ExceptionOccurred != null)
160        ExceptionOccurred(this, new ExceptionEventArgs(exception));
161    }
162    public event EventHandler ExecutionTimeChanged;
163    protected virtual void OnExecutionTimeChanged() {
164      if (ExecutionTimeChanged != null)
165        ExecutionTimeChanged(this, new EventArgs());
166    }
167    public event EventHandler Finished;
168    protected virtual void OnFinished() {
169      if (Finished != null)
170        Finished(this, new EventArgs());
171    }
172
173    #region Persistence Methods
174    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid,IStorable> persistedObjects) {
175      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
176
177      node.AppendChild(PersistenceManager.Persist("OperatorGraph", OperatorGraph, document, persistedObjects));
178      node.AppendChild(PersistenceManager.Persist("GlobalScope", GlobalScope, document, persistedObjects));
179
180      XmlNode stackNode = document.CreateNode(XmlNodeType.Element, "ExecutionStack", null);
181      IOperation[] operations = new IOperation[ExecutionStack.Count];
182      ExecutionStack.CopyTo(operations, 0);
183      for (int i = 0; i < operations.Length; i++)
184        stackNode.AppendChild(PersistenceManager.Persist(operations[i], document, persistedObjects));
185      node.AppendChild(stackNode);
186
187      XmlNode timeNode = document.CreateNode(XmlNodeType.Element, "ExecutionTime", null);
188      timeNode.InnerText = ExecutionTime.ToString();
189      node.AppendChild(timeNode);
190      return node;
191    }
192    public override void Populate(XmlNode node, IDictionary<Guid,IStorable> restoredObjects) {
193      base.Populate(node, restoredObjects);
194      myOperatorGraph = (IOperatorGraph)PersistenceManager.Restore(node.SelectSingleNode("OperatorGraph"), restoredObjects);
195      myGlobalScope = (IScope)PersistenceManager.Restore(node.SelectSingleNode("GlobalScope"), restoredObjects);
196
197      XmlNode stackNode = node.SelectSingleNode("ExecutionStack");
198      for (int i = stackNode.ChildNodes.Count - 1; i >= 0; i--)
199        myExecutionStack.Push((IOperation)PersistenceManager.Restore(stackNode.ChildNodes[i], restoredObjects));
200
201      XmlNode timeNode = node.SelectSingleNode("ExecutionTime");
202      myExecutionTime = TimeSpan.Parse(timeNode.InnerText);
203    }
204    #endregion
205  }
206}
Note: See TracBrowser for help on using the repository browser.