Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3717 was 3717, checked in by mkommend, 14 years ago

sorting of column names in RunCollection and fixed errors in RunCollectionConstraints (ticket #970)

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