[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) {
|
---|
| 57 | if (JobStopped != null)
|
---|
| 58 | JobStopped(this, new EventArgs());
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | #region IJob Members
|
---|
| 62 |
|
---|
| 63 | public event EventHandler JobStopped;
|
---|
[1655] | 64 | public event EventHandler JobFailed;
|
---|
[1440] | 65 |
|
---|
| 66 | public long JobId {
|
---|
| 67 | get;
|
---|
| 68 | set;
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | public double Progress {
|
---|
[1761] | 72 | get {
|
---|
| 73 | DoubleData progress = Engine.GlobalScope.GetVariableValue<DoubleData>("Progress", false,
|
---|
| 74 | false);
|
---|
| 75 | return progress == null ? 0.0 : progress.Data;
|
---|
| 76 | }
|
---|
[1440] | 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 | }
|
---|