[6370] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2010 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.Drawing;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 28 | using HeuristicLab.Hive;
|
---|
| 29 |
|
---|
| 30 | namespace HeuristicLab.Clients.Hive {
|
---|
| 31 | [Item("Item Job", "Represents a executable hive job which contains a HeuristicLab Item.")]
|
---|
| 32 | [StorableClass]
|
---|
| 33 | public abstract class ItemJob : DeepCloneable, IJob {
|
---|
| 34 | public virtual bool IsParallelizable {
|
---|
| 35 | get { return true; }
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | [Storable]
|
---|
| 39 | protected IItem item;
|
---|
| 40 | public IItem Item {
|
---|
| 41 | get { return item; }
|
---|
| 42 | set {
|
---|
| 43 | if (value != item) {
|
---|
| 44 | if (item != null) DeregisterItemEvents();
|
---|
| 45 | item = value;
|
---|
| 46 | if (item != null) RegisterItemEvents();
|
---|
| 47 | OnItemChanged();
|
---|
| 48 | }
|
---|
| 49 | }
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | [Storable]
|
---|
| 53 | protected bool computeInParallel;
|
---|
| 54 | public bool ComputeInParallel {
|
---|
| 55 | get { return computeInParallel; }
|
---|
| 56 | set {
|
---|
| 57 | if (computeInParallel != value) {
|
---|
| 58 | computeInParallel = value;
|
---|
| 59 | OnComputeInParallelChanged();
|
---|
| 60 | }
|
---|
| 61 | }
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | [Storable]
|
---|
| 65 | private bool collectChildJobs;
|
---|
| 66 | public bool CollectChildJobs {
|
---|
| 67 | get { return collectChildJobs; }
|
---|
| 68 | set { collectChildJobs = value; }
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | #region Constructors and Cloning
|
---|
| 72 | public ItemJob() { }
|
---|
| 73 |
|
---|
| 74 | [StorableConstructor]
|
---|
| 75 | protected ItemJob(bool deserializing) { }
|
---|
| 76 | protected ItemJob(ItemJob original, Cloner cloner)
|
---|
| 77 | : base(original, cloner) {
|
---|
| 78 | this.ComputeInParallel = original.ComputeInParallel;
|
---|
| 79 | this.CollectChildJobs = original.CollectChildJobs;
|
---|
| 80 | this.Item = cloner.Clone(original.Item);
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 84 | protected virtual void AfterDeserialization() {
|
---|
| 85 | RegisterItemEvents();
|
---|
| 86 | }
|
---|
| 87 | #endregion
|
---|
| 88 |
|
---|
| 89 | #region Item Events
|
---|
| 90 | protected virtual void RegisterItemEvents() {
|
---|
| 91 | item.ItemImageChanged += new EventHandler(item_ItemImageChanged);
|
---|
| 92 | item.ToStringChanged += new EventHandler(item_ToStringChanged);
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | protected virtual void DeregisterItemEvents() {
|
---|
| 96 | item.ItemImageChanged -= new EventHandler(item_ItemImageChanged);
|
---|
| 97 | item.ToStringChanged -= new EventHandler(item_ToStringChanged);
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | protected void item_ToStringChanged(object sender, EventArgs e) {
|
---|
| 101 | this.OnToStringChanged();
|
---|
| 102 | }
|
---|
| 103 | protected void item_ItemImageChanged(object sender, EventArgs e) {
|
---|
| 104 | this.OnItemImageChanged();
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | #endregion
|
---|
| 108 |
|
---|
| 109 | #region IJob Members
|
---|
| 110 |
|
---|
| 111 | public abstract ExecutionState ExecutionState { get; }
|
---|
| 112 |
|
---|
| 113 | public abstract TimeSpan ExecutionTime { get; }
|
---|
| 114 |
|
---|
| 115 | public abstract void Prepare();
|
---|
| 116 |
|
---|
| 117 | public abstract void Start();
|
---|
| 118 |
|
---|
| 119 | public abstract void Pause();
|
---|
| 120 |
|
---|
| 121 | public abstract void Stop();
|
---|
| 122 |
|
---|
| 123 | public abstract void Resume(IEnumerable<IJob> childJobs);
|
---|
| 124 |
|
---|
| 125 | public event EventHandler JobStarted;
|
---|
| 126 | protected virtual void OnJobStarted() {
|
---|
| 127 | EventHandler handler = JobStarted;
|
---|
| 128 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | public event EventHandler JobStopped;
|
---|
| 132 | protected virtual void OnJobStopped() {
|
---|
| 133 | EventHandler handler = JobStopped;
|
---|
| 134 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | public event EventHandler JobPaused;
|
---|
| 138 | protected virtual void OnJobPaused() {
|
---|
| 139 | EventHandler handler = JobPaused;
|
---|
| 140 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | public event EventHandler JobFailed;
|
---|
| 144 | protected virtual void OnJobFailed(EventArgs<Exception> e) {
|
---|
| 145 | EventHandler handler = JobFailed;
|
---|
| 146 | if (handler != null) handler(this, e);
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | public event EventHandler<EventArgs<IJob>> NewChildJob;
|
---|
| 150 | protected virtual void OnNewChildJob(IJob job) {
|
---|
| 151 | EventHandler<EventArgs<IJob>> handler = NewChildJob;
|
---|
| 152 | if (handler != null) handler(this, new EventArgs<IJob>(job));
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | public event EventHandler WaitForChildJobs;
|
---|
| 156 | protected virtual void OnWaitForChildJobs() {
|
---|
| 157 | EventHandler handler = WaitForChildJobs;
|
---|
| 158 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | public event EventHandler DeleteChildJobs;
|
---|
| 162 | protected virtual void OnDeleteChildJobs() {
|
---|
| 163 | EventHandler handler = DeleteChildJobs;
|
---|
| 164 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | public event EventHandler ComputeInParallelChanged;
|
---|
| 168 | protected virtual void OnComputeInParallelChanged() {
|
---|
| 169 | EventHandler handler = ComputeInParallelChanged;
|
---|
| 170 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | public event EventHandler ItemChanged;
|
---|
| 174 | protected virtual void OnItemChanged() {
|
---|
| 175 | EventHandler handler = ItemChanged;
|
---|
| 176 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 177 | }
|
---|
| 178 | #endregion
|
---|
| 179 |
|
---|
| 180 | #region INamedItem Members
|
---|
| 181 | public abstract bool CanChangeDescription { get; }
|
---|
| 182 |
|
---|
| 183 | public abstract bool CanChangeName { get; }
|
---|
| 184 |
|
---|
| 185 | public abstract string Description { get; set; }
|
---|
| 186 |
|
---|
| 187 | public abstract string Name { get; set; }
|
---|
| 188 | #endregion
|
---|
| 189 |
|
---|
| 190 | #region Events
|
---|
| 191 | public event EventHandler DescriptionChanged;
|
---|
| 192 | protected virtual void OnDescriptionChanged() {
|
---|
| 193 | var handler = DescriptionChanged;
|
---|
| 194 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 195 | }
|
---|
| 196 | public event EventHandler ItemImageChanged;
|
---|
| 197 | protected virtual void OnItemImageChanged() {
|
---|
| 198 | var handler = ItemImageChanged;
|
---|
| 199 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 200 | }
|
---|
| 201 | public event EventHandler ToStringChanged;
|
---|
| 202 | protected virtual void OnToStringChanged() {
|
---|
| 203 | var handler = ToStringChanged;
|
---|
| 204 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 205 | }
|
---|
| 206 | public event EventHandler NameChanged;
|
---|
| 207 | protected virtual void OnNameChanged() {
|
---|
| 208 | var handler = NameChanged;
|
---|
| 209 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 210 | }
|
---|
| 211 | public event EventHandler<CancelEventArgs<string>> NameChanging;
|
---|
| 212 | protected virtual void OnNameChanging(string value, bool cancel) {
|
---|
| 213 | var handler = NameChanging;
|
---|
| 214 | if (handler != null) handler(this, new CancelEventArgs<string>(value, cancel));
|
---|
| 215 | }
|
---|
| 216 | public event EventHandler ExecutionTimeChanged;
|
---|
| 217 | protected virtual void OnExecutionTimeChanged() {
|
---|
| 218 | EventHandler handler = ExecutionTimeChanged;
|
---|
| 219 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 220 | }
|
---|
| 221 | public event EventHandler ExecutionStateChanged;
|
---|
| 222 | protected virtual void OnExecutionStateChanged() {
|
---|
| 223 | EventHandler handler = ExecutionStateChanged;
|
---|
| 224 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 225 | }
|
---|
| 226 | #endregion
|
---|
| 227 |
|
---|
| 228 | #region IItem Members
|
---|
| 229 | public virtual string ItemDescription {
|
---|
| 230 | get { return item.ItemDescription; }
|
---|
| 231 | }
|
---|
| 232 |
|
---|
| 233 | public virtual Image ItemImage {
|
---|
| 234 | get { return item.ItemImage; }
|
---|
| 235 | }
|
---|
| 236 |
|
---|
| 237 | public virtual string ItemName {
|
---|
| 238 | get { return item.ItemName; }
|
---|
| 239 | }
|
---|
| 240 |
|
---|
| 241 | public virtual Version ItemVersion {
|
---|
| 242 | get { return item.ItemVersion; }
|
---|
| 243 | }
|
---|
| 244 | #endregion
|
---|
| 245 |
|
---|
| 246 | public override string ToString() {
|
---|
| 247 | return Name;
|
---|
| 248 | }
|
---|
| 249 | }
|
---|
| 250 | }
|
---|