2using System.Collections.Generic;
3using System.Threading.Tasks;
16 private readonly
object synchObj =
new object();
18 private DateTime[] timespans;
19 private double?[] values;
26 private int windowSize;
27 private int threshold;
29 private bool logNotice;
76 public override Task
FromXml(XmlElement Definition)
78 this.windowSize =
XML.
Attribute(Definition,
"windowSize", 0);
79 this.threshold =
XML.
Attribute(Definition,
"threshold", 0);
80 this.smooth =
XML.
Attribute(Definition,
"smooth",
false);
81 this.logNotice =
XML.
Attribute(Definition,
"logNotice",
true);
82 this.max = Definition.HasAttribute(
"max") ?
XML.
Attribute(Definition,
"max", 0.0) : (
double?)
null;
83 this.min = Definition.HasAttribute(
"min") ?
XML.
Attribute(Definition,
"min", 0.0) : (
double?)
null;
85 if (this.windowSize < 1)
86 throw new Exception(
"Windows size must be positive.");
88 if (this.threshold < 1)
89 throw new Exception(
"Threshold must be positive.");
91 this.timespans =
new DateTime[this.windowSize];
92 this.values =
new double?[this.windowSize];
96 this.avgPos = this.count / 2;
98 return base.FromXml(Definition);
116 if (this.filter is
null)
117 this.filter = Filter;
119 this.filter.
Append(Filter);
128 public bool Filter(ref DateTime Timestamp, ref
double Value)
130 if (this.min.HasValue && Value <
this.min.Value)
134 Log.
Notice(
"Outlier removed. Smaller than minimum value.", this.
For,
string.Empty,
"Outlier",
135 new KeyValuePair<string, object>(
"Value", Value));
143 if (this.max.HasValue && Value >
this.max.Value)
147 Log.
Notice(
"Outlier removed. Larger than maximum value.", this.
For,
string.Empty,
"Outlier",
148 new KeyValuePair<string, object>(
"Value", Value));
164 Old = this.values[this.pos];
166 this.timespans[this.pos] = Timestamp;
167 this.values[this.pos] = Value;
169 if (++this.pos == this.windowSize)
174 this.sum -= Old.Value;
181 Average = this.sum / this.count;
183 for (i = 0; i < this.windowSize; i++)
185 Old = this.values[i];
188 if (Old.Value > Average)
190 else if (Old.Value < Average)
195 Timestamp = this.timespans[this.avgPos];
196 Old = this.values[this.avgPos];
198 if (NrAbove <= this.threshold && NrBelow > this.threshold)
200 if (Old.HasValue && Old.Value > Average)
202 this.sum -= Old.Value;
204 this.values[this.avgPos] = Old =
null;
207 else if (NrBelow <= this.threshold && NrAbove > this.threshold)
209 if (Old.HasValue && Old.Value < Average)
211 this.sum -= Old.Value;
213 this.values[this.avgPos] = Old =
null;
217 if (++this.avgPos == this.windowSize)
225 Log.
Notice(
"Outlier removed. Threshold reached.", this.
For,
string.Empty,
"Outlier",
226 new KeyValuePair<string, object>(
"Value", Value));
Root node of a simulation model
void CountEvent(string CounterName)
Counts an event.
Removes outliers by comparing incoming samples with the average of the last samples.
override Task FromXml(XmlElement Definition)
Sets properties and attributes of class in accordance with XML definition.
override async Task Start()
Starts the node.
bool Smooth
If the average value should be used to also smooth the output.
void Append(IFilter Filter)
Appends a filter to the current filter.
bool Filter(ref DateTime Timestamp, ref double Value)
Filters a value
int Threshold
Threshold for outlier detection.
OutlierRemoval(ISimulationNode Parent, Model Model)
Removes outliers by comparing incoming samples with the average of the last samples.
override string LocalName
Local name of XML element defining contents of class.
override ISimulationNode Create(ISimulationNode Parent, Model Model)
Creates a new instance of the node.
int WindowSize
Number of samples to include in the average calculation.
Abstract base class for series references nodes.
ISimulationNode Parent
Parent node in the simulation model.
void Add(IFilter Filter)
Adds a filter to the bucket.
Helps with common XML-related tasks.
static string Attribute(XmlElement E, string Name)
Gets the value of an XML attribute.
Static class managing the application event log. Applications and services log events on this static ...
static void Notice(string Message, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, string StackTrace, params KeyValuePair< string, object >[] Tags)
Logs a notice event.
Basic interface for simulator nodes. Implementing this interface allows classes with default contruct...
Interface for sample filters
void Append(IFilter Filter)
Appends a filter to the current filter.