Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Scheduling/HeuristicLab.Optimization/3.3/RunCollection.cs @ 6824

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

Updated year of copyrights (#1406)

File size: 15.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Collections;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Optimization {
32  [Item("Run Collection", "Represents a collection of runs.")]
33  [Creatable("Testing & Analysis")]
34  [StorableClass]
35  public class RunCollection : ItemCollection<IRun>, IStringConvertibleMatrix, IStorableContent {
36    public string Filename { get; set; }
37
38    [StorableConstructor]
39    protected RunCollection(bool deserializing)
40      : base(deserializing) {
41      updateOfRunsInProgress = false;
42    }
43    protected RunCollection(RunCollection original, Cloner cloner)
44      : base(original, cloner) {
45      updateOfRunsInProgress = false;
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    }
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); }
63    private void Initialize() {
64      updateOfRunsInProgress = false;
65      parameterNames = new List<string>();
66      resultNames = new List<string>();
67      dataTypes = new Dictionary<string, HashSet<Type>>();
68      constraints = new RunCollectionConstraintCollection();
69      RegisterConstraintsEvents();
70    }
71
72    [Storable]
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    }
79
80    [Storable]
81    private RunCollectionConstraintCollection constraints;
82    public RunCollectionConstraintCollection Constraints {
83      get { return constraints; }
84    }
85
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
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      }
124      columnNameCache = null;
125      OnColumnsChanged();
126      OnColumnNamesChanged();
127      rowNamesCache = null;
128      base.OnCollectionReset(items, oldItems);
129      OnRowsChanged();
130      OnRowNamesChanged();
131      OnReset();
132      UpdateFiltering(false);
133    }
134    protected override void OnItemsAdded(IEnumerable<IRun> items) {
135      bool columnsChanged = false;
136      foreach (IRun run in items) {
137        foreach (KeyValuePair<string, IItem> parameter in run.Parameters)
138          columnsChanged |= AddParameter(parameter.Key, parameter.Value);
139        foreach (KeyValuePair<string, IItem> result in run.Results)
140          columnsChanged |= AddResult(result.Key, result.Value);
141      }
142      if (columnsChanged) columnNameCache = null;
143      rowNamesCache = null;
144      base.OnItemsAdded(items);
145      OnReset();
146      OnRowsChanged();
147      OnRowNamesChanged();
148      if (columnsChanged) {
149        OnColumnsChanged();
150        OnColumnNamesChanged();
151      }
152      UpdateFiltering(false);
153    }
154    protected override void OnItemsRemoved(IEnumerable<IRun> items) {
155      bool columnsChanged = false;
156      foreach (IRun run in items) {
157        foreach (string parameterName in run.Parameters.Keys)
158          columnsChanged |= RemoveParameterName(parameterName);
159        foreach (string resultName in run.Results.Keys)
160          columnsChanged |= RemoveResultName(resultName);
161      }
162      if (columnsChanged) columnNameCache = null;
163      rowNamesCache = null;
164      base.OnItemsRemoved(items);
165      OnReset();
166      OnRowsChanged();
167      OnRowNamesChanged();
168      if (columnsChanged) {
169        OnColumnsChanged();
170        OnColumnNamesChanged();
171      }
172    }
173
174    private bool AddParameter(string name, IItem value) {
175      if (value == null)
176        return false;
177      if (!parameterNames.Contains(name)) {
178        parameterNames.Add(name);
179        dataTypes[name] = new HashSet<Type>();
180        dataTypes[name].Add(value.GetType());
181        return true;
182      }
183      dataTypes[name].Add(value.GetType());
184      return false;
185    }
186    private bool AddResult(string name, IItem value) {
187      if (value == null)
188        return false;
189      if (!resultNames.Contains(name)) {
190        resultNames.Add(name);
191        dataTypes[name] = new HashSet<Type>();
192        dataTypes[name].Add(value.GetType());
193        return true;
194      }
195      dataTypes[name].Add(value.GetType());
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
213    public IItem GetValue(int rowIndex, int columnIndex) {
214      IRun run = this.list[rowIndex];
215      return GetValue(run, columnIndex);
216    }
217
218    public IItem GetValue(IRun run, int columnIndex) {
219      string name = ((IStringConvertibleMatrix)this).ColumnNames.ElementAt(columnIndex);
220      return GetValue(run, name);
221    }
222
223    public IItem GetValue(IRun run, string columnName) {
224      IItem value = null;
225      if (run.Parameters.ContainsKey(columnName))
226        value = run.Parameters[columnName];
227      else if (run.Results.ContainsKey(columnName))
228        value = run.Results[columnName];
229      return value;
230    }
231
232    #region IStringConvertibleMatrix Members
233    [Storable]
234    private List<string> parameterNames;
235    public IEnumerable<string> ParameterNames {
236      get { return this.parameterNames; }
237    }
238    [Storable]
239    private List<string> resultNames;
240    public IEnumerable<string> ResultNames {
241      get { return this.resultNames; }
242    }
243    int IStringConvertibleMatrix.Rows {
244      get { return this.Count; }
245      set { throw new NotSupportedException(); }
246    }
247    int IStringConvertibleMatrix.Columns {
248      get { return parameterNames.Count + resultNames.Count; }
249      set { throw new NotSupportedException(); }
250    }
251    private List<string> columnNameCache;
252    IEnumerable<string> IStringConvertibleMatrix.ColumnNames {
253      get {
254        if (columnNameCache == null) {
255          columnNameCache = new List<string>(parameterNames);
256          columnNameCache.AddRange(resultNames);
257          columnNameCache.Sort();
258        }
259        return columnNameCache;
260      }
261      set { throw new NotSupportedException(); }
262    }
263    private List<string> rowNamesCache;
264    IEnumerable<string> IStringConvertibleMatrix.RowNames {
265      get {
266        if (rowNamesCache == null)
267          rowNamesCache = list.Select(x => x.Name).ToList();
268        return rowNamesCache;
269      }
270      set { throw new NotSupportedException(); }
271    }
272    bool IStringConvertibleMatrix.SortableView {
273      get { return true; }
274      set { throw new NotSupportedException(); }
275    }
276    bool IStringConvertibleMatrix.ReadOnly {
277      get { return true; }
278    }
279
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();
285    }
286
287    public event EventHandler<EventArgs<int, int>> ItemChanged;
288    protected virtual void OnItemChanged(int rowIndex, int columnIndex) {
289      EventHandler<EventArgs<int, int>> handler = ItemChanged;
290      if (handler != null) handler(this, new EventArgs<int, int>(rowIndex, columnIndex));
291      OnToStringChanged();
292    }
293    public event EventHandler Reset;
294    protected virtual void OnReset() {
295      EventHandler handler = Reset;
296      if (handler != null) handler(this, EventArgs.Empty);
297      OnToStringChanged();
298    }
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    }
309    public event EventHandler ColumnNamesChanged;
310    protected virtual void OnColumnNamesChanged() {
311      EventHandler handler = ColumnNamesChanged;
312      if (handler != null) handler(this, EventArgs.Empty);
313    }
314    public event EventHandler RowNamesChanged;
315    protected virtual void OnRowNamesChanged() {
316      EventHandler handler = RowNamesChanged;
317      if (handler != null) handler(this, EventArgs.Empty);
318    }
319    public event EventHandler SortableViewChanged;
320    protected virtual void OnSortableViewChanged() {
321      EventHandler handler = SortableViewChanged;
322      if (handler != null) handler(this, EventArgs.Empty);
323    }
324
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
328
329    #region Filtering
330    private void UpdateFiltering(bool reset) {
331      UpdateOfRunsInProgress = true;
332      if (reset)
333        list.ForEach(r => r.Visible = true);
334      foreach (IRunCollectionConstraint constraint in this.constraints)
335        constraint.Check();
336      UpdateOfRunsInProgress = false;
337    }
338
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
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);
365      this.UpdateFiltering(true);
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;
371      this.UpdateFiltering(false);
372    }
373    protected virtual void Constraints_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IRunCollectionConstraint> e) {
374      DeregisterConstraintEvents(e.Items);
375      this.UpdateFiltering(true);
376    }
377    protected virtual void Constraint_ActiveChanged(object sender, EventArgs e) {
378      IRunCollectionConstraint constraint = (IRunCollectionConstraint)sender;
379      this.UpdateFiltering(!constraint.Active);
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) {
385      IRunCollectionConstraint constraint = (IRunCollectionConstraint)sender;
386      if (constraint.Active)
387        this.UpdateFiltering(true);
388    }
389    protected virtual void Constraint_ConstraintDataChanged(object sender, EventArgs e) {
390      IRunCollectionConstraint constraint = (IRunCollectionConstraint)sender;
391      if (constraint.Active)
392        this.UpdateFiltering(true);
393    }
394    #endregion
395  }
396}
Note: See TracBrowser for help on using the repository browser.