Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive/sources/HeuristicLab.Hive.New/HeuristicLab.Clients.Hive/3.3/HiveExperiment/HiveJob.cs @ 4629

Last change on this file since 4629 was 4629, checked in by cneumuel, 14 years ago
  • worked on new hive structure
  • created IIS hostable website for hive (old structure)

(#1233)

File size: 23.1 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Drawing;
25using System.Linq;
26using HeuristicLab.Collections;
27using HeuristicLab.Common;
28using HeuristicLab.Core;
29using HeuristicLab.Optimization;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31using HeuristicLab.Services.Hive.Common.DataTransfer;
32using HeuristicLab.Clients.Hive.Jobs;
33
34namespace HeuristicLab.Clients.Hive {
35  [StorableClass]
36  public class HiveJob : Item {
37    private static object locker = new object();
38
39    public override Image ItemImage {
40      get {
41        if (job.Id == Guid.Empty) { // not yet uploaded
42          return HeuristicLab.Common.Resources.VS2008ImageLibrary.Event;
43        } else {
44          if (job.State == JobState.Waiting) return HeuristicLab.Common.Resources.VS2008ImageLibrary.ExecutablePrepared;
45          else if (job.State == JobState.WaitForChildJobs) return HeuristicLab.Common.Resources.VS2008ImageLibrary.ExecutablePrepared;
46          else if (job.State == JobState.Calculating) return HeuristicLab.Common.Resources.VS2008ImageLibrary.ExecutableStarted;
47          else if (job.State == JobState.Aborted) return HeuristicLab.Common.Resources.VS2008ImageLibrary.ExecutableStopped;
48          else if (job.State == JobState.Failed) return HeuristicLab.Common.Resources.VS2008ImageLibrary.Error;
49          else if (job.State == JobState.Finished) return HeuristicLab.Common.Resources.VS2008ImageLibrary.ExecutableStopped;
50          else return HeuristicLab.Common.Resources.VS2008ImageLibrary.Event;
51        }
52      }
53    }
54
55    [Storable]
56    private Job job;
57    public Job Job {
58      get { return job; }
59      set {
60        if (job != value) {
61          job = value;
62          OnJobChanged();
63          OnToStringChanged();
64          OnItemImageChanged();
65        }
66      }
67    }
68
69    [Storable]
70    private OptimizerJob optimizerJob;
71    public OptimizerJob OptimizerJob {
72      get { return optimizerJob; }
73      private set {
74        if (optimizerJob != null && syncJobsWithOptimizers) {
75          this.childHiveJobs.Clear();
76        }
77        if (optimizerJob != value) {
78          DergisterOptimizerEvents();
79          optimizerJob = value;
80          if (optimizerJob.ExecutionState == ExecutionState.Stopped) {
81            IsFinishedOptimizerDownloaded = true;
82          }
83          RegisterOptimizerEvents();
84          OnOptimizerJobChanged();
85        }
86      }
87    }
88
89    [Storable]
90    private HiveJobList childHiveJobs;
91    public ReadOnlyItemList<HiveJob> ChildHiveJobs {
92      get { return childHiveJobs.AsReadOnly(); }
93    }
94
95    [Storable]
96    private bool isFinishedOptimizerDownloaded;
97    public bool IsFinishedOptimizerDownloaded {
98      get { return isFinishedOptimizerDownloaded; }
99      set {
100        if (isFinishedOptimizerDownloaded != value) {
101          isFinishedOptimizerDownloaded = value;
102          OnIsFinishedOptimizerDownloadedChanged();
103        }
104      }
105    }
106
107    [Storable]
108    private bool syncJobsWithOptimizers = true;
109
110    public HiveJob() {
111      this.Job = new Job() {
112        State = JobState.Waiting,
113        DateCreated = DateTime.Now,
114        CoresNeeded = 1,
115        MemoryNeeded = 0
116      };
117      this.childHiveJobs = new HiveJobList();
118      syncJobsWithOptimizers = true;
119    }
120
121    public HiveJob(Job jobDto)
122      : this() {
123      this.Job = jobDto;
124    }
125
126    public HiveJob(LightweightJob lightweightJob)
127      : this() {
128      UpdateFromLightweightJob(lightweightJob);
129    }
130
131    public HiveJob(OptimizerJob optimizerJob, bool autoCreateChildHiveJobs)
132      : this() {
133      this.syncJobsWithOptimizers = autoCreateChildHiveJobs;
134      this.OptimizerJob = optimizerJob;
135      this.syncJobsWithOptimizers = true;
136    }
137
138    public HiveJob(IOptimizer optimizer)
139      : this() {
140        this.OptimizerJob = new OptimizerJob(optimizer);
141    }
142
143    public HiveJob(JobData serializedJob, bool autoCreateChildHiveJobs)
144      : this() {
145      this.syncJobsWithOptimizers = autoCreateChildHiveJobs;
146      this.Job = serializedJob.JobInfo;
147      this.OptimizerJob = PersistenceUtil.Deserialize<OptimizerJob>(serializedJob.Data);
148      this.syncJobsWithOptimizers = true;
149    }
150
151    /// <summary>
152    /// if this.Optimizer is an experiment
153    ///   Uses the child-optimizers of this.HiveJob and creates HiveJob-childs
154    /// if this.Optimizer is a batchrun
155    ///   Creates a number of child-jobs according to repetitions
156    /// </summary>
157    private void UpdateChildHiveJobs() {
158      if (Job != null && syncJobsWithOptimizers) {
159        if (OptimizerJob.Optimizer is Optimization.Experiment) {
160          Optimization.Experiment experiment = (Optimization.Experiment)OptimizerJob.Optimizer;
161          foreach (IOptimizer childOpt in experiment.Optimizers) {
162            this.childHiveJobs.Add(new HiveJob(childOpt));
163          }
164        } else if (OptimizerJob.Optimizer is Optimization.BatchRun) {
165          Optimization.BatchRun batchRun = OptimizerJob.OptimizerAsBatchRun;
166          if (batchRun.Algorithm != null) {
167            while (this.childHiveJobs.Count < batchRun.Repetitions) {
168              this.childHiveJobs.Add(new HiveJob(batchRun.Algorithm));
169            }
170            while (this.childHiveJobs.Count > batchRun.Repetitions) {
171              this.childHiveJobs.Remove(this.childHiveJobs.Last());
172            }
173          }
174        }
175      }
176    }
177
178    private void RegisterOptimizerEvents() {
179      if (OptimizerJob != null) {
180        if (OptimizerJob.Optimizer is Optimization.Experiment) {
181          Optimization.Experiment experiment = OptimizerJob.OptimizerAsExperiment;
182          experiment.Optimizers.ItemsAdded += new Collections.CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_ItemsAdded);
183          experiment.Optimizers.ItemsReplaced += new Collections.CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_ItemsReplaced);
184          experiment.Optimizers.ItemsRemoved += new Collections.CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_ItemsRemoved);
185          experiment.Optimizers.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_CollectionReset);
186        } else if (OptimizerJob.Optimizer is Optimization.BatchRun) {
187          Optimization.BatchRun batchRun = OptimizerJob.OptimizerAsBatchRun;
188          batchRun.RepetitionsChanged += new EventHandler(batchRun_RepetitionsChanged);
189          batchRun.AlgorithmChanged += new EventHandler(batchRun_AlgorithmChanged);
190        }
191
192        OptimizerJob.ComputeInParallelChanged += new EventHandler(OptimizerJob_ComputeInParallelChanged);
193        OptimizerJob.ToStringChanged += new EventHandler(OptimizerJob_ToStringChanged);
194      }
195    }
196    private void DergisterOptimizerEvents() {
197      if (Job != null && OptimizerJob.Optimizer is Optimization.Experiment) {
198        if (OptimizerJob.Optimizer is Optimization.Experiment) {
199          Optimization.Experiment experiment = OptimizerJob.OptimizerAsExperiment;
200          experiment.Optimizers.ItemsAdded -= new Collections.CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_ItemsAdded);
201          experiment.Optimizers.ItemsReplaced -= new Collections.CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_ItemsReplaced);
202          experiment.Optimizers.ItemsRemoved -= new Collections.CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_ItemsRemoved);
203          experiment.Optimizers.CollectionReset -= new CollectionItemsChangedEventHandler<IndexedItem<IOptimizer>>(Optimizers_CollectionReset);
204        } else if (OptimizerJob.Optimizer is Optimization.BatchRun) {
205          Optimization.BatchRun batchRun = OptimizerJob.OptimizerAsBatchRun;
206          batchRun.RepetitionsChanged -= new EventHandler(batchRun_RepetitionsChanged);
207          batchRun.AlgorithmChanged -= new EventHandler(batchRun_AlgorithmChanged);
208        }
209
210        OptimizerJob.ComputeInParallelChanged -= new EventHandler(OptimizerJob_ComputeInParallelChanged);
211        OptimizerJob.ToStringChanged -= new EventHandler(OptimizerJob_ToStringChanged);
212      }
213    }
214       
215    void batchRun_AlgorithmChanged(object sender, EventArgs e) {
216      if (syncJobsWithOptimizers) {
217        this.childHiveJobs.Clear();
218        UpdateChildHiveJobs();
219      }
220    }
221
222    void batchRun_RepetitionsChanged(object sender, EventArgs e) {
223      if (syncJobsWithOptimizers) {
224        UpdateChildHiveJobs();
225      }
226    }
227
228    void OptimizerJob_ToStringChanged(object sender, EventArgs e) {
229      this.OnToStringChanged();
230    }
231
232    private void Optimizers_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IOptimizer>> e) {
233      if (syncJobsWithOptimizers && this.OptimizerJob.ComputeInParallel) {
234        foreach (var item in e.Items) {
235          if (GetChildByOptimizer(item.Value) == null && item.Value.Name != "Placeholder") {
236            this.childHiveJobs.Add(new HiveJob(item.Value));
237          }
238        }
239      }
240    }
241    private void Optimizers_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<IOptimizer>> e) {
242      if (syncJobsWithOptimizers && this.OptimizerJob.ComputeInParallel) {
243        foreach (var item in e.OldItems) {
244          this.childHiveJobs.Remove(this.GetChildByOptimizer(item.Value));
245        }
246        foreach (var item in e.Items) {
247          if (GetChildByOptimizer(item.Value) == null && item.Value.Name != "Placeholder") {
248            this.childHiveJobs.Add(new HiveJob(item.Value));
249          }
250        }
251      }
252    }
253    private void Optimizers_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<IOptimizer>> e) {
254      if (syncJobsWithOptimizers && this.OptimizerJob.ComputeInParallel) {
255        foreach (var item in e.Items) {
256          this.childHiveJobs.Remove(this.GetChildByOptimizer(item.Value));
257        }
258      }
259    }
260    void Optimizers_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<IOptimizer>> e) {
261      if (syncJobsWithOptimizers && this.OptimizerJob.ComputeInParallel) {
262        foreach (var item in e.Items) {
263          this.childHiveJobs.Remove(this.GetChildByOptimizer(item.Value));
264        }
265      }
266    }
267
268    void OptimizerJob_ComputeInParallelChanged(object sender, EventArgs e) {
269      if (OptimizerJob != null && syncJobsWithOptimizers) {
270        if (OptimizerJob.ComputeInParallel) {
271          // child-hive jobs are not yet created, so create them according to the child-optimizers
272          this.UpdateChildHiveJobs();
273        } else {
274          // child-hive jobs need to be deleted
275          this.childHiveJobs.Clear();
276        }
277      }
278    }
279
280    public void AddChildHiveJob(HiveJob hiveJob) {
281      this.childHiveJobs.Add(hiveJob);
282      syncJobsWithOptimizers = false;
283      if (this.OptimizerJob.Optimizer is Optimization.Experiment) {
284        if (!this.OptimizerJob.OptimizerAsExperiment.Optimizers.Contains(hiveJob.OptimizerJob.Optimizer)) {
285          UpdateOptimizerInExperiment(this.OptimizerJob.OptimizerAsExperiment, hiveJob.OptimizerJob);
286        }
287      } else if (this.OptimizerJob.Optimizer is Optimization.BatchRun) {
288        UpdateOptimizerInBatchRun(this.OptimizerJob.OptimizerAsBatchRun, hiveJob.OptimizerJob);
289      }
290      syncJobsWithOptimizers = true;
291    }
292
293    /// <summary>
294    /// if this.Optimizer is Experiment
295    ///   replace the child-optimizer in the experiment
296    /// if this.Optimizer is BatchRun
297    ///   add the runs from the optimizerJob to the batchrun and replace the algorithm
298    /// </summary>
299    public void UpdateChildOptimizer(OptimizerJob optimizerJob, Guid childJobId) {
300      syncJobsWithOptimizers = false; // don't sync with optimizers during this method
301      bool childIsFinishedOptimizerDownloaded = false;
302
303      if (this.OptimizerJob.Optimizer is Optimization.Experiment) {
304        UpdateOptimizerInExperiment(this.OptimizerJob.OptimizerAsExperiment, optimizerJob);
305        childIsFinishedOptimizerDownloaded = true;
306      } else if (this.OptimizerJob.Optimizer is Optimization.BatchRun) {
307        UpdateOptimizerInBatchRun(this.OptimizerJob.OptimizerAsBatchRun, optimizerJob);
308        if (this.OptimizerJob.OptimizerAsBatchRun.Repetitions == this.OptimizerJob.Optimizer.Runs.Count) {
309          childIsFinishedOptimizerDownloaded = true;
310        }
311      } else {
312        childIsFinishedOptimizerDownloaded = optimizerJob.Optimizer.ExecutionState == ExecutionState.Stopped;
313      }
314
315      HiveJob child = this.ChildHiveJobs.Single(j => j.Job.Id == childJobId);
316      if (!optimizerJob.ComputeInParallel) {
317        child.syncJobsWithOptimizers = false;
318        child.OptimizerJob = optimizerJob;
319        child.syncJobsWithOptimizers = true;
320      }
321      if (childIsFinishedOptimizerDownloaded) {
322        child.IsFinishedOptimizerDownloaded = true;
323      }
324      syncJobsWithOptimizers = true;
325    }
326
327    /// <summary>
328    /// Adds the runs from the optimizerJob to the batchrun and replaces the algorithm
329    /// Sideeffect: the optimizerJob.Optimizer will be prepared (scopes are deleted and executionstate will be reset)
330    /// </summary>
331    private void UpdateOptimizerInBatchRun(BatchRun batchRun, OptimizerJob optimizerJob) {
332      if (batchRun.Algorithm == null) {
333        batchRun.Algorithm = (IAlgorithm)optimizerJob.Optimizer; // only set the first optimizer as algorithm. if every time the Algorithm would be set, the runs would be cleared each time
334      }
335      foreach (IRun run in optimizerJob.Optimizer.Runs) {
336        if (!batchRun.Runs.Contains(run))
337          batchRun.Runs.Add(run);
338      }
339    }
340
341    /// <summary>
342    /// replace the child-optimizer in the experiment
343    /// Sideeffect: the optimizerJob.Optimizer will be prepared (scopes are deleted and executionstate will be reset)
344    /// </summary>
345    private void UpdateOptimizerInExperiment(Optimization.Experiment experiment, OptimizerJob optimizerJob) {
346      if (optimizerJob.IndexInParentOptimizerList < 0)
347        throw new IndexOutOfRangeException("IndexInParentOptimizerList must be equal or greater than zero! The Job is invalid and the optimizer-tree cannot be reassembled.");
348
349      while (experiment.Optimizers.Count < optimizerJob.IndexInParentOptimizerList) {
350        experiment.Optimizers.Add(new UserDefinedAlgorithm("Placeholder")); // add dummy-entries to Optimizers so that its possible to insert the optimizerJob at the correct position
351      }
352      if (experiment.Optimizers.Count < optimizerJob.IndexInParentOptimizerList + 1) {
353        experiment.Optimizers.Add(optimizerJob.Optimizer);
354      } else {
355        // if ComputeInParallel==true, don't replace the optimizer (except it is still a Placeholder)
356        // this is because Jobs with ComputeInParallel get submitted to hive with their child-optimizers deleted
357        if (!optimizerJob.ComputeInParallel || experiment.Optimizers[optimizerJob.IndexInParentOptimizerList].Name == "Placeholder") {
358          experiment.Optimizers[optimizerJob.IndexInParentOptimizerList] = optimizerJob.Optimizer;
359        }
360      }
361    }
362
363    /// <summary>
364    /// Sets the IndexInParentOptimizerList property of the OptimizerJob
365    /// according to the position in the OptimizerList of the parentHiveJob.Job
366    /// Recursively updates all the child-jobs as well
367    /// </summary>
368    internal void SetIndexInParentOptimizerList(HiveJob parentHiveJob) {
369      if (parentHiveJob != null) {
370        if (parentHiveJob.OptimizerJob.Optimizer is Optimization.Experiment) {
371          this.OptimizerJob.IndexInParentOptimizerList = parentHiveJob.OptimizerJob.OptimizerAsExperiment.Optimizers.IndexOf(this.OptimizerJob.Optimizer);
372        } else if (parentHiveJob.OptimizerJob.Optimizer is Optimization.BatchRun) {
373          this.OptimizerJob.IndexInParentOptimizerList = 0;
374        } else {
375          throw new NotSupportedException("Only Experiment and BatchRuns are supported");
376        }
377      }
378      foreach (HiveJob child in childHiveJobs) {
379        child.SetIndexInParentOptimizerList(this);
380      }
381    }
382
383    public override string ToString() {
384      if (optimizerJob != null) {
385        return optimizerJob.ToString();
386      } else {
387        return base.ToString();
388      }
389    }
390
391    public void UpdateFromLightweightJob(LightweightJob lightweightJob) {
392      if (lightweightJob != null) {
393        job.Id = lightweightJob.Id;
394        job.DateCreated = lightweightJob.DateCreated;
395        job.DateCalculated = lightweightJob.DateCalculated;
396        job.DateFinished = lightweightJob.DateFinished;
397        job.Exception = lightweightJob.Exception;
398        job.Id = lightweightJob.Id;
399        job.ExecutionTime = lightweightJob.ExecutionTime;
400        job.State = lightweightJob.State;
401        // what about parentJob
402        OnJobStateChanged();
403        OnToStringChanged();
404        OnItemImageChanged();
405      }
406    }
407
408    /// <summary>
409    /// Creates a SerializedJob object containing the Job and the IJob-Object as byte[]
410    /// </summary>
411    /// <param name="withoutChildOptimizers">
412    ///   if true the Child-Optimizers will not be serialized (if the job contains an Experiment)
413    /// </param>
414    public JobData GetAsSerializedJob(bool withoutChildOptimizers) {
415      byte[] jobByteArray;
416      if (withoutChildOptimizers && this.OptimizerJob.Optimizer is Optimization.Experiment) {
417        OptimizerJob clonedJob = (OptimizerJob)this.OptimizerJob.Clone(); // use a cloned job, so that the childHiveJob don't get confused
418        clonedJob.OptimizerAsExperiment.Optimizers.Clear();
419        jobByteArray = PersistenceUtil.Serialize(clonedJob);
420      } else if (withoutChildOptimizers && this.OptimizerJob.Optimizer is Optimization.BatchRun) {
421        OptimizerJob clonedJob = (OptimizerJob)this.OptimizerJob.Clone();
422        clonedJob.OptimizerAsBatchRun.Algorithm = null;
423        jobByteArray = PersistenceUtil.Serialize(clonedJob);
424      } else if (this.OptimizerJob.Optimizer is IAlgorithm) {
425        ((IAlgorithm)this.OptimizerJob.Optimizer).StoreAlgorithmInEachRun = false; // avoid storing the algorithm in runs to reduce size
426        jobByteArray = PersistenceUtil.Serialize(this.OptimizerJob);
427      } else {
428        jobByteArray = PersistenceUtil.Serialize(this.OptimizerJob);
429      }
430
431      UpdateRequiredPlugins();
432
433      JobData serializedJob = new JobData() {
434        JobId = job.Id,
435        Data = jobByteArray
436      };
437
438      return serializedJob;
439    }
440
441    /// <summary>
442    /// find out which which plugins are needed for the given object
443    /// </summary>
444    private void UpdateRequiredPlugins() {
445      //this.Job.PluginsNeeded = HivePluginInfoDto.FindPluginsNeeded(optimizerJob.GetType());
446      throw new NotImplementedException();
447    }
448
449    #region Events
450    public event EventHandler JobChanged;
451    private void OnJobChanged() {
452      LogMessage("JobChanged");
453      EventHandler handler = JobChanged;
454      if (handler != null) handler(this, EventArgs.Empty);
455    }
456
457    public event EventHandler JobStateChanged;
458    private void OnJobStateChanged() {
459      LogMessage("JobStateChanged (State: " + this.Job.State + ", ExecutionTime: " + this.Job.ExecutionTime.ToString() + ")");
460      EventHandler handler = JobStateChanged;
461      if (handler != null) handler(this, EventArgs.Empty);
462    }
463
464    public event EventHandler OptimizerJobChanged;
465    private void OnOptimizerJobChanged() {
466      OptimizerJob_ComputeInParallelChanged(this, EventArgs.Empty);
467      var handler = JobChanged;
468      if (handler != null) handler(this, EventArgs.Empty);
469    }
470
471    public event EventHandler IsFinishedOptimizerDownloadedChanged;
472    private void OnIsFinishedOptimizerDownloadedChanged() {
473      var handler = IsFinishedOptimizerDownloadedChanged;
474      if (handler != null) handler(this, EventArgs.Empty);
475    }
476    #endregion
477
478    public void LogMessage(string message) {
479      lock (locker) {
480        if (optimizerJob != null) {
481          optimizerJob.Log.LogMessage(message);
482        }
483      }
484    }
485
486    public override IDeepCloneable Clone(Cloner cloner) {
487      LogMessage("I am beeing cloned");
488      HiveJob clone = (HiveJob)base.Clone(cloner);
489      clone.job = (Job)cloner.Clone(this.job);
490      clone.optimizerJob = (OptimizerJob)cloner.Clone(this.optimizerJob);
491      return clone;
492    }
493
494    /// <summary>
495    /// Returns a list of HiveJobs including this and all its child-jobs recursively
496    /// </summary>
497    public IEnumerable<HiveJob> GetAllHiveJobs() {
498      List<HiveJob> jobs = new List<HiveJob>();
499      jobs.Add(this);
500      foreach (HiveJob child in this.ChildHiveJobs) {
501        jobs.AddRange(child.GetAllHiveJobs());
502      }
503      return jobs;
504    }
505
506    public HiveJob GetParentByJobId(Guid jobId) {
507      if (this.ChildHiveJobs.SingleOrDefault(j => j.job.Id == jobId) != null)
508        return this;
509      foreach (HiveJob child in this.childHiveJobs) {
510        HiveJob result = child.GetParentByJobId(jobId);
511        if (result != null)
512          return result;
513      }
514      return null;
515    }
516
517
518    public HiveJob GetChildByOptimizerJob(OptimizerJob optimizerJob) {
519      foreach (var child in ChildHiveJobs) {
520        if (child.OptimizerJob == optimizerJob)
521          return child;
522      }
523      return null;
524    }
525
526
527    public HiveJob GetChildByOptimizer(IOptimizer optimizer) {
528      foreach (var child in ChildHiveJobs) {
529        if (child.OptimizerJob.Optimizer == optimizer)
530          return child;
531      }
532      return null;
533    }
534
535    /// <summary>
536    /// Searches for an HiveJob object with the correct jobId recursively
537    /// </summary>
538    public HiveJob GetHiveJobByJobId(Guid jobId) {
539      if (this.Job.Id == jobId) {
540        return this;
541      } else {
542        foreach (HiveJob child in this.ChildHiveJobs) {
543          HiveJob result = child.GetHiveJobByJobId(jobId);
544          if (result != null)
545            return result;
546        }
547      }
548      return null;
549    }
550
551
552    public void RemoveByJobId(Guid jobId) {
553      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
554      foreach (HiveJob j in jobs) {
555        this.childHiveJobs.Remove(j);
556      }
557      foreach (HiveJob child in ChildHiveJobs) {
558        child.RemoveByJobId(jobId);
559      }
560    }
561
562  }
563}
Note: See TracBrowser for help on using the repository browser.