Free cookie consent management tool by TermsFeed Policy Generator

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

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

Moved interfaces and classes for deep cloning from HeuristicLab.Core to HeuristicLab.Common (#975).

File size: 7.6 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      this.ReadOnlyView = true;
88    }
89
90    private bool AddParameter(string name, IItem value) {
91      if (value == null)
92        return false;
93      if (!parameterNames.Contains(name) &&
94          viewableDataTypes.Any(x => x.IsAssignableFrom(value.GetType()))) {
95        parameterNames.Add(name);
96        return true;
97      }
98      return false;
99    }
100    private bool AddResult(string name, IItem value) {
101      if (value == null)
102        return false;
103      if (!resultNames.Contains(name) &&
104          viewableDataTypes.Any(x => x.IsAssignableFrom(value.GetType()))) {
105        resultNames.Add(name);
106        return true;
107      }
108      return false;
109    }
110    private bool RemoveParameterName(string name) {
111      if (!list.Any(x => x.Parameters.ContainsKey(name))) {
112        parameterNames.Remove(name);
113        return true;
114      }
115      return false;
116    }
117    private bool RemoveResultName(string name) {
118      if (!list.Any(x => x.Results.ContainsKey(name))) {
119        resultNames.Remove(name);
120        return true;
121      }
122      return false;
123    }
124
125    #region IStringConvertibleMatrix Members
126    [Storable]
127    private List<string> parameterNames;
128    [Storable]
129    private List<string> resultNames;
130    public int Rows {
131      get { return this.Count; }
132      set { throw new System.NotImplementedException(); }
133    }
134    public int Columns {
135      get { return parameterNames.Count + resultNames.Count; }
136      set { throw new NotSupportedException(); }
137    }
138    public IEnumerable<string> ColumnNames {
139      get {
140        List<string> value = new List<string>(parameterNames);
141        value.AddRange(resultNames);
142        return value;
143      }
144      set { throw new NotSupportedException(); }
145    }
146    public IEnumerable<string> RowNames {
147      get { return list.Select(x => x.Name).ToList(); }
148      set { throw new NotSupportedException(); }
149    }
150    public bool SortableView {
151      get { return true; }
152      set { throw new NotSupportedException(); }
153    }
154
155    public string GetValue(int rowIndex, int columnIndex) {
156      IRun run = this.list[rowIndex];
157      string value = string.Empty;
158
159      if (columnIndex < parameterNames.Count) {
160        string parameterName = parameterNames[columnIndex];
161        if (run.Parameters.ContainsKey(parameterName)) {
162          IItem param = run.Parameters[parameterName];
163          if (param != null) value = param.ToString();
164        }
165      } else if (columnIndex < parameterNames.Count + resultNames.Count) {
166        string resultName = resultNames[columnIndex - parameterNames.Count];
167        if (run.Results.ContainsKey(resultName)) {
168          IItem result = run.Results[resultName];
169          if (result != null) value = result.ToString();
170        }
171      }
172      return value;
173    }
174
175    public event EventHandler<EventArgs<int, int>> ItemChanged;
176    protected virtual void OnItemChanged(int rowIndex, int columnIndex) {
177      if (ItemChanged != null)
178        ItemChanged(this, new EventArgs<int, int>(rowIndex, columnIndex));
179      OnToStringChanged();
180    }
181    public event EventHandler Reset;
182    protected virtual void OnReset() {
183      if (Reset != null)
184        Reset(this, EventArgs.Empty);
185      OnToStringChanged();
186    }
187    public event EventHandler ColumnNamesChanged;
188    protected virtual void OnColumnNamesChanged() {
189      EventHandler handler = ColumnNamesChanged;
190      if (handler != null)
191        handler(this, EventArgs.Empty);
192    }
193    public event EventHandler RowNamesChanged;
194    protected virtual void OnRowNamesChanged() {
195      EventHandler handler = RowNamesChanged;
196      if (handler != null)
197        handler(this, EventArgs.Empty);
198    }
199
200    public event EventHandler SortableViewChanged;
201    protected virtual void OnSortableViewChanged() {
202      EventHandler handler = SortableViewChanged;
203      if (handler != null)
204        handler(this, EventArgs.Empty);
205    }
206
207    public bool Validate(string value, out string errorMessage) { throw new NotSupportedException(); }
208    public bool SetValue(string value, int rowIndex, int columnIndex) { throw new NotSupportedException(); }
209    #endregion
210  }
211}
Note: See TracBrowser for help on using the repository browser.