Class TrendClassificationReactor
Reactor that detects a decreasing trend (downward drift)
in numeric channel values using a sliding window and a slope-based metric.
Trend definition (slope-based)
For each channel, this classifier maintains the last windowSize numeric samples:
y0, y1, ..., y(n-1) with n = windowSize
It computes the slope b of the best-fit line y = a + b*x
using ordinary least squares, where x is the sample index 0..n-1:
b = Σ((xi - x̄)(yi - ȳ)) / Σ((xi - x̄)^2)
The slope is measured in "value units per sample". For SpO₂, that is "% per sample".
Directional warning condition
This classifier is configured to warn on decreasing trends only. A warning is raised when:
slope <= -delta
where delta is a non-negative threshold.
Persistence / Debouncing
To reduce false positives due to transient artifacts, the classifier optionally requires the
decreasing condition to hold for persistWindows consecutive evaluated windows before it emits a warning.
By default, persistWindows = 2.
Once in the "warning" state for a channel, this implementation emits a warning only on state entry, and resets once the condition is no longer met.
Inputs and outputs
- Input:
reactionFunction(Map)is invoked with a snapshotchannel -> value. - Output: A warning message is published to each configured output channel and also logged.
Assumptions: snapshot values are numeric (Number). Non-numeric values throw
ClassCastException.
-
Nested Class Summary
Nested classes/interfaces inherited from class com.framed.arn.Reactor
Reactor.Snapshot -
Field Summary
Fields inherited from class com.framed.arn.Reactor
addressGroup, id, inputChannels, lastLogicalFireTs, outputChannelsFields inherited from class com.framed.core.Service
ADDRESS_REGISTRY_SUFFIX, eventBus, formatter, logger -
Constructor Summary
ConstructorsConstructorDescriptionTrendClassificationReactor(EventBus eventBus, String id, org.json.JSONArray firingRules, String inputChannel, org.json.JSONArray outputChannels, int windowSize, int persistWindows, int delta, String direction, boolean atomic) Constructs aTrendClassifierthat warns on decreasing trends using regression slope, with configurable persistence. -
Method Summary
Modifier and TypeMethodDescriptionvoidreactionFunction(Map<String, Object> latestSnapshot) Updates per-channel windows with the newest snapshot values, computes the regression slope when enough samples exist, and emits warnings for channels exhibiting a persistent decreasing trend.Methods inherited from class com.framed.arn.Reactor
getInputChannels, getOutputChannels, valueMatchesExpectedMethods inherited from class com.framed.core.Service
addressRegistry, announceAddress, stop
-
Constructor Details
-
TrendClassificationReactor
public TrendClassificationReactor(EventBus eventBus, String id, org.json.JSONArray firingRules, String inputChannel, org.json.JSONArray outputChannels, int windowSize, int persistWindows, int delta, String direction, boolean atomic) Constructs aTrendClassifierthat warns on decreasing trends using regression slope, with configurable persistence.- Parameters:
eventBus- the event bus used by this actor; must not benullid- identifier of this classifier (often set in configuration); must not benullfiringRules- firing rules as accepted byReactor; must not benullinputChannel- channels observed by this classifier; must not benulloutputChannels- channels to publish warning messages to; must not benullwindowSize- number of recent samples per channel used for trend detection; must be>= 2persistWindows- required number of consecutive satisfied windows; must be>= 1delta- non-negative threshold per Channel; warning if slope<= -delta- Throws:
NullPointerException- if any required argument isnullIllegalArgumentException- ifwindowSize < 2,delta < 0, orpersistWindows < 1
-
-
Method Details
-
reactionFunction
Updates per-channel windows with the newest snapshot values, computes the regression slope when enough samples exist, and emits warnings for channels exhibiting a persistent decreasing trend.Behavior per channel:
- If the channel is missing in the snapshot, it is skipped.
- The numeric value is appended to the channel window (oldest removed if full).
- Once the window is full, compute regression slope over the window.
- If slope
<= -delta, increment the persistence counter; otherwise reset it. - If the counter reaches
persistWindows, enter warning state and emit a warning once. - If slope no longer meets the condition, exit warning state.
- Specified by:
reactionFunctionin classReactor- Parameters:
latestSnapshot- snapshot mapchannel -> value; values must beNumber- Throws:
ClassCastException- if any encountered snapshot value is not numeric
-
