[3226] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[14185] | 3 | * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[3226] | 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;
|
---|
[4533] | 23 | using System.Collections.Generic;
|
---|
[3226] | 24 | using System.Drawing;
|
---|
[15287] | 25 | using System.Threading;
|
---|
| 26 | using System.Threading.Tasks;
|
---|
[3716] | 27 | using HeuristicLab.Collections;
|
---|
[3265] | 28 | using HeuristicLab.Common;
|
---|
[3226] | 29 | using HeuristicLab.Core;
|
---|
[3716] | 30 | using HeuristicLab.Data;
|
---|
[3226] | 31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 32 |
|
---|
| 33 | namespace HeuristicLab.Optimization {
|
---|
[8194] | 34 | internal enum BatchRunAction { None, Prepare, Start, Pause, Stop };
|
---|
| 35 |
|
---|
[3226] | 36 | /// <summary>
|
---|
[5300] | 37 | /// A run in which an optimizer is executed a given number of times.
|
---|
[3226] | 38 | /// </summary>
|
---|
[5300] | 39 | [Item("Batch Run", "A run in which an optimizer is executed a given number of times.")]
|
---|
[12504] | 40 | [Creatable(CreatableAttribute.Categories.TestingAndAnalysis, Priority = 110)]
|
---|
[3226] | 41 | [StorableClass]
|
---|
[4419] | 42 | public sealed class BatchRun : NamedItem, IOptimizer, IStorableContent {
|
---|
| 43 | public string Filename { get; set; }
|
---|
| 44 |
|
---|
[7201] | 45 | public static new Image StaticItemImage {
|
---|
| 46 | get { return HeuristicLab.Common.Resources.VSImageLibrary.Event; }
|
---|
| 47 | }
|
---|
[3226] | 48 | public override Image ItemImage {
|
---|
[3351] | 49 | get {
|
---|
[6534] | 50 | if (ExecutionState == ExecutionState.Prepared) return HeuristicLab.Common.Resources.VSImageLibrary.BatchRunPrepared;
|
---|
| 51 | else if (ExecutionState == ExecutionState.Started) return HeuristicLab.Common.Resources.VSImageLibrary.BatchRunStarted;
|
---|
| 52 | else if (ExecutionState == ExecutionState.Paused) return HeuristicLab.Common.Resources.VSImageLibrary.BatchRunPaused;
|
---|
| 53 | else if (ExecutionState == ExecutionState.Stopped) return HeuristicLab.Common.Resources.VSImageLibrary.BatchRunStopped;
|
---|
[7201] | 54 | else return base.ItemImage;
|
---|
[3351] | 55 | }
|
---|
[3226] | 56 | }
|
---|
| 57 |
|
---|
[3265] | 58 | [Storable]
|
---|
| 59 | private ExecutionState executionState;
|
---|
| 60 | public ExecutionState ExecutionState {
|
---|
| 61 | get { return executionState; }
|
---|
| 62 | private set {
|
---|
| 63 | if (executionState != value) {
|
---|
| 64 | executionState = value;
|
---|
| 65 | OnExecutionStateChanged();
|
---|
[3351] | 66 | OnItemImageChanged();
|
---|
[3265] | 67 | }
|
---|
| 68 | }
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | [Storable]
|
---|
| 72 | private TimeSpan executionTime;
|
---|
| 73 | public TimeSpan ExecutionTime {
|
---|
| 74 | get {
|
---|
[5300] | 75 | if ((Optimizer != null) && (Optimizer.ExecutionState != ExecutionState.Stopped))
|
---|
| 76 | return executionTime + Optimizer.ExecutionTime;
|
---|
[3265] | 77 | else
|
---|
| 78 | return executionTime;
|
---|
| 79 | }
|
---|
| 80 | private set {
|
---|
| 81 | executionTime = value;
|
---|
| 82 | OnExecutionTimeChanged();
|
---|
| 83 | }
|
---|
| 84 | }
|
---|
| 85 |
|
---|
[3280] | 86 | [Storable]
|
---|
[8149] | 87 | private TimeSpan runsExecutionTime;
|
---|
| 88 |
|
---|
| 89 | [Storable]
|
---|
[5300] | 90 | private IOptimizer optimizer;
|
---|
| 91 | public IOptimizer Optimizer {
|
---|
| 92 | get { return optimizer; }
|
---|
[3226] | 93 | set {
|
---|
[5300] | 94 | if (optimizer != value) {
|
---|
| 95 | if (optimizer != null) {
|
---|
| 96 | DeregisterOptimizerEvents();
|
---|
| 97 | IEnumerable<IRun> runs = optimizer.Runs;
|
---|
| 98 | optimizer = null; //necessary to avoid removing the runs from the old optimizer
|
---|
[4115] | 99 | Runs.RemoveRange(runs);
|
---|
| 100 | }
|
---|
[5300] | 101 | optimizer = value;
|
---|
| 102 | if (optimizer != null) {
|
---|
| 103 | RegisterOptimizerEvents();
|
---|
| 104 | Runs.AddRange(optimizer.Runs);
|
---|
[4115] | 105 | }
|
---|
[5300] | 106 | OnOptimizerChanged();
|
---|
[3716] | 107 | Prepare();
|
---|
[3226] | 108 | }
|
---|
| 109 | }
|
---|
| 110 | }
|
---|
[5300] | 111 | // BackwardsCompatibility3.3
|
---|
| 112 | #region Backwards compatible code (remove with 3.4)
|
---|
[5409] | 113 | [Storable(AllowOneWay = true)]
|
---|
[5300] | 114 | private IAlgorithm algorithm {
|
---|
| 115 | set { optimizer = value; }
|
---|
| 116 | }
|
---|
| 117 | #endregion
|
---|
[3226] | 118 |
|
---|
| 119 | [Storable]
|
---|
| 120 | private int repetitions;
|
---|
| 121 | public int Repetitions {
|
---|
| 122 | get { return repetitions; }
|
---|
| 123 | set {
|
---|
| 124 | if (repetitions != value) {
|
---|
| 125 | repetitions = value;
|
---|
| 126 | OnRepetitionsChanged();
|
---|
[5300] | 127 | if ((Optimizer != null) && (Optimizer.ExecutionState == ExecutionState.Stopped))
|
---|
[3275] | 128 | Prepare();
|
---|
[3226] | 129 | }
|
---|
| 130 | }
|
---|
| 131 | }
|
---|
[3716] | 132 | [Storable]
|
---|
| 133 | private int repetitionsCounter;
|
---|
[8668] | 134 | public int RepetitionsCounter {
|
---|
| 135 | get { return repetitionsCounter; }
|
---|
| 136 | private set {
|
---|
| 137 | if (value != repetitionsCounter) {
|
---|
| 138 | repetitionsCounter = value;
|
---|
| 139 | OnRepetitionsCounterChanged();
|
---|
| 140 | }
|
---|
| 141 | }
|
---|
| 142 | }
|
---|
[3226] | 143 |
|
---|
| 144 | [Storable]
|
---|
[3260] | 145 | private RunCollection runs;
|
---|
| 146 | public RunCollection Runs {
|
---|
| 147 | get { return runs; }
|
---|
[3716] | 148 | private set {
|
---|
| 149 | if (value == null) throw new ArgumentNullException();
|
---|
| 150 | if (runs != value) {
|
---|
| 151 | if (runs != null) DeregisterRunsEvents();
|
---|
| 152 | runs = value;
|
---|
| 153 | if (runs != null) RegisterRunsEvents();
|
---|
| 154 | }
|
---|
| 155 | }
|
---|
[3226] | 156 | }
|
---|
| 157 |
|
---|
[5419] | 158 | public IEnumerable<IOptimizer> NestedOptimizers {
|
---|
| 159 | get {
|
---|
| 160 | if (Optimizer == null) yield break;
|
---|
| 161 |
|
---|
| 162 | yield return Optimizer;
|
---|
| 163 | foreach (IOptimizer opt in Optimizer.NestedOptimizers)
|
---|
| 164 | yield return opt;
|
---|
| 165 | }
|
---|
| 166 | }
|
---|
| 167 |
|
---|
[8194] | 168 | private BatchRunAction batchRunAction = BatchRunAction.None;
|
---|
[3226] | 169 |
|
---|
| 170 | public BatchRun()
|
---|
| 171 | : base() {
|
---|
[3280] | 172 | name = ItemName;
|
---|
| 173 | description = ItemDescription;
|
---|
[3265] | 174 | executionState = ExecutionState.Stopped;
|
---|
| 175 | executionTime = TimeSpan.Zero;
|
---|
[8149] | 176 | runsExecutionTime = TimeSpan.Zero;
|
---|
[3226] | 177 | repetitions = 10;
|
---|
[3716] | 178 | repetitionsCounter = 0;
|
---|
[8975] | 179 | Runs = new RunCollection { OptimizerName = Name };
|
---|
[3226] | 180 | }
|
---|
[3280] | 181 | public BatchRun(string name)
|
---|
| 182 | : base(name) {
|
---|
| 183 | description = ItemDescription;
|
---|
[3265] | 184 | executionState = ExecutionState.Stopped;
|
---|
| 185 | executionTime = TimeSpan.Zero;
|
---|
[8149] | 186 | runsExecutionTime = TimeSpan.Zero;
|
---|
[3226] | 187 | repetitions = 10;
|
---|
[3716] | 188 | repetitionsCounter = 0;
|
---|
[8975] | 189 | Runs = new RunCollection { OptimizerName = Name };
|
---|
[3226] | 190 | }
|
---|
[3280] | 191 | public BatchRun(string name, string description)
|
---|
| 192 | : base(name, description) {
|
---|
[3265] | 193 | executionState = ExecutionState.Stopped;
|
---|
| 194 | executionTime = TimeSpan.Zero;
|
---|
[8149] | 195 | runsExecutionTime = TimeSpan.Zero;
|
---|
[3226] | 196 | repetitions = 10;
|
---|
[3716] | 197 | repetitionsCounter = 0;
|
---|
[8975] | 198 | Runs = new RunCollection { OptimizerName = Name };
|
---|
[3226] | 199 | }
|
---|
[3280] | 200 | [StorableConstructor]
|
---|
[6767] | 201 | private BatchRun(bool deserializing) : base(deserializing) { }
|
---|
[3280] | 202 | [StorableHook(HookType.AfterDeserialization)]
|
---|
[4722] | 203 | private void AfterDeserialization() {
|
---|
| 204 | Initialize();
|
---|
[3280] | 205 | }
|
---|
| 206 |
|
---|
[4722] | 207 | private BatchRun(BatchRun original, Cloner cloner)
|
---|
| 208 | : base(original, cloner) {
|
---|
| 209 | executionState = original.executionState;
|
---|
| 210 | executionTime = original.executionTime;
|
---|
[8149] | 211 | runsExecutionTime = original.runsExecutionTime;
|
---|
[5300] | 212 | optimizer = cloner.Clone(original.optimizer);
|
---|
[4722] | 213 | repetitions = original.repetitions;
|
---|
| 214 | repetitionsCounter = original.repetitionsCounter;
|
---|
| 215 | runs = cloner.Clone(original.runs);
|
---|
[8194] | 216 | batchRunAction = original.batchRunAction;
|
---|
[4722] | 217 | Initialize();
|
---|
| 218 | }
|
---|
[3226] | 219 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[3280] | 220 | if (ExecutionState == ExecutionState.Started) throw new InvalidOperationException(string.Format("Clone not allowed in execution state \"{0}\".", ExecutionState));
|
---|
[4722] | 221 | return new BatchRun(this, cloner);
|
---|
[3226] | 222 | }
|
---|
| 223 |
|
---|
[4722] | 224 | private void Initialize() {
|
---|
[5300] | 225 | if (optimizer != null) RegisterOptimizerEvents();
|
---|
[4722] | 226 | if (runs != null) RegisterRunsEvents();
|
---|
| 227 | }
|
---|
| 228 |
|
---|
[3226] | 229 | public void Prepare() {
|
---|
[3275] | 230 | Prepare(false);
|
---|
[3226] | 231 | }
|
---|
[3275] | 232 | public void Prepare(bool clearRuns) {
|
---|
[3265] | 233 | if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused) && (ExecutionState != ExecutionState.Stopped))
|
---|
| 234 | throw new InvalidOperationException(string.Format("Prepare not allowed in execution state \"{0}\".", ExecutionState));
|
---|
[5300] | 235 | if (Optimizer != null) {
|
---|
[8149] | 236 | ExecutionTime = TimeSpan.Zero;
|
---|
[8668] | 237 | RepetitionsCounter = 0;
|
---|
[3716] | 238 | if (clearRuns) runs.Clear();
|
---|
[8194] | 239 | batchRunAction = BatchRunAction.Prepare;
|
---|
| 240 | // a race-condition may occur when the optimizer has changed the state by itself in the meantime
|
---|
[15287] | 241 | try { Optimizer.Prepare(clearRuns); } catch (InvalidOperationException) { }
|
---|
[6471] | 242 | } else {
|
---|
| 243 | ExecutionState = ExecutionState.Stopped;
|
---|
[3261] | 244 | }
|
---|
[3226] | 245 | }
|
---|
[3265] | 246 | public void Start() {
|
---|
[15287] | 247 | Start(CancellationToken.None);
|
---|
| 248 | }
|
---|
| 249 | public void Start(CancellationToken cancellationToken) {
|
---|
[3265] | 250 | if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused))
|
---|
| 251 | throw new InvalidOperationException(string.Format("Start not allowed in execution state \"{0}\".", ExecutionState));
|
---|
[6767] | 252 | if (Optimizer == null) return;
|
---|
[8194] | 253 | batchRunAction = BatchRunAction.Start;
|
---|
[6816] | 254 | if (Optimizer.ExecutionState == ExecutionState.Stopped) Optimizer.Prepare();
|
---|
[15287] | 255 | for (int i = repetitionsCounter; i < repetitions; i++) {
|
---|
| 256 | // a race-condition may occur when the optimizer has changed the state by itself in the meantime
|
---|
| 257 | try { Optimizer.Start(cancellationToken); } catch (InvalidOperationException) { }
|
---|
| 258 | if (ExecutionState == ExecutionState.Paused || ExecutionState == ExecutionState.Stopped) break;
|
---|
| 259 | Optimizer.Prepare();
|
---|
[15408] | 260 | if (ExecutionState == ExecutionState.Paused || ExecutionState == ExecutionState.Stopped) break;
|
---|
[15287] | 261 | }
|
---|
[3265] | 262 | }
|
---|
[15287] | 263 | public async Task StartAsync() { await StartAsync(CancellationToken.None); }
|
---|
| 264 | public async Task StartAsync(CancellationToken cancellationToken) {
|
---|
| 265 | await AsyncHelper.DoAsync(Start, cancellationToken);
|
---|
| 266 | }
|
---|
[3265] | 267 | public void Pause() {
|
---|
| 268 | if (ExecutionState != ExecutionState.Started)
|
---|
| 269 | throw new InvalidOperationException(string.Format("Pause not allowed in execution state \"{0}\".", ExecutionState));
|
---|
[6767] | 270 | if (Optimizer == null) return;
|
---|
[8194] | 271 | batchRunAction = BatchRunAction.Pause;
|
---|
[6767] | 272 | if (Optimizer.ExecutionState != ExecutionState.Started) return;
|
---|
[8194] | 273 | // a race-condition may occur when the optimizer has changed the state by itself in the meantime
|
---|
[15408] | 274 | try { Optimizer.Pause(); } catch (InvalidOperationException) { } catch (NotSupportedException) { }
|
---|
[3265] | 275 | }
|
---|
[3226] | 276 | public void Stop() {
|
---|
[3265] | 277 | if ((ExecutionState != ExecutionState.Started) && (ExecutionState != ExecutionState.Paused))
|
---|
| 278 | throw new InvalidOperationException(string.Format("Stop not allowed in execution state \"{0}\".", ExecutionState));
|
---|
[6767] | 279 | if (Optimizer == null) return;
|
---|
[8194] | 280 | batchRunAction = BatchRunAction.Stop;
|
---|
[8190] | 281 | if (Optimizer.ExecutionState != ExecutionState.Started && Optimizer.ExecutionState != ExecutionState.Paused) {
|
---|
| 282 | OnStopped();
|
---|
| 283 | return;
|
---|
| 284 | }
|
---|
[8194] | 285 | // a race-condition may occur when the optimizer has changed the state by itself in the meantime
|
---|
[15287] | 286 | try { Optimizer.Stop(); } catch (InvalidOperationException) { }
|
---|
[3226] | 287 | }
|
---|
| 288 |
|
---|
| 289 | #region Events
|
---|
[8738] | 290 | protected override void OnNameChanged() {
|
---|
| 291 | base.OnNameChanged();
|
---|
[8975] | 292 | runs.OptimizerName = Name;
|
---|
[8738] | 293 | }
|
---|
| 294 |
|
---|
[3265] | 295 | public event EventHandler ExecutionStateChanged;
|
---|
| 296 | private void OnExecutionStateChanged() {
|
---|
| 297 | EventHandler handler = ExecutionStateChanged;
|
---|
| 298 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 299 | }
|
---|
| 300 | public event EventHandler ExecutionTimeChanged;
|
---|
| 301 | private void OnExecutionTimeChanged() {
|
---|
| 302 | EventHandler handler = ExecutionTimeChanged;
|
---|
| 303 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 304 | }
|
---|
[5300] | 305 | public event EventHandler OptimizerChanged;
|
---|
| 306 | private void OnOptimizerChanged() {
|
---|
| 307 | EventHandler handler = OptimizerChanged;
|
---|
[3265] | 308 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
[3226] | 309 | }
|
---|
| 310 | public event EventHandler RepetitionsChanged;
|
---|
| 311 | private void OnRepetitionsChanged() {
|
---|
[3265] | 312 | EventHandler handler = RepetitionsChanged;
|
---|
| 313 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
[3226] | 314 | }
|
---|
[8668] | 315 | public event EventHandler RepetetionsCounterChanged;
|
---|
| 316 | private void OnRepetitionsCounterChanged() {
|
---|
| 317 | EventHandler handler = RepetetionsCounterChanged;
|
---|
| 318 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 319 | }
|
---|
[3226] | 320 | public event EventHandler Prepared;
|
---|
| 321 | private void OnPrepared() {
|
---|
[8194] | 322 | batchRunAction = BatchRunAction.None;
|
---|
[3265] | 323 | ExecutionState = ExecutionState.Prepared;
|
---|
| 324 | EventHandler handler = Prepared;
|
---|
| 325 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
[3226] | 326 | }
|
---|
| 327 | public event EventHandler Started;
|
---|
| 328 | private void OnStarted() {
|
---|
[8194] | 329 | // no reset of BatchRunAction.Started, because we need to differ which of the two was started by the user
|
---|
[3265] | 330 | ExecutionState = ExecutionState.Started;
|
---|
| 331 | EventHandler handler = Started;
|
---|
| 332 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
[3226] | 333 | }
|
---|
[3265] | 334 | public event EventHandler Paused;
|
---|
| 335 | private void OnPaused() {
|
---|
[8194] | 336 | batchRunAction = BatchRunAction.None;
|
---|
[3265] | 337 | ExecutionState = ExecutionState.Paused;
|
---|
| 338 | EventHandler handler = Paused;
|
---|
| 339 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 340 | }
|
---|
[3226] | 341 | public event EventHandler Stopped;
|
---|
| 342 | private void OnStopped() {
|
---|
[8194] | 343 | batchRunAction = BatchRunAction.None;
|
---|
[3265] | 344 | ExecutionState = ExecutionState.Stopped;
|
---|
| 345 | EventHandler handler = Stopped;
|
---|
| 346 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
[3226] | 347 | }
|
---|
[3265] | 348 | public event EventHandler<EventArgs<Exception>> ExceptionOccurred;
|
---|
[3226] | 349 | private void OnExceptionOccurred(Exception exception) {
|
---|
[3265] | 350 | EventHandler<EventArgs<Exception>> handler = ExceptionOccurred;
|
---|
| 351 | if (handler != null) handler(this, new EventArgs<Exception>(exception));
|
---|
[3226] | 352 | }
|
---|
| 353 |
|
---|
[5300] | 354 | private void RegisterOptimizerEvents() {
|
---|
| 355 | optimizer.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Optimizer_ExceptionOccurred);
|
---|
| 356 | optimizer.ExecutionTimeChanged += new EventHandler(Optimizer_ExecutionTimeChanged);
|
---|
| 357 | optimizer.Paused += new EventHandler(Optimizer_Paused);
|
---|
| 358 | optimizer.Prepared += new EventHandler(Optimizer_Prepared);
|
---|
| 359 | optimizer.Started += new EventHandler(Optimizer_Started);
|
---|
| 360 | optimizer.Stopped += new EventHandler(Optimizer_Stopped);
|
---|
| 361 | optimizer.Runs.CollectionReset += new CollectionItemsChangedEventHandler<IRun>(Optimizer_Runs_CollectionReset);
|
---|
| 362 | optimizer.Runs.ItemsAdded += new CollectionItemsChangedEventHandler<IRun>(Optimizer_Runs_ItemsAdded);
|
---|
| 363 | optimizer.Runs.ItemsRemoved += new CollectionItemsChangedEventHandler<IRun>(Optimizer_Runs_ItemsRemoved);
|
---|
[3261] | 364 | }
|
---|
[5300] | 365 | private void DeregisterOptimizerEvents() {
|
---|
| 366 | optimizer.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Optimizer_ExceptionOccurred);
|
---|
| 367 | optimizer.ExecutionTimeChanged -= new EventHandler(Optimizer_ExecutionTimeChanged);
|
---|
| 368 | optimizer.Paused -= new EventHandler(Optimizer_Paused);
|
---|
| 369 | optimizer.Prepared -= new EventHandler(Optimizer_Prepared);
|
---|
| 370 | optimizer.Started -= new EventHandler(Optimizer_Started);
|
---|
| 371 | optimizer.Stopped -= new EventHandler(Optimizer_Stopped);
|
---|
| 372 | optimizer.Runs.CollectionReset -= new CollectionItemsChangedEventHandler<IRun>(Optimizer_Runs_CollectionReset);
|
---|
| 373 | optimizer.Runs.ItemsAdded -= new CollectionItemsChangedEventHandler<IRun>(Optimizer_Runs_ItemsAdded);
|
---|
| 374 | optimizer.Runs.ItemsRemoved -= new CollectionItemsChangedEventHandler<IRun>(Optimizer_Runs_ItemsRemoved);
|
---|
[3226] | 375 | }
|
---|
[5300] | 376 | private void Optimizer_ExceptionOccurred(object sender, EventArgs<Exception> e) {
|
---|
[3261] | 377 | OnExceptionOccurred(e.Value);
|
---|
[3226] | 378 | }
|
---|
[5300] | 379 | private void Optimizer_ExecutionTimeChanged(object sender, EventArgs e) {
|
---|
[3265] | 380 | OnExecutionTimeChanged();
|
---|
| 381 | }
|
---|
[5300] | 382 | private void Optimizer_Paused(object sender, EventArgs e) {
|
---|
[6767] | 383 | if (ExecutionState == ExecutionState.Started) {
|
---|
| 384 | OnPaused();
|
---|
| 385 | }
|
---|
[3265] | 386 | }
|
---|
[5300] | 387 | private void Optimizer_Prepared(object sender, EventArgs e) {
|
---|
[8194] | 388 | if (batchRunAction == BatchRunAction.Prepare || ExecutionState == ExecutionState.Stopped) {
|
---|
[8150] | 389 | ExecutionTime = TimeSpan.Zero;
|
---|
| 390 | runsExecutionTime = TimeSpan.Zero;
|
---|
[8668] | 391 | RepetitionsCounter = 0;
|
---|
[3276] | 392 | OnPrepared();
|
---|
[6767] | 393 | }
|
---|
[3265] | 394 | }
|
---|
[5300] | 395 | private void Optimizer_Started(object sender, EventArgs e) {
|
---|
[3276] | 396 | if (ExecutionState != ExecutionState.Started)
|
---|
| 397 | OnStarted();
|
---|
[3261] | 398 | }
|
---|
[5300] | 399 | private void Optimizer_Stopped(object sender, EventArgs e) {
|
---|
[8668] | 400 | RepetitionsCounter++;
|
---|
[8149] | 401 | ExecutionTime += runsExecutionTime;
|
---|
| 402 | runsExecutionTime = TimeSpan.Zero;
|
---|
[3265] | 403 |
|
---|
[8194] | 404 | if (batchRunAction == BatchRunAction.Stop) OnStopped();
|
---|
[6767] | 405 | else if (repetitionsCounter >= repetitions) OnStopped();
|
---|
[8194] | 406 | else if (batchRunAction == BatchRunAction.Pause) OnPaused();
|
---|
[15287] | 407 | else if (batchRunAction == BatchRunAction.Start) return;
|
---|
| 408 | else if (executionState == ExecutionState.Started) {
|
---|
[8189] | 409 | // if the batch run hasn't been started but the inner optimizer was run then pause
|
---|
| 410 | OnPaused();
|
---|
[6767] | 411 | } else OnStopped();
|
---|
[3226] | 412 | }
|
---|
[5300] | 413 | private void Optimizer_Runs_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRun> e) {
|
---|
[3275] | 414 | Runs.RemoveRange(e.OldItems);
|
---|
| 415 | Runs.AddRange(e.Items);
|
---|
| 416 | }
|
---|
[5300] | 417 | private void Optimizer_Runs_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IRun> e) {
|
---|
[3275] | 418 | Runs.AddRange(e.Items);
|
---|
| 419 | }
|
---|
[5300] | 420 | private void Optimizer_Runs_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IRun> e) {
|
---|
[3275] | 421 | Runs.RemoveRange(e.Items);
|
---|
| 422 | }
|
---|
[3716] | 423 |
|
---|
| 424 | private void RegisterRunsEvents() {
|
---|
| 425 | runs.CollectionReset += new CollectionItemsChangedEventHandler<IRun>(Runs_CollectionReset);
|
---|
[4115] | 426 | runs.ItemsAdded += new CollectionItemsChangedEventHandler<IRun>(Runs_ItemsAdded);
|
---|
[3716] | 427 | runs.ItemsRemoved += new CollectionItemsChangedEventHandler<IRun>(Runs_ItemsRemoved);
|
---|
| 428 | }
|
---|
[4115] | 429 |
|
---|
[3716] | 430 | private void DeregisterRunsEvents() {
|
---|
| 431 | runs.CollectionReset -= new CollectionItemsChangedEventHandler<IRun>(Runs_CollectionReset);
|
---|
[4115] | 432 | runs.ItemsAdded -= new CollectionItemsChangedEventHandler<IRun>(Runs_ItemsAdded);
|
---|
[3716] | 433 | runs.ItemsRemoved -= new CollectionItemsChangedEventHandler<IRun>(Runs_ItemsRemoved);
|
---|
| 434 | }
|
---|
| 435 | private void Runs_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRun> e) {
|
---|
[5300] | 436 | if (Optimizer != null) Optimizer.Runs.RemoveRange(e.OldItems);
|
---|
[3716] | 437 | foreach (IRun run in e.Items) {
|
---|
| 438 | IItem item;
|
---|
| 439 | run.Results.TryGetValue("Execution Time", out item);
|
---|
| 440 | TimeSpanValue executionTime = item as TimeSpanValue;
|
---|
| 441 | if (executionTime != null) ExecutionTime += executionTime.Value;
|
---|
| 442 | }
|
---|
| 443 | }
|
---|
[4115] | 444 | private void Runs_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IRun> e) {
|
---|
| 445 | foreach (IRun run in e.Items) {
|
---|
| 446 | IItem item;
|
---|
| 447 | run.Results.TryGetValue("Execution Time", out item);
|
---|
| 448 | TimeSpanValue executionTime = item as TimeSpanValue;
|
---|
[8149] | 449 | if (executionTime != null) {
|
---|
| 450 | if (Optimizer.ExecutionState == ExecutionState.Started)
|
---|
| 451 | runsExecutionTime += executionTime.Value;
|
---|
| 452 | else
|
---|
| 453 | ExecutionTime += executionTime.Value;
|
---|
| 454 | }
|
---|
[4115] | 455 | }
|
---|
| 456 | }
|
---|
[3716] | 457 | private void Runs_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IRun> e) {
|
---|
[5300] | 458 | if (Optimizer != null) Optimizer.Runs.RemoveRange(e.Items);
|
---|
[3716] | 459 | }
|
---|
[3226] | 460 | #endregion
|
---|
| 461 | }
|
---|
| 462 | }
|
---|