Changeset 6033 for branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive/3.4/ExperimentManager/HiveJob.cs
- Timestamp:
- 04/19/11 18:58:53 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive/3.4/ExperimentManager/HiveJob.cs
r6006 r6033 24 24 using System.Drawing; 25 25 using System.Linq; 26 using HeuristicLab.Clients.Hive.Jobs;27 26 using HeuristicLab.Collections; 28 27 using HeuristicLab.Common; 29 28 using HeuristicLab.Core; 30 using HeuristicLab. Optimization;29 using HeuristicLab.Hive; 31 30 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 32 31 using HeuristicLab.PluginInfrastructure; … … 36 35 [Item("Hive Job", "Represents a hive job.")] 37 36 [StorableClass] 38 public class HiveJob : NamedItem, IItemTree {39 pr ivatestatic object locker = new object();37 public class HiveJob : NamedItem, IItemTree<HiveJob> { 38 protected static object locker = new object(); 40 39 41 40 public override Image ItemImage { … … 56 55 } 57 56 58 pr ivateJob job;57 protected Job job; 59 58 public Job Job { 60 59 get { return job; } … … 70 69 71 70 [Storable] 72 pr ivate OptimizerJob optimizerJob;73 public OptimizerJob OptimizerJob{74 get { return optimizerJob; }71 protected ItemJob jobItem; 72 public ItemJob JobItem { 73 get { return jobItem; } 75 74 internal set { 76 if ( optimizerJob!= null && syncJobsWithOptimizers) {75 if (jobItem != null && syncJobsWithOptimizers) { 77 76 this.childHiveJobs.Clear(); 78 77 } 79 if ( optimizerJob!= value) {80 Dergister OptimizerEvents();81 optimizerJob= value;82 Register OptimizerEvents();83 On OptimizerJobChanged();78 if (jobItem != value) { 79 DergisterJobItemsEvents(); 80 jobItem = value; 81 RegisterJobItemEvents(); 82 OnJobItemChanged(); 84 83 } 85 84 } … … 87 86 88 87 [Storable] 89 pr ivateItemList<HiveJob> childHiveJobs;90 public ReadOnlyItemList<HiveJob> ChildHiveJobs {88 protected ItemList<HiveJob> childHiveJobs; 89 public virtual ReadOnlyItemList<HiveJob> ChildHiveJobs { 91 90 get { return childHiveJobs.AsReadOnly(); } 92 91 } 93 92 94 93 [Storable] 95 private bool syncJobsWithOptimizers = true; 96 94 protected bool syncJobsWithOptimizers = true; 95 96 #region Constructors and Cloning 97 97 public HiveJob() { 98 98 this.Job = new Job() { … … 106 106 } 107 107 108 public HiveJob( OptimizerJob optimizerJob, bool autoCreateChildHiveJobs)108 public HiveJob(ItemJob jobItem, bool autoCreateChildHiveJobs) 109 109 : this() { 110 110 this.syncJobsWithOptimizers = autoCreateChildHiveJobs; 111 this. OptimizerJob = optimizerJob;111 this.JobItem = jobItem; 112 112 this.syncJobsWithOptimizers = true; 113 }114 115 public HiveJob(IOptimizer optimizer)116 : this() {117 this.OptimizerJob = new OptimizerJob(optimizer);118 113 } 119 114 … … 122 117 this.Job = job; 123 118 try { 124 this. OptimizerJob = PersistenceUtil.Deserialize<OptimizerJob>(jobData.Data);119 this.JobItem = PersistenceUtil.Deserialize<ItemJob>(jobData.Data); 125 120 } 126 121 catch { 127 this. OptimizerJob= null;122 this.JobItem = null; 128 123 } 129 124 this.childHiveJobs = new ItemList<HiveJob>(); … … 135 130 : base(original, cloner) { 136 131 this.Job = cloner.Clone(original.job); 137 this.OptimizerJob = cloner.Clone(original.OptimizerJob); 132 this.JobItem = cloner.Clone(original.JobItem); 133 this.childHiveJobs = cloner.Clone(original.childHiveJobs); 134 this.syncJobsWithOptimizers = original.syncJobsWithOptimizers; 138 135 } 139 136 public override IDeepCloneable Clone(Cloner cloner) { 140 137 return new HiveJob(this, cloner); 141 138 } 142 143 /// <summary> 144 /// if this.Optimizer is an experiment 145 /// Uses the child-optimizers of this.HiveJob and creates HiveJob-childs 146 /// if this.Optimizer is a batchrun 147 /// Creates a number of child-jobs according to repetitions 148 /// </summary> 149 private void UpdateChildHiveJobs() { 150 if (Job != null && syncJobsWithOptimizers) { 151 if (!OptimizerJob.ComputeInParallel) { 152 this.childHiveJobs.Clear(); 153 } else { 154 if (OptimizerJob.Optimizer is Optimization.Experiment) { 155 Optimization.Experiment experiment = (Optimization.Experiment)OptimizerJob.Optimizer; 156 foreach (IOptimizer childOpt in experiment.Optimizers) { 157 this.childHiveJobs.Add(new HiveJob(childOpt)); 158 } 159 } else if (OptimizerJob.Optimizer is Optimization.BatchRun) { 160 Optimization.BatchRun batchRun = OptimizerJob.OptimizerAsBatchRun; 161 if (batchRun.Optimizer != null) { 162 while (this.childHiveJobs.Count < batchRun.Repetitions) { 163 this.childHiveJobs.Add(new HiveJob(batchRun.Optimizer)); 164 } 165 while (this.childHiveJobs.Count > batchRun.Repetitions) { 166 this.childHiveJobs.Remove(this.childHiveJobs.Last()); 167 } 168 } 169 } 170 } 171 } 172 } 173 174 private void RegisterOptimizerEvents() { 175 if (OptimizerJob != null) { 176 if (OptimizerJob.Optimizer is Optimization.Experiment) { 177 Optimization.Experiment experiment = OptimizerJob.OptimizerAsExperiment; 178 experiment.Optimizers.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_ItemsAdded); 179 experiment.Optimizers.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_ItemsReplaced); 180 experiment.Optimizers.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_ItemsRemoved); 181 experiment.Optimizers.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_CollectionReset); 182 } else if (OptimizerJob.Optimizer is Optimization.BatchRun) { 183 Optimization.BatchRun batchRun = OptimizerJob.OptimizerAsBatchRun; 184 batchRun.RepetitionsChanged += new EventHandler(batchRun_RepetitionsChanged); 185 batchRun.OptimizerChanged += new EventHandler(batchRun_OptimizerChanged); 186 } 187 OptimizerJob.ComputeInParallelChanged += new EventHandler(OptimizerJob_ComputeInParallelChanged); 188 OptimizerJob.ToStringChanged += new EventHandler(OptimizerJob_ToStringChanged); 189 } 190 } 191 private void DergisterOptimizerEvents() { 192 if (OptimizerJob != null) { 193 if (OptimizerJob.Optimizer is Optimization.Experiment) { 194 Optimization.Experiment experiment = OptimizerJob.OptimizerAsExperiment; 195 experiment.Optimizers.ItemsAdded -= new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_ItemsAdded); 196 experiment.Optimizers.ItemsReplaced -= new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_ItemsReplaced); 197 experiment.Optimizers.ItemsRemoved -= new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_ItemsRemoved); 198 experiment.Optimizers.CollectionReset -= new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_CollectionReset); 199 } else if (OptimizerJob.Optimizer is Optimization.BatchRun) { 200 Optimization.BatchRun batchRun = OptimizerJob.OptimizerAsBatchRun; 201 batchRun.RepetitionsChanged -= new EventHandler(batchRun_RepetitionsChanged); 202 batchRun.OptimizerChanged -= new EventHandler(batchRun_OptimizerChanged); 203 } 204 OptimizerJob.ComputeInParallelChanged -= new EventHandler(OptimizerJob_ComputeInParallelChanged); 205 OptimizerJob.ToStringChanged -= new EventHandler(OptimizerJob_ToStringChanged); 206 } 207 } 208 209 private void RegisterChildHiveJobEvents() { 139 #endregion 140 141 protected virtual void UpdateChildHiveJobs() { } 142 143 protected virtual void RegisterJobItemEvents() { 144 if (JobItem != null) { 145 JobItem.ComputeInParallelChanged += new EventHandler(JobItem_ComputeInParallelChanged); 146 JobItem.ToStringChanged += new EventHandler(JobItem_ToStringChanged); 147 } 148 } 149 protected virtual void DergisterJobItemsEvents() { 150 if (JobItem != null) { 151 JobItem.ComputeInParallelChanged -= new EventHandler(JobItem_ComputeInParallelChanged); 152 JobItem.ToStringChanged -= new EventHandler(JobItem_ToStringChanged); 153 } 154 } 155 156 protected virtual void RegisterChildHiveJobEvents() { 210 157 this.childHiveJobs.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<HiveJob>>(OnItemsAdded); 211 158 this.childHiveJobs.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<HiveJob>>(OnItemsRemoved); 212 159 this.childHiveJobs.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<HiveJob>>(OnCollectionReset); 213 160 } 214 pr ivatevoid DeregisterChildHiveJobEvents() {161 protected virtual void DeregisterChildHiveJobEvents() { 215 162 this.childHiveJobs.ItemsAdded -= new CollectionItemsChangedEventHandler<IndexedItem<HiveJob>>(OnItemsAdded); 216 163 this.childHiveJobs.ItemsRemoved -= new CollectionItemsChangedEventHandler<IndexedItem<HiveJob>>(OnItemsRemoved); … … 218 165 } 219 166 220 private void batchRun_OptimizerChanged(object sender, EventArgs e) { 221 if (syncJobsWithOptimizers) { 222 UpdateChildHiveJobs(); 223 } 224 } 225 226 private void batchRun_RepetitionsChanged(object sender, EventArgs e) { 227 if (syncJobsWithOptimizers) { 228 UpdateChildHiveJobs(); 229 } 230 } 231 232 private void OptimizerJob_ToStringChanged(object sender, EventArgs e) { 167 protected virtual void JobItem_ToStringChanged(object sender, EventArgs e) { 233 168 this.OnToStringChanged(); 234 169 } 235 236 private void Optimizers_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IOptimizer>> e) { 237 if (syncJobsWithOptimizers && this.OptimizerJob.ComputeInParallel) { 238 foreach (var item in e.Items) { 239 if (GetChildByOptimizer(item.Value) == null && item.Value.Name != "Placeholder") { 240 this.childHiveJobs.Add(new HiveJob(item.Value)); 241 } 242 } 243 } 244 } 245 private void Optimizers_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<IOptimizer>> e) { 246 if (syncJobsWithOptimizers && this.OptimizerJob.ComputeInParallel) { 247 foreach (var item in e.OldItems) { 248 this.childHiveJobs.Remove(this.GetChildByOptimizer(item.Value)); 249 } 250 foreach (var item in e.Items) { 251 if (GetChildByOptimizer(item.Value) == null && item.Value.Name != "Placeholder") { 252 this.childHiveJobs.Add(new HiveJob(item.Value)); 253 } 254 } 255 } 256 } 257 private void Optimizers_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<IOptimizer>> e) { 258 if (syncJobsWithOptimizers && this.OptimizerJob.ComputeInParallel) { 259 foreach (var item in e.Items) { 260 this.childHiveJobs.Remove(this.GetChildByOptimizer(item.Value)); 261 } 262 } 263 } 264 private void Optimizers_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<IOptimizer>> e) { 265 if (syncJobsWithOptimizers && this.OptimizerJob.ComputeInParallel) { 266 foreach (var item in e.Items) { 267 this.childHiveJobs.Remove(this.GetChildByOptimizer(item.Value)); 268 } 269 } 270 } 271 272 private void OptimizerJob_ComputeInParallelChanged(object sender, EventArgs e) { 273 if (OptimizerJob != null && syncJobsWithOptimizers) { 170 171 protected virtual void JobItem_ComputeInParallelChanged(object sender, EventArgs e) { 172 if (JobItem != null && syncJobsWithOptimizers) { 274 173 this.UpdateChildHiveJobs(); 275 174 } 276 175 } 277 176 278 public v oid AddChildHiveJob(HiveJob hiveJob) {177 public virtual void AddChildHiveJob(HiveJob hiveJob) { 279 178 this.childHiveJobs.Add(hiveJob); 280 syncJobsWithOptimizers = false;281 if (this.OptimizerJob != null && hiveJob.OptimizerJob != null) {282 // if job is in state Paused, it has to preserve its ResultCollection, which is cleared when a optimizer is added to an experiment283 OptimizerJob optimizerJobClone = null;284 if (hiveJob.Job.State == JobState.Paused) {285 optimizerJobClone = (OptimizerJob)hiveJob.OptimizerJob.Clone();286 }287 288 if (this.OptimizerJob.Optimizer is Optimization.Experiment) {289 if (!this.OptimizerJob.OptimizerAsExperiment.Optimizers.Contains(hiveJob.OptimizerJob.Optimizer)) {290 UpdateOptimizerInExperiment(this.OptimizerJob.OptimizerAsExperiment, hiveJob.OptimizerJob);291 }292 } else if (this.OptimizerJob.Optimizer is Optimization.BatchRun) {293 UpdateOptimizerInBatchRun(this.OptimizerJob.OptimizerAsBatchRun, hiveJob.OptimizerJob);294 }295 296 if (hiveJob.Job.State == JobState.Paused) {297 hiveJob.OptimizerJob = optimizerJobClone;298 }299 }300 syncJobsWithOptimizers = true;301 }302 303 /// <summary>304 /// if this.Optimizer is Experiment305 /// replace the child-optimizer in the experiment306 /// if this.Optimizer is BatchRun307 /// add the runs from the optimizerJob to the batchrun and replace the Optimizer308 /// </summary>309 public void UpdateChildOptimizer(OptimizerJob optimizerJob, Guid childJobId) {310 syncJobsWithOptimizers = false; // don't sync with optimizers during this method311 bool childIsFinishedOptimizerDownloaded = false;312 313 if (this.OptimizerJob != null && this.OptimizerJob.Optimizer != null) {314 if (this.OptimizerJob.Optimizer is Optimization.Experiment) {315 UpdateOptimizerInExperiment(this.OptimizerJob.OptimizerAsExperiment, optimizerJob);316 childIsFinishedOptimizerDownloaded = true;317 } else if (this.OptimizerJob.Optimizer is Optimization.BatchRun) {318 UpdateOptimizerInBatchRun(this.OptimizerJob.OptimizerAsBatchRun, optimizerJob);319 if (this.OptimizerJob.OptimizerAsBatchRun.Repetitions == this.OptimizerJob.Optimizer.Runs.Count) {320 childIsFinishedOptimizerDownloaded = true;321 }322 } else {323 childIsFinishedOptimizerDownloaded = optimizerJob.Optimizer.ExecutionState == ExecutionState.Stopped;324 }325 }326 327 HiveJob child = this.ChildHiveJobs.Single(j => j.Job.Id == childJobId);328 if (!optimizerJob.ComputeInParallel) {329 child.syncJobsWithOptimizers = false;330 child.OptimizerJob = optimizerJob;331 child.syncJobsWithOptimizers = true;332 }333 if (childIsFinishedOptimizerDownloaded) {334 //child.IsFinishedOptimizerDownloaded = true; // todo: clean up with childIsFinishedOptimizerDownloaded335 }336 syncJobsWithOptimizers = true;337 }338 339 /// <summary>340 /// Adds the runs from the optimizerJob to the batchrun and replaces the Optimizer341 /// Sideeffect: the optimizerJob.Optimizer will be prepared (scopes are deleted and executionstate will be reset)342 /// </summary>343 private void UpdateOptimizerInBatchRun(BatchRun batchRun, OptimizerJob optimizerJob) {344 if (batchRun.Optimizer == null) {345 batchRun.Optimizer = (IOptimizer)optimizerJob.Optimizer; // only set the first optimizer as Optimizer. if every time the Optimizer would be set, the runs would be cleared each time346 }347 foreach (IRun run in optimizerJob.Optimizer.Runs) {348 if (!batchRun.Runs.Contains(run)) {349 run.Name = GetNewRunName(run, batchRun.Runs);350 batchRun.Runs.Add(run);351 }352 }353 }354 355 /// <summary>356 /// Parses the run numbers out of runs and renames the run to the next number357 /// </summary>358 private static string GetNewRunName(IRun run, RunCollection runs) {359 int idx = run.Name.IndexOf("Run ") + 4;360 361 if (idx == -1 || runs.Count == 0)362 return run.Name;363 364 int maxRunNumber = int.MinValue;365 foreach (IRun r in runs) {366 int number = GetRunNumber(r.Name);367 maxRunNumber = Math.Max(maxRunNumber, number);368 }369 370 return run.Name.Substring(0, idx) + (maxRunNumber + 1).ToString();371 }372 373 /// <summary>374 /// Parses the number of a Run out of its name. Example "Genetic Algorithm Run 3" -> 3375 /// </summary>376 private static int GetRunNumber(string runName) {377 int idx = runName.IndexOf("Run ") + 4;378 if (idx == -1) {379 return 0;380 } else {381 return int.Parse(runName.Substring(idx, runName.Length - idx));382 }383 }384 385 /// <summary>386 /// replace the child-optimizer in the experiment387 /// Sideeffect: the optimizerJob.Optimizer will be prepared (scopes are deleted and executionstate will be reset)388 /// </summary>389 private void UpdateOptimizerInExperiment(Optimization.Experiment experiment, OptimizerJob optimizerJob) {390 if (optimizerJob.IndexInParentOptimizerList < 0)391 throw new IndexOutOfRangeException("IndexInParentOptimizerList must be equal or greater than zero! The Job is invalid and the optimizer-tree cannot be reassembled.");392 393 while (experiment.Optimizers.Count < optimizerJob.IndexInParentOptimizerList) {394 experiment.Optimizers.Add(new UserDefinedAlgorithm("Placeholder")); // add dummy-entries to Optimizers so that its possible to insert the optimizerJob at the correct position395 }396 if (experiment.Optimizers.Count < optimizerJob.IndexInParentOptimizerList + 1) {397 experiment.Optimizers.Add(optimizerJob.Optimizer);398 } else {399 // if ComputeInParallel==true, don't replace the optimizer (except it is still a Placeholder)400 // this is because Jobs with ComputeInParallel get submitted to hive with their child-optimizers deleted401 if (!optimizerJob.ComputeInParallel || experiment.Optimizers[optimizerJob.IndexInParentOptimizerList].Name == "Placeholder") {402 experiment.Optimizers[optimizerJob.IndexInParentOptimizerList] = optimizerJob.Optimizer;403 }404 }405 }406 407 /// <summary>408 /// Sets the IndexInParentOptimizerList property of the OptimizerJob409 /// according to the position in the OptimizerList of the parentHiveJob.Job410 /// Recursively updates all the child-jobs as well411 /// </summary>412 internal void SetIndexInParentOptimizerList(HiveJob parentHiveJob) {413 if (parentHiveJob != null) {414 if (parentHiveJob.OptimizerJob.Optimizer is Optimization.Experiment) {415 this.OptimizerJob.IndexInParentOptimizerList = parentHiveJob.OptimizerJob.OptimizerAsExperiment.Optimizers.IndexOf(this.OptimizerJob.Optimizer);416 } else if (parentHiveJob.OptimizerJob.Optimizer is Optimization.BatchRun) {417 this.OptimizerJob.IndexInParentOptimizerList = 0;418 } else {419 throw new NotSupportedException("Only Experiment and BatchRuns are supported");420 }421 }422 foreach (HiveJob child in childHiveJobs) {423 child.SetIndexInParentOptimizerList(this);424 }425 179 } 426 180 427 181 public override string ToString() { 428 if ( optimizerJob!= null) {429 return optimizerJob.ToString();182 if (jobItem != null) { 183 return jobItem.ToString(); 430 184 } else { 431 185 return base.ToString(); … … 433 187 } 434 188 435 public v oid UpdateFromLightweightJob(LightweightJob lightweightJob) {189 public virtual void UpdateFromLightweightJob(LightweightJob lightweightJob) { 436 190 if (lightweightJob != null) { 437 191 job.Id = lightweightJob.Id; … … 455 209 /// if true the Child-Optimizers will not be serialized (if the job contains an Experiment) 456 210 /// </param> 457 public JobData GetAsJobData(bool withoutChildOptimizers, out List<IPluginDescription> plugins) { 458 plugins = new List<IPluginDescription>(); 459 if (this.optimizerJob == null || this.optimizerJob.Optimizer == null) 460 return null; 461 462 IEnumerable<Type> usedTypes; 463 byte[] jobByteArray; 464 if (withoutChildOptimizers && this.OptimizerJob.Optimizer is Optimization.Experiment) { 465 OptimizerJob clonedJob = (OptimizerJob)this.OptimizerJob.Clone(); // use a cloned job, so that the childHiveJob don't get confused 466 clonedJob.OptimizerAsExperiment.Optimizers.Clear(); 467 jobByteArray = PersistenceUtil.Serialize(clonedJob, out usedTypes); 468 } else if (withoutChildOptimizers && this.OptimizerJob.Optimizer is Optimization.BatchRun) { 469 OptimizerJob clonedJob = (OptimizerJob)this.OptimizerJob.Clone(); 470 clonedJob.OptimizerAsBatchRun.Optimizer = null; 471 jobByteArray = PersistenceUtil.Serialize(clonedJob, out usedTypes); 472 } else if (this.OptimizerJob.Optimizer is IAlgorithm) { 473 ((IAlgorithm)this.OptimizerJob.Optimizer).StoreAlgorithmInEachRun = false; // avoid storing the algorithm in runs to reduce size 474 jobByteArray = PersistenceUtil.Serialize(this.OptimizerJob, out usedTypes); 475 } else { 476 jobByteArray = PersistenceUtil.Serialize(this.OptimizerJob, out usedTypes); 477 } 478 479 JobData jobData = new JobData() { 480 JobId = job.Id, 481 Data = jobByteArray 482 }; 483 484 PluginUtil.CollectDeclaringPlugins(plugins, usedTypes); 485 486 return jobData; 487 } 488 211 public virtual JobData GetAsJobData(bool withoutChildOptimizers, out List<IPluginDescription> plugins) { 212 plugins = null; 213 return null; 214 } 489 215 490 216 #region Events 491 217 public event EventHandler JobChanged; 492 218 private void OnJobChanged() { 493 LogMessage("JobChanged");494 219 EventHandler handler = JobChanged; 495 220 if (handler != null) handler(this, EventArgs.Empty); … … 498 223 public event EventHandler JobStateChanged; 499 224 private void OnJobStateChanged() { 500 LogMessage("JobStateChanged (State: " + this.Job.State + ", ExecutionTime: " + this.Job.ExecutionTime.ToString() + ")");501 225 EventHandler handler = JobStateChanged; 502 226 if (handler != null) handler(this, EventArgs.Empty); 503 227 } 504 228 505 public event EventHandler OptimizerJobChanged;506 private void On OptimizerJobChanged() {507 OptimizerJob_ComputeInParallelChanged(this, EventArgs.Empty);508 var handler = OptimizerJobChanged;229 public event EventHandler JobItemChanged; 230 private void OnJobItemChanged() { 231 JobItem_ComputeInParallelChanged(this, EventArgs.Empty); 232 var handler = JobItemChanged; 509 233 if (handler != null) handler(this, EventArgs.Empty); 510 234 } … … 516 240 } 517 241 #endregion 518 519 public void LogMessage(string message) { 520 lock (locker) { 521 if (optimizerJob != null) { 522 optimizerJob.Log.LogMessage(message); 523 } 524 } 525 } 526 242 527 243 /// <summary> 528 244 /// Returns a list of HiveJobs including this and all its child-jobs recursively 529 245 /// </summary> 530 246 public IEnumerable<HiveJob> GetAllHiveJobs() { 531 List<HiveJob>jobs = new List<HiveJob>();247 var jobs = new List<HiveJob>(); 532 248 jobs.Add(this); 533 249 foreach (HiveJob child in this.ChildHiveJobs) { … … 544 260 if (result != null) 545 261 return result; 546 }547 return null;548 }549 550 551 public HiveJob GetChildByOptimizerJob(OptimizerJob optimizerJob) {552 foreach (var child in ChildHiveJobs) {553 if (child.OptimizerJob == optimizerJob)554 return child;555 }556 return null;557 }558 559 560 public HiveJob GetChildByOptimizer(IOptimizer optimizer) {561 foreach (var child in ChildHiveJobs) {562 if (child.OptimizerJob.Optimizer == optimizer)563 return child;564 262 } 565 263 return null; … … 582 280 } 583 281 584 585 282 public void RemoveByJobId(Guid jobId) { 586 283 IEnumerable<HiveJob> jobs = ChildHiveJobs.Where(j => j.Job.Id == jobId).ToList(); // if Guid.Empty needs to be removed, there could be more than one with this jobId … … 593 290 } 594 291 595 public IEnumerable<IItemTree > GetChildItems() {292 public IEnumerable<IItemTree<HiveJob>> GetChildItems() { 596 293 return this.childHiveJobs; 597 294 } … … 599 296 #region INotifyObservableCollectionItemsChanged<IItemTree> Members 600 297 601 public event CollectionItemsChangedEventHandler<IItemTree > CollectionReset;298 public event CollectionItemsChangedEventHandler<IItemTree<HiveJob>> CollectionReset; 602 299 private void OnCollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<HiveJob>> e) { 603 300 var handler = CollectionReset; … … 605 302 } 606 303 607 public event CollectionItemsChangedEventHandler<IItemTree > ItemsAdded;304 public event CollectionItemsChangedEventHandler<IItemTree<HiveJob>> ItemsAdded; 608 305 private void OnItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<HiveJob>> e) { 609 306 var handler = ItemsAdded; … … 611 308 } 612 309 613 public event CollectionItemsChangedEventHandler<IItemTree > ItemsRemoved;310 public event CollectionItemsChangedEventHandler<IItemTree<HiveJob>> ItemsRemoved; 614 311 private void OnItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<HiveJob>> e) { 615 312 var handler = ItemsRemoved; … … 617 314 } 618 315 619 private static CollectionItemsChangedEventArgs<IItemTree > ToCollectionItemsChangedEventArgs(CollectionItemsChangedEventArgs<IndexedItem<HiveJob>> e) {620 return new CollectionItemsChangedEventArgs<IItemTree >(e.Items.Select(x => x.Value), e.OldItems == null ? null : e.OldItems.Select(x => x.Value));316 private static CollectionItemsChangedEventArgs<IItemTree<HiveJob>> ToCollectionItemsChangedEventArgs(CollectionItemsChangedEventArgs<IndexedItem<HiveJob>> e) { 317 return new CollectionItemsChangedEventArgs<IItemTree<HiveJob>>(e.Items.Select(x => x.Value), e.OldItems == null ? null : e.OldItems.Select(x => x.Value)); 621 318 } 622 319 #endregion … … 634 331 JobData jobData = new JobData(); 635 332 jobData.JobId = this.job.Id; 636 jobData.Data = PersistenceUtil.Serialize(this. optimizerJob);333 jobData.Data = PersistenceUtil.Serialize(this.jobItem); 637 334 service.UpdateJobData(this.Job, jobData); 638 335 service.RestartJob(this.job.Id); … … 641 338 }); 642 339 } 340 341 public ICollection<IItemTreeNodeAction<HiveJob>> Actions { 342 get { 343 return new List<IItemTreeNodeAction<HiveJob>>(); 344 } 345 } 346 } 347 348 [Item("Hive Job", "Represents a hive job.")] 349 [StorableClass] 350 public class HiveJob<T> : HiveJob where T : ItemJob { 351 352 public new T JobItem { 353 get { return (T)base.JobItem; } 354 internal set { base.JobItem = value; } 355 } 356 357 #region Constructors and Cloning 358 public HiveJob() : base() { } 359 protected HiveJob(HiveJob original, Cloner cloner) 360 : base(original, cloner) { 361 } 362 public override IDeepCloneable Clone(Cloner cloner) { 363 return new HiveJob<T>(this, cloner); 364 } 365 #endregion 643 366 } 644 367 }
Note: See TracChangeset
for help on using the changeset viewer.