Skip to main content

predicate/
predicate.rs

1//! Demonstrates simple and composite predicates with the `RESTRICT` operation.
2
3use darwen::prelude::{Predicate, Relation, RelationBuilder, Scalar, ScalarType};
4use darwen::{heading, tuple, AttributeName};
5
6fn users() -> Relation {
7    RelationBuilder::new()
8        .with_heading(
9            heading!(
10                name = ScalarType::String,
11                age = ScalarType::Integer,
12                city = ScalarType::String,
13                home_city = ScalarType::String
14            )
15            .unwrap(),
16        )
17        .with_body(vec![
18            tuple!(
19                name = "Monica",
20                age = 18,
21                city = "Sochi",
22                home_city = "Sochi"
23            )
24            .unwrap(),
25            tuple!(
26                name = "Erica",
27                age = 19,
28                city = "Tbilisi",
29                home_city = "Paris"
30            )
31            .unwrap(),
32            tuple!(
33                name = "Rita",
34                age = 20,
35                city = "Berlin",
36                home_city = "Berlin"
37            )
38            .unwrap(),
39            tuple!(
40                name = "Tina",
41                age = 21,
42                city = "Tbilisi",
43                home_city = "Tbilisi"
44            )
45            .unwrap(),
46            tuple!(
47                name = "Sandra",
48                age = 22,
49                city = "Paris",
50                home_city = "Berlin"
51            )
52            .unwrap(),
53            tuple!(
54                name = "Mary",
55                age = 23,
56                city = "Berlin",
57                home_city = "Berlin"
58            )
59            .unwrap(),
60        ])
61        .build()
62        .unwrap()
63}
64
65fn show(title: &str, relation: &Relation, predicate: Predicate) {
66    println!("{title}");
67    println!("{}", relation.restrict(&predicate).unwrap());
68}
69
70fn main() {
71    let users = users();
72
73    println!("Source relation");
74    println!("{users}");
75
76    show(
77        "Predicate::eq(city, \"Berlin\")",
78        &users,
79        Predicate::eq(AttributeName::from("city"), Scalar::from("Berlin")),
80    );
81
82    show(
83        "Predicate::eq(city, home_city)",
84        &users,
85        Predicate::eq(
86            AttributeName::from("city"),
87            AttributeName::from("home_city"),
88        ),
89    );
90
91    show(
92        "Predicate::gt(age, 20)",
93        &users,
94        Predicate::gt(AttributeName::from("age"), Scalar::from(20_i64)),
95    );
96
97    show(
98        "Predicate::lt(age, 21)",
99        &users,
100        Predicate::lt(AttributeName::from("age"), Scalar::from(21_i64)),
101    );
102
103    show(
104        "Predicate::and(gt(age, 20), eq(city, \"Berlin\"))",
105        &users,
106        Predicate::and(
107            Predicate::gt(AttributeName::from("age"), Scalar::from(20_i64)),
108            Predicate::eq(AttributeName::from("city"), Scalar::from("Berlin")),
109        ),
110    );
111
112    show(
113        "Predicate::or(eq(city, \"Berlin\"), eq(city, \"Paris\"))",
114        &users,
115        Predicate::or(
116            Predicate::eq(AttributeName::from("city"), Scalar::from("Berlin")),
117            Predicate::eq(AttributeName::from("city"), Scalar::from("Paris")),
118        ),
119    );
120
121    show(
122        "Predicate::not(eq(city, \"Tbilisi\"))",
123        &users,
124        Predicate::not(Predicate::eq(
125            AttributeName::from("city"),
126            Scalar::from("Tbilisi"),
127        )),
128    );
129}