Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Optimization/3.3/RunCollection.cs @ 5637

Last change on this file since 5637 was 5445, checked in by swagner, 14 years ago

Updated year of copyrights (#1406)

File size: 15.3 KB
RevLine 
[3260]1#region License Information
2/* HeuristicLab
[5445]3 * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[3260]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
[3329]22using System;
[3260]23using System.Collections.Generic;
24using System.Linq;
[4068]25using HeuristicLab.Collections;
[3376]26using HeuristicLab.Common;
[3260]27using HeuristicLab.Core;
[3329]28using HeuristicLab.Data;
[3260]29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Optimization {
[3716]32  [Item("Run Collection", "Represents a collection of runs.")]
33  [Creatable("Testing & Analysis")]
[3260]34  [StorableClass]
[4419]35  public class RunCollection : ItemCollection<IRun>, IStringConvertibleMatrix, IStorableContent {
36    public string Filename { get; set; }
37
[4164]38    [StorableConstructor]
[4888]39    protected RunCollection(bool deserializing)
40      : base(deserializing) {
41      updateOfRunsInProgress = false;
42    }
[4722]43    protected RunCollection(RunCollection original, Cloner cloner)
44      : base(original, cloner) {
[4888]45      updateOfRunsInProgress = false;
[4722]46      resultNames = new List<string>(original.resultNames);
47      parameterNames = new List<string>(original.parameterNames);
48      dataTypes = new Dictionary<string, HashSet<Type>>();
49      foreach (string s in original.dataTypes.Keys)
50        dataTypes[s] = new HashSet<Type>(original.dataTypes[s]);
51
52      constraints = new RunCollectionConstraintCollection(original.constraints.Select(x => cloner.Clone(x)));
53      foreach (IRunCollectionConstraint constraint in constraints)
54        constraint.ConstrainedValue = this;
55      RegisterConstraintsEvents();
56      RegisterConstraintEvents(constraints);
57
58      UpdateFiltering(true);
59    }
[3329]60    public RunCollection() : base() { Initialize(); }
61    public RunCollection(int capacity) : base(capacity) { Initialize(); }
62    public RunCollection(IEnumerable<IRun> collection) : base(collection) { Initialize(); this.OnItemsAdded(collection); }
[3447]63    private void Initialize() {
[4888]64      updateOfRunsInProgress = false;
[3447]65      parameterNames = new List<string>();
66      resultNames = new List<string>();
[3614]67      dataTypes = new Dictionary<string, HashSet<Type>>();
68      constraints = new RunCollectionConstraintCollection();
[4164]69      RegisterConstraintsEvents();
[3447]70    }
[4164]71
[3625]72    [Storable]
[3614]73    private Dictionary<string, HashSet<Type>> dataTypes;
74    public IEnumerable<Type> GetDataType(string columnName) {
75      if (!dataTypes.ContainsKey(columnName))
76        return new Type[0];
77      return dataTypes[columnName];
78    }
[4164]79
80    [Storable]
[3614]81    private RunCollectionConstraintCollection constraints;
82    public RunCollectionConstraintCollection Constraints {
[3625]83      get { return constraints; }
[3614]84    }
[3329]85
[4888]86    private bool updateOfRunsInProgress;
87    public bool UpdateOfRunsInProgress {
88      get { return updateOfRunsInProgress; }
89      set {
90        if (updateOfRunsInProgress != value) {
91          updateOfRunsInProgress = value;
92          OnUpdateOfRunsInProgressChanged();
93        }
94      }
95    }
96
97    [StorableHook(HookType.AfterDeserialization)]
98    private void AfterDeserialization() {
99      if (constraints == null) constraints = new RunCollectionConstraintCollection();
100      RegisterConstraintsEvents();
101      RegisterConstraintEvents(constraints);
102      UpdateFiltering(true);
103    }
104
105    public override IDeepCloneable Clone(Cloner cloner) {
106      return new RunCollection(this, cloner);
107    }
108
109    public event EventHandler UpdateOfRunsInProgressChanged;
110    protected virtual void OnUpdateOfRunsInProgressChanged() {
111      var handler = UpdateOfRunsInProgressChanged;
112      if (handler != null) handler(this, EventArgs.Empty);
113    }
114
[3329]115    protected override void OnCollectionReset(IEnumerable<IRun> items, IEnumerable<IRun> oldItems) {
116      parameterNames.Clear();
117      resultNames.Clear();
118      foreach (IRun run in items) {
119        foreach (KeyValuePair<string, IItem> parameter in run.Parameters)
120          AddParameter(parameter.Key, parameter.Value);
121        foreach (KeyValuePair<string, IItem> result in run.Results)
122          AddResult(result.Key, result.Value);
123      }
[4200]124      columnNameCache = null;
[5150]125      OnColumnsChanged();
[3329]126      OnColumnNamesChanged();
[4200]127      rowNamesCache = null;
[4518]128      base.OnCollectionReset(items, oldItems);
[5150]129      OnRowsChanged();
[3329]130      OnRowNamesChanged();
[4518]131      OnReset();
132      UpdateFiltering(false);
[3329]133    }
134    protected override void OnItemsAdded(IEnumerable<IRun> items) {
[5150]135      bool columnsChanged = false;
[3329]136      foreach (IRun run in items) {
137        foreach (KeyValuePair<string, IItem> parameter in run.Parameters)
[5150]138          columnsChanged |= AddParameter(parameter.Key, parameter.Value);
[3329]139        foreach (KeyValuePair<string, IItem> result in run.Results)
[5150]140          columnsChanged |= AddResult(result.Key, result.Value);
[3329]141      }
[5150]142      if (columnsChanged) columnNameCache = null;
[4200]143      rowNamesCache = null;
[4518]144      base.OnItemsAdded(items);
[4707]145      OnReset();
[5150]146      OnRowsChanged();
[3329]147      OnRowNamesChanged();
[5150]148      if (columnsChanged) {
149        OnColumnsChanged();
150        OnColumnNamesChanged();
151      }
[4518]152      UpdateFiltering(false);
[3329]153    }
154    protected override void OnItemsRemoved(IEnumerable<IRun> items) {
[5150]155      bool columnsChanged = false;
[3329]156      foreach (IRun run in items) {
157        foreach (string parameterName in run.Parameters.Keys)
[5150]158          columnsChanged |= RemoveParameterName(parameterName);
[3329]159        foreach (string resultName in run.Results.Keys)
[5150]160          columnsChanged |= RemoveResultName(resultName);
[3329]161      }
[5150]162      if (columnsChanged) columnNameCache = null;
[4200]163      rowNamesCache = null;
[4518]164      base.OnItemsRemoved(items);
[4707]165      OnReset();
[5152]166      OnRowsChanged();
[3329]167      OnRowNamesChanged();
[5150]168      if (columnsChanged) {
169        OnColumnsChanged();
170        OnColumnNamesChanged();
171      }
[3329]172    }
173
174    private bool AddParameter(string name, IItem value) {
175      if (value == null)
176        return false;
[3441]177      if (!parameterNames.Contains(name)) {
[3329]178        parameterNames.Add(name);
[3614]179        dataTypes[name] = new HashSet<Type>();
180        dataTypes[name].Add(value.GetType());
[3329]181        return true;
182      }
[3614]183      dataTypes[name].Add(value.GetType());
[3329]184      return false;
185    }
186    private bool AddResult(string name, IItem value) {
187      if (value == null)
188        return false;
[3441]189      if (!resultNames.Contains(name)) {
[3329]190        resultNames.Add(name);
[3614]191        dataTypes[name] = new HashSet<Type>();
192        dataTypes[name].Add(value.GetType());
[3329]193        return true;
194      }
[3614]195      dataTypes[name].Add(value.GetType());
[3329]196      return false;
197    }
198    private bool RemoveParameterName(string name) {
199      if (!list.Any(x => x.Parameters.ContainsKey(name))) {
200        parameterNames.Remove(name);
201        return true;
202      }
203      return false;
204    }
205    private bool RemoveResultName(string name) {
206      if (!list.Any(x => x.Results.ContainsKey(name))) {
207        resultNames.Remove(name);
208        return true;
209      }
210      return false;
211    }
212
[3447]213    public IItem GetValue(int rowIndex, int columnIndex) {
214      IRun run = this.list[rowIndex];
[3492]215      return GetValue(run, columnIndex);
216    }
217
218    public IItem GetValue(IRun run, int columnIndex) {
[3717]219      string name = ((IStringConvertibleMatrix)this).ColumnNames.ElementAt(columnIndex);
220      return GetValue(run, name);
221    }
222
223    public IItem GetValue(IRun run, string columnName) {
[3447]224      IItem value = null;
[3767]225      if (run.Parameters.ContainsKey(columnName))
226        value = run.Parameters[columnName];
227      else if (run.Results.ContainsKey(columnName))
228        value = run.Results[columnName];
[3447]229      return value;
230    }
231
[3329]232    #region IStringConvertibleMatrix Members
[3347]233    [Storable]
[3329]234    private List<string> parameterNames;
[3492]235    public IEnumerable<string> ParameterNames {
236      get { return this.parameterNames; }
237    }
[3347]238    [Storable]
[3329]239    private List<string> resultNames;
[3492]240    public IEnumerable<string> ResultNames {
241      get { return this.resultNames; }
242    }
[3447]243    int IStringConvertibleMatrix.Rows {
[3329]244      get { return this.Count; }
[3447]245      set { throw new NotSupportedException(); }
[3329]246    }
[3447]247    int IStringConvertibleMatrix.Columns {
[3329]248      get { return parameterNames.Count + resultNames.Count; }
249      set { throw new NotSupportedException(); }
250    }
[4200]251    private List<string> columnNameCache;
[3447]252    IEnumerable<string> IStringConvertibleMatrix.ColumnNames {
[3329]253      get {
[4200]254        if (columnNameCache == null) {
255          columnNameCache = new List<string>(parameterNames);
256          columnNameCache.AddRange(resultNames);
257          columnNameCache.Sort();
258        }
259        return columnNameCache;
[3329]260      }
261      set { throw new NotSupportedException(); }
262    }
[4200]263    private List<string> rowNamesCache;
[3447]264    IEnumerable<string> IStringConvertibleMatrix.RowNames {
[4200]265      get {
266        if (rowNamesCache == null)
267          rowNamesCache = list.Select(x => x.Name).ToList();
268        return rowNamesCache;
269      }
[3329]270      set { throw new NotSupportedException(); }
271    }
[3447]272    bool IStringConvertibleMatrix.SortableView {
[3329]273      get { return true; }
274      set { throw new NotSupportedException(); }
275    }
[3447]276    bool IStringConvertibleMatrix.ReadOnly {
277      get { return true; }
[3430]278    }
[3329]279
[3447]280    string IStringConvertibleMatrix.GetValue(int rowIndex, int columnIndex) {
281      IItem value = GetValue(rowIndex, columnIndex);
282      if (value == null)
283        return string.Empty;
284      return value.ToString();
[3329]285    }
286
287    public event EventHandler<EventArgs<int, int>> ItemChanged;
288    protected virtual void OnItemChanged(int rowIndex, int columnIndex) {
[4722]289      EventHandler<EventArgs<int, int>> handler = ItemChanged;
290      if (handler != null) handler(this, new EventArgs<int, int>(rowIndex, columnIndex));
[3329]291      OnToStringChanged();
292    }
293    public event EventHandler Reset;
294    protected virtual void OnReset() {
[4722]295      EventHandler handler = Reset;
296      if (handler != null) handler(this, EventArgs.Empty);
[3329]297      OnToStringChanged();
298    }
[5150]299    public event EventHandler ColumnsChanged;
300    protected virtual void OnColumnsChanged() {
301      var handler = ColumnsChanged;
302      if (handler != null) handler(this, EventArgs.Empty);
303    }
304    public event EventHandler RowsChanged;
305    protected virtual void OnRowsChanged() {
306      var handler = RowsChanged;
307      if (handler != null) handler(this, EventArgs.Empty);
308    }
[3329]309    public event EventHandler ColumnNamesChanged;
310    protected virtual void OnColumnNamesChanged() {
311      EventHandler handler = ColumnNamesChanged;
[4722]312      if (handler != null) handler(this, EventArgs.Empty);
[3329]313    }
314    public event EventHandler RowNamesChanged;
315    protected virtual void OnRowNamesChanged() {
316      EventHandler handler = RowNamesChanged;
[4722]317      if (handler != null) handler(this, EventArgs.Empty);
[3329]318    }
319    public event EventHandler SortableViewChanged;
[3333]320    protected virtual void OnSortableViewChanged() {
321      EventHandler handler = SortableViewChanged;
[4722]322      if (handler != null) handler(this, EventArgs.Empty);
[3333]323    }
324
[3329]325    public bool Validate(string value, out string errorMessage) { throw new NotSupportedException(); }
326    public bool SetValue(string value, int rowIndex, int columnIndex) { throw new NotSupportedException(); }
327    #endregion
[3614]328
[4888]329    #region Filtering
[3632]330    private void UpdateFiltering(bool reset) {
[4888]331      UpdateOfRunsInProgress = true;
[3632]332      if (reset)
333        list.ForEach(r => r.Visible = true);
[3614]334      foreach (IRunCollectionConstraint constraint in this.constraints)
335        constraint.Check();
[4888]336      UpdateOfRunsInProgress = false;
[3614]337    }
338
[4164]339    private void RegisterConstraintsEvents() {
340      constraints.ItemsAdded += new CollectionItemsChangedEventHandler<IRunCollectionConstraint>(Constraints_ItemsAdded);
341      constraints.ItemsRemoved += new CollectionItemsChangedEventHandler<IRunCollectionConstraint>(Constraints_ItemsRemoved);
342      constraints.CollectionReset += new CollectionItemsChangedEventHandler<IRunCollectionConstraint>(Constraints_CollectionReset);
343    }
344
[3614]345    protected virtual void RegisterConstraintEvents(IEnumerable<IRunCollectionConstraint> constraints) {
346      foreach (IRunCollectionConstraint constraint in constraints) {
347        constraint.ActiveChanged += new EventHandler(Constraint_ActiveChanged);
348        constraint.ConstrainedValueChanged += new EventHandler(Constraint_ConstrainedValueChanged);
349        constraint.ConstraintOperationChanged += new EventHandler(Constraint_ConstraintOperationChanged);
350        constraint.ConstraintDataChanged += new EventHandler(Constraint_ConstraintDataChanged);
351      }
352    }
353    protected virtual void DeregisterConstraintEvents(IEnumerable<IRunCollectionConstraint> constraints) {
354      foreach (IRunCollectionConstraint constraint in constraints) {
355        constraint.ActiveChanged -= new EventHandler(Constraint_ActiveChanged);
356        constraint.ConstrainedValueChanged -= new EventHandler(Constraint_ConstrainedValueChanged);
357        constraint.ConstraintOperationChanged -= new EventHandler(Constraint_ConstraintOperationChanged);
358        constraint.ConstraintDataChanged -= new EventHandler(Constraint_ConstraintDataChanged);
359      }
360    }
361
362    protected virtual void Constraints_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRunCollectionConstraint> e) {
363      DeregisterConstraintEvents(e.OldItems);
364      RegisterConstraintEvents(e.Items);
[3632]365      this.UpdateFiltering(true);
[3614]366    }
367    protected virtual void Constraints_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IRunCollectionConstraint> e) {
368      RegisterConstraintEvents(e.Items);
369      foreach (IRunCollectionConstraint constraint in e.Items)
370        constraint.ConstrainedValue = this;
[3632]371      this.UpdateFiltering(false);
[3614]372    }
373    protected virtual void Constraints_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IRunCollectionConstraint> e) {
374      DeregisterConstraintEvents(e.Items);
[3632]375      this.UpdateFiltering(true);
[3614]376    }
377    protected virtual void Constraint_ActiveChanged(object sender, EventArgs e) {
[3632]378      IRunCollectionConstraint constraint = (IRunCollectionConstraint)sender;
379      this.UpdateFiltering(!constraint.Active);
[3614]380    }
381    protected virtual void Constraint_ConstrainedValueChanged(object sender, EventArgs e) {
382      //mkommend: this method is intentionally left empty, because the constrainedValue is set in the ItemsAdded method
383    }
384    protected virtual void Constraint_ConstraintOperationChanged(object sender, EventArgs e) {
[3632]385      IRunCollectionConstraint constraint = (IRunCollectionConstraint)sender;
386      if (constraint.Active)
387        this.UpdateFiltering(true);
[3614]388    }
389    protected virtual void Constraint_ConstraintDataChanged(object sender, EventArgs e) {
[3632]390      IRunCollectionConstraint constraint = (IRunCollectionConstraint)sender;
391      if (constraint.Active)
392        this.UpdateFiltering(true);
[3614]393    }
394    #endregion
[3260]395  }
396}
Note: See TracBrowser for help on using the repository browser.