Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceSpeedUp/HeuristicLab.Optimization/3.3/RunCollectionSorter.cs @ 7729

Last change on this file since 7729 was 6760, checked in by epitzer, 13 years ago

#1530 integrate changes from trunk

File size: 3.3 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using HeuristicLab.Common;
6using HeuristicLab.Core;
7using HeuristicLab.Data;
8using HeuristicLab.Parameters;
9using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
10
11namespace HeuristicLab.Optimization {
12  [Item("RunCollection Sorter", "Sorts a run collection according the specified key variable.")]
13  [StorableClass]
14  public class RunCollectionSorter : ParameterizedNamedItem, IRunCollectionModifier {
15
16    public override bool CanChangeName { get { return false; } }
17    public override bool CanChangeDescription { get { return false; } }
18
19    #region Parameters
20    public ValueParameter<StringValue> ValueParameter {
21      get { return (ValueParameter<StringValue>)Parameters["Value"]; }
22    }
23    #endregion
24
25    private string Value { get { return ValueParameter.Value.Value; } }
26
27    #region Construction & Cloning
28    [StorableConstructor]
29    protected RunCollectionSorter(bool deserializing) : base(deserializing) { }
30    protected RunCollectionSorter(RunCollectionSorter original, Cloner cloner)
31      : base(original, cloner) {
32      RegisterEvents();
33    }
34    public RunCollectionSorter() {
35      Parameters.Add(new ValueParameter<StringValue>("Value", "The variable name used as sorting key.", new StringValue("Value")));
36      RegisterEvents();
37      UpdateName();
38    }
39    public override IDeepCloneable Clone(Cloner cloner) {
40      return new RunCollectionSorter(this, cloner);
41    }
42    [StorableHook(HookType.AfterDeserialization)]
43    private void AfterDeserialization() {
44      RegisterEvents();
45    }
46    #endregion
47
48    private void RegisterEvents() {
49      ValueParameter.ToStringChanged += Parameter_NameChanged;
50    }
51
52    private void Parameter_NameChanged(object sender, EventArgs e) {
53      UpdateName();
54    }
55
56    private void UpdateName() {
57      name = string.Format("Sort by {0}", Value);
58      OnNameChanged();
59    }
60
61    private class ValueComparer : IComparer<IComparable> {
62
63      #region IComparer<IComparable> Members
64
65      public int Compare(IComparable x, IComparable y) {
66        if (x == null && y == null) return 0;
67        if (x == null) return -1;
68        if (y == null) return 1;
69        return x.CompareTo(y);
70      }
71
72      #endregion
73    }
74
75    private static readonly ValueComparer Comparer = new ValueComparer();
76
77    #region IRunCollectionModifier Members
78
79    public void Modify(List<IRun> runs) {
80      var sortedRuns = runs
81        .Select(r => new {Run = r, Key = GetValue(r)})
82        .OrderBy(r => r.Key, Comparer)
83        .Select(r => r.Run).ToList();
84      runs.Clear();
85      runs.AddRange(sortedRuns);
86    }
87
88    private IComparable GetValue(IRun run) {
89      return GetValue(run.Results) ?? GetValue(run.Parameters);
90    }
91
92    private IComparable GetValue(IDictionary<string, IItem> variables) {
93      IItem value;
94      variables.TryGetValue(Value, out value);
95      var intValue = value as IntValue;
96      if (intValue != null)
97        return intValue;
98      var doubleValue = value as DoubleValue;
99      if (doubleValue != null)
100        return doubleValue;
101      if (value != null)
102        return value.ToString();
103      return null;
104    }
105
106    #endregion
107  }
108}
Note: See TracBrowser for help on using the repository browser.