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, 13 years ago

Enabled saving only for some specific items (#1193)

File size: 14.0 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.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) : base(deserializing) { }
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); }
43    private void Initialize() {
44      parameterNames = new List<string>();
45      resultNames = new List<string>();
46      dataTypes = new Dictionary<string, HashSet<Type>>();
47      constraints = new RunCollectionConstraintCollection();
48      RegisterConstraintsEvents();
49    }
50
51    [Storable]
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    }
58
59    [Storable]
60    private RunCollectionConstraintCollection constraints;
61    public RunCollectionConstraintCollection Constraints {
62      get { return constraints; }
63    }
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();
76      columnNameCache = null;
77      OnColumnNamesChanged();
78      rowNamesCache = null;
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();
91      if (columnNamesChanged) {
92        columnNameCache = null;
93        OnColumnNamesChanged();
94      }
95      rowNamesCache = null;
96      OnRowNamesChanged();
97      this.UpdateFiltering(false);
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();
109      if (columnNamesChanged) {
110        columnNameCache = null;
111        OnColumnNamesChanged();
112      }
113      rowNamesCache = null;
114      OnRowNamesChanged();
115    }
116
117    private bool AddParameter(string name, IItem value) {
118      if (value == null)
119        return false;
120      if (!parameterNames.Contains(name)) {
121        parameterNames.Add(name);
122        dataTypes[name] = new HashSet<Type>();
123        dataTypes[name].Add(value.GetType());
124        return true;
125      }
126      dataTypes[name].Add(value.GetType());
127      return false;
128    }
129    private bool AddResult(string name, IItem value) {
130      if (value == null)
131        return false;
132      if (!resultNames.Contains(name)) {
133        resultNames.Add(name);
134        dataTypes[name] = new HashSet<Type>();
135        dataTypes[name].Add(value.GetType());
136        return true;
137      }
138      dataTypes[name].Add(value.GetType());
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
156    public IItem GetValue(int rowIndex, int columnIndex) {
157      IRun run = this.list[rowIndex];
158      return GetValue(run, columnIndex);
159    }
160
161    public IItem GetValue(IRun run, int columnIndex) {
162      string name = ((IStringConvertibleMatrix)this).ColumnNames.ElementAt(columnIndex);
163      return GetValue(run, name);
164    }
165
166    public IItem GetValue(IRun run, string columnName) {
167      IItem value = null;
168      if (run.Parameters.ContainsKey(columnName))
169        value = run.Parameters[columnName];
170      else if (run.Results.ContainsKey(columnName))
171        value = run.Results[columnName];
172      return value;
173    }
174
175    [StorableHook(HookType.AfterDeserialization)]
176    private void AfterDeserializationHook() {
177      if (constraints == null) constraints = new RunCollectionConstraintCollection();
178      RegisterConstraintsEvents();
179      RegisterConstraintEvents(constraints);
180      UpdateFiltering(true);
181    }
182
183    public override IDeepCloneable Clone(Cloner cloner) {
184      RunCollection clone = (RunCollection)base.Clone(cloner);
185      clone.resultNames = new List<string>(this.resultNames);
186      clone.parameterNames = new List<string>(this.parameterNames);
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]);
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);
198      return clone;
199    }
200
201    #region IStringConvertibleMatrix Members
202    [Storable]
203    private List<string> parameterNames;
204    public IEnumerable<string> ParameterNames {
205      get { return this.parameterNames; }
206    }
207    [Storable]
208    private List<string> resultNames;
209    public IEnumerable<string> ResultNames {
210      get { return this.resultNames; }
211    }
212    int IStringConvertibleMatrix.Rows {
213      get { return this.Count; }
214      set { throw new NotSupportedException(); }
215    }
216    int IStringConvertibleMatrix.Columns {
217      get { return parameterNames.Count + resultNames.Count; }
218      set { throw new NotSupportedException(); }
219    }
220    private List<string> columnNameCache;
221    IEnumerable<string> IStringConvertibleMatrix.ColumnNames {
222      get {
223        if (columnNameCache == null) {
224          columnNameCache = new List<string>(parameterNames);
225          columnNameCache.AddRange(resultNames);
226          columnNameCache.Sort();
227        }
228        return columnNameCache;
229      }
230      set { throw new NotSupportedException(); }
231    }
232    private List<string> rowNamesCache;
233    IEnumerable<string> IStringConvertibleMatrix.RowNames {
234      get {
235        if (rowNamesCache == null)
236          rowNamesCache = list.Select(x => x.Name).ToList();
237        return rowNamesCache;
238      }
239      set { throw new NotSupportedException(); }
240    }
241    bool IStringConvertibleMatrix.SortableView {
242      get { return true; }
243      set { throw new NotSupportedException(); }
244    }
245    bool IStringConvertibleMatrix.ReadOnly {
246      get { return true; }
247    }
248
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();
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;
281    protected virtual void OnSortableViewChanged() {
282      EventHandler handler = SortableViewChanged;
283      if (handler != null)
284        handler(this, EventArgs.Empty);
285    }
286
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
290
291    #region filtering
292    private void UpdateFiltering(bool reset) {
293      if (reset)
294        list.ForEach(r => r.Visible = true);
295      foreach (IRunCollectionConstraint constraint in this.constraints)
296        constraint.Check();
297    }
298
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
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);
325      this.UpdateFiltering(true);
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;
331      this.UpdateFiltering(false);
332    }
333    protected virtual void Constraints_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IRunCollectionConstraint> e) {
334      DeregisterConstraintEvents(e.Items);
335      this.UpdateFiltering(true);
336    }
337    protected virtual void Constraint_ActiveChanged(object sender, EventArgs e) {
338      IRunCollectionConstraint constraint = (IRunCollectionConstraint)sender;
339      this.UpdateFiltering(!constraint.Active);
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) {
345      IRunCollectionConstraint constraint = (IRunCollectionConstraint)sender;
346      if (constraint.Active)
347        this.UpdateFiltering(true);
348    }
349    protected virtual void Constraint_ConstraintDataChanged(object sender, EventArgs e) {
350      IRunCollectionConstraint constraint = (IRunCollectionConstraint)sender;
351      if (constraint.Active)
352        this.UpdateFiltering(true);
353    }
354    #endregion
355  }
356}
Note: See TracBrowser for help on using the repository browser.