Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Hive.Engine/3.2/HiveEngine.cs @ 1730

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

Implemented calculation of list of necessary plugins for a job in the HiveEngine #545 (Engine which can be executed in the Hive).

File size: 8.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 HeuristicLab.Core;
26using System.Threading;
27using HeuristicLab.Hive.JobBase;
28using HeuristicLab.Hive.Contracts.Interfaces;
29using HeuristicLab.Hive.Contracts;
30using HeuristicLab.PluginInfrastructure;
31using HeuristicLab.Hive.Contracts.BusinessObjects;
32using System.IO;
33using System.Xml;
34using System.IO.Compression;
35
36namespace HeuristicLab.Hive.Engine {
37  /// <summary>
38  /// Represents an engine that executes its operator-graph on the hive.
39  /// in parallel.
40  /// </summary>
41  public class HiveEngine : ItemBase, IEngine, IEditable {
42    private Guid jobId;
43    private Job job;
44    public string HiveServerUrl { get; set; }
45
46    public HiveEngine() {
47      job = new Job();
48    }
49
50    #region IEngine Members
51
52    public IOperatorGraph OperatorGraph {
53      get { return job.Engine.OperatorGraph; }
54    }
55
56    public IScope GlobalScope {
57      get { return job.Engine.GlobalScope; }
58    }
59
60    public TimeSpan ExecutionTime {
61      get { return job.Engine.ExecutionTime; }
62    }
63
64    public bool Running {
65      get { return job.Engine.Running; }
66    }
67
68    public bool Canceled {
69      get { return job.Engine.Canceled; }
70    }
71
72    public bool Terminated {
73      get { return job.Engine.Terminated; }
74    }
75
76    public void Execute() {
77      IExecutionEngineFacade executionEngineFacade = ServiceLocator.CreateExecutionEngineFacade(HiveServerUrl);
78
79      var jobObj = CreateJobObj();
80
81      ResponseObject<Contracts.BusinessObjects.Job> res = executionEngineFacade.AddJob(jobObj);
82      jobId = res.Obj.Id;
83    }
84
85    private HeuristicLab.Hive.Contracts.BusinessObjects.Job CreateJobObj() {
86      HeuristicLab.Hive.Contracts.BusinessObjects.Job jobObj = new HeuristicLab.Hive.Contracts.BusinessObjects.Job();
87
88      MemoryStream memStream = new MemoryStream();
89      GZipStream stream = new GZipStream(memStream, CompressionMode.Compress, true);
90      XmlDocument document = PersistenceManager.CreateXmlDocument();
91      Dictionary<Guid, IStorable> dictionary = new Dictionary<Guid, IStorable>();
92      XmlNode rootNode = document.CreateElement("Root");
93      document.AppendChild(rootNode);
94      rootNode.AppendChild(PersistenceManager.Persist(job, document, dictionary));
95      document.Save(stream);
96      stream.Close();
97      jobObj.SerializedJob = memStream.ToArray();
98
99      DiscoveryService service = new DiscoveryService();
100      List<PluginInfo> plugins = new List<PluginInfo>();
101
102      foreach (IStorable storeable in dictionary.Values) {
103        PluginInfo pluginInfo = service.GetDeclaringPlugin(storeable.GetType());
104        if (!plugins.Contains(pluginInfo)) plugins.Add(pluginInfo);
105      }
106
107      List<HivePluginInfo> pluginsNeeded =
108        new List<HivePluginInfo>();
109      foreach (PluginInfo uniquePlugin in plugins) {
110        HivePluginInfo pluginInfo =
111          new HivePluginInfo();
112        pluginInfo.Name = uniquePlugin.Name;
113        pluginInfo.Version = uniquePlugin.Version.ToString();
114        pluginInfo.BuildDate = uniquePlugin.BuildDate;
115        pluginsNeeded.Add(pluginInfo);
116      }
117
118      jobObj.PluginsNeeded = pluginsNeeded;
119      jobObj.State = HeuristicLab.Hive.Contracts.BusinessObjects.State.offline;
120      return jobObj;
121    }
122
123    public void RequestSnapshot() {
124      IExecutionEngineFacade executionEngineFacade = ServiceLocator.CreateExecutionEngineFacade(HiveServerUrl);
125
126      //Requests the last result.
127      //false: There will always be a result that is been sent back
128      //true: if you hit "requestsnapshot" before - it won't send you the job back if
129      //      the snapshot hasn't been submitted to the server (because the client needs
130      //      more time).
131      var result = executionEngineFacade.GetLastResult(jobId, false);
132      if (result.Success) {
133        JobResult jobResult = result.Obj;
134        if (jobResult != null) {
135          job = (Job)PersistenceManager.RestoreFromGZip(jobResult.Result);
136          PluginManager.ControlManager.ShowControl(job.Engine.CreateView());
137        }
138      } else {
139        Exception ex = new Exception(result.Obj.Exception.Message);
140        ThreadPool.QueueUserWorkItem(delegate(object state) { OnExceptionOccurred(ex); });
141      }
142    }
143
144
145    public void ExecuteStep() {
146      throw new NotSupportedException();
147    }
148
149    public void ExecuteSteps(int steps) {
150      throw new NotSupportedException();
151    }
152
153    public void Abort() {
154      IExecutionEngineFacade executionEngineFacade = ServiceLocator.CreateExecutionEngineFacade(HiveServerUrl);
155
156      //This are just Stubs on the server right now. There won't be any effect right now...
157      executionEngineFacade.AbortJob(jobId);
158      OnFinished();
159    }
160
161    public void Reset() {
162      job.Engine.Reset();
163      jobId = Guid.NewGuid();
164      OnInitialized();
165    }
166
167    public event EventHandler Initialized;
168    /// <summary>
169    /// Fires a new <c>Initialized</c> event.
170    /// </summary>
171    protected virtual void OnInitialized() {
172      if (Initialized != null)
173        Initialized(this, new EventArgs());
174    }
175
176    public event EventHandler<OperationEventArgs> OperationExecuted;
177    /// <summary>
178    /// Fires a new <c>OperationExecuted</c> event.
179    /// </summary>
180    /// <param name="operation">The operation that has been executed.</param>
181    protected virtual void OnOperationExecuted(IOperation operation) {
182      if (OperationExecuted != null)
183        OperationExecuted(this, new OperationEventArgs(operation));
184    }
185
186    public event EventHandler<ExceptionEventArgs> ExceptionOccurred;
187    /// <summary>
188    /// Aborts the execution and fires a new <c>ExceptionOccurred</c> event.
189    /// </summary>
190    /// <param name="exception">The exception that was thrown.</param>
191    protected virtual void OnExceptionOccurred(Exception exception) {
192      Abort();
193      if (ExceptionOccurred != null)
194        ExceptionOccurred(this, new ExceptionEventArgs(exception));
195    }
196
197    public event EventHandler ExecutionTimeChanged;
198    /// <summary>
199    /// Fires a new <c>ExecutionTimeChanged</c> event.
200    /// </summary>
201    protected virtual void OnExecutionTimeChanged() {
202      if (ExecutionTimeChanged != null)
203        ExecutionTimeChanged(this, new EventArgs());
204    }
205
206    public event EventHandler Finished;
207    /// <summary>
208    /// Fires a new <c>Finished</c> event.
209    /// </summary>
210    protected virtual void OnFinished() {
211      if (Finished != null)
212        Finished(this, new EventArgs());
213    }
214
215    #endregion
216
217    public override IView CreateView() {
218      return new HiveEngineEditor(this);
219    }
220
221    #region IEditable Members
222
223    public IEditor CreateEditor() {
224      return new HiveEngineEditor(this);
225    }
226    #endregion
227
228    public override System.Xml.XmlNode GetXmlNode(string name, System.Xml.XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
229      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
230      XmlAttribute attr = document.CreateAttribute("HiveServerUrl");
231      attr.Value = HiveServerUrl;
232      node.Attributes.Append(attr);
233      node.AppendChild(PersistenceManager.Persist("Job", job, document, persistedObjects));
234      return node;
235    }
236
237    public override void Populate(System.Xml.XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
238      base.Populate(node, restoredObjects);
239      HiveServerUrl = node.Attributes["HiveServerUrl"].Value;
240      job = (Job)PersistenceManager.Restore(node.SelectSingleNode("Job"), restoredObjects);
241    }
242  }
243}
Note: See TracBrowser for help on using the repository browser.