pddl_core/pretty_print/
action.rs1use std::ops::Deref;
5
6use crate::pretty_print::{sealed, PrettyRenderer};
7use crate::types::{
8 ActionDefinition, ActionSymbol, DerivedPredicate, DurativeActionDefinition,
9 PreconditionGoalDescriptions, ProcessDefinition, StructureDef, StructureDefs, TypedVariables,
10};
11use crate::visitor::{Accept, Visitor};
12use pretty::RcDoc;
13
14impl sealed::Sealed for ActionDefinition {}
15impl sealed::Sealed for DurativeActionDefinition {}
16impl sealed::Sealed for DerivedPredicate {}
17impl sealed::Sealed for ProcessDefinition {}
18impl sealed::Sealed for StructureDef {}
19impl sealed::Sealed for StructureDefs {}
20
21impl Visitor<ActionDefinition, RcDoc<'static>> for PrettyRenderer {
22 fn visit(&self, value: &ActionDefinition) -> RcDoc<'static> {
23 let effect = value.effect().as_ref().map(|e| e.accept(self));
24 self.action_like(
25 "(:action ",
26 value.symbol(),
27 value.parameters(),
28 value.precondition(),
29 effect,
30 )
31 }
32}
33
34impl Visitor<ProcessDefinition, RcDoc<'static>> for PrettyRenderer {
35 fn visit(&self, value: &ProcessDefinition) -> RcDoc<'static> {
36 let effect = value.effect().as_ref().map(|e| e.accept(self));
37 self.action_like(
38 "(:process ",
39 value.symbol(),
40 value.parameters(),
41 value.precondition(),
42 effect,
43 )
44 }
45}
46
47impl PrettyRenderer {
48 fn action_like(
51 &self,
52 opening: &'static str,
53 symbol: &ActionSymbol,
54 parameters: &TypedVariables,
55 precondition: &PreconditionGoalDescriptions,
56 effect: Option<RcDoc<'static>>,
57 ) -> RcDoc<'static> {
58 RcDoc::text(opening)
59 .append(symbol.accept(self))
60 .append(RcDoc::hardline())
61 .append(self.keyword_line("parameters"))
62 .append(RcDoc::text(" ("))
63 .append(parameters.accept(self))
64 .append(")")
65 .append(RcDoc::hardline())
66 .append(self.keyword_line("precondition"))
67 .append(RcDoc::text(" "))
68 .append(precondition.accept(self))
69 .append(RcDoc::hardline())
70 .append(self.keyword_line("effect"))
71 .append(RcDoc::text(" "))
72 .append(effect.unwrap_or_else(|| RcDoc::text("()")))
73 .append(")")
74 }
75}
76
77impl Visitor<DurativeActionDefinition, RcDoc<'static>> for PrettyRenderer {
78 fn visit(&self, value: &DurativeActionDefinition) -> RcDoc<'static> {
79 let mut doc = RcDoc::text("(:durative-action ")
80 .append(value.symbol().accept(self))
81 .append(RcDoc::hardline())
82 .append(self.keyword_line("parameters"))
83 .append(RcDoc::text(" ("))
84 .append(value.parameters().accept(self))
85 .append(")");
86
87 let empty = || RcDoc::text("()");
89 let parts = [
90 (
91 "duration",
92 value.duration().as_ref().map(|d| d.accept(self)),
93 ),
94 (
95 "condition",
96 value.condition().as_ref().map(|c| c.accept(self)),
97 ),
98 ("effect", value.effect().as_ref().map(|e| e.accept(self))),
99 ];
100 for (keyword, part) in parts {
101 doc = doc
102 .append(RcDoc::hardline())
103 .append(self.keyword_line(keyword))
104 .append(RcDoc::text(" "))
105 .append(part.unwrap_or_else(empty));
106 }
107
108 doc.append(")")
109 }
110}
111
112impl Visitor<DerivedPredicate, RcDoc<'static>> for PrettyRenderer {
113 fn visit(&self, value: &DerivedPredicate) -> RcDoc<'static> {
114 RcDoc::text("(:derived ")
115 .append(value.predicate().accept(self))
116 .append(" ")
117 .append(value.expression().accept(self))
118 .append(")")
119 }
120}
121
122impl Visitor<StructureDef, RcDoc<'static>> for PrettyRenderer {
123 fn visit(&self, value: &StructureDef) -> RcDoc<'static> {
124 match value {
125 StructureDef::Action(a) => a.accept(self),
126 StructureDef::DurativeAction(da) => da.as_ref().accept(self),
127 StructureDef::Derived(d) => d.accept(self),
128 StructureDef::Process(p) => p.as_ref().accept(self),
129 StructureDef::Event(e) => self.action_like(
130 "(:event ",
131 e.symbol(),
132 e.parameters(),
133 e.precondition(),
134 e.effect().as_ref().map(|effect| effect.accept(self)),
135 ),
136 }
137 }
138}
139
140impl Visitor<StructureDefs, RcDoc<'static>> for PrettyRenderer {
141 fn visit(&self, value: &StructureDefs) -> RcDoc<'static> {
142 RcDoc::intersperse(
143 value.deref().iter().map(|s| self.visit(s)),
144 RcDoc::hardline(),
145 )
146 }
147}
148
149#[cfg(all(test, feature = "parser"))]
151mod tests {
152 use crate::parsers::Parser;
153 use crate::pretty_print::Pretty;
154 use assert2::check;
155
156 #[test]
157 fn action_with_effect() {
158 let input = r"(define (domain test)
159 (:requirements :strips)
160 (:predicates (p))
161 (:action move
162 :parameters (?x)
163 :precondition (and)
164 :effect (and (p))
165 )
166)";
167 let domain = crate::Domain::from_str(input).unwrap();
168 let output = domain.pretty(80).to_string();
169 check!(output.contains(":effect"));
170 check!(output.contains("(and (p))"));
171 }
172
173 #[test]
174 fn action_without_effect() {
175 let input = r"(define (domain test)
176 (:requirements :strips)
177 (:predicates (p))
178 (:action noop
179 :parameters ()
180 :precondition (and)
181 )
182)";
183 let domain = crate::Domain::from_str(input).unwrap();
184 let output = domain.pretty(80).to_string();
185 check!(output.contains(":effect ()"));
186 }
187
188 #[test]
189 fn durative_action_all_options() {
190 let input = r"(define (domain test)
191 (:requirements :durative-actions)
192 (:predicates (p))
193 (:durative-action move
194 :parameters (?x)
195 :duration (= ?duration 10)
196 :condition (and (at start (p)))
197 :effect (and (at end (p)))
198 )
199)";
200 let domain = crate::Domain::from_str(input).unwrap();
201 let output = domain.pretty(80).to_string();
202 check!(output.contains(":duration"));
203 check!(output.contains(":condition"));
204 check!(output.contains(":effect"));
205 }
206
207 #[test]
208 fn durative_action_without_parts_prints_empty_lists() {
209 use crate::{DurativeActionDefinition, DurativeActionSymbol, TypedList};
210 let action = DurativeActionDefinition::new(
211 DurativeActionSymbol::string("move"),
212 TypedList::default(),
213 None,
214 None,
215 None,
216 );
217 let output = action.pretty(80).to_string();
218 check!(output.contains(":duration ()"));
219 check!(output.contains(":condition ()"));
220 check!(output.contains(":effect ()"));
221 check!(crate::DurativeActionDefinition::from_str(&output).is_ok());
222 }
223
224 #[test]
225 fn durative_action_minimal() {
226 let input = r"(define (domain test)
227 (:requirements :durative-actions :strips)
228 (:durative-action noop
229 :parameters ()
230 )
231)";
232 let domain = crate::Domain::from_str(input);
233 if let Ok(domain) = domain {
234 let output = domain.pretty(80).to_string();
235 check!(output.contains("(:durative-action noop"));
236 }
237 }
238
239 #[test]
240 fn derived_predicate() {
241 let input = r"(define (domain test)
242 (:requirements :derived-predicates)
243 (:predicates (p) (q))
244 (:derived (p) (and))
245)";
246 let domain = crate::Domain::from_str(input).unwrap();
247 let output = domain.pretty(80).to_string();
248 check!(output.contains("(:derived"));
249 }
250
251 #[test]
252 fn structure_def_action() {
253 let input = r"(define (domain test)
254 (:requirements :strips)
255 (:action a
256 :parameters ()
257 :precondition (and)
258 :effect (and)
259 )
260)";
261 let domain = crate::Domain::from_str(input).unwrap();
262 let output = domain.pretty(80).to_string();
263 check!(output.contains("(:action a"));
264 }
265}