Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/19/11 18:58:53 (13 years ago)
Author:
cneumuel
Message:

#1233

  • created baseclass for jobs (ItemJob) which derives OperatorJobs and EngineJobs
  • created special view for OptimizerJobs which derives from a more general view
  • removed logic from domain class HiveExperiment and moved it into RefreshableHiveExperiment
  • improved ItemTreeView
  • corrected plugin dependencies
  • fixed bug in database trigger when deleting HiveExperiments
  • added delete cascade for Plugin and PluginData
  • lots of fixes
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive/3.4/ExperimentManager/HiveJob.cs

    r6006 r6033  
    2424using System.Drawing;
    2525using System.Linq;
    26 using HeuristicLab.Clients.Hive.Jobs;
    2726using HeuristicLab.Collections;
    2827using HeuristicLab.Common;
    2928using HeuristicLab.Core;
    30 using HeuristicLab.Optimization;
     29using HeuristicLab.Hive;
    3130using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    3231using HeuristicLab.PluginInfrastructure;
     
    3635  [Item("Hive Job", "Represents a hive job.")]
    3736  [StorableClass]
    38   public class HiveJob : NamedItem, IItemTree {
    39     private static object locker = new object();
     37  public class HiveJob : NamedItem, IItemTree<HiveJob> {
     38    protected static object locker = new object();
    4039
    4140    public override Image ItemImage {
     
    5655    }
    5756
    58     private Job job;
     57    protected Job job;
    5958    public Job Job {
    6059      get { return job; }
     
    7069
    7170    [Storable]
    72     private OptimizerJob optimizerJob;
    73     public OptimizerJob OptimizerJob {
    74       get { return optimizerJob; }
     71    protected ItemJob jobItem;
     72    public ItemJob JobItem {
     73      get { return jobItem; }
    7574      internal set {
    76         if (optimizerJob != null && syncJobsWithOptimizers) {
     75        if (jobItem != null && syncJobsWithOptimizers) {
    7776          this.childHiveJobs.Clear();
    7877        }
    79         if (optimizerJob != value) {
    80           DergisterOptimizerEvents();
    81           optimizerJob = value;
    82           RegisterOptimizerEvents();
    83           OnOptimizerJobChanged();
     78        if (jobItem != value) {
     79          DergisterJobItemsEvents();
     80          jobItem = value;
     81          RegisterJobItemEvents();
     82          OnJobItemChanged();
    8483        }
    8584      }
     
    8786
    8887    [Storable]
    89     private ItemList<HiveJob> childHiveJobs;
    90     public ReadOnlyItemList<HiveJob> ChildHiveJobs {
     88    protected ItemList<HiveJob> childHiveJobs;
     89    public virtual ReadOnlyItemList<HiveJob> ChildHiveJobs {
    9190      get { return childHiveJobs.AsReadOnly(); }
    9291    }
    9392
    9493    [Storable]
    95     private bool syncJobsWithOptimizers = true;
    96 
     94    protected bool syncJobsWithOptimizers = true;
     95
     96    #region Constructors and Cloning
    9797    public HiveJob() {
    9898      this.Job = new Job() {
     
    106106    }
    107107
    108     public HiveJob(OptimizerJob optimizerJob, bool autoCreateChildHiveJobs)
     108    public HiveJob(ItemJob jobItem, bool autoCreateChildHiveJobs)
    109109      : this() {
    110110      this.syncJobsWithOptimizers = autoCreateChildHiveJobs;
    111       this.OptimizerJob = optimizerJob;
     111      this.JobItem = jobItem;
    112112      this.syncJobsWithOptimizers = true;
    113     }
    114 
    115     public HiveJob(IOptimizer optimizer)
    116       : this() {
    117       this.OptimizerJob = new OptimizerJob(optimizer);
    118113    }
    119114
     
    122117      this.Job = job;
    123118      try {
    124         this.OptimizerJob = PersistenceUtil.Deserialize<OptimizerJob>(jobData.Data);
     119        this.JobItem = PersistenceUtil.Deserialize<ItemJob>(jobData.Data);
    125120      }
    126121      catch {
    127         this.OptimizerJob = null;
     122        this.JobItem = null;
    128123      }
    129124      this.childHiveJobs = new ItemList<HiveJob>();
     
    135130      : base(original, cloner) {
    136131      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;
    138135    }
    139136    public override IDeepCloneable Clone(Cloner cloner) {
    140137      return new HiveJob(this, cloner);
    141138    }
    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() {
    210157      this.childHiveJobs.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<HiveJob>>(OnItemsAdded);
    211158      this.childHiveJobs.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<HiveJob>>(OnItemsRemoved);
    212159      this.childHiveJobs.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<HiveJob>>(OnCollectionReset);
    213160    }
    214     private void DeregisterChildHiveJobEvents() {
     161    protected virtual void DeregisterChildHiveJobEvents() {
    215162      this.childHiveJobs.ItemsAdded -= new CollectionItemsChangedEventHandler<IndexedItem<HiveJob>>(OnItemsAdded);
    216163      this.childHiveJobs.ItemsRemoved -= new CollectionItemsChangedEventHandler<IndexedItem<HiveJob>>(OnItemsRemoved);
     
    218165    }
    219166
    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) {
    233168      this.OnToStringChanged();
    234169    }
    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) {
    274173        this.UpdateChildHiveJobs();
    275174      }
    276175    }
    277176
    278     public void AddChildHiveJob(HiveJob hiveJob) {
     177    public virtual void AddChildHiveJob(HiveJob hiveJob) {
    279178      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 experiment
    283         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 Experiment
    305     ///   replace the child-optimizer in the experiment
    306     /// if this.Optimizer is BatchRun
    307     ///   add the runs from the optimizerJob to the batchrun and replace the Optimizer
    308     /// </summary>
    309     public void UpdateChildOptimizer(OptimizerJob optimizerJob, Guid childJobId) {
    310       syncJobsWithOptimizers = false; // don't sync with optimizers during this method
    311       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 childIsFinishedOptimizerDownloaded
    335       }
    336       syncJobsWithOptimizers = true;
    337     }
    338 
    339     /// <summary>
    340     /// Adds the runs from the optimizerJob to the batchrun and replaces the Optimizer
    341     /// 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 time
    346       }
    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 number
    357     /// </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" -> 3
    375     /// </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 experiment
    387     /// 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 position
    395       }
    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 deleted
    401         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 OptimizerJob
    409     /// according to the position in the OptimizerList of the parentHiveJob.Job
    410     /// Recursively updates all the child-jobs as well
    411     /// </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       }
    425179    }
    426180
    427181    public override string ToString() {
    428       if (optimizerJob != null) {
    429         return optimizerJob.ToString();
     182      if (jobItem != null) {
     183        return jobItem.ToString();
    430184      } else {
    431185        return base.ToString();
     
    433187    }
    434188
    435     public void UpdateFromLightweightJob(LightweightJob lightweightJob) {
     189    public virtual void UpdateFromLightweightJob(LightweightJob lightweightJob) {
    436190      if (lightweightJob != null) {
    437191        job.Id = lightweightJob.Id;
     
    455209    ///   if true the Child-Optimizers will not be serialized (if the job contains an Experiment)
    456210    /// </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    }
    489215
    490216    #region Events
    491217    public event EventHandler JobChanged;
    492218    private void OnJobChanged() {
    493       LogMessage("JobChanged");
    494219      EventHandler handler = JobChanged;
    495220      if (handler != null) handler(this, EventArgs.Empty);
     
    498223    public event EventHandler JobStateChanged;
    499224    private void OnJobStateChanged() {
    500       LogMessage("JobStateChanged (State: " + this.Job.State + ", ExecutionTime: " + this.Job.ExecutionTime.ToString() + ")");
    501225      EventHandler handler = JobStateChanged;
    502226      if (handler != null) handler(this, EventArgs.Empty);
    503227    }
    504228
    505     public event EventHandler OptimizerJobChanged;
    506     private void OnOptimizerJobChanged() {
    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;
    509233      if (handler != null) handler(this, EventArgs.Empty);
    510234    }
     
    516240    }
    517241    #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   
    527243    /// <summary>
    528244    /// Returns a list of HiveJobs including this and all its child-jobs recursively
    529245    /// </summary>
    530246    public IEnumerable<HiveJob> GetAllHiveJobs() {
    531       List<HiveJob> jobs = new List<HiveJob>();
     247      var jobs = new List<HiveJob>();
    532248      jobs.Add(this);
    533249      foreach (HiveJob child in this.ChildHiveJobs) {
     
    544260        if (result != null)
    545261          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;
    564262      }
    565263      return null;
     
    582280    }
    583281
    584 
    585282    public void RemoveByJobId(Guid jobId) {
    586283      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
     
    593290    }
    594291
    595     public IEnumerable<IItemTree> GetChildItems() {
     292    public IEnumerable<IItemTree<HiveJob>> GetChildItems() {
    596293      return this.childHiveJobs;
    597294    }
     
    599296    #region INotifyObservableCollectionItemsChanged<IItemTree> Members
    600297
    601     public event CollectionItemsChangedEventHandler<IItemTree> CollectionReset;
     298    public event CollectionItemsChangedEventHandler<IItemTree<HiveJob>> CollectionReset;
    602299    private void OnCollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<HiveJob>> e) {
    603300      var handler = CollectionReset;
     
    605302    }
    606303
    607     public event CollectionItemsChangedEventHandler<IItemTree> ItemsAdded;
     304    public event CollectionItemsChangedEventHandler<IItemTree<HiveJob>> ItemsAdded;
    608305    private void OnItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<HiveJob>> e) {
    609306      var handler = ItemsAdded;
     
    611308    }
    612309
    613     public event CollectionItemsChangedEventHandler<IItemTree> ItemsRemoved;
     310    public event CollectionItemsChangedEventHandler<IItemTree<HiveJob>> ItemsRemoved;
    614311    private void OnItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<HiveJob>> e) {
    615312      var handler = ItemsRemoved;
     
    617314    }
    618315
    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));
    621318    }
    622319    #endregion
     
    634331        JobData jobData = new JobData();
    635332        jobData.JobId = this.job.Id;
    636         jobData.Data = PersistenceUtil.Serialize(this.optimizerJob);
     333        jobData.Data = PersistenceUtil.Serialize(this.jobItem);
    637334        service.UpdateJobData(this.Job, jobData);
    638335        service.RestartJob(this.job.Id);
     
    641338      });
    642339    }
     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
    643366  }
    644367}
Note: See TracChangeset for help on using the changeset viewer.