Skip to main content

pddl_core/parsers/structure/
predicates_def.rs

1//! Provides parsers for predicate definitions (`<predicates-def>`).
2
3use crate::parsers::error::R;
4use crate::parsers::sexpr::Node;
5use crate::parsers::{parse_atomic_formula_skeleton, prefix};
6use crate::types::PredicateDefinitions;
7
8/// Parses a predicate definition (`<predicates-def>`), i.e. `(:predicates <atomic formula skeleton>⁺)`.
9pub(crate) fn parse_predicates_def(node: &Node) -> R<PredicateDefinitions> {
10    prefix(node, ":predicates", |args| {
11        args.list1(parse_atomic_formula_skeleton)
12            .map(PredicateDefinitions::new)
13    })
14}
15
16crate::parsers::impl_parser!(PredicateDefinitions => parse_predicates_def);
17
18#[cfg(test)]
19mod tests {
20    use crate::parsers::UnwrapValue;
21    use assert2::check;
22
23    use crate::*;
24
25    #[test]
26    fn test_parse() {
27        let input = r"(:predicates
28                             (at ?x - physob ?y - location)
29                             (in ?x ?y - physob)
30                        )";
31
32        check!(
33            PredicateDefinitions::from_str(input).is_value(PredicateDefinitions::new(vec![
34                AtomicFormulaSkeleton::new(
35                    Predicate::from("at"),
36                    TypedList::from_iter([
37                        Variable::from("x").to_typed("physob"),
38                        Variable::from("y").to_typed("location"),
39                    ])
40                ),
41                AtomicFormulaSkeleton::new(
42                    Predicate::from("in"),
43                    TypedList::from_iter([
44                        Variable::from("x").to_typed("physob"),
45                        Variable::from("y").to_typed("physob"),
46                    ])
47                )
48            ]))
49        );
50    }
51
52    #[test]
53    fn doc_example_0() {
54        let input = r"(:predicates
55                            (at ?x - physob ?y - location)
56                            (in ?x ?y - physob)
57                       )";
58
59        check!(
60            PredicateDefinitions::from_str(input).is_value(PredicateDefinitions::new(vec![
61                AtomicFormulaSkeleton::new(
62                    Predicate::from("at"),
63                    TypedList::from_iter([
64                        Variable::from("x").to_typed("physob"),
65                        Variable::from("y").to_typed("location"),
66                    ])
67                ),
68                AtomicFormulaSkeleton::new(
69                    Predicate::from("in"),
70                    TypedList::from_iter([
71                        Variable::from("x").to_typed("physob"),
72                        Variable::from("y").to_typed("physob"),
73                    ])
74                )
75            ]))
76        );
77    }
78}