Class Detector.Builder

java.lang.Object
io.github.romanhaendler.rbed.Detector.Builder
Enclosing class:
Detector

public static final class Detector.Builder extends Object
Builds a Detector.
  • Method Details

    • rules

      public Detector.Builder rules(Rule... rules)
      Adds Rules, evaluated in the order they are added. A Rule may only refer to Rules added before it.
    • watch

      public Detector.Builder watch(Expression... expressions)
      Registers Expressions whose values the built Detector reports through Detector.value(io.github.romanhaendler.rbed.Expression). They are bound after all Rules and need not be used by any Rule; their Channels count as used. May be called more than once; watching an Expression twice is harmless.

      Watching adds no allocation per Sample. Each moving average in a watched Expression keeps a window of its own, even if a Rule uses the same instance: its entries take memory, count in Detector.retainedValues() and against the Retention Limit, and each push updates it. Everything else in a watched Expression adds no work per push. Watching latest(r).min/max/mean(c) makes Rule r aggregate Channel c, so r's Events gain Aggregates of c; which Events are found, and when, is unchanged.

    • restore

      public Detector.Builder restore(byte[] snapshot)
      Makes build() produce a Detector in the state of a Detector.snapshot() instead of a fresh one, ready for the next Sample. The Rules, the sampling rate, the Retention Limit and the watched Expressions of this Builder must be those of the Detector that took the Snapshot. The listener is not part of a Snapshot: set it on this Builder.

      build() throws IllegalArgumentException, with a message naming the cause, if the array is not a Snapshot, is corrupted or truncated, has a format version this rbed does not read, was taken with other Rules (also a renamed or changed Rule) or other options (sampling rate, Retention Limit, watched Expressions), or if history was also called (a Snapshot already contains the History that was known).

      Throws:
      NullPointerException - if snapshot is null
    • listener

      public Detector.Builder listener(EventListener listener)
    • samplingRate

      public Detector.Builder samplingRate(double hz)
      Declares a regularly sampled series of hz Samples per second (at most 1000, the millisecond resolution of timestamps), so Samples are pushed without timestamps.
    • retentionLimit

      public Detector.Builder retentionLimit(int n)
      Sets the Retention Limit: the maximum number of entries a single averageOverDuration window keeps (default 1,000,000), and the maximum number of timestamps kept per counted Rule for count(...).within(...). When a full window or timestamp buffer gets a new entry, its oldest entry is dropped, and the Detector logs one warning through System.Logger. See Detector.droppedValues(). build() rejects a Rule or watched Expression using averageOverSamples(n) with n above the limit.
      Throws:
      IllegalArgumentException - if n is below 1
    • history

      public Detector.Builder history(Collection<Event> events)
      Supplies Events known from before this Detector started (e.g. stored before a restart), so that Conditions.after(io.github.romanhaendler.rbed.Rule) and After.unless see them. They must be Confirmed and Ended before the first Sample, and are not reported to the listener.
    • build

      public Detector build()
      Builds the Detector and binds every Rule's Conditions and every watched Expression to it.
      Throws:
      IllegalArgumentException - if a Rule references itself, a Rule added after it or a Rule not added, if a watched Expression references a Rule not added, or if History contains an Event of no added Rule. A Rule without when and duplicate Rule names are already rejected by rules.