Skip to main content

pddl_core/parsers/durative/
timed_gd.rs

1//! Provides parsers for timed goal descriptions (`<timed-GD>`).
2
3use crate::parsers::error::R;
4use crate::parsers::sexpr::Node;
5use crate::parsers::{alt, parse_gd, parse_interval, parse_time_specifier, prefix};
6use crate::types::TimedGoalDescription;
7
8/// Parses a timed goal description (`<timed-GD>`), i.e. `(at <time-specifier> <GD>) | (over <interval> <GD>)`.
9pub(crate) fn parse_timed_gd(node: &Node) -> R<TimedGoalDescription> {
10    alt(
11        node,
12        &[
13            &|n| {
14                prefix(n, "at", |args| {
15                    Ok((parse_time_specifier(args.next()?)?, parse_gd(args.next()?)?))
16                })
17                .map(TimedGoalDescription::from)
18            },
19            &|n| {
20                prefix(n, "over", |args| {
21                    Ok((parse_interval(args.next()?)?, parse_gd(args.next()?)?))
22                })
23                .map(TimedGoalDescription::from)
24            },
25        ],
26    )
27}
28
29crate::parsers::impl_parser!(TimedGoalDescription => parse_timed_gd);
30
31#[cfg(test)]
32mod tests {
33    use crate::parsers::UnwrapValue;
34    use assert2::check;
35
36    use crate::*;
37
38    #[test]
39    fn it_works() {
40        let input = "(over all (can-move ?from-waypoint ?to-waypoint))";
41        let _gd = TimedGoalDescription::from_str(input).unwrap();
42    }
43
44    #[test]
45    fn test_parse() {
46        check!(
47            TimedGoalDescription::from_str("(at start (= x y))").is_value(
48                TimedGoalDescription::at(
49                    TimeSpecifier::Start,
50                    GoalDescription::AtomicFormula(AtomicFormula::new_equality(
51                        Term::Name("x".into()),
52                        Term::Name("y".into())
53                    ))
54                )
55            )
56        );
57
58        check!(
59            TimedGoalDescription::from_str("(over all (= x y))").is_value(
60                TimedGoalDescription::over(
61                    Interval::All,
62                    GoalDescription::AtomicFormula(AtomicFormula::new_equality(
63                        Term::Name("x".into()),
64                        Term::Name("y".into())
65                    ))
66                )
67            )
68        );
69    }
70
71    #[test]
72    fn doc_example_0() {
73        check!(
74            TimedGoalDescription::from_str("(at start (= x y))").is_value(
75                TimedGoalDescription::at(
76                    TimeSpecifier::Start,
77                    GoalDescription::AtomicFormula(AtomicFormula::new_equality(
78                        Term::Name("x".into()),
79                        Term::Name("y".into())
80                    ))
81                )
82            )
83        );
84
85        check!(
86            TimedGoalDescription::from_str("(over all (= x y))").is_value(
87                TimedGoalDescription::over(
88                    Interval::All,
89                    GoalDescription::AtomicFormula(AtomicFormula::new_equality(
90                        Term::Name("x".into()),
91                        Term::Name("y".into())
92                    ))
93                )
94            )
95        );
96    }
97}