Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive_Milestone2/sources/HeuristicLab.Hive.Engine/3.2/Job.cs @ 6004

Last change on this file since 6004 was 1815, checked in by swagner, 16 years ago

Added a property for the thread priority of an engine's worker thread to EngineBase (#623)

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