fabula 0.1.0

Incremental pattern matching over temporal graphs — core library
Documentation
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
//! Ergonomic builder API for constructing patterns.
//!
//! Provides a fluent interface for building [`Pattern`] structs with stages,
//! temporal constraints, and negation windows. See [`crate::pattern`] for the
//! underlying types and their research lineage.
//!
//! ```rust
//! use fabula::builder::PatternBuilder;
//!
//! let pattern = PatternBuilder::<String, String>::new("betrayal_after_failure")
//!     .stage("event1", |s| s
//!         .edge("event1", "type".to_string(), "institutional_failure".to_string())
//!         .edge_bind("event1", "target".to_string(), "character"))
//!     .stage("event2", |s| s
//!         .edge("event2", "type".to_string(), "trust_violation".to_string())
//!         .edge_bind("event2", "target".to_string(), "character"))
//!     .unless_between("event1", "event2", |neg| neg
//!         .edge("recovery", "type".to_string(), "trust_restored".to_string())
//!         .edge_bind("recovery", "target".to_string(), "character"))
//!     .build();
//! ```

use crate::interval::AllenRelation;
use crate::pattern::*;
use std::collections::HashMap;

/// Builder for constructing a [`Pattern`].
pub struct PatternBuilder<L, V> {
    name: String,
    stages: Vec<Stage<L, V>>,
    temporal: Vec<TemporalConstraint>,
    negations: Vec<Negation<L, V>>,
    metadata: HashMap<String, String>,
    deadline_ticks: Option<u64>,
    unordered_groups: Vec<Vec<usize>>,
    private: bool,
}

impl<L: Clone, V: Clone> PatternBuilder<L, V> {
    /// Start building a new pattern with the given name.
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            stages: Vec::new(),
            temporal: Vec::new(),
            negations: Vec::new(),
            metadata: HashMap::new(),
            deadline_ticks: None,
            unordered_groups: Vec::new(),
            private: false,
        }
    }

    /// Add an event stage. The `anchor` names the event variable.
    /// Use the callback to add clauses.
    pub fn stage(
        mut self,
        anchor: impl Into<String>,
        build: impl FnOnce(StageBuilder<L, V>) -> StageBuilder<L, V>,
    ) -> Self {
        let builder = StageBuilder::new(anchor.into());
        let builder = build(builder);
        self.stages.push(builder.build());
        self
    }

    /// Add an explicit temporal constraint (beyond implicit stage ordering).
    pub fn temporal(
        mut self,
        left: impl Into<String>,
        relation: AllenRelation,
        right: impl Into<String>,
    ) -> Self {
        self.temporal.push(TemporalConstraint {
            left: Var::new(left),
            relation,
            right: Var::new(right),
            gap: None,
        });
        self
    }

    /// Add a temporal constraint with a metric gap bound (STN-style).
    pub fn temporal_with_gap(
        mut self,
        left: impl Into<String>,
        relation: AllenRelation,
        right: impl Into<String>,
        gap: MetricGap,
    ) -> Self {
        self.temporal.push(TemporalConstraint {
            left: Var::new(left),
            relation,
            right: Var::new(right),
            gap: Some(gap),
        });
        self
    }

    /// Add a negation window: these clauses must NOT match between two events.
    pub fn unless_between(
        mut self,
        start: impl Into<String>,
        end: impl Into<String>,
        build: impl FnOnce(NegationBuilder<L, V>) -> NegationBuilder<L, V>,
    ) -> Self {
        let builder = NegationBuilder::new(start.into(), Some(end.into()));
        let builder = build(builder);
        self.negations.push(builder.build());
        self
    }

    /// Add a negation window with an open end (up to "now").
    pub fn unless_after(
        mut self,
        start: impl Into<String>,
        build: impl FnOnce(NegationBuilder<L, V>) -> NegationBuilder<L, V>,
    ) -> Self {
        let builder = NegationBuilder::new(start.into(), None);
        let builder = build(builder);
        self.negations.push(builder.build());
        self
    }

    /// Set a deadline in engine ticks for partial match expiry.
    pub fn deadline(mut self, ticks: u64) -> Self {
        self.deadline_ticks = Some(ticks);
        self
    }

    /// Mark this pattern as private — the engine evaluates it internally but
    /// suppresses its matches and events from output.
    pub fn private(mut self) -> Self {
        self.private = true;
        self
    }

    /// Attach a metadata key-value pair to the pattern.
    pub fn metadata(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.metadata.insert(key.into(), value.into());
        self
    }

    /// Add a negation that spans the entire pattern (first stage to last stage).
    /// Equivalent to Winnow's `(unless-event ... where ...)` with no `between`.
    pub fn unless_global(
        mut self,
        build: impl FnOnce(NegationBuilder<L, V>) -> NegationBuilder<L, V>,
    ) -> Self {
        // Will be resolved to first/last stage anchors at build time
        let builder = NegationBuilder::new(String::new(), None);
        let builder = build(builder);
        let mut neg = builder.build();
        neg.is_global = true;
        self.negations.push(neg);
        self
    }

    /// Add a group of stages that may match in any order (concurrent).
    ///
    /// Use the callback to add stages — they are appended to the pattern
    /// and their indices are recorded as an unordered group. The engine
    /// will try all unmatched stages in the group against each incoming
    /// edge and advance past the group when all are matched.
    ///
    /// ```rust,ignore
    /// let pattern = PatternBuilder::<String, String>::new("concurrent_events")
    ///     .stage("setup", |s| s.edge("setup", "type".into(), "start".into()))
    ///     .unordered_group(|b| b
    ///         .stage("a", |s| s.edge("a", "type".into(), "event_a".into()))
    ///         .stage("b", |s| s.edge("b", "type".into(), "event_b".into())))
    ///     .stage("conclusion", |s| s.edge("conclusion", "type".into(), "end".into()))
    ///     .build();
    /// ```
    pub fn unordered_group(
        mut self,
        build: impl FnOnce(UnorderedGroupBuilder<L, V>) -> UnorderedGroupBuilder<L, V>,
    ) -> Self {
        let start_idx = self.stages.len();
        let group_builder = UnorderedGroupBuilder::new();
        let group_builder = build(group_builder);
        let stages = group_builder.stages;
        let count = stages.len();
        debug_assert!(count >= 2, "unordered group should have at least 2 stages");
        self.stages.extend(stages);
        if count > 0 {
            let end_idx = start_idx + count - 1;
            debug_assert!(
                end_idx < 64,
                "unordered group stage indices must be < 64 (matched_stages is u64)"
            );
            let indices: Vec<usize> = (start_idx..start_idx + count).collect();
            self.unordered_groups.push(indices);
        }
        self
    }

    /// Build the pattern.
    pub fn build(mut self) -> Pattern<L, V> {
        // Resolve global negation bounds to first/last stage anchors
        let first_anchor = self.stages.first().map(|s| s.anchor.0.clone());
        let last_anchor = self.stages.last().map(|s| s.anchor.0.clone());
        for neg in &mut self.negations {
            if neg.is_global {
                if let Some(ref first) = first_anchor {
                    neg.between_start = Var::new(first.clone());
                    // B7 fix: if single stage, use open-ended window (None)
                    // instead of same anchor which creates zero-width window
                    neg.between_end = match &last_anchor {
                        Some(last) if last != first => Some(Var::new(last.clone())),
                        _ => None, // single stage or no stages → open-ended
                    };
                }
                neg.is_global = false; // B5b: always clear, even if no stages
            }
        }
        Pattern {
            name: self.name,
            stages: self.stages,
            temporal: self.temporal,
            negations: self.negations,
            group: None,
            metadata: self.metadata,
            deadline_ticks: self.deadline_ticks,
            repeat_range: None,
            unordered_groups: self.unordered_groups,
            private: self.private,
        }
    }
}

/// Builder for a single event stage within a pattern.
pub struct StageBuilder<L, V> {
    anchor: String,
    clauses: Vec<Clause<L, V>>,
}

impl<L: Clone, V: Clone> StageBuilder<L, V> {
    fn new(anchor: String) -> Self {
        Self {
            anchor,
            clauses: Vec::new(),
        }
    }

    /// Add a clause: `source --[label]--> literal_value`.
    pub fn edge(mut self, source: impl Into<String>, label: L, value: V) -> Self {
        self.clauses.push(Clause {
            source: Var::new(source),
            label,
            target: Target::Literal(value),
            negated: false,
        });
        self
    }

    /// Add a clause: `source --[label]--> ?bind_var` (traverse and bind).
    pub fn edge_bind(
        mut self,
        source: impl Into<String>,
        label: L,
        bind_to: impl Into<String>,
    ) -> Self {
        self.clauses.push(Clause {
            source: Var::new(source),
            label,
            target: Target::Bind(Var::new(bind_to)),
            negated: false,
        });
        self
    }

    /// Add a constrained clause: `source --[label]--> (constraint)`.
    pub fn edge_constrained(
        mut self,
        source: impl Into<String>,
        label: L,
        constraint: crate::datasource::ValueConstraint<V>,
    ) -> Self {
        self.clauses.push(Clause {
            source: Var::new(source),
            label,
            target: Target::Constraint(constraint),
            negated: false,
        });
        self
    }

    /// Add a clause comparing edge target against a bound variable: `source --[label]--> (== ?var)`.
    pub fn edge_eq_var(
        mut self,
        source: impl Into<String>,
        label: L,
        var_name: impl Into<String>,
    ) -> Self {
        self.clauses.push(Clause {
            source: Var::new(source),
            label,
            target: Target::Constraint(crate::datasource::ValueConstraint::EqVar(var_name.into())),
            negated: false,
        });
        self
    }

    /// Add a clause: `source --[label]--> (< ?var)`.
    pub fn edge_lt_var(
        mut self,
        source: impl Into<String>,
        label: L,
        var_name: impl Into<String>,
    ) -> Self {
        self.clauses.push(Clause {
            source: Var::new(source),
            label,
            target: Target::Constraint(crate::datasource::ValueConstraint::LtVar(var_name.into())),
            negated: false,
        });
        self
    }

    /// Add a clause: `source --[label]--> (> ?var)`.
    pub fn edge_gt_var(
        mut self,
        source: impl Into<String>,
        label: L,
        var_name: impl Into<String>,
    ) -> Self {
        self.clauses.push(Clause {
            source: Var::new(source),
            label,
            target: Target::Constraint(crate::datasource::ValueConstraint::GtVar(var_name.into())),
            negated: false,
        });
        self
    }

    /// Add a clause: `source --[label]--> (<= ?var)`.
    pub fn edge_lte_var(
        mut self,
        source: impl Into<String>,
        label: L,
        var_name: impl Into<String>,
    ) -> Self {
        self.clauses.push(Clause {
            source: Var::new(source),
            label,
            target: Target::Constraint(crate::datasource::ValueConstraint::LteVar(var_name.into())),
            negated: false,
        });
        self
    }

    /// Add a clause: `source --[label]--> (>= ?var)`.
    pub fn edge_gte_var(
        mut self,
        source: impl Into<String>,
        label: L,
        var_name: impl Into<String>,
    ) -> Self {
        self.clauses.push(Clause {
            source: Var::new(source),
            label,
            target: Target::Constraint(crate::datasource::ValueConstraint::GteVar(var_name.into())),
            negated: false,
        });
        self
    }

    /// Add a negated clause: the edge must NOT exist.
    pub fn not_edge(mut self, source: impl Into<String>, label: L, value: V) -> Self {
        self.clauses.push(Clause {
            source: Var::new(source),
            label,
            target: Target::Literal(value),
            negated: true,
        });
        self
    }

    fn build(self) -> Stage<L, V> {
        Stage {
            anchor: Var::new(self.anchor),
            clauses: self.clauses,
        }
    }
}

/// Builder for a negation window.
pub struct NegationBuilder<L, V> {
    between_start: String,
    between_end: Option<String>,
    clauses: Vec<Clause<L, V>>,
}

impl<L: Clone, V: Clone> NegationBuilder<L, V> {
    fn new(start: String, end: Option<String>) -> Self {
        Self {
            between_start: start,
            between_end: end,
            clauses: Vec::new(),
        }
    }

    /// Add a clause to the negation (edge that must NOT exist in the window).
    pub fn edge(mut self, source: impl Into<String>, label: L, value: V) -> Self {
        self.clauses.push(Clause {
            source: Var::new(source),
            label,
            target: Target::Literal(value),
            negated: false,
        });
        self
    }

    /// Add a binding clause to the negation.
    pub fn edge_bind(
        mut self,
        source: impl Into<String>,
        label: L,
        bind_to: impl Into<String>,
    ) -> Self {
        self.clauses.push(Clause {
            source: Var::new(source),
            label,
            target: Target::Bind(Var::new(bind_to)),
            negated: false,
        });
        self
    }

    /// Add a constrained clause to the negation.
    pub fn edge_constrained(
        mut self,
        source: impl Into<String>,
        label: L,
        constraint: crate::datasource::ValueConstraint<V>,
    ) -> Self {
        self.clauses.push(Clause {
            source: Var::new(source),
            label,
            target: Target::Constraint(constraint),
            negated: false,
        });
        self
    }

    fn build(self) -> Negation<L, V> {
        Negation {
            between_start: Var::new(self.between_start),
            between_end: self.between_end.map(Var::new),
            clauses: self.clauses,
            is_global: false,
        }
    }
}

/// Builder for an unordered (concurrent) group of stages within a pattern.
pub struct UnorderedGroupBuilder<L, V> {
    stages: Vec<Stage<L, V>>,
}

impl<L: Clone, V: Clone> UnorderedGroupBuilder<L, V> {
    fn new() -> Self {
        Self { stages: Vec::new() }
    }

    /// Add a stage to the unordered group.
    pub fn stage(
        mut self,
        anchor: impl Into<String>,
        build: impl FnOnce(StageBuilder<L, V>) -> StageBuilder<L, V>,
    ) -> Self {
        let builder = StageBuilder::new(anchor.into());
        let builder = build(builder);
        self.stages.push(builder.build());
        self
    }
}