Free cookie consent management tool by TermsFeed Policy Generator

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

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

added new method GetValue in RunCollection (ticket #970)

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