Free cookie consent management tool by TermsFeed Policy Generator

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

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

Enabled saving only for some specific items (#1193)

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