Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
09/04/14 19:40:22 (10 years ago)
Author:
abeham
Message:

#2120:

  • Parameters and Results are now ObservableDictionaries
  • PropertyChanged event handler replaces the Changed event handler
  • RunCollection listens to changed events to each run's parameters and results (8 additional event handlers per run)
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Optimization/3.3/Run.cs

    r11171 r11344  
    2222using System;
    2323using System.Collections.Generic;
     24using System.ComponentModel;
    2425using System.Drawing;
    25 using System.Linq;
     26using HeuristicLab.Collections;
    2627using HeuristicLab.Common;
    2728using HeuristicLab.Core;
     
    4445      algorithm = cloner.Clone(original.algorithm);
    4546
    46       parameters = new Dictionary<string, IItem>();
     47      parameters = new ObservableDictionary<string, IItem>();
    4748      foreach (string key in original.parameters.Keys)
    4849        parameters.Add(key, cloner.Clone(original.parameters[key]));
    4950
    50       results = new Dictionary<string, IItem>();
     51      results = new ObservableDictionary<string, IItem>();
    5152      foreach (string key in original.results.Keys)
    5253        results.Add(key, cloner.Clone(original.results[key]));
    5354    }
     55
    5456    public override IDeepCloneable Clone(Cloner cloner) {
    5557      return new Run(this, cloner);
     
    6264      color = Color.Black;
    6365      algorithm = null;
    64       parameters = new Dictionary<string, IItem>();
    65       results = new Dictionary<string, IItem>();
     66      parameters = new ObservableDictionary<string, IItem>();
     67      results = new ObservableDictionary<string, IItem>();
    6668    }
    6769    public Run(IAlgorithm algorithm)
     
    8890
    8991    private void Initialize(IAlgorithm algorithm) {
    90       parameters = new Dictionary<string, IItem>();
    91       results = new Dictionary<string, IItem>();
     92      parameters = new ObservableDictionary<string, IItem>();
     93      results = new ObservableDictionary<string, IItem>();
    9294
    9395      if (algorithm.StoreAlgorithmInEachRun) {
    94         IAlgorithm clone = (IAlgorithm)algorithm.Clone();
     96        var clone = (IAlgorithm)algorithm.Clone();
    9597        clone.CollectParameterValues(parameters);
    9698        clone.CollectResultValues(results);
     
    98100        this.algorithm = clone;
    99101      } else {
    100         algorithm.CollectParameterValues(parameters);
    101         algorithm.CollectResultValues(results);
    102         Cloner cloner = new Cloner();
    103         parameters = parameters.Select(x => new KeyValuePair<string, IItem>(x.Key, cloner.Clone(x.Value))).ToDictionary(x => x.Key, x => x.Value);
    104         results = results.Select(x => new KeyValuePair<string, IItem>(x.Key, cloner.Clone(x.Value))).ToDictionary(x => x.Key, x => x.Value);
     102        var par = new Dictionary<string, IItem>();
     103        var res = new Dictionary<string, IItem>();
     104        algorithm.CollectParameterValues(par);
     105        algorithm.CollectResultValues(res);
     106        var cloner = new Cloner();
     107        foreach (var k in par) parameters.Add(k.Key, cloner.Clone(k.Value));
     108        foreach (var k in res) results.Add(k.Key, cloner.Clone(k.Value));
    105109      }
    106110    }
     
    115119      get { return algorithm; }
    116120    }
    117     [Storable]
    118     private Dictionary<string, IItem> parameters;
    119     public IDictionary<string, IItem> Parameters {
     121
     122    [Storable(Name = "parameters")]
     123    private IDictionary<string, IItem> StorableParameters {
    120124      get { return parameters; }
     125      set {
     126        if (!(value is IObservableDictionary<string, IItem>))
     127          parameters = new ObservableDictionary<string, IItem>(value);
     128        else parameters = (IObservableDictionary<string, IItem>)value;
     129      }
    121130    }
    122     [Storable]
    123     private Dictionary<string, IItem> results;
    124     public IDictionary<string, IItem> Results {
     131    private IObservableDictionary<string, IItem> parameters;
     132    public IObservableDictionary<string, IItem> Parameters {
     133      get { return parameters; }
     134      private set {
     135        if (parameters != value) {
     136          parameters = value;
     137          OnPropertyChanged("Parameters");
     138        }
     139      }
     140    }
     141
     142    [Storable(Name = "results")]
     143    private IDictionary<string, IItem> StorableResults {
    125144      get { return results; }
     145      set {
     146        if (!(value is IObservableDictionary<string, IItem>))
     147          results = new ObservableDictionary<string, IItem>(value);
     148        else results = (IObservableDictionary<string, IItem>)value;
     149      }
     150    }
     151    private IObservableDictionary<string, IItem> results;
     152    public IObservableDictionary<string, IItem> Results {
     153      get { return results; }
     154      set {
     155        if (results != value) {
     156          results = value;
     157          OnPropertyChanged("Results");
     158        }
     159      }
    126160    }
    127161
     
    133167        if (color != value) {
    134168          this.color = value;
    135           this.OnChanged();
     169          OnPropertyChanged("Color");
    136170        }
    137171      }
     
    143177        if (visible != value) {
    144178          this.visible = value;
    145           this.OnChanged();
     179          OnPropertyChanged("Visible");
    146180        }
    147181      }
    148182    }
    149     public event EventHandler Changed;
    150     private void OnChanged() {
    151       EventHandler handler = Changed;
    152       if (handler != null)
    153         handler(this, EventArgs.Empty);
     183
     184    public event PropertyChangedEventHandler PropertyChanged;
     185    private void OnPropertyChanged(string property) {
     186      var handler = PropertyChanged;
     187      if (handler != null) handler(this, new PropertyChangedEventArgs(property));
    154188    }
    155189  }
Note: See TracChangeset for help on using the changeset viewer.