1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2016 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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Drawing;
|
---|
25 | using System.Linq;
|
---|
26 | using System.Threading;
|
---|
27 | using System.Threading.Tasks;
|
---|
28 | using HeuristicLab.Collections;
|
---|
29 | using HeuristicLab.Common;
|
---|
30 | using HeuristicLab.Core;
|
---|
31 | using HeuristicLab.Data;
|
---|
32 | using HeuristicLab.Optimization;
|
---|
33 | using HeuristicLab.Parameters;
|
---|
34 | using HeuristicLab.Persistence;
|
---|
35 | using HeuristicLab.PluginInfrastructure;
|
---|
36 |
|
---|
37 | namespace HeuristicLab.Algorithms.Benchmarks {
|
---|
38 | [Item("Benchmark Algorithm", "An algorithm to execute performance benchmarks (Linpack, Dhrystone, Whetstone, etc.).")]
|
---|
39 | [Creatable(CreatableAttribute.Categories.TestingAndAnalysis, Priority = 130)]
|
---|
40 | [StorableType("ba854a73-3342-471c-a7f9-314435879f90")]
|
---|
41 | public sealed class BenchmarkAlgorithm : IAlgorithm {
|
---|
42 | private CancellationTokenSource cancellationTokenSource;
|
---|
43 |
|
---|
44 | public string ItemName {
|
---|
45 | get { return ItemAttribute.GetName(this.GetType()); }
|
---|
46 | }
|
---|
47 | public string ItemDescription {
|
---|
48 | get { return ItemAttribute.GetDescription(this.GetType()); }
|
---|
49 | }
|
---|
50 | public Version ItemVersion {
|
---|
51 | get { return ItemAttribute.GetVersion(this.GetType()); }
|
---|
52 | }
|
---|
53 | public static Image StaticItemImage {
|
---|
54 | get { return HeuristicLab.Common.Resources.VSImageLibrary.Event; }
|
---|
55 | }
|
---|
56 | public Image ItemImage {
|
---|
57 | get {
|
---|
58 | if (ExecutionState == ExecutionState.Prepared) return HeuristicLab.Common.Resources.VSImageLibrary.ExecutablePrepared;
|
---|
59 | else if (ExecutionState == ExecutionState.Started) return HeuristicLab.Common.Resources.VSImageLibrary.ExecutableStarted;
|
---|
60 | else if (ExecutionState == ExecutionState.Paused) return HeuristicLab.Common.Resources.VSImageLibrary.ExecutablePaused;
|
---|
61 | else if (ExecutionState == ExecutionState.Stopped) return HeuristicLab.Common.Resources.VSImageLibrary.ExecutableStopped;
|
---|
62 | else return ItemAttribute.GetImage(this.GetType());
|
---|
63 | }
|
---|
64 | }
|
---|
65 |
|
---|
66 | [Storable]
|
---|
67 | private DateTime lastUpdateTime;
|
---|
68 |
|
---|
69 | [Storable]
|
---|
70 | private IBenchmark benchmark;
|
---|
71 | public IBenchmark Benchmark {
|
---|
72 | get { return benchmark; }
|
---|
73 | set {
|
---|
74 | if (value == null) throw new ArgumentNullException();
|
---|
75 | benchmark = value;
|
---|
76 | }
|
---|
77 | }
|
---|
78 |
|
---|
79 | [Storable]
|
---|
80 | private ExecutionState executionState;
|
---|
81 | public ExecutionState ExecutionState {
|
---|
82 | get { return executionState; }
|
---|
83 | private set {
|
---|
84 | if (executionState != value) {
|
---|
85 | executionState = value;
|
---|
86 | OnExecutionStateChanged();
|
---|
87 | OnItemImageChanged();
|
---|
88 | }
|
---|
89 | }
|
---|
90 | }
|
---|
91 |
|
---|
92 | [Storable]
|
---|
93 | private TimeSpan executionTime;
|
---|
94 | public TimeSpan ExecutionTime {
|
---|
95 | get { return executionTime; }
|
---|
96 | private set {
|
---|
97 | executionTime = value;
|
---|
98 | OnExecutionTimeChanged();
|
---|
99 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 | [Storable]
|
---|
103 | private bool storeAlgorithmInEachRun;
|
---|
104 | public bool StoreAlgorithmInEachRun {
|
---|
105 | get { return storeAlgorithmInEachRun; }
|
---|
106 | set {
|
---|
107 | if (storeAlgorithmInEachRun != value) {
|
---|
108 | storeAlgorithmInEachRun = value;
|
---|
109 | OnStoreAlgorithmInEachRunChanged();
|
---|
110 | }
|
---|
111 | }
|
---|
112 | }
|
---|
113 |
|
---|
114 | [Storable]
|
---|
115 | private int runsCounter;
|
---|
116 |
|
---|
117 | [Storable]
|
---|
118 | private RunCollection runs = new RunCollection();
|
---|
119 | public RunCollection Runs {
|
---|
120 | get { return runs; }
|
---|
121 | private set {
|
---|
122 | if (value == null) throw new ArgumentNullException();
|
---|
123 | if (runs != value) {
|
---|
124 | if (runs != null) DeregisterRunsEvents();
|
---|
125 | runs = value;
|
---|
126 | if (runs != null) RegisterRunsEvents();
|
---|
127 | }
|
---|
128 | }
|
---|
129 | }
|
---|
130 |
|
---|
131 | [Storable]
|
---|
132 | private ResultCollection results;
|
---|
133 | public ResultCollection Results {
|
---|
134 | get { return results; }
|
---|
135 | }
|
---|
136 |
|
---|
137 | public Type ProblemType {
|
---|
138 | get {
|
---|
139 | // BenchmarkAlgorithm does not have a problem, so return a type which is no problem for sure
|
---|
140 | return typeof(BenchmarkAlgorithm);
|
---|
141 | }
|
---|
142 | }
|
---|
143 |
|
---|
144 | public IProblem Problem {
|
---|
145 | get { return null; }
|
---|
146 | set { throw new NotImplementedException("BenchmarkAlgorithm does not have a problem."); }
|
---|
147 | }
|
---|
148 |
|
---|
149 | [Storable]
|
---|
150 | private string name;
|
---|
151 | public string Name {
|
---|
152 | get { return name; }
|
---|
153 | set {
|
---|
154 | if (!CanChangeName) throw new NotSupportedException("Name cannot be changed.");
|
---|
155 | if (!(name.Equals(value) || (value == null) && (name == string.Empty))) {
|
---|
156 | CancelEventArgs<string> e = value == null ? new CancelEventArgs<string>(string.Empty) : new CancelEventArgs<string>(value);
|
---|
157 | OnNameChanging(e);
|
---|
158 | if (!e.Cancel) {
|
---|
159 | name = value == null ? string.Empty : value;
|
---|
160 | OnNameChanged();
|
---|
161 | runs.OptimizerName = name;
|
---|
162 | }
|
---|
163 | }
|
---|
164 | }
|
---|
165 | }
|
---|
166 | public bool CanChangeName {
|
---|
167 | get { return true; }
|
---|
168 | }
|
---|
169 |
|
---|
170 | [Storable]
|
---|
171 | private string description;
|
---|
172 | public string Description {
|
---|
173 | get { return description; }
|
---|
174 | set {
|
---|
175 | if (!CanChangeDescription) throw new NotSupportedException("Description cannot be changed.");
|
---|
176 | if (!(description.Equals(value) || (value == null) && (description == string.Empty))) {
|
---|
177 | description = value == null ? string.Empty : value;
|
---|
178 | OnDescriptionChanged();
|
---|
179 | }
|
---|
180 | }
|
---|
181 | }
|
---|
182 | public bool CanChangeDescription {
|
---|
183 | get { return true; }
|
---|
184 | }
|
---|
185 |
|
---|
186 | [Storable]
|
---|
187 | private ParameterCollection parameters = new ParameterCollection();
|
---|
188 | public IKeyedItemCollection<string, IParameter> Parameters {
|
---|
189 | get { return parameters; }
|
---|
190 | }
|
---|
191 | private ReadOnlyKeyedItemCollection<string, IParameter> readOnlyParameters;
|
---|
192 | IKeyedItemCollection<string, IParameter> IParameterizedItem.Parameters {
|
---|
193 | get {
|
---|
194 | if (readOnlyParameters == null) readOnlyParameters = parameters.AsReadOnly();
|
---|
195 | return readOnlyParameters;
|
---|
196 | }
|
---|
197 | }
|
---|
198 |
|
---|
199 | public IEnumerable<IOptimizer> NestedOptimizers {
|
---|
200 | get { return Enumerable.Empty<IOptimizer>(); }
|
---|
201 | }
|
---|
202 |
|
---|
203 | #region Parameter Properties
|
---|
204 | public IConstrainedValueParameter<IBenchmark> BenchmarkParameter {
|
---|
205 | get { return (IConstrainedValueParameter<IBenchmark>)Parameters["Benchmark"]; }
|
---|
206 | }
|
---|
207 | private ValueParameter<IntValue> ChunkSizeParameter {
|
---|
208 | get { return (ValueParameter<IntValue>)Parameters["ChunkSize"]; }
|
---|
209 | }
|
---|
210 | private ValueParameter<DoubleValue> TimeLimitParameter {
|
---|
211 | get { return (ValueParameter<DoubleValue>)Parameters["TimeLimit"]; }
|
---|
212 | }
|
---|
213 | #endregion
|
---|
214 |
|
---|
215 | #region Constructors
|
---|
216 | [StorableConstructor]
|
---|
217 | private BenchmarkAlgorithm(StorableConstructorFlag deserializing) { }
|
---|
218 | private BenchmarkAlgorithm(BenchmarkAlgorithm original, Cloner cloner) {
|
---|
219 | if (original.ExecutionState == ExecutionState.Started) throw new InvalidOperationException(string.Format("Clone not allowed in execution state \"{0}\".", ExecutionState));
|
---|
220 | cloner.RegisterClonedObject(original, this);
|
---|
221 | name = original.name;
|
---|
222 | description = original.description;
|
---|
223 | parameters = cloner.Clone(original.parameters);
|
---|
224 | readOnlyParameters = null;
|
---|
225 | executionState = original.executionState;
|
---|
226 | executionTime = original.executionTime;
|
---|
227 | storeAlgorithmInEachRun = original.storeAlgorithmInEachRun;
|
---|
228 | runsCounter = original.runsCounter;
|
---|
229 | Runs = cloner.Clone(original.runs);
|
---|
230 | results = cloner.Clone(original.results);
|
---|
231 | }
|
---|
232 | public BenchmarkAlgorithm() {
|
---|
233 | name = ItemName;
|
---|
234 | description = ItemDescription;
|
---|
235 | parameters = new ParameterCollection();
|
---|
236 | readOnlyParameters = null;
|
---|
237 | executionState = ExecutionState.Stopped;
|
---|
238 | executionTime = TimeSpan.Zero;
|
---|
239 | storeAlgorithmInEachRun = false;
|
---|
240 | runsCounter = 0;
|
---|
241 | Runs = new RunCollection() { OptimizerName = name };
|
---|
242 | results = new ResultCollection();
|
---|
243 | CreateParameters();
|
---|
244 | DiscoverBenchmarks();
|
---|
245 | Prepare();
|
---|
246 | }
|
---|
247 | #endregion
|
---|
248 |
|
---|
249 | private void CreateParameters() {
|
---|
250 | Parameters.Add(new ValueParameter<IntValue>("ChunkSize", "The size in MB of the chunk data array that is generated.", new IntValue(0)));
|
---|
251 | Parameters.Add(new ValueParameter<DoubleValue>("TimeLimit", "The time limit in minutes for a benchmark run (zero means a fixed number of iterations).", new DoubleValue(0)));
|
---|
252 | }
|
---|
253 | private void DiscoverBenchmarks() {
|
---|
254 | var benchmarks = from t in ApplicationManager.Manager.GetTypes(typeof(IBenchmark))
|
---|
255 | select t;
|
---|
256 | ItemSet<IBenchmark> values = new ItemSet<IBenchmark>();
|
---|
257 | foreach (var benchmark in benchmarks) {
|
---|
258 | IBenchmark b = (IBenchmark)Activator.CreateInstance(benchmark);
|
---|
259 | values.Add(b);
|
---|
260 | }
|
---|
261 | string paramName = "Benchmark";
|
---|
262 | if (!Parameters.ContainsKey(paramName)) {
|
---|
263 | if (values.Count > 0) {
|
---|
264 | Parameters.Add(new ConstrainedValueParameter<IBenchmark>(paramName, "The benchmark which should be executed.", values, values.First(a => a is IBenchmark)));
|
---|
265 | } else {
|
---|
266 | Parameters.Add(new ConstrainedValueParameter<IBenchmark>(paramName, "The benchmark which should be executed.", values));
|
---|
267 | }
|
---|
268 | }
|
---|
269 | }
|
---|
270 |
|
---|
271 | public IDeepCloneable Clone(Cloner cloner) {
|
---|
272 | return new BenchmarkAlgorithm(this, cloner);
|
---|
273 | }
|
---|
274 | public object Clone() {
|
---|
275 | return Clone(new Cloner());
|
---|
276 | }
|
---|
277 |
|
---|
278 | public override string ToString() {
|
---|
279 | return Name;
|
---|
280 | }
|
---|
281 |
|
---|
282 | public void Prepare() {
|
---|
283 | if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused) && (ExecutionState != ExecutionState.Stopped))
|
---|
284 | throw new InvalidOperationException(string.Format("Prepare not allowed in execution state \"{0}\".", ExecutionState));
|
---|
285 | results.Clear();
|
---|
286 | OnPrepared();
|
---|
287 | }
|
---|
288 | public void Prepare(bool clearRuns) {
|
---|
289 | if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused) && (ExecutionState != ExecutionState.Stopped))
|
---|
290 | throw new InvalidOperationException(string.Format("Prepare not allowed in execution state \"{0}\".", ExecutionState));
|
---|
291 | if (clearRuns) runs.Clear();
|
---|
292 | Prepare();
|
---|
293 | }
|
---|
294 | public void Pause() {
|
---|
295 | if (ExecutionState != ExecutionState.Started)
|
---|
296 | throw new InvalidOperationException(string.Format("Pause not allowed in execution state \"{0}\".", ExecutionState));
|
---|
297 | }
|
---|
298 | public void Stop() {
|
---|
299 | if ((ExecutionState != ExecutionState.Started) && (ExecutionState != ExecutionState.Paused))
|
---|
300 | throw new InvalidOperationException(string.Format("Stop not allowed in execution state \"{0}\".", ExecutionState));
|
---|
301 | cancellationTokenSource.Cancel();
|
---|
302 | }
|
---|
303 | public void Start() {
|
---|
304 | cancellationTokenSource = new CancellationTokenSource();
|
---|
305 | OnStarted();
|
---|
306 | Task task = Task.Factory.StartNew(Run, cancellationTokenSource.Token, cancellationTokenSource.Token);
|
---|
307 | task.ContinueWith(t => {
|
---|
308 | try {
|
---|
309 | t.Wait();
|
---|
310 | } catch (AggregateException ex) {
|
---|
311 | try {
|
---|
312 | ex.Flatten().Handle(x => x is OperationCanceledException);
|
---|
313 | } catch (AggregateException remaining) {
|
---|
314 | if (remaining.InnerExceptions.Count == 1) OnExceptionOccurred(remaining.InnerExceptions[0]);
|
---|
315 | else OnExceptionOccurred(remaining);
|
---|
316 | }
|
---|
317 | }
|
---|
318 |
|
---|
319 | cancellationTokenSource.Dispose();
|
---|
320 | cancellationTokenSource = null;
|
---|
321 | OnStopped();
|
---|
322 | });
|
---|
323 | }
|
---|
324 |
|
---|
325 | private void Run(object state) {
|
---|
326 | CancellationToken cancellationToken = (CancellationToken)state;
|
---|
327 | lastUpdateTime = DateTime.UtcNow;
|
---|
328 | System.Timers.Timer timer = new System.Timers.Timer(250);
|
---|
329 | timer.AutoReset = true;
|
---|
330 | timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
|
---|
331 | timer.Start();
|
---|
332 | try {
|
---|
333 | Benchmark = (IBenchmark)BenchmarkParameter.ActualValue;
|
---|
334 | int chunkSize = ((IntValue)ChunkSizeParameter.ActualValue).Value;
|
---|
335 | if (chunkSize > 0) {
|
---|
336 | Benchmark.ChunkData = CreateChunkData(chunkSize);
|
---|
337 | } else if (chunkSize < 0) {
|
---|
338 | throw new ArgumentException("ChunkSize must not be negativ.");
|
---|
339 | }
|
---|
340 | TimeSpan timelimit = TimeSpan.FromMinutes(((DoubleValue)TimeLimitParameter.ActualValue).Value);
|
---|
341 | if (timelimit.TotalMilliseconds < 0) {
|
---|
342 | throw new ArgumentException("TimeLimit must not be negativ. ");
|
---|
343 | }
|
---|
344 | Benchmark.TimeLimit = timelimit;
|
---|
345 | Benchmark.Run(cancellationToken, results);
|
---|
346 | } catch (OperationCanceledException) {
|
---|
347 | } finally {
|
---|
348 | timer.Elapsed -= new System.Timers.ElapsedEventHandler(timer_Elapsed);
|
---|
349 | timer.Stop();
|
---|
350 | ExecutionTime += DateTime.UtcNow - lastUpdateTime;
|
---|
351 | }
|
---|
352 | }
|
---|
353 |
|
---|
354 | private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) {
|
---|
355 | System.Timers.Timer timer = (System.Timers.Timer)sender;
|
---|
356 | timer.Enabled = false;
|
---|
357 | DateTime now = DateTime.UtcNow;
|
---|
358 | ExecutionTime += now - lastUpdateTime;
|
---|
359 | lastUpdateTime = now;
|
---|
360 | timer.Enabled = true;
|
---|
361 | }
|
---|
362 |
|
---|
363 | public void CollectResultValues(IDictionary<string, IItem> values) {
|
---|
364 | values.Add("Execution Time", new TimeSpanValue(ExecutionTime));
|
---|
365 | CollectResultsRecursively("", Results, values);
|
---|
366 | }
|
---|
367 | private void CollectResultsRecursively(string path, ResultCollection results, IDictionary<string, IItem> values) {
|
---|
368 | foreach (IResult result in results) {
|
---|
369 | values.Add(path + result.Name, result.Value);
|
---|
370 | ResultCollection childCollection = result.Value as ResultCollection;
|
---|
371 | if (childCollection != null) {
|
---|
372 | CollectResultsRecursively(path + result.Name + ".", childCollection, values);
|
---|
373 | }
|
---|
374 | }
|
---|
375 | }
|
---|
376 | public void CollectParameterValues(IDictionary<string, IItem> values) {
|
---|
377 | foreach (IValueParameter param in parameters.OfType<IValueParameter>()) {
|
---|
378 | if (param.GetsCollected && param.Value != null) values.Add(param.Name, param.Value);
|
---|
379 | if (param.Value is IParameterizedItem) {
|
---|
380 | Dictionary<string, IItem> children = new Dictionary<string, IItem>();
|
---|
381 | ((IParameterizedItem)param.Value).CollectParameterValues(children);
|
---|
382 | foreach (string key in children.Keys)
|
---|
383 | values.Add(param.Name + "." + key, children[key]);
|
---|
384 | }
|
---|
385 | }
|
---|
386 | }
|
---|
387 |
|
---|
388 | private byte[][] CreateChunkData(int megaBytes) {
|
---|
389 | if (megaBytes <= 0) {
|
---|
390 | throw new ArgumentException("MegaBytes must be greater than zero", "megaBytes");
|
---|
391 | }
|
---|
392 | Random random = new Random();
|
---|
393 | byte[][] chunk = new byte[megaBytes][];
|
---|
394 | for (int i = 0; i < chunk.Length; i++) {
|
---|
395 | chunk[i] = new byte[1024 * 1024];
|
---|
396 | random.NextBytes(chunk[i]);
|
---|
397 | }
|
---|
398 | return chunk;
|
---|
399 | }
|
---|
400 |
|
---|
401 | #region Events
|
---|
402 | public event EventHandler ExecutionStateChanged;
|
---|
403 | private void OnExecutionStateChanged() {
|
---|
404 | EventHandler handler = ExecutionStateChanged;
|
---|
405 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
406 | }
|
---|
407 | public event EventHandler ExecutionTimeChanged;
|
---|
408 | private void OnExecutionTimeChanged() {
|
---|
409 | EventHandler handler = ExecutionTimeChanged;
|
---|
410 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
411 | }
|
---|
412 | public event EventHandler ProblemChanged { add { } remove { } }
|
---|
413 | public event EventHandler StoreAlgorithmInEachRunChanged;
|
---|
414 | private void OnStoreAlgorithmInEachRunChanged() {
|
---|
415 | EventHandler handler = StoreAlgorithmInEachRunChanged;
|
---|
416 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
417 | }
|
---|
418 | public event EventHandler Prepared;
|
---|
419 | private void OnPrepared() {
|
---|
420 | ExecutionState = ExecutionState.Prepared;
|
---|
421 | ExecutionTime = TimeSpan.Zero;
|
---|
422 | foreach (IStatefulItem statefulObject in this.GetObjectGraphObjects().OfType<IStatefulItem>()) {
|
---|
423 | statefulObject.InitializeState();
|
---|
424 | }
|
---|
425 | EventHandler handler = Prepared;
|
---|
426 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
427 | }
|
---|
428 | public event EventHandler Started;
|
---|
429 | private void OnStarted() {
|
---|
430 | ExecutionState = ExecutionState.Started;
|
---|
431 | EventHandler handler = Started;
|
---|
432 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
433 | }
|
---|
434 | public event EventHandler Paused;
|
---|
435 | private void OnPaused() {
|
---|
436 | ExecutionState = ExecutionState.Paused;
|
---|
437 | EventHandler handler = Paused;
|
---|
438 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
439 | }
|
---|
440 | public event EventHandler Stopped;
|
---|
441 | private void OnStopped() {
|
---|
442 | ExecutionState = ExecutionState.Stopped;
|
---|
443 | foreach (IStatefulItem statefulObject in this.GetObjectGraphObjects().OfType<IStatefulItem>()) {
|
---|
444 | statefulObject.ClearState();
|
---|
445 | }
|
---|
446 | runsCounter++;
|
---|
447 | runs.Add(new Run(string.Format("{0} Run {1}", Name, runsCounter), this));
|
---|
448 | EventHandler handler = Stopped;
|
---|
449 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
450 | }
|
---|
451 | public event EventHandler<EventArgs<Exception>> ExceptionOccurred;
|
---|
452 | private void OnExceptionOccurred(Exception exception) {
|
---|
453 | EventHandler<EventArgs<Exception>> handler = ExceptionOccurred;
|
---|
454 | if (handler != null) handler(this, new EventArgs<Exception>(exception));
|
---|
455 | }
|
---|
456 |
|
---|
457 | public event EventHandler<CancelEventArgs<string>> NameChanging;
|
---|
458 | private void OnNameChanging(CancelEventArgs<string> e) {
|
---|
459 | var handler = NameChanging;
|
---|
460 | if (handler != null) handler(this, e);
|
---|
461 | }
|
---|
462 |
|
---|
463 | public event EventHandler NameChanged;
|
---|
464 | private void OnNameChanged() {
|
---|
465 | var handler = NameChanged;
|
---|
466 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
467 | OnToStringChanged();
|
---|
468 | }
|
---|
469 |
|
---|
470 | public event EventHandler DescriptionChanged;
|
---|
471 | private void OnDescriptionChanged() {
|
---|
472 | var handler = DescriptionChanged;
|
---|
473 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
474 | }
|
---|
475 |
|
---|
476 | public event EventHandler ItemImageChanged;
|
---|
477 | private void OnItemImageChanged() {
|
---|
478 | EventHandler handler = ItemImageChanged;
|
---|
479 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
480 | }
|
---|
481 | public event EventHandler ToStringChanged;
|
---|
482 | private void OnToStringChanged() {
|
---|
483 | EventHandler handler = ToStringChanged;
|
---|
484 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
485 | }
|
---|
486 |
|
---|
487 | private void DeregisterRunsEvents() {
|
---|
488 | runs.CollectionReset -= new CollectionItemsChangedEventHandler<IRun>(Runs_CollectionReset);
|
---|
489 | }
|
---|
490 | private void RegisterRunsEvents() {
|
---|
491 | runs.CollectionReset += new CollectionItemsChangedEventHandler<IRun>(Runs_CollectionReset);
|
---|
492 | }
|
---|
493 | private void Runs_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRun> e) {
|
---|
494 | runsCounter = runs.Count;
|
---|
495 | }
|
---|
496 | #endregion
|
---|
497 | }
|
---|
498 | }
|
---|