pddl_core/parsers/durative/
da_def.rs1use crate::parsers::error::R;
4use crate::parsers::sexpr::Node;
5use crate::parsers::{
6 empty_or, keyword_arg, opt_keyword_arg, parse_da_effect, parse_da_gd, parse_da_symbol,
7 parse_duration_constraint, prefix, typed_variables,
8};
9use crate::types::DurativeActionDefinition;
10
11pub(crate) fn parse_da_def(node: &Node) -> R<DurativeActionDefinition> {
16 prefix(node, ":durative-action", |args| {
17 let symbol = parse_da_symbol(args.next()?)?;
18 let parameters = keyword_arg(args, ":parameters", typed_variables)?;
19 let duration = keyword_arg(args, ":duration", parse_duration_constraint)?;
20 let condition = opt_keyword_arg(args, ":condition", |n| empty_or(n, parse_da_gd))?;
21 let effect = keyword_arg(args, ":effect", |n| empty_or(n, parse_da_effect))?;
22 Ok(DurativeActionDefinition::new(
23 symbol,
24 parameters,
25 duration,
26 condition.flatten(),
27 effect,
28 ))
29 })
30}
31
32crate::parsers::impl_parser!(DurativeActionDefinition => parse_da_def);
33
34#[cfg(test)]
35mod tests {
36 use assert2::check;
37
38 use crate::*;
39
40 #[test]
41 fn it_works() {
42 let input = r"(:durative-action move
43 :parameters
44 (?r - rover
45 ?fromwp - waypoint
46 ?towp - waypoint)
47
48 :duration
49 (= ?duration 5)
50
51 :condition
52 (and
53 (at start (rover ?rover))
54 (at start (waypoint ?from-waypoint))
55 (at start (waypoint ?to-waypoint))
56 (over all (can-move ?from-waypoint ?to-waypoint))
57 (at start (at ?rover ?from-waypoint))
58 (at start (> (battery-amount ?rover) 8)))
59
60 :effect
61 (and
62 (decrease (fuel-level ?t) (* 2 #t))
63 (at end (at ?rover ?to-waypoint))
64 (at end (been-at ?rover ?to-waypoint))
65 (at start (not (at ?rover ?from-waypoint)))
66 (at start (decrease (battery-amount ?rover) 8))
67 (at end (increase (distance-travelled) 5))
68 )
69 )";
70 let _gd = DurativeActionDefinition::from_str(input).unwrap();
71 }
72
73 #[test]
74 fn doc_example_0() {
75 let input = r"(:durative-action move
76 :parameters
77 (?r - rover
78 ?fromwp - waypoint
79 ?towp - waypoint)
80
81 :duration
82 (= ?duration 5)
83
84 :condition
85 (and
86 (at start (rover ?rover))
87 (at start (waypoint ?from-waypoint))
88 (at start (waypoint ?to-waypoint))
89 (over all (can-move ?from-waypoint ?to-waypoint))
90 (at start (at ?rover ?from-waypoint))
91 (at start (> (battery-amount ?rover) 8)))
92
93 :effect
94 (and
95 (decrease (fuel-level ?t) (* 2 #t))
96 (at end (at ?rover ?to-waypoint))
97 (at end (been-at ?rover ?to-waypoint))
98 (at start (not (at ?rover ?from-waypoint)))
99 (at start (decrease (battery-amount ?rover) 8))
100 (at end (increase (distance-travelled) 5))
101 )
102 )";
103
104 let da_def = DurativeActionDefinition::from_str(input).unwrap();
105 check!(da_def.symbol() == &"move".into());
106 check!(da_def.parameters().len() == 3);
107 check!(da_def.duration().is_some());
108 check!(da_def.condition().is_some());
109 check!(da_def.effect().is_some());
110 }
111
112 #[test]
113 fn the_condition_may_be_left_out() {
114 let action = DurativeActionDefinition::from_str(
115 "(:durative-action wait :parameters () :duration (= ?duration 1) :effect (at end (p)))",
116 )
117 .unwrap();
118 check!(action.condition().is_none());
119 check!(action.effect().is_some());
120 check!(DurativeActionDefinition::from_str(
121 "(:durative-action wait :parameters () :duration (= ?duration 1) :condition ())"
122 )
123 .is_err());
124 }
125}