Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.3-HiveMigration/sources/HeuristicLab.Hive/HeuristicLab.Hive.Experiment/3.3/JobItem.cs @ 4264

Last change on this file since 4264 was 4264, checked in by cneumuel, 14 years ago

Split up "State" to "JobState" and "SlaveState" (#1159)

File size: 6.0 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using HeuristicLab.Core;
6using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
7using HeuristicLab.Hive.Contracts.BusinessObjects;
8using HeuristicLab.Hive.Contracts;
9using System.Drawing;
10using HeuristicLab.Common;
11using System.Diagnostics;
12using HeuristicLab.Optimization;
13using HeuristicLab.Hive.Contracts.ResponseObjects;
14
15namespace HeuristicLab.Hive.Experiment {
16  public enum SnapshotRequestedState { Idle, Requested }
17
18  [StorableClass]
19  public class JobItem : Item {
20    private static object locker = new object();
21
22    public override Image ItemImage {
23      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;
29        else return HeuristicLab.Common.Resources.VS2008ImageLibrary.Event;
30      }
31    }
32
33    [Storable]
34    private JobDto jobDto;
35    /// <summary>
36    /// Some static information about the job. Don't use State-Information out of there
37    /// </summary>
38    public JobDto JobDto {
39      get { return jobDto; }
40      set {
41        if (jobDto != value) {
42          jobDto = value;
43          OnJobDtoChanged();
44          OnToStringChanged();
45          OnItemImageChanged();
46        }
47      }
48    }
49
50    [Storable]
51    private JobResult jobResult;
52    public JobResult JobResult {
53      private get { return jobResult; }
54      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; }
88      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
129    public JobItem() {
130      log = new Log();
131    }
132
133    public override string ToString() {
134      if (jobDto != null) {
135        return optimizer.Name;
136      } else {
137        return base.ToString();
138      }
139    }
140
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
148    public event EventHandler JobDtoChanged;
149    private void OnJobDtoChanged() {
150      LogMessage("JobDtoChanged");
151      EventHandler handler = JobDtoChanged;
152      if (handler != null) handler(this, EventArgs.Empty);
153    }
154
155    public event EventHandler JobStateChanged;
156    private void OnJobStateChanged() {
157      LogMessage("JobStateChanged (State: " + this.State + ", Percentage: " + this.Percentage + ")");
158      EventHandler handler = JobStateChanged;
159      if (handler != null) handler(this, EventArgs.Empty);
160    }
161
162    public void LogMessage(string message) {
163      lock (locker) {
164        log.LogMessage(message);
165      }
166    }
167
168    public override IDeepCloneable Clone(Cloner cloner) {
169      LogMessage("I am beeing cloned");
170      JobItem clone = (JobItem)base.Clone(cloner);
171      clone.latestSnapshotTime = this.latestSnapshotTime;
172      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);
177      return clone;
178    }
179
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
192  }
193}
Note: See TracBrowser for help on using the repository browser.