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.

Location:
trunk/sources/HeuristicLab.Optimization/3.3
Files:
2 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}
  • trunk/sources/HeuristicLab.Optimization/3.3/RunCollectionModificationEvaluator.cs

    r6677 r6693  
    4848    #endregion
    4949
     50    [StorableHook(HookType.AfterDeserialization)]
     51    private void AfterDeserialization() {
     52      if (RunCollection != null && RunCollection.Modifiers != null) {
     53        RunCollection.Modifiers.AddRange(Modifiers);
     54        foreach (var modifier in RunCollection.Modifiers) {
     55          RunCollection.Modifiers.SetItemCheckedState(modifier, Modifiers.ItemChecked(modifier));
     56        }
     57        Modifiers.Clear();
     58      }
     59    }
     60
    5061    public void Evaluate() {
    5162      RunCollection.UpdateOfRunsInProgress = true;
    52       var runs = RunCollection.Select((r, i) => new {Run = r, r.Visible, Index = i}).ToList();
     63      var runs = RunCollection.Select((r, i) => new { Run = r, r.Visible, Index = i }).ToList();
    5364      var visibleRuns = runs.Where(r => r.Visible).Select(r => r.Run).ToList();
    5465      int n = visibleRuns.Count;
     
    7384        RunCollection.AddRange(newRuns);
    7485      } else if (runs.Count > 0) { // force update
    75         var run = (IRun) runs[0].Run.Clone();
     86        var run = (IRun)runs[0].Run.Clone();
    7687        RunCollection.Add(run);
    7788        RunCollection.Remove(run);
Note: See TracChangeset for help on using the changeset viewer.