Skip to main content

varpulis_sase/
lib.rs

1//! SASE+ Pattern Matching Engine
2//!
3//! Implementation of the SASE+ algorithm for Complex Event Processing.
4//! Based on the paper: "High-Performance Complex Event Processing over Streams"
5//! by Wu, Diao, Rizvi (SIGMOD 2006)
6//!
7//! Key features:
8//! - NFA-based pattern matching with stack for Kleene closure
9//! - Efficient event selection strategies (skip-till-any-match, skip-till-next-match)
10//! - Negation support with temporal windows
11//! - Partition-by attribute optimization (SASEXT extension)
12//!
13//! Pattern syntax supported:
14//! - SEQ(A, B, C): Sequence of events
15//! - AND(A, B): Both events in any order
16//! - OR(A, B): Either event
17//! - NOT(A): Negation (absence of event)
18//! - A+: Kleene plus (one or more)
19//! - A*: Kleene star (zero or more)
20//! - WITHIN(pattern, duration): Temporal constraint
21
22// Module declarations
23mod advance;
24mod and_op;
25mod backpressure;
26mod builder;
27pub mod clock;
28mod engine;
29mod enumeration;
30mod event_index;
31mod event_time;
32mod kleene;
33mod metrics;
34mod negation;
35mod nfa;
36pub(crate) mod predicate;
37mod run;
38mod types;
39
40// Re-export all public types to maintain API compatibility
41pub use and_op::{AndBranch, AndConfig, AndState, NegationInfo};
42pub use backpressure::{
43    BackpressureError, BackpressureStrategy, ProcessResult, ProcessStats, ProcessWarning,
44    SaseExtendedStats,
45};
46pub use builder::PatternBuilder;
47pub use clock::Timestamp;
48pub use engine::SaseEngine;
49pub use event_index::EventTypeIndex;
50pub use event_time::{EventTimeConfig, EventTimeManager, EventTimeResult};
51pub use kleene::KleeneCapture;
52pub use metrics::{LatencyHistogram, MetricsSummary, SaseMetrics};
53pub use negation::NegationConstraint;
54pub use nfa::{Nfa, NfaCompiler, State, StateType};
55pub use predicate::{classify_predicate, PredicateClass};
56pub use run::Run;
57// ---------------------------------------------------------------------------
58// Expression evaluator trait (abstraction for engine dependency)
59// ---------------------------------------------------------------------------
60use rustc_hash::FxHashMap;
61pub use types::{
62    CompareOp, GlobalNegation, MatchResult, Predicate, RunSnapshot, SasePattern, SaseStats,
63    SelectionStrategy, SharedEvent, StackEntry, TimeSemantics, MAX_ENUMERATION_RESULTS,
64    MAX_KLEENE_EVENTS,
65};
66use varpulis_core::{Event, Value};
67
68/// Context for evaluating sequence expressions within SASE predicates.
69///
70/// This provides captured events from prior sequence steps so that
71/// cross-event comparisons (e.g., `a.price < b.price`) can be evaluated.
72#[derive(Debug, Clone, Default)]
73pub struct SequenceContext {
74    /// Captured events by alias
75    pub captured: FxHashMap<String, Event>,
76    /// Previous event in sequence (accessible as `$`)
77    pub previous: Option<Event>,
78}
79
80/// Trait for evaluating custom expressions in SASE predicates.
81///
82/// Implementations bridge the SASE engine to a host expression evaluator
83/// (e.g., the VPL runtime engine's `eval_filter_expr`).
84pub trait ExprEvaluator: Send + Sync {
85    /// Evaluate an expression against an event and captured context.
86    ///
87    /// Returns `Some(Value)` on success, `None` if the expression cannot be evaluated.
88    fn eval(
89        &self,
90        expr: &varpulis_core::ast::Expr,
91        event: &Event,
92        captured: &FxHashMap<String, SharedEvent>,
93    ) -> Option<Value>;
94}