[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;
|
---|
[3376] | 25 | using HeuristicLab.Common;
|
---|
[3260] | 26 | using HeuristicLab.Core;
|
---|
[3329] | 27 | using HeuristicLab.Data;
|
---|
[3260] | 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[3614] | 29 | using HeuristicLab.Collections;
|
---|
[3260] | 30 |
|
---|
| 31 | namespace HeuristicLab.Optimization {
|
---|
[3716] | 32 | [Item("Run Collection", "Represents a collection of runs.")]
|
---|
| 33 | [Creatable("Testing & Analysis")]
|
---|
[3260] | 34 | [StorableClass]
|
---|
[3329] | 35 | public class RunCollection : ItemCollection<IRun>, IStringConvertibleMatrix {
|
---|
| 36 | public RunCollection() : base() { Initialize(); }
|
---|
| 37 | public RunCollection(int capacity) : base(capacity) { Initialize(); }
|
---|
| 38 | public RunCollection(IEnumerable<IRun> collection) : base(collection) { Initialize(); this.OnItemsAdded(collection); }
|
---|
[3447] | 39 | private void Initialize() {
|
---|
| 40 | parameterNames = new List<string>();
|
---|
| 41 | resultNames = new List<string>();
|
---|
[3614] | 42 | dataTypes = new Dictionary<string, HashSet<Type>>();
|
---|
| 43 | constraints = new RunCollectionConstraintCollection();
|
---|
| 44 | constraints.ItemsAdded += new CollectionItemsChangedEventHandler<IRunCollectionConstraint>(Constraints_ItemsAdded);
|
---|
| 45 | constraints.ItemsRemoved += new CollectionItemsChangedEventHandler<IRunCollectionConstraint>(Constraints_ItemsRemoved);
|
---|
| 46 | constraints.CollectionReset += new CollectionItemsChangedEventHandler<IRunCollectionConstraint>(Constraints_CollectionReset);
|
---|
[3447] | 47 | }
|
---|
[3625] | 48 | [Storable]
|
---|
[3614] | 49 | private Dictionary<string, HashSet<Type>> dataTypes;
|
---|
| 50 | public IEnumerable<Type> GetDataType(string columnName) {
|
---|
| 51 | if (!dataTypes.ContainsKey(columnName))
|
---|
| 52 | return new Type[0];
|
---|
| 53 | return dataTypes[columnName];
|
---|
| 54 | }
|
---|
| 55 | private RunCollectionConstraintCollection constraints;
|
---|
| 56 | public RunCollectionConstraintCollection Constraints {
|
---|
[3625] | 57 | get { return constraints; }
|
---|
[3614] | 58 | }
|
---|
[3329] | 59 |
|
---|
| 60 | protected override void OnCollectionReset(IEnumerable<IRun> items, IEnumerable<IRun> oldItems) {
|
---|
| 61 | parameterNames.Clear();
|
---|
| 62 | resultNames.Clear();
|
---|
| 63 | foreach (IRun run in items) {
|
---|
| 64 | foreach (KeyValuePair<string, IItem> parameter in run.Parameters)
|
---|
| 65 | AddParameter(parameter.Key, parameter.Value);
|
---|
| 66 | foreach (KeyValuePair<string, IItem> result in run.Results)
|
---|
| 67 | AddResult(result.Key, result.Value);
|
---|
| 68 | }
|
---|
| 69 | base.OnCollectionReset(items, oldItems);
|
---|
| 70 | OnReset();
|
---|
| 71 | OnColumnNamesChanged();
|
---|
| 72 | OnRowNamesChanged();
|
---|
| 73 | }
|
---|
| 74 | protected override void OnItemsAdded(IEnumerable<IRun> items) {
|
---|
| 75 | bool columnNamesChanged = false;
|
---|
| 76 | foreach (IRun run in items) {
|
---|
| 77 | foreach (KeyValuePair<string, IItem> parameter in run.Parameters)
|
---|
| 78 | columnNamesChanged |= AddParameter(parameter.Key, parameter.Value);
|
---|
| 79 | foreach (KeyValuePair<string, IItem> result in run.Results)
|
---|
| 80 | columnNamesChanged |= AddResult(result.Key, result.Value);
|
---|
| 81 | }
|
---|
| 82 | base.OnItemsAdded(items);
|
---|
| 83 | OnReset();
|
---|
| 84 | if (columnNamesChanged)
|
---|
| 85 | OnColumnNamesChanged();
|
---|
| 86 | OnRowNamesChanged();
|
---|
[3632] | 87 | this.UpdateFiltering(false);
|
---|
[3329] | 88 | }
|
---|
| 89 | protected override void OnItemsRemoved(IEnumerable<IRun> items) {
|
---|
| 90 | bool columnNamesChanged = false;
|
---|
| 91 | foreach (IRun run in items) {
|
---|
| 92 | foreach (string parameterName in run.Parameters.Keys)
|
---|
| 93 | columnNamesChanged |= RemoveParameterName(parameterName);
|
---|
| 94 | foreach (string resultName in run.Results.Keys)
|
---|
| 95 | columnNamesChanged |= RemoveResultName(resultName);
|
---|
| 96 | }
|
---|
| 97 | base.OnItemsRemoved(items);
|
---|
| 98 | OnReset();
|
---|
| 99 | if (columnNamesChanged)
|
---|
| 100 | OnColumnNamesChanged();
|
---|
| 101 | OnRowNamesChanged();
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | private bool AddParameter(string name, IItem value) {
|
---|
| 105 | if (value == null)
|
---|
| 106 | return false;
|
---|
[3441] | 107 | if (!parameterNames.Contains(name)) {
|
---|
[3329] | 108 | parameterNames.Add(name);
|
---|
[3614] | 109 | dataTypes[name] = new HashSet<Type>();
|
---|
| 110 | dataTypes[name].Add(value.GetType());
|
---|
[3329] | 111 | return true;
|
---|
| 112 | }
|
---|
[3614] | 113 | dataTypes[name].Add(value.GetType());
|
---|
[3329] | 114 | return false;
|
---|
| 115 | }
|
---|
| 116 | private bool AddResult(string name, IItem value) {
|
---|
| 117 | if (value == null)
|
---|
| 118 | return false;
|
---|
[3441] | 119 | if (!resultNames.Contains(name)) {
|
---|
[3329] | 120 | resultNames.Add(name);
|
---|
[3614] | 121 | dataTypes[name] = new HashSet<Type>();
|
---|
| 122 | dataTypes[name].Add(value.GetType());
|
---|
[3329] | 123 | return true;
|
---|
| 124 | }
|
---|
[3614] | 125 | dataTypes[name].Add(value.GetType());
|
---|
[3329] | 126 | return false;
|
---|
| 127 | }
|
---|
| 128 | private bool RemoveParameterName(string name) {
|
---|
| 129 | if (!list.Any(x => x.Parameters.ContainsKey(name))) {
|
---|
| 130 | parameterNames.Remove(name);
|
---|
| 131 | return true;
|
---|
| 132 | }
|
---|
| 133 | return false;
|
---|
| 134 | }
|
---|
| 135 | private bool RemoveResultName(string name) {
|
---|
| 136 | if (!list.Any(x => x.Results.ContainsKey(name))) {
|
---|
| 137 | resultNames.Remove(name);
|
---|
| 138 | return true;
|
---|
| 139 | }
|
---|
| 140 | return false;
|
---|
| 141 | }
|
---|
| 142 |
|
---|
[3447] | 143 | public IItem GetValue(int rowIndex, int columnIndex) {
|
---|
| 144 | IRun run = this.list[rowIndex];
|
---|
[3492] | 145 | return GetValue(run, columnIndex);
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | public IItem GetValue(IRun run, int columnIndex) {
|
---|
[3717] | 149 | string name = ((IStringConvertibleMatrix)this).ColumnNames.ElementAt(columnIndex);
|
---|
| 150 | return GetValue(run, name);
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | public IItem GetValue(IRun run, string columnName) {
|
---|
[3447] | 154 | IItem value = null;
|
---|
[3767] | 155 | if (run.Parameters.ContainsKey(columnName))
|
---|
| 156 | value = run.Parameters[columnName];
|
---|
| 157 | else if (run.Results.ContainsKey(columnName))
|
---|
| 158 | value = run.Results[columnName];
|
---|
[3447] | 159 | return value;
|
---|
| 160 | }
|
---|
| 161 |
|
---|
[3717] | 162 |
|
---|
[3625] | 163 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 164 | RunCollection clone = (RunCollection)base.Clone(cloner);
|
---|
[3632] | 165 | clone.resultNames = new List<string>(this.resultNames);
|
---|
| 166 | clone.parameterNames = new List<string>(this.parameterNames);
|
---|
[3625] | 167 | clone.dataTypes = new Dictionary<string, HashSet<Type>>();
|
---|
| 168 | foreach (string s in this.dataTypes.Keys)
|
---|
| 169 | clone.dataTypes[s] = new HashSet<Type>(this.dataTypes[s]);
|
---|
| 170 | return clone;
|
---|
| 171 | }
|
---|
| 172 |
|
---|
[3329] | 173 | #region IStringConvertibleMatrix Members
|
---|
[3347] | 174 | [Storable]
|
---|
[3329] | 175 | private List<string> parameterNames;
|
---|
[3492] | 176 | public IEnumerable<string> ParameterNames {
|
---|
| 177 | get { return this.parameterNames; }
|
---|
| 178 | }
|
---|
[3347] | 179 | [Storable]
|
---|
[3329] | 180 | private List<string> resultNames;
|
---|
[3492] | 181 | public IEnumerable<string> ResultNames {
|
---|
| 182 | get { return this.resultNames; }
|
---|
| 183 | }
|
---|
[3447] | 184 | int IStringConvertibleMatrix.Rows {
|
---|
[3329] | 185 | get { return this.Count; }
|
---|
[3447] | 186 | set { throw new NotSupportedException(); }
|
---|
[3329] | 187 | }
|
---|
[3447] | 188 | int IStringConvertibleMatrix.Columns {
|
---|
[3329] | 189 | get { return parameterNames.Count + resultNames.Count; }
|
---|
| 190 | set { throw new NotSupportedException(); }
|
---|
| 191 | }
|
---|
[3447] | 192 | IEnumerable<string> IStringConvertibleMatrix.ColumnNames {
|
---|
[3329] | 193 | get {
|
---|
| 194 | List<string> value = new List<string>(parameterNames);
|
---|
| 195 | value.AddRange(resultNames);
|
---|
[3717] | 196 | value.Sort();
|
---|
[3329] | 197 | return value;
|
---|
| 198 | }
|
---|
| 199 | set { throw new NotSupportedException(); }
|
---|
| 200 | }
|
---|
[3447] | 201 | IEnumerable<string> IStringConvertibleMatrix.RowNames {
|
---|
[3329] | 202 | get { return list.Select(x => x.Name).ToList(); }
|
---|
| 203 | set { throw new NotSupportedException(); }
|
---|
| 204 | }
|
---|
[3447] | 205 | bool IStringConvertibleMatrix.SortableView {
|
---|
[3329] | 206 | get { return true; }
|
---|
| 207 | set { throw new NotSupportedException(); }
|
---|
| 208 | }
|
---|
[3447] | 209 | bool IStringConvertibleMatrix.ReadOnly {
|
---|
| 210 | get { return true; }
|
---|
[3430] | 211 | }
|
---|
[3329] | 212 |
|
---|
[3447] | 213 | string IStringConvertibleMatrix.GetValue(int rowIndex, int columnIndex) {
|
---|
| 214 | IItem value = GetValue(rowIndex, columnIndex);
|
---|
| 215 | if (value == null)
|
---|
| 216 | return string.Empty;
|
---|
| 217 | return value.ToString();
|
---|
[3329] | 218 | }
|
---|
| 219 |
|
---|
| 220 | public event EventHandler<EventArgs<int, int>> ItemChanged;
|
---|
| 221 | protected virtual void OnItemChanged(int rowIndex, int columnIndex) {
|
---|
| 222 | if (ItemChanged != null)
|
---|
| 223 | ItemChanged(this, new EventArgs<int, int>(rowIndex, columnIndex));
|
---|
| 224 | OnToStringChanged();
|
---|
| 225 | }
|
---|
| 226 | public event EventHandler Reset;
|
---|
| 227 | protected virtual void OnReset() {
|
---|
| 228 | if (Reset != null)
|
---|
| 229 | Reset(this, EventArgs.Empty);
|
---|
| 230 | OnToStringChanged();
|
---|
| 231 | }
|
---|
| 232 | public event EventHandler ColumnNamesChanged;
|
---|
| 233 | protected virtual void OnColumnNamesChanged() {
|
---|
| 234 | EventHandler handler = ColumnNamesChanged;
|
---|
| 235 | if (handler != null)
|
---|
| 236 | handler(this, EventArgs.Empty);
|
---|
| 237 | }
|
---|
| 238 | public event EventHandler RowNamesChanged;
|
---|
| 239 | protected virtual void OnRowNamesChanged() {
|
---|
| 240 | EventHandler handler = RowNamesChanged;
|
---|
| 241 | if (handler != null)
|
---|
| 242 | handler(this, EventArgs.Empty);
|
---|
| 243 | }
|
---|
| 244 | public event EventHandler SortableViewChanged;
|
---|
[3333] | 245 | protected virtual void OnSortableViewChanged() {
|
---|
| 246 | EventHandler handler = SortableViewChanged;
|
---|
| 247 | if (handler != null)
|
---|
| 248 | handler(this, EventArgs.Empty);
|
---|
| 249 | }
|
---|
| 250 |
|
---|
[3329] | 251 | public bool Validate(string value, out string errorMessage) { throw new NotSupportedException(); }
|
---|
| 252 | public bool SetValue(string value, int rowIndex, int columnIndex) { throw new NotSupportedException(); }
|
---|
| 253 | #endregion
|
---|
[3614] | 254 |
|
---|
| 255 | #region filtering
|
---|
[3632] | 256 | private void UpdateFiltering(bool reset) {
|
---|
| 257 | if (reset)
|
---|
| 258 | list.ForEach(r => r.Visible = true);
|
---|
[3614] | 259 | foreach (IRunCollectionConstraint constraint in this.constraints)
|
---|
| 260 | constraint.Check();
|
---|
| 261 | }
|
---|
| 262 |
|
---|
| 263 | protected virtual void RegisterConstraintEvents(IEnumerable<IRunCollectionConstraint> constraints) {
|
---|
| 264 | foreach (IRunCollectionConstraint constraint in constraints) {
|
---|
| 265 | constraint.ActiveChanged += new EventHandler(Constraint_ActiveChanged);
|
---|
| 266 | constraint.ConstrainedValueChanged += new EventHandler(Constraint_ConstrainedValueChanged);
|
---|
| 267 | constraint.ConstraintOperationChanged += new EventHandler(Constraint_ConstraintOperationChanged);
|
---|
| 268 | constraint.ConstraintDataChanged += new EventHandler(Constraint_ConstraintDataChanged);
|
---|
| 269 | }
|
---|
| 270 | }
|
---|
| 271 | protected virtual void DeregisterConstraintEvents(IEnumerable<IRunCollectionConstraint> constraints) {
|
---|
| 272 | foreach (IRunCollectionConstraint constraint in constraints) {
|
---|
| 273 | constraint.ActiveChanged -= new EventHandler(Constraint_ActiveChanged);
|
---|
| 274 | constraint.ConstrainedValueChanged -= new EventHandler(Constraint_ConstrainedValueChanged);
|
---|
| 275 | constraint.ConstraintOperationChanged -= new EventHandler(Constraint_ConstraintOperationChanged);
|
---|
| 276 | constraint.ConstraintDataChanged -= new EventHandler(Constraint_ConstraintDataChanged);
|
---|
| 277 | }
|
---|
| 278 | }
|
---|
| 279 |
|
---|
| 280 | protected virtual void Constraints_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRunCollectionConstraint> e) {
|
---|
| 281 | DeregisterConstraintEvents(e.OldItems);
|
---|
| 282 | RegisterConstraintEvents(e.Items);
|
---|
[3632] | 283 | this.UpdateFiltering(true);
|
---|
[3614] | 284 | }
|
---|
| 285 | protected virtual void Constraints_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IRunCollectionConstraint> e) {
|
---|
| 286 | RegisterConstraintEvents(e.Items);
|
---|
| 287 | foreach (IRunCollectionConstraint constraint in e.Items)
|
---|
| 288 | constraint.ConstrainedValue = this;
|
---|
[3632] | 289 | this.UpdateFiltering(false);
|
---|
[3614] | 290 | }
|
---|
| 291 | protected virtual void Constraints_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IRunCollectionConstraint> e) {
|
---|
| 292 | DeregisterConstraintEvents(e.Items);
|
---|
[3632] | 293 | this.UpdateFiltering(true);
|
---|
[3614] | 294 | }
|
---|
| 295 | protected virtual void Constraint_ActiveChanged(object sender, EventArgs e) {
|
---|
[3632] | 296 | IRunCollectionConstraint constraint = (IRunCollectionConstraint)sender;
|
---|
| 297 | this.UpdateFiltering(!constraint.Active);
|
---|
[3614] | 298 | }
|
---|
| 299 | protected virtual void Constraint_ConstrainedValueChanged(object sender, EventArgs e) {
|
---|
| 300 | //mkommend: this method is intentionally left empty, because the constrainedValue is set in the ItemsAdded method
|
---|
| 301 | }
|
---|
| 302 | protected virtual void Constraint_ConstraintOperationChanged(object sender, EventArgs e) {
|
---|
[3632] | 303 | IRunCollectionConstraint constraint = (IRunCollectionConstraint)sender;
|
---|
| 304 | if (constraint.Active)
|
---|
| 305 | this.UpdateFiltering(true);
|
---|
[3614] | 306 | }
|
---|
| 307 | protected virtual void Constraint_ConstraintDataChanged(object sender, EventArgs e) {
|
---|
[3632] | 308 | IRunCollectionConstraint constraint = (IRunCollectionConstraint)sender;
|
---|
| 309 | if (constraint.Active)
|
---|
| 310 | this.UpdateFiltering(true);
|
---|
[3614] | 311 | }
|
---|
| 312 | #endregion
|
---|
[3260] | 313 | }
|
---|
| 314 | }
|
---|