Changeset 4368 for branches/3.3-Hive/sources/HeuristicLab.Hive/HeuristicLab.Hive.Experiment/3.3/JobItem.cs
- Timestamp:
- 09/07/10 10:22:27 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/3.3-Hive/sources/HeuristicLab.Hive/HeuristicLab.Hive.Experiment/3.3/JobItem.cs
r4264 r4368 12 12 using HeuristicLab.Optimization; 13 13 using HeuristicLab.Hive.Contracts.ResponseObjects; 14 using HeuristicLab.Hive.JobBase; 15 using System.IO; 16 using HeuristicLab.Persistence.Default.Xml; 17 using HeuristicLab.PluginInfrastructure; 18 using System.Reflection; 19 using HeuristicLab.Hive.Experiment.Jobs; 14 20 15 21 namespace HeuristicLab.Hive.Experiment { … … 22 28 public override Image ItemImage { 23 29 get { 24 if ( State == JobState.Offline) return HeuristicLab.Common.Resources.VS2008ImageLibrary.ExecutablePrepared;25 else if ( State == JobState.Calculating) return HeuristicLab.Common.Resources.VS2008ImageLibrary.ExecutableStarted;26 else if ( State == JobState.Aborted) return HeuristicLab.Common.Resources.VS2008ImageLibrary.ExecutableStopped;27 else if ( State == JobState.Failed) return HeuristicLab.Common.Resources.VS2008ImageLibrary.Error;28 else if ( State == JobState.Finished) return HeuristicLab.Common.Resources.VS2008ImageLibrary.ExecutableStopped;30 if (jobDto.State == JobState.Offline) return HeuristicLab.Common.Resources.VS2008ImageLibrary.ExecutablePrepared; 31 else if (jobDto.State == JobState.Calculating) return HeuristicLab.Common.Resources.VS2008ImageLibrary.ExecutableStarted; 32 else if (jobDto.State == JobState.Aborted) return HeuristicLab.Common.Resources.VS2008ImageLibrary.ExecutableStopped; 33 else if (jobDto.State == JobState.Failed) return HeuristicLab.Common.Resources.VS2008ImageLibrary.Error; 34 else if (jobDto.State == JobState.Finished) return HeuristicLab.Common.Resources.VS2008ImageLibrary.ExecutableStopped; 29 35 else return HeuristicLab.Common.Resources.VS2008ImageLibrary.Event; 30 36 } … … 33 39 [Storable] 34 40 private JobDto jobDto; 35 /// <summary>36 /// Some static information about the job. Don't use State-Information out of there37 /// </summary>38 41 public JobDto JobDto { 39 42 get { return jobDto; } … … 49 52 50 53 [Storable] 51 private JobResult jobResult;52 public JobResult JobResult{53 private get { return jobResult; }54 private IJob job; 55 public IJob Job { 56 get { return job; } 54 57 set { 55 if (jobResult != value) { 56 jobResult = value; 57 OnJobStateChanged(); 58 OnToStringChanged(); 59 OnItemImageChanged(); 60 } 61 } 62 } 63 64 public JobState State { 65 get { return jobResult != null ? JobResult.State : JobDto.State; } 66 } 67 68 public double? Percentage { 69 get { return jobResult != null ? JobResult.Percentage : JobDto.Percentage; } 70 } 71 72 public string Exception { 73 get { return jobResult != null ? JobResult.Exception : JobDto.Exception; } 74 } 75 76 public DateTime? DateCalculated { 77 get { return jobResult != null ? JobResult.DateCalculated : JobDto.DateCalculated; } 78 } 79 80 public DateTime? DateFinished { 81 get { return jobResult != null ? JobResult.DateFinished : JobDto.DateFinished; } 82 } 83 84 [Storable] 85 private ResponseObject<SerializedJob> latestSnapshot; 86 public ResponseObject<SerializedJob> LatestSnapshot { 87 get { return latestSnapshot; } 58 if (job != value) { 59 job = value; 60 IsFinalResultAvailable = job.ExecutionState == ExecutionState.Stopped; 61 OnJobChanged(); 62 } 63 } 64 } 65 66 [Storable] 67 private ICollection<JobItem> childJobItems; 68 public IEnumerable<JobItem> ChildJobItems { 69 get { return childJobItems; } 70 } 71 72 [Storable] 73 private bool isFinalResultAvailable; 74 public bool IsFinalResultAvailable { 75 get { return isFinalResultAvailable; } 88 76 set { 89 if (latestSnapshot != value) { 90 latestSnapshot = value; 91 if (value != null) { 92 latestSnapshotTime = DateTime.Now; 93 } 94 SnapshotRequestedState = Experiment.SnapshotRequestedState.Idle; 95 OnLatestSnapshotChanged(); 96 } 97 } 98 } 99 100 [Storable] 101 private DateTime latestSnapshotTime; 102 public DateTime LatestSnapshotTime { 103 get { return latestSnapshotTime; } 104 } 105 106 [Storable] 107 private ILog log; 108 public ILog Log { 109 get { return log; } 110 } 111 112 [Storable] 113 private IOptimizer optimizer; 114 public IOptimizer Optimizer { 115 get { return optimizer; } 116 set { this.optimizer = value; } 117 } 118 119 [Storable] 120 private SnapshotRequestedState snapshotRequestedState; 121 public SnapshotRequestedState SnapshotRequestedState { 122 get { return snapshotRequestedState; } 123 private set { 124 this.snapshotRequestedState = value; 125 OnSnapshotRequestedStateChanged(); 126 } 127 } 128 77 if (isFinalResultAvailable != value) { 78 isFinalResultAvailable = value; 79 if (isFinalResultAvailable) 80 OnFinalResultAvailable(); 81 } 82 } 83 } 84 129 85 public JobItem() { 130 log = new Log(); 86 childJobItems = new List<JobItem>(); 87 } 88 89 public JobItem(JobResult jobResult) : this() { 90 this.jobDto = new JobDto(); 91 UpdateJob(jobResult); 131 92 } 132 93 133 94 public override string ToString() { 134 95 if (jobDto != null) { 135 return optimizer.Name;96 return job.Name; 136 97 } else { 137 98 return base.ToString(); … … 139 100 } 140 101 141 public event EventHandler LatestSnapshotChanged; 142 private void OnLatestSnapshotChanged() { 143 LogMessage("LatestSnapshotChanged"); 144 EventHandler handler = LatestSnapshotChanged; 145 if (handler != null) handler(this, EventArgs.Empty); 146 } 147 102 public void UpdateJob(JobResult jobResult) { 103 if (jobResult != null) { 104 jobDto.Id = jobResult.Id; 105 jobDto.DateCreated = jobResult.DateCreated; 106 jobDto.DateCalculated = jobResult.DateCalculated; 107 jobDto.DateFinished = jobResult.DateFinished; 108 jobDto.Exception = jobResult.Exception; 109 jobDto.Id = jobResult.Id; 110 jobDto.Percentage = jobResult.Percentage; 111 jobDto.State = jobResult.State; 112 // what about parentJob 113 OnJobStateChanged(); 114 OnToStringChanged(); 115 OnItemImageChanged(); 116 } 117 } 118 119 public void AddChildJob(JobItem child) { 120 this.childJobItems.Add(child); 121 OnChildJobAdded(); 122 } 123 124 //public IEnumerable<JobItem> GetChildJobItems() { 125 // if (childJobItems == null) 126 // childJobItems = CreateChildJobItems(); 127 // return childJobItems; 128 //} 129 130 //protected IEnumerable<JobItem> CreateChildJobItems() { 131 // return (from j in Job.ChildJobs 132 // select new JobItem() { 133 // Job = j, 134 // JobDto = new JobDto() { 135 // State = JobState.Offline 136 // } 137 // }).ToList(); 138 //} 139 140 public SerializedJob ToSerializedJob() { 141 MemoryStream memStream = new MemoryStream(); 142 XmlGenerator.Serialize(job, memStream); 143 byte[] jobByteArray = memStream.ToArray(); 144 memStream.Dispose(); 145 146 UpdateRequiredPlugins(); 147 148 this.JobDto.CoresNeeded = job.CoresNeeded; 149 this.JobDto.MemoryNeeded = job.MemoryNeeded; 150 151 SerializedJob serializedJob = new SerializedJob() { 152 JobInfo = jobDto, 153 SerializedJobData = jobByteArray 154 }; 155 156 return serializedJob; 157 } 158 159 private void UpdateRequiredPlugins() { 160 // find out which which plugins are needed for the given object 161 this.JobDto.PluginsNeeded = HivePluginInfoDto.FindPluginsNeeded(job.GetType()); 162 } 163 148 164 public event EventHandler JobDtoChanged; 149 165 private void OnJobDtoChanged() { … … 155 171 public event EventHandler JobStateChanged; 156 172 private void OnJobStateChanged() { 157 LogMessage("JobStateChanged (State: " + this. State + ", Percentage: " + this.Percentage + ")");173 LogMessage("JobStateChanged (State: " + this.JobDto.State + ", Percentage: " + this.JobDto.Percentage + ")"); 158 174 EventHandler handler = JobStateChanged; 175 if (handler != null) handler(this, EventArgs.Empty); 176 } 177 178 public event EventHandler ChildJobAdded; 179 private void OnChildJobAdded() { 180 var handler = ChildJobAdded; 181 if (handler != null) handler(this, EventArgs.Empty); 182 } 183 184 public event EventHandler JobChanged; 185 private void OnJobChanged() { 186 var handler = JobChanged; 187 if (handler != null) handler(this, EventArgs.Empty); 188 } 189 190 public event EventHandler FinalResultAvailable; 191 private void OnFinalResultAvailable() { 192 var handler = FinalResultAvailable; 159 193 if (handler != null) handler(this, EventArgs.Empty); 160 194 } … … 162 196 public void LogMessage(string message) { 163 197 lock (locker) { 164 log.LogMessage(message); 198 if (job != null) { 199 job.Log.LogMessage(message); 200 } 165 201 } 166 202 } … … 169 205 LogMessage("I am beeing cloned"); 170 206 JobItem clone = (JobItem)base.Clone(cloner); 171 clone.latestSnapshotTime = this.latestSnapshotTime;172 207 clone.jobDto = (JobDto)cloner.Clone(this.jobDto); 173 clone.latestSnapshot = (ResponseObject<SerializedJob>)cloner.Clone(this.latestSnapshot); 174 clone.log = (ILog)cloner.Clone(this.log); 175 clone.optimizer = (IOptimizer)cloner.Clone(this.optimizer); 176 clone.jobResult = (JobResult)cloner.Clone(this.jobResult); 208 clone.job = (IJob)cloner.Clone(this.job); 177 209 return clone; 178 210 } 179 211 180 181 public event EventHandler SnapshotRequestedStateChanged; 182 private void OnSnapshotRequestedStateChanged() { 183 EventHandler handler = SnapshotRequestedStateChanged; 184 if (handler != null) handler(this, EventArgs.Empty); 185 } 186 187 public void RequestSnapshot() { 188 this.SnapshotRequestedState = Experiment.SnapshotRequestedState.Requested; 189 } 190 191 212 public void UpdateFinalResults(IJob j) { 213 if (this.Job is OptimizerJob && j is OptimizerJob) { 214 ((OptimizerJob)this.Job).Optimizer.Runs.AddRange(((OptimizerJob)j).Optimizer.Runs); 215 IsFinalResultAvailable = true; //job.ExecutionState == ExecutionState.Stopped; //?? 216 } else { 217 throw new NotSupportedException(); 218 } 219 } 220 221 internal bool ReplaceChildJob(IJob job) { 222 var matches = this.childJobItems.Where(child => child.Job.InternalId == job.InternalId); 223 if (matches.Count() > 0) { 224 matches.First().job = job; 225 return true; 226 } 227 return false; 228 } 192 229 } 193 230 }
Note: See TracChangeset
for help on using the changeset viewer.