Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CEDMA-Exporter-715/sources/HeuristicLab.Hive.Engine/3.2/Job.cs @ 2273

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

Implemented discrimination of snapshots vs. end results in HiveEngine and Job. #545 (Engine which can be executed in the Hive)

File size: 3.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.Text;
25using HeuristicLab.Core;
26using System.Threading;
27using HeuristicLab.Hive.JobBase;
28using System.Xml;
29using HeuristicLab.Data;
30
31namespace HeuristicLab.Hive.Engine {
32  /// <summary>
33  /// Represents an job that wraps an engine that should be executed in the hive.
34  /// </summary>
35  public class Job : StorableBase, IJob {
36    private IEngine engine;
37    public IEngine Engine {
38      get { return engine; }
39    }
40
41    public Job()
42      : base() {
43      engine = new SequentialEngine.SequentialEngine();
44      engine.Priority = ThreadPriority.Lowest;
45      RegisterEvents();
46    }
47
48    private void RegisterEvents() {
49      engine.Finished += new EventHandler(engine_Finished);
50    }
51
52    private void DeregisterEvents() {
53      engine.Finished -= new EventHandler(engine_Finished);
54    }
55
56    void engine_Finished(object sender, EventArgs e) {
57      if (Engine.Canceled) this.progress = 0.0;
58      else this.progress = 1.0;
59      if (JobStopped != null)
60        JobStopped(this, new EventArgs());
61    }
62
63    #region IJob Members
64
65    public event EventHandler JobStopped;
66    public event EventHandler JobFailed;
67
68    public long JobId {
69      get;
70      set;
71    }
72
73    private double progress;
74    public double Progress {
75      get {
76        DoubleData progress = Engine.GlobalScope.GetVariableValue<DoubleData>("Progress", false,
77          false);
78        return progress == null ? this.progress : progress.Data;
79      }
80      set { throw new NotSupportedException(); }
81    }
82
83    public bool Running {
84      get {
85        return Engine.Running;
86      }
87      set {
88        throw new NotSupportedException();
89      }
90    }
91
92    public void Run() {
93      throw new NotSupportedException();
94    }
95
96    public void Start() {
97      Engine.Execute();
98    }
99
100    public void Stop() {
101      Engine.Abort();
102    }
103
104    #endregion
105
106    public override System.Xml.XmlNode GetXmlNode(string name, System.Xml.XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
107      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
108      XmlAttribute idAttr = document.CreateAttribute("JobId");
109      idAttr.Value = XmlConvert.ToString(JobId);
110      XmlAttribute progressAttr = document.CreateAttribute("Progress");
111      progressAttr.Value = XmlConvert.ToString(progress);
112      node.Attributes.Append(idAttr);
113      node.Attributes.Append(progressAttr);
114      node.AppendChild(PersistenceManager.Persist("Engine", Engine, document, persistedObjects));
115      return node;
116    }
117    public override void Populate(System.Xml.XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
118      base.Populate(node, restoredObjects);
119      JobId = XmlConvert.ToInt64(node.Attributes["JobId"].Value);
120      progress = XmlConvert.ToDouble(node.Attributes["Progress"].Value);
121      DeregisterEvents();
122      engine = (SequentialEngine.SequentialEngine)PersistenceManager.Restore(node.SelectSingleNode("Engine"), restoredObjects);
123      RegisterEvents();
124    }
125  }
126}
Note: See TracBrowser for help on using the repository browser.