[3260] | 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 |
|
---|
[3329] | 22 | using System;
|
---|
[3260] | 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
[3329] | 26 | using HeuristicLab.Data;
|
---|
[3260] | 27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[3329] | 28 | using HeuristicLab.Common;
|
---|
[3260] | 29 |
|
---|
| 30 | namespace HeuristicLab.Optimization {
|
---|
| 31 | [StorableClass]
|
---|
| 32 | [Item("RunCollection", "Represents a collection of runs.")]
|
---|
[3329] | 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 | private List<string> parameterNames;
|
---|
| 127 | private List<string> resultNames;
|
---|
| 128 | public int Rows {
|
---|
| 129 | get { return this.Count; }
|
---|
| 130 | set { throw new System.NotImplementedException(); }
|
---|
| 131 | }
|
---|
| 132 | public int Columns {
|
---|
| 133 | get { return parameterNames.Count + resultNames.Count; }
|
---|
| 134 | set { throw new NotSupportedException(); }
|
---|
| 135 | }
|
---|
| 136 | public IEnumerable<string> ColumnNames {
|
---|
| 137 | get {
|
---|
| 138 | List<string> value = new List<string>(parameterNames);
|
---|
| 139 | value.AddRange(resultNames);
|
---|
| 140 | return value;
|
---|
| 141 | }
|
---|
| 142 | set { throw new NotSupportedException(); }
|
---|
| 143 | }
|
---|
| 144 | public IEnumerable<string> RowNames {
|
---|
| 145 | get { return list.Select(x => x.Name).ToList(); }
|
---|
| 146 | set { throw new NotSupportedException(); }
|
---|
| 147 | }
|
---|
| 148 | public bool SortableView {
|
---|
| 149 | get { return true; }
|
---|
| 150 | set { throw new NotSupportedException(); }
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | public string GetValue(int rowIndex, int columnIndex) {
|
---|
| 154 | IRun run = this.list[rowIndex];
|
---|
| 155 | string value = string.Empty;
|
---|
| 156 |
|
---|
| 157 | if (columnIndex < parameterNames.Count) {
|
---|
| 158 | string parameterName = parameterNames[columnIndex];
|
---|
| 159 | if (run.Parameters.ContainsKey(parameterName)) {
|
---|
| 160 | IItem param = run.Parameters[parameterName];
|
---|
| 161 | if (param != null) value = param.ToString();
|
---|
| 162 | }
|
---|
| 163 | } else if (columnIndex < parameterNames.Count + resultNames.Count) {
|
---|
| 164 | string resultName = resultNames[columnIndex - parameterNames.Count];
|
---|
| 165 | if (run.Results.ContainsKey(resultName)) {
|
---|
| 166 | IItem result = run.Results[resultName];
|
---|
| 167 | if (result != null) value = result.ToString();
|
---|
| 168 | }
|
---|
| 169 | }
|
---|
| 170 | return value;
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | public event EventHandler<EventArgs<int, int>> ItemChanged;
|
---|
| 174 | protected virtual void OnItemChanged(int rowIndex, int columnIndex) {
|
---|
| 175 | if (ItemChanged != null)
|
---|
| 176 | ItemChanged(this, new EventArgs<int, int>(rowIndex, columnIndex));
|
---|
| 177 | OnToStringChanged();
|
---|
| 178 | }
|
---|
| 179 | public event EventHandler Reset;
|
---|
| 180 | protected virtual void OnReset() {
|
---|
| 181 | if (Reset != null)
|
---|
| 182 | Reset(this, EventArgs.Empty);
|
---|
| 183 | OnToStringChanged();
|
---|
| 184 | }
|
---|
| 185 | public event EventHandler ColumnNamesChanged;
|
---|
| 186 | protected virtual void OnColumnNamesChanged() {
|
---|
| 187 | EventHandler handler = ColumnNamesChanged;
|
---|
| 188 | if (handler != null)
|
---|
| 189 | handler(this, EventArgs.Empty);
|
---|
| 190 | }
|
---|
| 191 | public event EventHandler RowNamesChanged;
|
---|
| 192 | protected virtual void OnRowNamesChanged() {
|
---|
| 193 | EventHandler handler = RowNamesChanged;
|
---|
| 194 | if (handler != null)
|
---|
| 195 | handler(this, EventArgs.Empty);
|
---|
| 196 | }
|
---|
| 197 |
|
---|
| 198 | public event EventHandler SortableViewChanged;
|
---|
[3333] | 199 | protected virtual void OnSortableViewChanged() {
|
---|
| 200 | EventHandler handler = SortableViewChanged;
|
---|
| 201 | if (handler != null)
|
---|
| 202 | handler(this, EventArgs.Empty);
|
---|
| 203 | }
|
---|
| 204 |
|
---|
[3329] | 205 | public bool Validate(string value, out string errorMessage) { throw new NotSupportedException(); }
|
---|
| 206 | public bool SetValue(string value, int rowIndex, int columnIndex) { throw new NotSupportedException(); }
|
---|
| 207 | #endregion
|
---|
[3260] | 208 | }
|
---|
| 209 | }
|
---|