Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Hive.Engine/3.2/Job.cs @ 1761

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

added progress calculation to hive-engine (just reads variable "Progress" vom global scope) #545 (Engine which can be executed in the Hive)

File size: 3.3 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      RegisterEvents();
45    }
46
47    private void RegisterEvents() {
48      engine.Finished += new EventHandler(engine_Finished);
49    }
50
51    private void DeregisterEvents() {
52      engine.Finished -= new EventHandler(engine_Finished);
53    }
54
55    void engine_Finished(object sender, EventArgs e) {
56      if (JobStopped != null)
57        JobStopped(this, new EventArgs());
58    }
59
60    #region IJob Members
61
62    public event EventHandler JobStopped;
63    public event EventHandler JobFailed;
64
65    public long JobId {
66      get;
67      set;
68    }
69
70    public double Progress {
71      get {
72        DoubleData progress = Engine.GlobalScope.GetVariableValue<DoubleData>("Progress", false,
73          false);
74        return progress == null ? 0.0 : progress.Data;
75      }
76      set { throw new NotSupportedException(); }
77    }
78
79    public bool Running {
80      get {
81        return Engine.Running;
82      }
83      set {
84        throw new NotSupportedException();
85      }
86    }
87
88    public void Run() {
89      throw new NotSupportedException();
90    }
91
92    public void Start() {
93      Engine.Execute();
94    }
95
96    public void Stop() {
97      Engine.Abort();
98    }
99
100    #endregion
101
102    public override System.Xml.XmlNode GetXmlNode(string name, System.Xml.XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
103      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
104      node.AppendChild(PersistenceManager.Persist("Engine", Engine, document, persistedObjects));
105      return node;
106    }
107    public override void Populate(System.Xml.XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
108      base.Populate(node, restoredObjects);
109      DeregisterEvents();
110      engine = (SequentialEngine.SequentialEngine)PersistenceManager.Restore(node.SelectSingleNode("Engine"), restoredObjects);
111      RegisterEvents();
112    }
113  }
114}
Note: See TracBrowser for help on using the repository browser.