[3260] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[7259] | 3 | * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[3260] | 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;
|
---|
[4068] | 25 | using HeuristicLab.Collections;
|
---|
[3376] | 26 | using HeuristicLab.Common;
|
---|
[3260] | 27 | using HeuristicLab.Core;
|
---|
[3329] | 28 | using HeuristicLab.Data;
|
---|
[3260] | 29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 30 |
|
---|
| 31 | namespace HeuristicLab.Optimization {
|
---|
[3716] | 32 | [Item("Run Collection", "Represents a collection of runs.")]
|
---|
| 33 | [Creatable("Testing & Analysis")]
|
---|
[3260] | 34 | [StorableClass]
|
---|
[4419] | 35 | public class RunCollection : ItemCollection<IRun>, IStringConvertibleMatrix, IStorableContent {
|
---|
| 36 | public string Filename { get; set; }
|
---|
| 37 |
|
---|
[4164] | 38 | [StorableConstructor]
|
---|
[4888] | 39 | protected RunCollection(bool deserializing)
|
---|
| 40 | : base(deserializing) {
|
---|
| 41 | updateOfRunsInProgress = false;
|
---|
| 42 | }
|
---|
[4722] | 43 | protected RunCollection(RunCollection original, Cloner cloner)
|
---|
| 44 | : base(original, cloner) {
|
---|
[4888] | 45 | updateOfRunsInProgress = false;
|
---|
[4722] | 46 | resultNames = new List<string>(original.resultNames);
|
---|
| 47 | parameterNames = new List<string>(original.parameterNames);
|
---|
| 48 | dataTypes = new Dictionary<string, HashSet<Type>>();
|
---|
| 49 | foreach (string s in original.dataTypes.Keys)
|
---|
| 50 | dataTypes[s] = new HashSet<Type>(original.dataTypes[s]);
|
---|
| 51 |
|
---|
| 52 | constraints = new RunCollectionConstraintCollection(original.constraints.Select(x => cloner.Clone(x)));
|
---|
[6693] | 53 | modifiers = new CheckedItemList<IRunCollectionModifier>(original.modifiers.Select(cloner.Clone));
|
---|
[4722] | 54 | foreach (IRunCollectionConstraint constraint in constraints)
|
---|
| 55 | constraint.ConstrainedValue = this;
|
---|
| 56 | RegisterConstraintsEvents();
|
---|
| 57 | RegisterConstraintEvents(constraints);
|
---|
| 58 |
|
---|
| 59 | UpdateFiltering(true);
|
---|
| 60 | }
|
---|
[3329] | 61 | public RunCollection() : base() { Initialize(); }
|
---|
| 62 | public RunCollection(int capacity) : base(capacity) { Initialize(); }
|
---|
| 63 | public RunCollection(IEnumerable<IRun> collection) : base(collection) { Initialize(); this.OnItemsAdded(collection); }
|
---|
[3447] | 64 | private void Initialize() {
|
---|
[4888] | 65 | updateOfRunsInProgress = false;
|
---|
[3447] | 66 | parameterNames = new List<string>();
|
---|
| 67 | resultNames = new List<string>();
|
---|
[3614] | 68 | dataTypes = new Dictionary<string, HashSet<Type>>();
|
---|
| 69 | constraints = new RunCollectionConstraintCollection();
|
---|
[6693] | 70 | modifiers = new CheckedItemList<IRunCollectionModifier>();
|
---|
[4164] | 71 | RegisterConstraintsEvents();
|
---|
[3447] | 72 | }
|
---|
[4164] | 73 |
|
---|
[3625] | 74 | [Storable]
|
---|
[3614] | 75 | private Dictionary<string, HashSet<Type>> dataTypes;
|
---|
| 76 | public IEnumerable<Type> GetDataType(string columnName) {
|
---|
| 77 | if (!dataTypes.ContainsKey(columnName))
|
---|
| 78 | return new Type[0];
|
---|
| 79 | return dataTypes[columnName];
|
---|
| 80 | }
|
---|
[4164] | 81 |
|
---|
| 82 | [Storable]
|
---|
[3614] | 83 | private RunCollectionConstraintCollection constraints;
|
---|
| 84 | public RunCollectionConstraintCollection Constraints {
|
---|
[3625] | 85 | get { return constraints; }
|
---|
[3614] | 86 | }
|
---|
[3329] | 87 |
|
---|
[6693] | 88 | [Storable]
|
---|
| 89 | private CheckedItemList<IRunCollectionModifier> modifiers;
|
---|
| 90 | public CheckedItemList<IRunCollectionModifier> Modifiers {
|
---|
| 91 | get { return modifiers; }
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 |
|
---|
[4888] | 95 | private bool updateOfRunsInProgress;
|
---|
| 96 | public bool UpdateOfRunsInProgress {
|
---|
| 97 | get { return updateOfRunsInProgress; }
|
---|
| 98 | set {
|
---|
| 99 | if (updateOfRunsInProgress != value) {
|
---|
| 100 | updateOfRunsInProgress = value;
|
---|
| 101 | OnUpdateOfRunsInProgressChanged();
|
---|
| 102 | }
|
---|
| 103 | }
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 107 | private void AfterDeserialization() {
|
---|
| 108 | if (constraints == null) constraints = new RunCollectionConstraintCollection();
|
---|
[6693] | 109 | if (modifiers == null) modifiers = new CheckedItemList<IRunCollectionModifier>();
|
---|
[4888] | 110 | RegisterConstraintsEvents();
|
---|
| 111 | RegisterConstraintEvents(constraints);
|
---|
| 112 | UpdateFiltering(true);
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 116 | return new RunCollection(this, cloner);
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | public event EventHandler UpdateOfRunsInProgressChanged;
|
---|
| 120 | protected virtual void OnUpdateOfRunsInProgressChanged() {
|
---|
| 121 | var handler = UpdateOfRunsInProgressChanged;
|
---|
| 122 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 123 | }
|
---|
| 124 |
|
---|
[3329] | 125 | protected override void OnCollectionReset(IEnumerable<IRun> items, IEnumerable<IRun> oldItems) {
|
---|
| 126 | parameterNames.Clear();
|
---|
| 127 | resultNames.Clear();
|
---|
| 128 | foreach (IRun run in items) {
|
---|
| 129 | foreach (KeyValuePair<string, IItem> parameter in run.Parameters)
|
---|
| 130 | AddParameter(parameter.Key, parameter.Value);
|
---|
| 131 | foreach (KeyValuePair<string, IItem> result in run.Results)
|
---|
| 132 | AddResult(result.Key, result.Value);
|
---|
| 133 | }
|
---|
[4200] | 134 | columnNameCache = null;
|
---|
[5150] | 135 | OnColumnsChanged();
|
---|
[3329] | 136 | OnColumnNamesChanged();
|
---|
[4200] | 137 | rowNamesCache = null;
|
---|
[4518] | 138 | base.OnCollectionReset(items, oldItems);
|
---|
[5150] | 139 | OnRowsChanged();
|
---|
[3329] | 140 | OnRowNamesChanged();
|
---|
[4518] | 141 | OnReset();
|
---|
| 142 | UpdateFiltering(false);
|
---|
[3329] | 143 | }
|
---|
| 144 | protected override void OnItemsAdded(IEnumerable<IRun> items) {
|
---|
[5150] | 145 | bool columnsChanged = false;
|
---|
[3329] | 146 | foreach (IRun run in items) {
|
---|
| 147 | foreach (KeyValuePair<string, IItem> parameter in run.Parameters)
|
---|
[5150] | 148 | columnsChanged |= AddParameter(parameter.Key, parameter.Value);
|
---|
[3329] | 149 | foreach (KeyValuePair<string, IItem> result in run.Results)
|
---|
[5150] | 150 | columnsChanged |= AddResult(result.Key, result.Value);
|
---|
[3329] | 151 | }
|
---|
[5150] | 152 | if (columnsChanged) columnNameCache = null;
|
---|
[4200] | 153 | rowNamesCache = null;
|
---|
[4518] | 154 | base.OnItemsAdded(items);
|
---|
[4707] | 155 | OnReset();
|
---|
[5150] | 156 | OnRowsChanged();
|
---|
[3329] | 157 | OnRowNamesChanged();
|
---|
[5150] | 158 | if (columnsChanged) {
|
---|
| 159 | OnColumnsChanged();
|
---|
| 160 | OnColumnNamesChanged();
|
---|
| 161 | }
|
---|
[4518] | 162 | UpdateFiltering(false);
|
---|
[3329] | 163 | }
|
---|
| 164 | protected override void OnItemsRemoved(IEnumerable<IRun> items) {
|
---|
[5150] | 165 | bool columnsChanged = false;
|
---|
[3329] | 166 | foreach (IRun run in items) {
|
---|
| 167 | foreach (string parameterName in run.Parameters.Keys)
|
---|
[5150] | 168 | columnsChanged |= RemoveParameterName(parameterName);
|
---|
[3329] | 169 | foreach (string resultName in run.Results.Keys)
|
---|
[5150] | 170 | columnsChanged |= RemoveResultName(resultName);
|
---|
[3329] | 171 | }
|
---|
[5150] | 172 | if (columnsChanged) columnNameCache = null;
|
---|
[4200] | 173 | rowNamesCache = null;
|
---|
[4518] | 174 | base.OnItemsRemoved(items);
|
---|
[4707] | 175 | OnReset();
|
---|
[5152] | 176 | OnRowsChanged();
|
---|
[3329] | 177 | OnRowNamesChanged();
|
---|
[5150] | 178 | if (columnsChanged) {
|
---|
| 179 | OnColumnsChanged();
|
---|
| 180 | OnColumnNamesChanged();
|
---|
| 181 | }
|
---|
[3329] | 182 | }
|
---|
| 183 |
|
---|
| 184 | private bool AddParameter(string name, IItem value) {
|
---|
| 185 | if (value == null)
|
---|
| 186 | return false;
|
---|
[3441] | 187 | if (!parameterNames.Contains(name)) {
|
---|
[3329] | 188 | parameterNames.Add(name);
|
---|
[3614] | 189 | dataTypes[name] = new HashSet<Type>();
|
---|
| 190 | dataTypes[name].Add(value.GetType());
|
---|
[3329] | 191 | return true;
|
---|
| 192 | }
|
---|
[3614] | 193 | dataTypes[name].Add(value.GetType());
|
---|
[3329] | 194 | return false;
|
---|
| 195 | }
|
---|
| 196 | private bool AddResult(string name, IItem value) {
|
---|
| 197 | if (value == null)
|
---|
| 198 | return false;
|
---|
[3441] | 199 | if (!resultNames.Contains(name)) {
|
---|
[3329] | 200 | resultNames.Add(name);
|
---|
[3614] | 201 | dataTypes[name] = new HashSet<Type>();
|
---|
| 202 | dataTypes[name].Add(value.GetType());
|
---|
[3329] | 203 | return true;
|
---|
| 204 | }
|
---|
[3614] | 205 | dataTypes[name].Add(value.GetType());
|
---|
[3329] | 206 | return false;
|
---|
| 207 | }
|
---|
| 208 | private bool RemoveParameterName(string name) {
|
---|
| 209 | if (!list.Any(x => x.Parameters.ContainsKey(name))) {
|
---|
| 210 | parameterNames.Remove(name);
|
---|
| 211 | return true;
|
---|
| 212 | }
|
---|
| 213 | return false;
|
---|
| 214 | }
|
---|
| 215 | private bool RemoveResultName(string name) {
|
---|
| 216 | if (!list.Any(x => x.Results.ContainsKey(name))) {
|
---|
| 217 | resultNames.Remove(name);
|
---|
| 218 | return true;
|
---|
| 219 | }
|
---|
| 220 | return false;
|
---|
| 221 | }
|
---|
| 222 |
|
---|
[3447] | 223 | public IItem GetValue(int rowIndex, int columnIndex) {
|
---|
| 224 | IRun run = this.list[rowIndex];
|
---|
[3492] | 225 | return GetValue(run, columnIndex);
|
---|
| 226 | }
|
---|
| 227 |
|
---|
| 228 | public IItem GetValue(IRun run, int columnIndex) {
|
---|
[3717] | 229 | string name = ((IStringConvertibleMatrix)this).ColumnNames.ElementAt(columnIndex);
|
---|
| 230 | return GetValue(run, name);
|
---|
| 231 | }
|
---|
| 232 |
|
---|
| 233 | public IItem GetValue(IRun run, string columnName) {
|
---|
[3447] | 234 | IItem value = null;
|
---|
[3767] | 235 | if (run.Parameters.ContainsKey(columnName))
|
---|
| 236 | value = run.Parameters[columnName];
|
---|
| 237 | else if (run.Results.ContainsKey(columnName))
|
---|
| 238 | value = run.Results[columnName];
|
---|
[3447] | 239 | return value;
|
---|
| 240 | }
|
---|
| 241 |
|
---|
[3329] | 242 | #region IStringConvertibleMatrix Members
|
---|
[3347] | 243 | [Storable]
|
---|
[3329] | 244 | private List<string> parameterNames;
|
---|
[3492] | 245 | public IEnumerable<string> ParameterNames {
|
---|
| 246 | get { return this.parameterNames; }
|
---|
| 247 | }
|
---|
[3347] | 248 | [Storable]
|
---|
[3329] | 249 | private List<string> resultNames;
|
---|
[3492] | 250 | public IEnumerable<string> ResultNames {
|
---|
| 251 | get { return this.resultNames; }
|
---|
| 252 | }
|
---|
[3447] | 253 | int IStringConvertibleMatrix.Rows {
|
---|
[3329] | 254 | get { return this.Count; }
|
---|
[3447] | 255 | set { throw new NotSupportedException(); }
|
---|
[3329] | 256 | }
|
---|
[3447] | 257 | int IStringConvertibleMatrix.Columns {
|
---|
[3329] | 258 | get { return parameterNames.Count + resultNames.Count; }
|
---|
| 259 | set { throw new NotSupportedException(); }
|
---|
| 260 | }
|
---|
[4200] | 261 | private List<string> columnNameCache;
|
---|
[3447] | 262 | IEnumerable<string> IStringConvertibleMatrix.ColumnNames {
|
---|
[3329] | 263 | get {
|
---|
[4200] | 264 | if (columnNameCache == null) {
|
---|
| 265 | columnNameCache = new List<string>(parameterNames);
|
---|
| 266 | columnNameCache.AddRange(resultNames);
|
---|
| 267 | columnNameCache.Sort();
|
---|
| 268 | }
|
---|
| 269 | return columnNameCache;
|
---|
[3329] | 270 | }
|
---|
| 271 | set { throw new NotSupportedException(); }
|
---|
| 272 | }
|
---|
[4200] | 273 | private List<string> rowNamesCache;
|
---|
[3447] | 274 | IEnumerable<string> IStringConvertibleMatrix.RowNames {
|
---|
[4200] | 275 | get {
|
---|
| 276 | if (rowNamesCache == null)
|
---|
| 277 | rowNamesCache = list.Select(x => x.Name).ToList();
|
---|
| 278 | return rowNamesCache;
|
---|
| 279 | }
|
---|
[3329] | 280 | set { throw new NotSupportedException(); }
|
---|
| 281 | }
|
---|
[3447] | 282 | bool IStringConvertibleMatrix.SortableView {
|
---|
[3329] | 283 | get { return true; }
|
---|
| 284 | set { throw new NotSupportedException(); }
|
---|
| 285 | }
|
---|
[3447] | 286 | bool IStringConvertibleMatrix.ReadOnly {
|
---|
| 287 | get { return true; }
|
---|
[3430] | 288 | }
|
---|
[3329] | 289 |
|
---|
[3447] | 290 | string IStringConvertibleMatrix.GetValue(int rowIndex, int columnIndex) {
|
---|
| 291 | IItem value = GetValue(rowIndex, columnIndex);
|
---|
| 292 | if (value == null)
|
---|
| 293 | return string.Empty;
|
---|
| 294 | return value.ToString();
|
---|
[3329] | 295 | }
|
---|
| 296 |
|
---|
| 297 | public event EventHandler<EventArgs<int, int>> ItemChanged;
|
---|
| 298 | protected virtual void OnItemChanged(int rowIndex, int columnIndex) {
|
---|
[4722] | 299 | EventHandler<EventArgs<int, int>> handler = ItemChanged;
|
---|
| 300 | if (handler != null) handler(this, new EventArgs<int, int>(rowIndex, columnIndex));
|
---|
[3329] | 301 | OnToStringChanged();
|
---|
| 302 | }
|
---|
| 303 | public event EventHandler Reset;
|
---|
| 304 | protected virtual void OnReset() {
|
---|
[4722] | 305 | EventHandler handler = Reset;
|
---|
| 306 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
[3329] | 307 | OnToStringChanged();
|
---|
| 308 | }
|
---|
[5150] | 309 | public event EventHandler ColumnsChanged;
|
---|
| 310 | protected virtual void OnColumnsChanged() {
|
---|
| 311 | var handler = ColumnsChanged;
|
---|
| 312 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 313 | }
|
---|
| 314 | public event EventHandler RowsChanged;
|
---|
| 315 | protected virtual void OnRowsChanged() {
|
---|
| 316 | var handler = RowsChanged;
|
---|
| 317 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 318 | }
|
---|
[3329] | 319 | public event EventHandler ColumnNamesChanged;
|
---|
| 320 | protected virtual void OnColumnNamesChanged() {
|
---|
| 321 | EventHandler handler = ColumnNamesChanged;
|
---|
[4722] | 322 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
[3329] | 323 | }
|
---|
| 324 | public event EventHandler RowNamesChanged;
|
---|
| 325 | protected virtual void OnRowNamesChanged() {
|
---|
| 326 | EventHandler handler = RowNamesChanged;
|
---|
[4722] | 327 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
[3329] | 328 | }
|
---|
| 329 | public event EventHandler SortableViewChanged;
|
---|
[3333] | 330 | protected virtual void OnSortableViewChanged() {
|
---|
| 331 | EventHandler handler = SortableViewChanged;
|
---|
[4722] | 332 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
[3333] | 333 | }
|
---|
| 334 |
|
---|
[3329] | 335 | public bool Validate(string value, out string errorMessage) { throw new NotSupportedException(); }
|
---|
| 336 | public bool SetValue(string value, int rowIndex, int columnIndex) { throw new NotSupportedException(); }
|
---|
| 337 | #endregion
|
---|
[3614] | 338 |
|
---|
[4888] | 339 | #region Filtering
|
---|
[3632] | 340 | private void UpdateFiltering(bool reset) {
|
---|
[4888] | 341 | UpdateOfRunsInProgress = true;
|
---|
[3632] | 342 | if (reset)
|
---|
| 343 | list.ForEach(r => r.Visible = true);
|
---|
[3614] | 344 | foreach (IRunCollectionConstraint constraint in this.constraints)
|
---|
| 345 | constraint.Check();
|
---|
[4888] | 346 | UpdateOfRunsInProgress = false;
|
---|
[3614] | 347 | }
|
---|
| 348 |
|
---|
[4164] | 349 | private void RegisterConstraintsEvents() {
|
---|
| 350 | constraints.ItemsAdded += new CollectionItemsChangedEventHandler<IRunCollectionConstraint>(Constraints_ItemsAdded);
|
---|
| 351 | constraints.ItemsRemoved += new CollectionItemsChangedEventHandler<IRunCollectionConstraint>(Constraints_ItemsRemoved);
|
---|
| 352 | constraints.CollectionReset += new CollectionItemsChangedEventHandler<IRunCollectionConstraint>(Constraints_CollectionReset);
|
---|
| 353 | }
|
---|
| 354 |
|
---|
[3614] | 355 | protected virtual void RegisterConstraintEvents(IEnumerable<IRunCollectionConstraint> constraints) {
|
---|
| 356 | foreach (IRunCollectionConstraint constraint in constraints) {
|
---|
| 357 | constraint.ActiveChanged += new EventHandler(Constraint_ActiveChanged);
|
---|
| 358 | constraint.ConstrainedValueChanged += new EventHandler(Constraint_ConstrainedValueChanged);
|
---|
| 359 | constraint.ConstraintOperationChanged += new EventHandler(Constraint_ConstraintOperationChanged);
|
---|
| 360 | constraint.ConstraintDataChanged += new EventHandler(Constraint_ConstraintDataChanged);
|
---|
| 361 | }
|
---|
| 362 | }
|
---|
| 363 | protected virtual void DeregisterConstraintEvents(IEnumerable<IRunCollectionConstraint> constraints) {
|
---|
| 364 | foreach (IRunCollectionConstraint constraint in constraints) {
|
---|
| 365 | constraint.ActiveChanged -= new EventHandler(Constraint_ActiveChanged);
|
---|
| 366 | constraint.ConstrainedValueChanged -= new EventHandler(Constraint_ConstrainedValueChanged);
|
---|
| 367 | constraint.ConstraintOperationChanged -= new EventHandler(Constraint_ConstraintOperationChanged);
|
---|
| 368 | constraint.ConstraintDataChanged -= new EventHandler(Constraint_ConstraintDataChanged);
|
---|
| 369 | }
|
---|
| 370 | }
|
---|
| 371 |
|
---|
| 372 | protected virtual void Constraints_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRunCollectionConstraint> e) {
|
---|
| 373 | DeregisterConstraintEvents(e.OldItems);
|
---|
| 374 | RegisterConstraintEvents(e.Items);
|
---|
[3632] | 375 | this.UpdateFiltering(true);
|
---|
[3614] | 376 | }
|
---|
| 377 | protected virtual void Constraints_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IRunCollectionConstraint> e) {
|
---|
| 378 | RegisterConstraintEvents(e.Items);
|
---|
| 379 | foreach (IRunCollectionConstraint constraint in e.Items)
|
---|
| 380 | constraint.ConstrainedValue = this;
|
---|
[3632] | 381 | this.UpdateFiltering(false);
|
---|
[3614] | 382 | }
|
---|
| 383 | protected virtual void Constraints_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IRunCollectionConstraint> e) {
|
---|
| 384 | DeregisterConstraintEvents(e.Items);
|
---|
[3632] | 385 | this.UpdateFiltering(true);
|
---|
[3614] | 386 | }
|
---|
| 387 | protected virtual void Constraint_ActiveChanged(object sender, EventArgs e) {
|
---|
[3632] | 388 | IRunCollectionConstraint constraint = (IRunCollectionConstraint)sender;
|
---|
| 389 | this.UpdateFiltering(!constraint.Active);
|
---|
[3614] | 390 | }
|
---|
| 391 | protected virtual void Constraint_ConstrainedValueChanged(object sender, EventArgs e) {
|
---|
| 392 | //mkommend: this method is intentionally left empty, because the constrainedValue is set in the ItemsAdded method
|
---|
| 393 | }
|
---|
| 394 | protected virtual void Constraint_ConstraintOperationChanged(object sender, EventArgs e) {
|
---|
[3632] | 395 | IRunCollectionConstraint constraint = (IRunCollectionConstraint)sender;
|
---|
| 396 | if (constraint.Active)
|
---|
| 397 | this.UpdateFiltering(true);
|
---|
[3614] | 398 | }
|
---|
| 399 | protected virtual void Constraint_ConstraintDataChanged(object sender, EventArgs e) {
|
---|
[3632] | 400 | IRunCollectionConstraint constraint = (IRunCollectionConstraint)sender;
|
---|
| 401 | if (constraint.Active)
|
---|
| 402 | this.UpdateFiltering(true);
|
---|
[3614] | 403 | }
|
---|
| 404 | #endregion
|
---|
[6693] | 405 |
|
---|
| 406 | #region Modification
|
---|
| 407 | public void Modify() {
|
---|
| 408 | UpdateOfRunsInProgress = true;
|
---|
| 409 | var runs = this.ToList();
|
---|
| 410 | var selectedRuns = runs.Where(r => r.Visible).ToList();
|
---|
| 411 | int nSelected = selectedRuns.Count;
|
---|
| 412 | if (nSelected > 0) {
|
---|
| 413 | foreach (var modifier in Modifiers.CheckedItems)
|
---|
| 414 | modifier.Value.Modify(selectedRuns);
|
---|
| 415 | if (nSelected != selectedRuns.Count || HaveDifferentOrder(selectedRuns, runs.Where(r => r.Visible))) {
|
---|
| 416 | Clear();
|
---|
| 417 | AddRange(ReplaceVisibleRuns(runs, selectedRuns));
|
---|
| 418 | } else if (runs.Count > 0) {
|
---|
| 419 | OnCollectionReset(this, runs);
|
---|
| 420 | }
|
---|
| 421 | }
|
---|
| 422 | UpdateOfRunsInProgress = false;
|
---|
| 423 | }
|
---|
| 424 |
|
---|
| 425 | private static IEnumerable<IRun> ReplaceVisibleRuns(IEnumerable<IRun> runs, IEnumerable<IRun> visibleRuns) {
|
---|
| 426 | var newRuns = new List<IRun>();
|
---|
| 427 | var runIt = runs.GetEnumerator();
|
---|
| 428 | var visibleRunIt = visibleRuns.GetEnumerator();
|
---|
| 429 | while (runIt.MoveNext()) {
|
---|
| 430 | if (runIt.Current != null && !runIt.Current.Visible)
|
---|
| 431 | newRuns.Add(runIt.Current);
|
---|
| 432 | else if (visibleRunIt.MoveNext())
|
---|
| 433 | newRuns.Add(visibleRunIt.Current);
|
---|
| 434 | }
|
---|
| 435 | while (visibleRunIt.MoveNext())
|
---|
| 436 | newRuns.Add(visibleRunIt.Current);
|
---|
| 437 | return newRuns;
|
---|
| 438 | }
|
---|
| 439 |
|
---|
| 440 | private static bool HaveDifferentOrder(IEnumerable<IRun> l1, IEnumerable<IRun> l2) {
|
---|
| 441 | return l1.Zip(l2, (r1, r2) => r1 != r2).Any();
|
---|
| 442 | }
|
---|
| 443 | #endregion
|
---|
[3260] | 444 | }
|
---|
| 445 | }
|
---|