Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
09/01/11 15:51:30 (13 years ago)
Author:
epitzer
Message:

Integrate functionality of RunCollectionModificationEvaluator directly into RunCollection and its view. (#1622)

This update includes a modified RunCollectionModificationEvaluator that automatic migrates all modifies into the RunCollection.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Optimization/3.3/RunCollection.cs

    r5445 r6693  
    5151
    5252      constraints = new RunCollectionConstraintCollection(original.constraints.Select(x => cloner.Clone(x)));
     53      modifiers = new CheckedItemList<IRunCollectionModifier>(original.modifiers.Select(cloner.Clone));
    5354      foreach (IRunCollectionConstraint constraint in constraints)
    5455        constraint.ConstrainedValue = this;
     
    6768      dataTypes = new Dictionary<string, HashSet<Type>>();
    6869      constraints = new RunCollectionConstraintCollection();
     70      modifiers = new CheckedItemList<IRunCollectionModifier>();
    6971      RegisterConstraintsEvents();
    7072    }
     
    8385      get { return constraints; }
    8486    }
     87
     88    [Storable]
     89    private CheckedItemList<IRunCollectionModifier> modifiers;
     90    public CheckedItemList<IRunCollectionModifier> Modifiers {
     91      get { return modifiers; }
     92    }
     93
    8594
    8695    private bool updateOfRunsInProgress;
     
    98107    private void AfterDeserialization() {
    99108      if (constraints == null) constraints = new RunCollectionConstraintCollection();
     109      if (modifiers == null) modifiers = new CheckedItemList<IRunCollectionModifier>();
    100110      RegisterConstraintsEvents();
    101111      RegisterConstraintEvents(constraints);
     
    393403    }
    394404    #endregion
     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
    395444  }
    396445}
Note: See TracChangeset for help on using the changeset viewer.