Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core/EngineBase.cs @ 23

Last change on this file since 23 was 2, checked in by swagner, 17 years ago

Added HeuristicLab 3.0 sources from former SVN repository at revision 52

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      OperatorGraph.Reset();
106      GlobalScope.Clear();
107      ExecutionTime = new TimeSpan();
108      myExecutionStack.Clear();
109      if (OperatorGraph.InitialOperator != null)
110        myExecutionStack.Push(new AtomicOperation(OperatorGraph.InitialOperator, GlobalScope));
111      OnInitialized();
112    }
113
114    private void Run(object state) {
115      if (state == null) Run();
116      else RunSteps((int)state);
117      myRunning = false;
118      OnFinished();
119    }
120    private void Run() {
121      DateTime start = DateTime.Now;
122      DateTime end;
123      while ((!Canceled) && (!Terminated)) {
124        ProcessNextOperation();
125        end = DateTime.Now;
126        ExecutionTime += end - start;
127        start = end;
128      }
129      ExecutionTime += DateTime.Now - start;
130    }
131    private void RunSteps(int steps) {
132      DateTime start = DateTime.Now;
133      DateTime end;
134      int step = 0;
135      while ((!Canceled) && (!Terminated) && (step < steps)) {
136        ProcessNextOperation();
137        step++;
138        end = DateTime.Now;
139        ExecutionTime += end - start;
140        start = end;
141      }
142      ExecutionTime += DateTime.Now - start;
143    }
144
145    protected abstract void ProcessNextOperation();
146
147    public event EventHandler Initialized;
148    protected virtual void OnInitialized() {
149      if (Initialized != null)
150        Initialized(this, new EventArgs());
151    }
152    public event EventHandler<OperationEventArgs> OperationExecuted;
153    protected virtual void OnOperationExecuted(IOperation operation) {
154      if (OperationExecuted != null)
155        OperationExecuted(this, new OperationEventArgs(operation));
156    }
157    public event EventHandler<ExceptionEventArgs> ExceptionOccurred;
158    protected virtual void OnExceptionOccurred(Exception exception) {
159      Abort();
160      if (ExceptionOccurred != null)
161        ExceptionOccurred(this, new ExceptionEventArgs(exception));
162    }
163    public event EventHandler ExecutionTimeChanged;
164    protected virtual void OnExecutionTimeChanged() {
165      if (ExecutionTimeChanged != null)
166        ExecutionTimeChanged(this, new EventArgs());
167    }
168    public event EventHandler Finished;
169    protected virtual void OnFinished() {
170      if (Finished != null)
171        Finished(this, new EventArgs());
172    }
173
174    #region Persistence Methods
175    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid,IStorable> persistedObjects) {
176      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
177
178      node.AppendChild(PersistenceManager.Persist("OperatorGraph", OperatorGraph, document, persistedObjects));
179      node.AppendChild(PersistenceManager.Persist("GlobalScope", GlobalScope, document, persistedObjects));
180
181      XmlNode stackNode = document.CreateNode(XmlNodeType.Element, "ExecutionStack", null);
182      IOperation[] operations = new IOperation[ExecutionStack.Count];
183      ExecutionStack.CopyTo(operations, 0);
184      for (int i = 0; i < operations.Length; i++)
185        stackNode.AppendChild(PersistenceManager.Persist(operations[i], document, persistedObjects));
186      node.AppendChild(stackNode);
187
188      XmlNode timeNode = document.CreateNode(XmlNodeType.Element, "ExecutionTime", null);
189      timeNode.InnerText = ExecutionTime.ToString();
190      node.AppendChild(timeNode);
191      return node;
192    }
193    public override void Populate(XmlNode node, IDictionary<Guid,IStorable> restoredObjects) {
194      base.Populate(node, restoredObjects);
195      myOperatorGraph = (IOperatorGraph)PersistenceManager.Restore(node.SelectSingleNode("OperatorGraph"), restoredObjects);
196      myGlobalScope = (IScope)PersistenceManager.Restore(node.SelectSingleNode("GlobalScope"), restoredObjects);
197
198      XmlNode stackNode = node.SelectSingleNode("ExecutionStack");
199      for (int i = stackNode.ChildNodes.Count - 1; i >= 0; i--)
200        myExecutionStack.Push((IOperation)PersistenceManager.Restore(stackNode.ChildNodes[i], restoredObjects));
201
202      XmlNode timeNode = node.SelectSingleNode("ExecutionTime");
203      myExecutionTime = TimeSpan.Parse(timeNode.InnerText);
204    }
205    #endregion
206  }
207}
Note: See TracBrowser for help on using the repository browser.