[6976] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[7259] | 3 | * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[6976] | 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.Drawing;
|
---|
| 24 | using HeuristicLab.Common;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Hive;
|
---|
| 27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 28 |
|
---|
| 29 | namespace HeuristicLab.Clients.Hive {
|
---|
| 30 | [Item("Item Task", "Represents a executable hive task which contains a HeuristicLab Item.")]
|
---|
| 31 | [StorableClass]
|
---|
| 32 | public abstract class ItemTask : NamedItem, ITask {
|
---|
| 33 | public virtual bool IsParallelizable {
|
---|
| 34 | get { return true; }
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | [Storable]
|
---|
| 38 | protected IItem item;
|
---|
| 39 | public IItem Item {
|
---|
| 40 | get { return item; }
|
---|
| 41 | set {
|
---|
| 42 | if (value != item) {
|
---|
| 43 | if (item != null) DeregisterItemEvents();
|
---|
| 44 | item = value;
|
---|
| 45 | if (item != null) RegisterItemEvents();
|
---|
| 46 | OnItemChanged();
|
---|
| 47 | }
|
---|
| 48 | }
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | [Storable]
|
---|
| 52 | protected bool computeInParallel;
|
---|
| 53 | public bool ComputeInParallel {
|
---|
| 54 | get { return computeInParallel; }
|
---|
| 55 | set {
|
---|
| 56 | if (computeInParallel != value) {
|
---|
| 57 | computeInParallel = value;
|
---|
| 58 | OnComputeInParallelChanged();
|
---|
| 59 | }
|
---|
| 60 | }
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | #region Constructors and Cloning
|
---|
| 64 | public ItemTask() { }
|
---|
| 65 |
|
---|
| 66 | [StorableConstructor]
|
---|
| 67 | protected ItemTask(bool deserializing) { }
|
---|
| 68 | protected ItemTask(ItemTask original, Cloner cloner)
|
---|
| 69 | : base(original, cloner) {
|
---|
| 70 | this.ComputeInParallel = original.ComputeInParallel;
|
---|
| 71 | this.Item = cloner.Clone(original.Item);
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 75 | protected virtual void AfterDeserialization() {
|
---|
| 76 | RegisterItemEvents();
|
---|
| 77 | }
|
---|
| 78 | #endregion
|
---|
| 79 |
|
---|
| 80 | #region Item Events
|
---|
| 81 | protected virtual void RegisterItemEvents() {
|
---|
| 82 | item.ItemImageChanged += new EventHandler(item_ItemImageChanged);
|
---|
| 83 | item.ToStringChanged += new EventHandler(item_ToStringChanged);
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | protected virtual void DeregisterItemEvents() {
|
---|
| 87 | item.ItemImageChanged -= new EventHandler(item_ItemImageChanged);
|
---|
| 88 | item.ToStringChanged -= new EventHandler(item_ToStringChanged);
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | protected void item_ToStringChanged(object sender, EventArgs e) {
|
---|
| 92 | this.OnToStringChanged();
|
---|
| 93 | }
|
---|
| 94 | protected void item_ItemImageChanged(object sender, EventArgs e) {
|
---|
| 95 | this.OnItemImageChanged();
|
---|
| 96 | }
|
---|
| 97 | #endregion
|
---|
| 98 |
|
---|
| 99 | #region ITask Members
|
---|
| 100 | public abstract ExecutionState ExecutionState { get; }
|
---|
| 101 |
|
---|
| 102 | public abstract TimeSpan ExecutionTime { get; }
|
---|
| 103 |
|
---|
| 104 | public abstract void Prepare();
|
---|
| 105 |
|
---|
| 106 | public abstract void Start();
|
---|
| 107 |
|
---|
| 108 | public abstract void Pause();
|
---|
| 109 |
|
---|
| 110 | public abstract void Stop();
|
---|
| 111 |
|
---|
| 112 | public event EventHandler TaskStarted;
|
---|
| 113 | protected virtual void OnTaskStarted() {
|
---|
| 114 | EventHandler handler = TaskStarted;
|
---|
| 115 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | public event EventHandler TaskStopped;
|
---|
| 119 | protected virtual void OnTaskStopped() {
|
---|
| 120 | EventHandler handler = TaskStopped;
|
---|
| 121 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | public event EventHandler TaskPaused;
|
---|
| 125 | protected virtual void OnTaskPaused() {
|
---|
| 126 | EventHandler handler = TaskPaused;
|
---|
| 127 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | public event EventHandler TaskFailed;
|
---|
| 131 | protected virtual void OnTaskFailed(EventArgs<Exception> e) {
|
---|
| 132 | EventHandler handler = TaskFailed;
|
---|
| 133 | if (handler != null) handler(this, e);
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | public event EventHandler ComputeInParallelChanged;
|
---|
| 137 | protected virtual void OnComputeInParallelChanged() {
|
---|
| 138 | EventHandler handler = ComputeInParallelChanged;
|
---|
| 139 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | public event EventHandler ItemChanged;
|
---|
| 143 | protected virtual void OnItemChanged() {
|
---|
| 144 | EventHandler handler = ItemChanged;
|
---|
| 145 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 146 | }
|
---|
| 147 | #endregion
|
---|
| 148 |
|
---|
| 149 | #region INamedItem Members
|
---|
[7048] | 150 | public abstract new bool CanChangeDescription { get; }
|
---|
[6976] | 151 |
|
---|
[7048] | 152 | public abstract new bool CanChangeName { get; }
|
---|
[6976] | 153 |
|
---|
[7048] | 154 | public abstract new string Description { get; set; }
|
---|
[6976] | 155 |
|
---|
[7048] | 156 | public abstract new string Name { get; set; }
|
---|
[6976] | 157 | #endregion
|
---|
| 158 |
|
---|
| 159 | #region Events
|
---|
| 160 | public event EventHandler ExecutionTimeChanged;
|
---|
| 161 | protected virtual void OnExecutionTimeChanged() {
|
---|
| 162 | EventHandler handler = ExecutionTimeChanged;
|
---|
| 163 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 164 | }
|
---|
| 165 | public event EventHandler ExecutionStateChanged;
|
---|
| 166 | protected virtual void OnExecutionStateChanged() {
|
---|
| 167 | EventHandler handler = ExecutionStateChanged;
|
---|
| 168 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 169 | }
|
---|
| 170 | #endregion
|
---|
| 171 |
|
---|
| 172 | #region IItem Members
|
---|
[7532] | 173 | public override string ItemDescription {
|
---|
[7530] | 174 | get {
|
---|
| 175 | if (item == null)
|
---|
| 176 | return string.Empty;
|
---|
| 177 | else
|
---|
| 178 | return item.ItemDescription;
|
---|
| 179 | }
|
---|
[6976] | 180 | }
|
---|
| 181 |
|
---|
[7532] | 182 | public override Image ItemImage {
|
---|
[7530] | 183 | get {
|
---|
| 184 | if (item == null)
|
---|
| 185 | return HeuristicLab.Common.Resources.VSImageLibrary.Class;
|
---|
| 186 | else
|
---|
| 187 | return item.ItemImage;
|
---|
| 188 | }
|
---|
[6976] | 189 | }
|
---|
| 190 |
|
---|
[7532] | 191 | public override string ItemName {
|
---|
[7530] | 192 | get {
|
---|
| 193 | if (item == null)
|
---|
| 194 | return string.Empty;
|
---|
| 195 | else
|
---|
| 196 | return item.ItemName;
|
---|
| 197 | }
|
---|
[6976] | 198 | }
|
---|
| 199 |
|
---|
[7530] | 200 | public virtual new Version ItemVersion {
|
---|
| 201 | get {
|
---|
| 202 | if (item == null)
|
---|
| 203 | return ItemAttribute.GetVersion(this.GetType());
|
---|
| 204 | else
|
---|
| 205 | return item.ItemVersion;
|
---|
| 206 | }
|
---|
[6976] | 207 | }
|
---|
| 208 | #endregion
|
---|
| 209 |
|
---|
| 210 | public override string ToString() {
|
---|
| 211 | return Name;
|
---|
| 212 | }
|
---|
| 213 | }
|
---|
| 214 | }
|
---|