Repository navigation
Expand file tree
/
Copy pathDistributionDataItem.cs
More file actions
345 lines (322 loc) · 12.5 KB
/
Copy pathDistributionDataItem.cs
File metadata and controls
345 lines (322 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using Numerics.Data;
using Numerics.Distributions;
namespace NumericControls
{
/// <summary>
/// Represents a data item that manages distribution data for uncertain ordered paired data.
/// Handles the collection of distribution row items and synchronizes changes between the data model and UI.
/// </summary>
/// <remarks>
/// <para>
/// <b> Authors: </b>
/// <list type="bullet">
/// <item><description>
/// Haden Smith, USACE Risk Management Center, cole.h.smith@usace.army.mil
/// </description></item>
/// <item><description>
/// Woodrow Fields, USACE Risk Management Center, woodrow.l.fields@usace.army.mil
/// </description></item>
/// </list>
/// </para>
/// </remarks>
public class DistributionDataItem
{
private UncertainOrderedPairedData _data;
private bool _updatingData = false;
private double _maxXValue;
private double _minXValue;
private double _maxYValue;
private double _minYValue;
private bool _isStrictX;
private bool _isStrictY;
private SortOrder _xOrder;
private SortOrder _yOrder;
/// <summary>
/// Gets or sets the maximum allowed X value for validation.
/// </summary>
public double MaxXValue
{
get { return _maxXValue; }
set
{
if (_maxXValue != value)
{
_maxXValue = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(MaxXValue)));
}
}
}
/// <summary>
/// Gets or sets the minimum allowed X value for validation.
/// </summary>
public double MinXValue
{
get { return _minXValue; }
set
{
if (_minXValue != value)
{
_minXValue = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(MinXValue)));
}
}
}
/// <summary>
/// Gets or sets the maximum allowed Y value for validation.
/// </summary>
public double MaxYValue
{
get { return _maxYValue; }
set
{
if (_maxYValue != value)
{
_maxYValue = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(MaxYValue)));
}
}
}
/// <summary>
/// Gets or sets the minimum allowed Y value for validation.
/// </summary>
public double MinYValue
{
get { return _minYValue; }
set
{
if (_minYValue != value)
{
_minYValue = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(MinYValue)));
}
}
}
/// <summary>
/// Gets or sets whether X values must be strictly ordered (no duplicates allowed).
/// </summary>
public bool IsStrictX
{
get { return _isStrictX; }
set
{
if (_isStrictX != value)
{
_isStrictX = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(IsStrictX)));
}
}
}
/// <summary>
/// Gets or sets whether Y values must be strictly ordered (no duplicates allowed).
/// </summary>
public bool IsStrictY
{
get { return _isStrictY; }
set
{
if (_isStrictY != value)
{
_isStrictY = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(IsStrictY)));
}
}
}
/// <summary>
/// Gets or sets the sort order for X values.
/// </summary>
public SortOrder XOrder
{
get { return _xOrder; }
set
{
if (_xOrder != value)
{
_xOrder = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(XOrder)));
}
}
}
/// <summary>
/// Gets or sets the sort order for Y values.
/// </summary>
public SortOrder YOrder
{
get { return _yOrder; }
set
{
if (_yOrder != value)
{
_yOrder = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(YOrder)));
}
}
}
/// <summary>
/// Gets or sets the underlying uncertain ordered paired data.
/// </summary>
public UncertainOrderedPairedData Data
{
get { return _data; }
set
{
if (_data == null && value == null) return;
if (_data == value) return;
if (_data != null) _data.CollectionChanged -= DataCollectionChanged;
_data = value;
Refresh();
if (_data != null) _data.CollectionChanged += DataCollectionChanged;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Data)));
}
}
/// <summary>
/// Gets the distribution type for this data item.
/// </summary>
public UnivariateDistributionType Distribution { get; private set; }
/// <summary>
/// Gets the display name of the distribution type.
/// </summary>
public string DistributionName { get; private set; }
/// <summary>
/// Gets the collection of distribution row items for display in the data grid.
/// </summary>
public ObservableCollection<object> DistributionRows { get; private set; } = new ObservableCollection<object>();
/// <summary>
/// Occurs when data in a row changes.
/// </summary>
public event DataChangedEventHandler DataChanged;
/// <summary>
/// Represents a method that handles data changed events.
/// </summary>
/// <param name="dataChangedIndex">The index of the data item that changed.</param>
public delegate void DataChangedEventHandler(int dataChangedIndex);
/// <summary>
/// Occurs when a property value changes.
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// Initializes a new instance of the <see cref="DistributionDataItem"/> class.
/// </summary>
/// <param name="d">The uncertain ordered paired data.</param>
/// <param name="dType">The distribution type.</param>
/// <param name="minX">The minimum allowed X value.</param>
/// <param name="maxX">The maximum allowed X value.</param>
/// <param name="minY">The minimum allowed Y value.</param>
/// <param name="maxY">The maximum allowed Y value.</param>
/// <param name="strictX">Whether X values must be strictly ordered.</param>
/// <param name="strictY">Whether Y values must be strictly ordered.</param>
/// <param name="orderX">The sort order for X values.</param>
/// <param name="orderY">The sort order for Y values.</param>
public DistributionDataItem(UncertainOrderedPairedData d, UnivariateDistributionType dType, double minX, double maxX, double minY, double maxY, bool strictX, bool strictY, SortOrder orderX, SortOrder orderY)
{
_data = d;
if (_data != null) _data.CollectionChanged += DataCollectionChanged;
Distribution = dType;
_minXValue = minX;
_maxXValue = maxX;
_minYValue = minY;
_maxYValue = maxY;
_isStrictX = strictX;
_isStrictY = strictY;
_xOrder = orderX;
_yOrder = orderY;
if (_data is not null && _data.Count > 0)
DistributionName = _data[0].Y.DisplayName;
else if (UnivariateDistributionFactory.TryCreateDistribution(Distribution, out var distribution) &&
distribution is not null)
DistributionName = distribution.DisplayName;
else
DistributionName = Distribution.ToString();
Refresh();
}
/// <summary>
/// Handles collection changed events from the underlying data source and synchronizes with the UI.
/// </summary>
/// <param name="sender">The data collection that changed.</param>
/// <param name="e">The collection changed event arguments.</param>
private void DataCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (_updatingData == true) return;
switch (e.Action)
{
case NotifyCollectionChangedAction.Add:
{
Refresh();
break;
}
case NotifyCollectionChangedAction.Remove:
{
int startIndex = e.OldStartingIndex;
if (startIndex == -1 || e.OldItems == null)
{
Refresh();
}
else
{
for (int i = 1; i <= e.OldItems.Count; i++)
DistributionRows.RemoveAt(startIndex);
}
break;
}
case NotifyCollectionChangedAction.Replace:
{
int index = _data.IndexOf((UncertainOrdinate)e.NewItems[0]);
Refresh(index);
break;
}
default:
{
Refresh();
break;
}
}
}
/// <summary>
/// Refreshes all distribution rows from the underlying data.
/// </summary>
public void Refresh()
{
foreach (DistributionRowItem row in DistributionRows)
row.PropertyChanged -= RowItem_PropertyChanged;
DistributionRows.Clear();
if (Data == null) return;
DistributionRowItem rowItem;
foreach (var o in Data)
{
rowItem = new DistributionRowItem(o.X, o.Y, DistributionRows, MinXValue, MaxXValue, MinYValue, MaxYValue, IsStrictX, IsStrictY, XOrder, YOrder);
rowItem.PropertyChanged += RowItem_PropertyChanged;
DistributionRows.Add(rowItem);
}
}
/// <summary>
/// Refreshes a specific distribution row from the underlying data.
/// </summary>
/// <param name="rowIndex">The index of the row to refresh.</param>
public void Refresh(int rowIndex)
{
if (_data == null) return;
if ((rowIndex >= _data.Count) || (rowIndex < 0)) return;
((DistributionRowItem)DistributionRows[rowIndex]).PropertyChanged -= RowItem_PropertyChanged;
var rowItem = new DistributionRowItem(Data[rowIndex].X, Data[rowIndex].Y, DistributionRows, MinXValue, MaxXValue, MinYValue, MaxYValue, IsStrictX, IsStrictY, XOrder, YOrder);
rowItem.PropertyChanged += RowItem_PropertyChanged;
DistributionRows[rowIndex] = rowItem;
}
/// <summary>
/// Handles property changed events for row items and updates the data source.
/// </summary>
/// <param name="sender">The row item that changed.</param>
/// <param name="e">The property changed event arguments.</param>
private void RowItem_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
_updatingData = true;
DistributionRowItem rItem = (DistributionRowItem)sender;
int dataIndex = DistributionRows.IndexOf(rItem);
if (dataIndex < 0) { _updatingData = false; return; }
_data[dataIndex] = new UncertainOrdinate(rItem.X, rItem.Distribution.Clone());
_updatingData = false;
DataChanged?.Invoke(dataIndex);
}
}
}