Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3439 was 3431, checked in by swagner, 15 years ago

Removed property ReadOnlyView (#969)

File size: 7.7 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;
29
30namespace HeuristicLab.Optimization {
31  [StorableClass]
32  [Item("RunCollection", "Represents a collection of runs.")]
33  public class RunCollection : ItemCollection<IRun>, IStringConvertibleMatrix {
34    public RunCollection() : base() { Initialize(); }
35    public RunCollection(int capacity) : base(capacity) { Initialize(); }
36    public RunCollection(IEnumerable<IRun> collection) : base(collection) { Initialize(); this.OnItemsAdded(collection); }
37
38    protected static Type[] viewableDataTypes = new Type[]{typeof(BoolValue), typeof(DoubleValue), typeof(IntValue),
39      typeof(PercentValue), typeof(StringValue)};
40
41    protected override void OnCollectionReset(IEnumerable<IRun> items, IEnumerable<IRun> oldItems) {
42      parameterNames.Clear();
43      resultNames.Clear();
44      foreach (IRun run in items) {
45        foreach (KeyValuePair<string, IItem> parameter in run.Parameters)
46          AddParameter(parameter.Key, parameter.Value);
47        foreach (KeyValuePair<string, IItem> result in run.Results)
48          AddResult(result.Key, result.Value);
49      }
50      base.OnCollectionReset(items, oldItems);
51      OnReset();
52      OnColumnNamesChanged();
53      OnRowNamesChanged();
54    }
55    protected override void OnItemsAdded(IEnumerable<IRun> items) {
56      bool columnNamesChanged = false;
57      foreach (IRun run in items) {
58        foreach (KeyValuePair<string, IItem> parameter in run.Parameters)
59          columnNamesChanged |= AddParameter(parameter.Key, parameter.Value);
60        foreach (KeyValuePair<string, IItem> result in run.Results)
61          columnNamesChanged |= AddResult(result.Key, result.Value);
62      }
63      base.OnItemsAdded(items);
64      OnReset();
65      if (columnNamesChanged)
66        OnColumnNamesChanged();
67      OnRowNamesChanged();
68    }
69    protected override void OnItemsRemoved(IEnumerable<IRun> items) {
70      bool columnNamesChanged = false;
71      foreach (IRun run in items) {
72        foreach (string parameterName in run.Parameters.Keys)
73          columnNamesChanged |= RemoveParameterName(parameterName);
74        foreach (string resultName in run.Results.Keys)
75          columnNamesChanged |= RemoveResultName(resultName);
76      }
77      base.OnItemsRemoved(items);
78      OnReset();
79      if (columnNamesChanged)
80        OnColumnNamesChanged();
81      OnRowNamesChanged();
82    }
83
84    private void Initialize() {
85      parameterNames = new List<string>();
86      resultNames = new List<string>();
87    }
88
89    private bool AddParameter(string name, IItem value) {
90      if (value == null)
91        return false;
92      if (!parameterNames.Contains(name) &&
93          viewableDataTypes.Any(x => x.IsAssignableFrom(value.GetType()))) {
94        parameterNames.Add(name);
95        return true;
96      }
97      return false;
98    }
99    private bool AddResult(string name, IItem value) {
100      if (value == null)
101        return false;
102      if (!resultNames.Contains(name) &&
103          viewableDataTypes.Any(x => x.IsAssignableFrom(value.GetType()))) {
104        resultNames.Add(name);
105        return true;
106      }
107      return false;
108    }
109    private bool RemoveParameterName(string name) {
110      if (!list.Any(x => x.Parameters.ContainsKey(name))) {
111        parameterNames.Remove(name);
112        return true;
113      }
114      return false;
115    }
116    private bool RemoveResultName(string name) {
117      if (!list.Any(x => x.Results.ContainsKey(name))) {
118        resultNames.Remove(name);
119        return true;
120      }
121      return false;
122    }
123
124    #region IStringConvertibleMatrix Members
125    [Storable]
126    private List<string> parameterNames;
127    [Storable]
128    private List<string> resultNames;
129    public int Rows {
130      get { return this.Count; }
131      set { throw new System.NotImplementedException(); }
132    }
133    public int Columns {
134      get { return parameterNames.Count + resultNames.Count; }
135      set { throw new NotSupportedException(); }
136    }
137    public IEnumerable<string> ColumnNames {
138      get {
139        List<string> value = new List<string>(parameterNames);
140        value.AddRange(resultNames);
141        return value;
142      }
143      set { throw new NotSupportedException(); }
144    }
145    public IEnumerable<string> RowNames {
146      get { return list.Select(x => x.Name).ToList(); }
147      set { throw new NotSupportedException(); }
148    }
149    public bool SortableView {
150      get { return true; }
151      set { throw new NotSupportedException(); }
152    }
153    public bool ReadOnly {
154      get { return false; }
155    }
156
157    public string GetValue(int rowIndex, int columnIndex) {
158      IRun run = this.list[rowIndex];
159      string value = string.Empty;
160
161      if (columnIndex < parameterNames.Count) {
162        string parameterName = parameterNames[columnIndex];
163        if (run.Parameters.ContainsKey(parameterName)) {
164          IItem param = run.Parameters[parameterName];
165          if (param != null) value = param.ToString();
166        }
167      } else if (columnIndex < parameterNames.Count + resultNames.Count) {
168        string resultName = resultNames[columnIndex - parameterNames.Count];
169        if (run.Results.ContainsKey(resultName)) {
170          IItem result = run.Results[resultName];
171          if (result != null) value = result.ToString();
172        }
173      }
174      return value;
175    }
176
177    public event EventHandler<EventArgs<int, int>> ItemChanged;
178    protected virtual void OnItemChanged(int rowIndex, int columnIndex) {
179      if (ItemChanged != null)
180        ItemChanged(this, new EventArgs<int, int>(rowIndex, columnIndex));
181      OnToStringChanged();
182    }
183    public event EventHandler Reset;
184    protected virtual void OnReset() {
185      if (Reset != null)
186        Reset(this, EventArgs.Empty);
187      OnToStringChanged();
188    }
189    public event EventHandler ColumnNamesChanged;
190    protected virtual void OnColumnNamesChanged() {
191      EventHandler handler = ColumnNamesChanged;
192      if (handler != null)
193        handler(this, EventArgs.Empty);
194    }
195    public event EventHandler RowNamesChanged;
196    protected virtual void OnRowNamesChanged() {
197      EventHandler handler = RowNamesChanged;
198      if (handler != null)
199        handler(this, EventArgs.Empty);
200    }
201
202    public event EventHandler SortableViewChanged;
203    protected virtual void OnSortableViewChanged() {
204      EventHandler handler = SortableViewChanged;
205      if (handler != null)
206        handler(this, EventArgs.Empty);
207    }
208
209    public bool Validate(string value, out string errorMessage) { throw new NotSupportedException(); }
210    public bool SetValue(string value, int rowIndex, int columnIndex) { throw new NotSupportedException(); }
211    #endregion
212  }
213}
Note: See TracBrowser for help on using the repository browser.