Skip to main content

pddl_core/parsers/durative/
da_effect.rs

1//! Provides parsers for durative action effects (`<da-effect>`).
2
3use crate::parsers::error::R;
4use crate::parsers::sexpr::Node;
5use crate::parsers::{alt, parse_da_gd, parse_timed_effect, prefix, typed_variables};
6use crate::types::DurativeActionEffect;
7
8/// Parses a durative action effect (`<da-effect>`), i.e. `<timed-effect> | (and <da-effect>*)
9/// | (forall (<typed list (variable)>) <da-effect>) | (when <da-GD> <timed-effect>)`.
10pub(crate) fn parse_da_effect(node: &Node) -> R<DurativeActionEffect> {
11    alt(
12        node,
13        &[
14            &|n| {
15                prefix(n, "and", |args| args.list0(parse_da_effect))
16                    .map(DurativeActionEffect::from_iter)
17            },
18            &|n| {
19                prefix(n, "forall", |args| {
20                    Ok((
21                        typed_variables(args.next()?)?,
22                        parse_da_effect(args.next()?)?,
23                    ))
24                })
25                .map(DurativeActionEffect::from)
26            },
27            &|n| {
28                prefix(n, "when", |args| {
29                    Ok((
30                        parse_da_gd(args.next()?)?,
31                        parse_timed_effect(args.next()?)?,
32                    ))
33                })
34                .map(DurativeActionEffect::from)
35            },
36            &|n| parse_timed_effect(n).map(DurativeActionEffect::from),
37        ],
38    )
39}
40
41crate::parsers::impl_parser!(DurativeActionEffect => parse_da_effect);
42
43#[cfg(test)]
44mod tests {
45    use crate::parsers::UnwrapValue;
46    use assert2::check;
47
48    use crate::*;
49
50    #[test]
51    fn simple_works() {
52        let input = r"(and
53                (decrease (fuel-level ?t) (* 2 #t))
54                (at end (at ?rover ?to-waypoint))
55                (at end (been-at ?rover ?to-waypoint))
56                (at start (not (at ?rover ?from-waypoint)))
57                (at start (decrease (battery-amount ?rover) 8))
58                (at end (increase (distance-travelled) 5))
59                )";
60
61        let _effect = DurativeActionEffect::from_str(input).unwrap();
62    }
63
64    #[test]
65    fn complex_works() {
66        let input = r"(and
67                (decrease (fuel-level ?t) (* 2 #t))
68                (at end (at ?rover ?to-waypoint))
69                (at end (been-at ?rover ?to-waypoint))
70                (at start (not (at ?rover ?from-waypoint)))
71                (at start (decrease (battery-amount ?rover) 8))
72                (at end (increase (distance-travelled) 5))
73                )";
74
75        let _effect = DurativeActionEffect::from_str(input).unwrap();
76    }
77
78    #[test]
79    fn test_at_start() {
80        check!(
81            DurativeActionEffect::from_str("(at start (= x y))").is_value(
82                DurativeActionEffect::Timed(TimedEffect::conditional(
83                    TimeSpecifier::Start,
84                    EffectCondition::new(PrimitiveEffect::AtomicFormula(AtomicFormula::Equality(
85                        EqualityAtomicFormula::new(Term::Name("x".into()), Term::Name("y".into()))
86                    )))
87                ))
88            )
89        );
90    }
91
92    #[test]
93    fn test_and_empty() {
94        check!(DurativeActionEffect::from_str("(and )").is_value(DurativeActionEffect::and([])));
95    }
96
97    #[test]
98    fn test_and() {
99        check!(
100            DurativeActionEffect::from_str("(and (at start (= x y)) (and ))").is_value(
101                DurativeActionEffect::and([
102                    DurativeActionEffect::Timed(TimedEffect::conditional(
103                        TimeSpecifier::Start,
104                        EffectCondition::new(PrimitiveEffect::AtomicFormula(
105                            AtomicFormula::Equality(EqualityAtomicFormula::new(
106                                Term::Name("x".into()),
107                                Term::Name("y".into())
108                            ))
109                        ))
110                    )),
111                    DurativeActionEffect::and([])
112                ])
113            )
114        );
115    }
116
117    #[test]
118    fn test_forall() {
119        check!(
120            DurativeActionEffect::from_str("(forall (?a ?b) (at start (= a b)))").is_value(
121                DurativeActionEffect::forall(
122                    TypedList::from_iter([
123                        Typed::object(Variable::new_string("a")),
124                        Typed::object(Variable::new_string("b")),
125                    ]),
126                    DurativeActionEffect::Timed(TimedEffect::conditional(
127                        TimeSpecifier::Start,
128                        EffectCondition::new(PrimitiveEffect::AtomicFormula(
129                            AtomicFormula::Equality(EqualityAtomicFormula::new(
130                                Term::Name("a".into()),
131                                Term::Name("b".into())
132                            ))
133                        ))
134                    ))
135                )
136            )
137        );
138    }
139
140    #[test]
141    fn doc_example_0() {
142        check!(
143            DurativeActionEffect::from_str("(at start (= x y))").is_value(
144                DurativeActionEffect::Timed(TimedEffect::conditional(
145                    TimeSpecifier::Start,
146                    EffectCondition::new(PrimitiveEffect::AtomicFormula(AtomicFormula::Equality(
147                        EqualityAtomicFormula::new(Term::Name("x".into()), Term::Name("y".into()))
148                    )))
149                ))
150            )
151        );
152
153        check!(DurativeActionEffect::from_str("(and )").is_value(DurativeActionEffect::new_and([])));
154
155        check!(
156            DurativeActionEffect::from_str("(and (at start (= x y)) (and ))").is_value(
157                DurativeActionEffect::new_and([
158                    DurativeActionEffect::Timed(TimedEffect::conditional(
159                        TimeSpecifier::Start,
160                        EffectCondition::new(PrimitiveEffect::AtomicFormula(
161                            AtomicFormula::Equality(EqualityAtomicFormula::new(
162                                Term::Name("x".into()),
163                                Term::Name("y".into())
164                            ))
165                        ))
166                    )),
167                    DurativeActionEffect::new_and([])
168                ])
169            )
170        );
171
172        check!(
173            DurativeActionEffect::from_str("(forall (?a ?b) (at start (= a b)))").is_value(
174                DurativeActionEffect::new_forall(
175                    TypedList::from_iter([
176                        Typed::object(Variable::new_string("a")),
177                        Typed::object(Variable::new_string("b")),
178                    ]),
179                    DurativeActionEffect::Timed(TimedEffect::conditional(
180                        TimeSpecifier::Start,
181                        EffectCondition::new(PrimitiveEffect::AtomicFormula(
182                            AtomicFormula::Equality(EqualityAtomicFormula::new(
183                                Term::Name("a".into()),
184                                Term::Name("b".into())
185                            ))
186                        ))
187                    ))
188                )
189            )
190        );
191    }
192}