Skip to main content

valid_time/
valid_time.rs

1//! Deterministic valid-time relations with snapshot and restriction queries.
2
3use relmath::{
4    UnaryRelation,
5    temporal::{Interval, ValidTimeRelation},
6};
7
8fn main() {
9    let assignments = ValidTimeRelation::from_facts([
10        (
11            ("alice", "review"),
12            Interval::new(1, 3).expect("expected valid interval"),
13        ),
14        (
15            ("alice", "review"),
16            Interval::new(3, 5).expect("expected valid interval"),
17        ),
18        (
19            ("bob", "approve"),
20            Interval::new(2, 4).expect("expected valid interval"),
21        ),
22    ]);
23
24    let alice = UnaryRelation::singleton("alice");
25    let fact_support = assignments.support();
26    let exact_support = assignments.to_binary_relation();
27    let audit_window = Interval::new(2, 4).expect("expected valid interval");
28    let active_at_three = assignments.snapshot_at(&3);
29    let audit_assignments = assignments.restrict_to(&audit_window);
30
31    assert_eq!(
32        assignments
33            .valid_time_of(&("alice", "review"))
34            .expect("expected support")
35            .to_vec(),
36        vec![Interval::new(1, 5).expect("expected valid interval")]
37    );
38    assert!(assignments.is_active_at(&("alice", "review"), &4));
39    assert!(!assignments.is_active_at(&("alice", "review"), &5));
40    assert_eq!(
41        active_at_three.to_vec(),
42        vec![("alice", "review"), ("bob", "approve")]
43    );
44    assert_eq!(
45        fact_support.to_vec(),
46        vec![("alice", "review"), ("bob", "approve")]
47    );
48    assert_eq!(
49        audit_assignments
50            .valid_time_of(&("alice", "review"))
51            .expect("expected support")
52            .to_vec(),
53        vec![Interval::new(2, 4).expect("expected valid interval")]
54    );
55    assert_eq!(
56        audit_assignments.to_binary_relation().to_vec(),
57        vec![("alice", "review"), ("bob", "approve")]
58    );
59    assert_eq!(exact_support.image(&alice).to_vec(), vec!["review"]);
60
61    println!(
62        "fact support: {:?}; active at t=3: {:?}; audit restriction: {:?}",
63        fact_support.to_vec(),
64        active_at_three.to_vec(),
65        audit_assignments
66            .valid_time_of(&("alice", "review"))
67            .expect("expected support")
68            .to_vec()
69    );
70}