Skip to main content

pddl_core/parsers/structure/
structure_def.rs

1//! Provides parsers for structure definitions (`<structure-def>`).
2
3use crate::parsers::error::R;
4use crate::parsers::sexpr::Node;
5use crate::parsers::{
6    alt, parse_action_def, parse_da_def, parse_derived_predicate, parse_event_def,
7    parse_process_def,
8};
9use crate::types::StructureDef;
10
11/// Parses a structure definition (`<structure-def>`), i.e. `<action-def> | <durative-action-def> | <derived-def>`,
12/// or a PDDL+ `<process-def> | <event-def>`.
13pub(crate) fn parse_structure_def(node: &Node) -> R<StructureDef> {
14    alt(
15        node,
16        &[
17            // :derived-predicates
18            &|n| parse_derived_predicate(n).map(StructureDef::new_derived),
19            &|n| parse_action_def(n).map(StructureDef::new_action),
20            // :durative-actions
21            &|n| parse_da_def(n).map(StructureDef::new_durative_action),
22            // PDDL+
23            &|n| parse_process_def(n).map(StructureDef::process),
24            &|n| parse_event_def(n).map(StructureDef::event),
25        ],
26    )
27}
28
29crate::parsers::impl_parser!(StructureDef => parse_structure_def);
30
31#[cfg(test)]
32mod tests {
33    use crate::parsers::UnwrapValue;
34    use assert2::check;
35
36    use crate::*;
37
38    #[test]
39    fn test_parse() {
40        let input = r"(:action take-out
41                             :parameters (?x - physob)
42                             :precondition (not (= ?x B))
43                             :effect (not (in ?x))
44                         )";
45
46        let action = StructureDef::from_str(input);
47
48        check!(action.is_value(StructureDef::action(ActionDefinition::new(
49            ActionSymbol::from("take-out"),
50            TypedList::from_iter([Variable::from("x").to_typed("physob")]),
51            PreconditionGoalDescriptions::preference(PreferenceGoalDescription::from_gd(
52                GoalDescription::not(GoalDescription::atomic_formula(AtomicFormula::equality(
53                    Term::Variable(Variable::from("x")),
54                    Term::Name(Name::new("B"))
55                )))
56            )),
57            Some(Effects::new(ConditionalEffect::new_primitive_effect(
58                PrimitiveEffect::NotAtomicFormula(AtomicFormula::predicate(
59                    Predicate::from("in"),
60                    vec![Term::Variable(Variable::from("x"))]
61                ))
62            )))
63        ))));
64    }
65
66    #[test]
67    fn doc_example_0() {
68        let input = r"(:action take-out
69                            :parameters (?x - physob)
70                            :precondition (not (= ?x B))
71                            :effect (not (in ?x))
72                        )";
73
74        let action = StructureDef::from_str(input);
75
76        check!(action.is_value(StructureDef::action(ActionDefinition::new(
77            ActionSymbol::from("take-out"),
78            TypedList::from_iter([Variable::from("x").to_typed("physob")]),
79            PreconditionGoalDescriptions::preference(PreferenceGoalDescription::from_gd(
80                GoalDescription::not(GoalDescription::atomic_formula(AtomicFormula::equality(
81                    Term::Variable(Variable::from("x")),
82                    Term::Name(Name::new("B"))
83                )))
84            )),
85            Some(Effects::new(ConditionalEffect::new_primitive_effect(
86                PrimitiveEffect::NotAtomicFormula(AtomicFormula::predicate(
87                    Predicate::from("in"),
88                    vec![Term::Variable(Variable::from("x"))]
89                ))
90            )))
91        ))));
92    }
93}