Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Async/HeuristicLab.Algorithms.DataAnalysis/3.4/FixedDataAnalysisAlgorithm.cs @ 15065

Last change on this file since 15065 was 15065, checked in by jkarder, 7 years ago

#2258: refactored async methods

  • synchronously called IExecutables are now executed in the caller's thread
  • removed old synchronization code from unit tests
File size: 3.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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
22using System;
23using System.Threading;
24using HeuristicLab.Common;
25using HeuristicLab.Optimization;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27using HeuristicLab.Problems.DataAnalysis;
28
29namespace HeuristicLab.Algorithms.DataAnalysis {
30  [StorableClass]
31  public abstract class FixedDataAnalysisAlgorithm<T> : Algorithm,
32    IDataAnalysisAlgorithm<T>,
33    IStorableContent
34    where T : class, IDataAnalysisProblem {
35    public string Filename { get; set; }
36
37    #region Properties
38    public override Type ProblemType {
39      get { return typeof(T); }
40    }
41    public new T Problem {
42      get { return (T)base.Problem; }
43      set { base.Problem = value; }
44    }
45    [Storable]
46    private ResultCollection results;
47    public override ResultCollection Results {
48      get { return results; }
49    }
50    #endregion
51
52    private DateTime lastUpdateTime;
53
54    [StorableConstructor]
55    protected FixedDataAnalysisAlgorithm(bool deserializing) : base(deserializing) { }
56    protected FixedDataAnalysisAlgorithm(FixedDataAnalysisAlgorithm<T> original, Cloner cloner)
57      : base(original, cloner) {
58      results = cloner.Clone(original.Results);
59    }
60    public FixedDataAnalysisAlgorithm()
61      : base() {
62      results = new ResultCollection();
63    }
64
65    public override void Prepare() {
66      if (Problem != null) base.Prepare();
67      results.Clear();
68      OnPrepared();
69    }
70
71    public override void Start(CancellationToken cancellationToken) {
72      base.Start(cancellationToken);
73      OnStarted();
74
75      try {
76        Run(cancellationToken);
77      } catch (OperationCanceledException) {
78      } catch (AggregateException ae) {
79        if (ae.InnerExceptions.Count == 1) OnExceptionOccurred(ae.InnerExceptions[0]);
80        else OnExceptionOccurred(ae);
81      } catch (Exception e) {
82        OnExceptionOccurred(e);
83      }
84
85      OnStopped();
86    }
87    private void Run(object state) {
88      CancellationToken cancellationToken = (CancellationToken)state;
89      lastUpdateTime = DateTime.UtcNow;
90      System.Timers.Timer timer = new System.Timers.Timer(250);
91      timer.AutoReset = true;
92      timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
93      timer.Start();
94      try {
95        Run();
96      }
97      finally {
98        timer.Elapsed -= new System.Timers.ElapsedEventHandler(timer_Elapsed);
99        timer.Stop();
100        ExecutionTime += DateTime.UtcNow - lastUpdateTime;
101      }
102
103      cancellationToken.ThrowIfCancellationRequested();
104    }
105    protected abstract void Run();
106    #region Events
107    protected override void OnProblemChanged() {
108      Problem.Reset += new EventHandler(Problem_Reset);
109      base.OnProblemChanged();
110    }
111    private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) {
112      System.Timers.Timer timer = (System.Timers.Timer)sender;
113      timer.Enabled = false;
114      DateTime now = DateTime.UtcNow;
115      ExecutionTime += now - lastUpdateTime;
116      lastUpdateTime = now;
117      timer.Enabled = true;
118    }
119    #endregion
120
121  }
122}
Note: See TracBrowser for help on using the repository browser.