Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Visualization/3.2/AvgLineAggregator.cs @ 1688

Last change on this file since 1688 was 1607, checked in by cbahner, 16 years ago

#519 test cases + code review

File size: 4.3 KB
RevLine 
[1325]1using System;
2using System.Collections.Generic;
3
4namespace HeuristicLab.Visualization {
[1350]5  public class AvgLineAggregator : DataRowBase {
[1325]6
7    private readonly List<double> dataRow = new List<double>();
[1607]8    readonly List<IDataRow> dataRowWatches = new List<IDataRow>();
[1325]9
10    #region IAggregator Members
11
[1607]12    public void AddWatch(IDataRow watchDataRow) {
13      dataRowWatches.Add(watchDataRow);
14      watchDataRow.ValueChanged += dataRow_ValueChanged;
15      watchDataRow.ValuesChanged += dataRow_ValuesChanged;
16      watchDataRow.DataRowChanged += dataRow_DataRowChanged;
[1325]17    }
18
[1607]19    public void RemoveWatch(IDataRow watchDataRow) {
20      dataRowWatches.Remove(watchDataRow);
21      watchDataRow.DataRowChanged -= dataRow_DataRowChanged;
22      watchDataRow.ValuesChanged -= dataRow_ValuesChanged;
23      watchDataRow.ValueChanged -= dataRow_ValueChanged;
[1325]24    }
25
26    #endregion
27
28    void dataRow_ValueChanged(IDataRow row, double value, int index, Action action) {
[1605]29      switch (action) {
30        case Action.Added:
[1607]31          refreshLastValues(row);
[1605]32          break;
33        case Action.Modified:
34          refreshValue();
35          break;
36        case Action.Deleted:
[1607]37          refreshLastValues(row);
[1605]38          break;
39        default:
40          throw new ArgumentOutOfRangeException("action");
41      }
[1325]42    }
43
44    void dataRow_ValuesChanged(IDataRow row, double[] values, int index, Action action) {
45      for (int i = 0; i < values.Length; i++) {
[1605]46        refreshValue();
[1325]47      }
48    }
49
50    void dataRow_DataRowChanged(IDataRow row) {
[1605]51      refreshValue();
[1325]52    }
53
[1605]54    private void refreshValue() {
[1607]55     
[1325]56      int count = dataRowWatches.Count;
57
58      IDataRow firstRow = dataRowWatches[0];
59      int count1 = firstRow.Count;
[1607]60      Console.WriteLine("count: " + count1);
[1325]61
62      dataRow.Clear();
63
64      if (dataRowWatches.Count >= 2) {
65        for (int i = 0; i < count1; i++) {
[1607]66          double tmpSum = 0;
[1325]67          for (int j = 0; j < count; j++) {
68            if (dataRowWatches[j].Count > i) {
69              tmpSum += dataRowWatches[j][i];
70            }
71          }
72
[1607]73          dataRow.Add(tmpSum / count);
[1325]74          OnValueChanged(tmpSum / count, dataRow.Count - 1, Action.Added);
75        }
76      }
[1605]77    }
[1325]78
[1607]79    private void refreshLastValues(IDataRow row) {
[1605]80
81      int index = row.Count - 1;
82      double curAvg = 0;
83
84      foreach (IDataRow watch in dataRowWatches) {
85        if (watch.Count >= index +1) {
[1607]86          curAvg += watch[index];
[1605]87        }
88      }
89
90      if (dataRowWatches.Count > 0)
91        curAvg /= dataRowWatches.Count;
92
93
94      if (dataRow.Count <= index) {
95        dataRow.Add(curAvg);
96        OnValueChanged(curAvg, dataRow.Count - 1, Action.Added);     
97      }
98      else {
99        dataRow[index] = curAvg;
100        OnValueChanged(curAvg, dataRow.Count - 1, Action.Modified); 
101      }
[1325]102    }
103
104    #region IDataRow Members
105
[1350]106    public override void AddValue(double value) {
[1325]107      dataRow.Add(value);
108      OnValueChanged(value, dataRow.Count - 1, Action.Added);
109    }
110
[1350]111    public override void AddValue(double value, int index) {
[1607]112      throw new NotSupportedException();
[1325]113    }
114
[1350]115    public override void AddValues(double[] values) {
[1325]116      throw new NotSupportedException();
117    }
118
[1350]119    public override void AddValues(double[] values, int index) {
[1325]120      throw new NotSupportedException();
121    }
122
[1350]123    public override void ModifyValue(double value, int index) {
[1325]124      throw new NotSupportedException();
125    }
126
[1350]127    public override void ModifyValues(double[] values, int index) {
[1325]128      throw new NotSupportedException();
129    }
130
[1350]131    public override void RemoveValue(int index) {
[1325]132      throw new NotSupportedException();
133    }
134
[1607]135    public override void RemoveValues(int index, int countVals) {
[1325]136      throw new NotSupportedException();
137    }
138
[1350]139    public override int Count {
[1383]140      get { return dataRow.Count; } //return dataRowWatches.Count; }
[1325]141    }
142
[1350]143    public override double this[int index] {
[1325]144      get { return dataRow[index]; }
145      set {
146        dataRow[index] = value;
147        OnValueChanged(value, index, Action.Modified);
148      }
149    }
150
[1350]151    public override double MinValue {
152      get { return 0; }
[1327]153    }
154
[1350]155    public override double MaxValue {
156      get { return 0; }
[1327]157    }
158
[1325]159    #endregion
160  }
161}
Note: See TracBrowser for help on using the repository browser.