Skip to main content

pddl_core/parsers/structure/
goal_def.rs

1//! Provides parsers for problem goal descriptions (`<goal>`).
2
3use crate::parsers::error::R;
4use crate::parsers::sexpr::Node;
5use crate::parsers::{parse_pre_gd, prefix};
6use crate::types::ProblemGoalDescription;
7
8/// Parses a problem goal description (`<goal>`), i.e. `(:goal <pre-GD>)`.
9pub(crate) fn parse_problem_goal_def(node: &Node) -> R<ProblemGoalDescription> {
10    prefix(node, ":goal", |args| {
11        parse_pre_gd(args.next()?).map(ProblemGoalDescription::new)
12    })
13}
14
15crate::parsers::impl_parser!(ProblemGoalDescription => parse_problem_goal_def);
16
17#[cfg(test)]
18mod tests {
19    use crate::parsers::UnwrapValue;
20    use assert2::check;
21
22    use crate::*;
23
24    #[test]
25    fn test_parse() {
26        let input = "(:goal (= x y))";
27        check!(
28            ProblemGoalDescription::from_str(input).is_value(ProblemGoalDescription::from(
29                PreconditionGoalDescription::Preference(PreferenceGoalDescription::Goal(
30                    GoalDescription::AtomicFormula(AtomicFormula::new_equality(
31                        Term::Name("x".into()),
32                        Term::Name("y".into())
33                    ))
34                ))
35            ))
36        );
37    }
38
39    #[test]
40    fn doc_example_0() {
41        let input = "(:goal (= x y))";
42        check!(
43            ProblemGoalDescription::from_str(input).is_value(ProblemGoalDescription::from(
44                PreconditionGoalDescription::Preference(PreferenceGoalDescription::Goal(
45                    GoalDescription::AtomicFormula(AtomicFormula::new_equality(
46                        Term::Name("x".into()),
47                        Term::Name("y".into())
48                    ))
49                ))
50            ))
51        );
52    }
53}