Free cookie consent management tool by TermsFeed Policy Generator

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

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

migration from 3.2 to 3.3 completed. Hive Server and Client are now executable and as functional as they were in 3.2. (#1096)

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