[1440] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Text;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
| 26 | using System.Threading;
|
---|
| 27 | using HeuristicLab.Hive.JobBase;
|
---|
| 28 | using System.Xml;
|
---|
[1761] | 29 | using HeuristicLab.Data;
|
---|
[1440] | 30 |
|
---|
| 31 | namespace 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();
|
---|
[1815] | 44 | engine.Priority = ThreadPriority.Lowest;
|
---|
[1440] | 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) {
|
---|
[2111] | 57 | if (Engine.Canceled) this.progress = 0.0;
|
---|
| 58 | else this.progress = 1.0;
|
---|
[1440] | 59 | if (JobStopped != null)
|
---|
| 60 | JobStopped(this, new EventArgs());
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | #region IJob Members
|
---|
| 64 |
|
---|
| 65 | public event EventHandler JobStopped;
|
---|
[1655] | 66 | public event EventHandler JobFailed;
|
---|
[1440] | 67 |
|
---|
| 68 | public long JobId {
|
---|
| 69 | get;
|
---|
| 70 | set;
|
---|
| 71 | }
|
---|
| 72 |
|
---|
[2111] | 73 | private double progress;
|
---|
[1440] | 74 | public double Progress {
|
---|
[1761] | 75 | get {
|
---|
| 76 | DoubleData progress = Engine.GlobalScope.GetVariableValue<DoubleData>("Progress", false,
|
---|
| 77 | false);
|
---|
[2111] | 78 | return progress == null ? this.progress : progress.Data;
|
---|
[1761] | 79 | }
|
---|
[1440] | 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);
|
---|
[2089] | 108 | XmlAttribute idAttr = document.CreateAttribute("JobId");
|
---|
| 109 | idAttr.Value = XmlConvert.ToString(JobId);
|
---|
[2111] | 110 | XmlAttribute progressAttr = document.CreateAttribute("Progress");
|
---|
| 111 | progressAttr.Value = XmlConvert.ToString(progress);
|
---|
[2089] | 112 | node.Attributes.Append(idAttr);
|
---|
[2111] | 113 | node.Attributes.Append(progressAttr);
|
---|
[1440] | 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);
|
---|
[2089] | 119 | JobId = XmlConvert.ToInt64(node.Attributes["JobId"].Value);
|
---|
[2111] | 120 | progress = XmlConvert.ToDouble(node.Attributes["Progress"].Value);
|
---|
[1440] | 121 | DeregisterEvents();
|
---|
| 122 | engine = (SequentialEngine.SequentialEngine)PersistenceManager.Restore(node.SelectSingleNode("Engine"), restoredObjects);
|
---|
| 123 | RegisterEvents();
|
---|
| 124 | }
|
---|
| 125 | }
|
---|
| 126 | }
|
---|