Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.3-HiveMigration/sources/HeuristicLab.Hive/HeuristicLab.Hive.Engine/3.3/Job.cs @ 4091

Last change on this file since 4091 was 4091, checked in by cneumuel, 14 years ago

resolved issues with 3.3, hive-server now executable (1096)

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