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