Skip to main content

pddl_core/pretty_print/
timed.rs

1//! Pretty-printing of durative-action conditions, effects and duration constraints.
2
3use crate::pretty_print::{sealed, PrettyRenderer};
4use crate::types::{
5    DurationConstraint, DurationValue, DurativeActionEffect, DurativeActionGoalDescription,
6    PreferenceTimedGoalDescription, SimpleDurationConstraint, TimedEffect, TimedGoalDescription,
7};
8use crate::visitor::{Accept, Visitor};
9use pretty::RcDoc;
10
11impl sealed::Sealed for DurationValue {}
12impl sealed::Sealed for SimpleDurationConstraint {}
13impl sealed::Sealed for DurationConstraint {}
14impl sealed::Sealed for TimedGoalDescription {}
15impl sealed::Sealed for PreferenceTimedGoalDescription {}
16impl sealed::Sealed for TimedEffect {}
17impl sealed::Sealed for DurativeActionEffect {}
18impl sealed::Sealed for DurativeActionGoalDescription {}
19
20impl Visitor<DurationValue, RcDoc<'static>> for PrettyRenderer {
21    fn visit(&self, value: &DurationValue) -> RcDoc<'static> {
22        match value {
23            DurationValue::Number(n) => self.f_exp_number(*n),
24            DurationValue::FluentExpression(e) => e.accept(self),
25        }
26    }
27}
28
29impl Visitor<SimpleDurationConstraint, RcDoc<'static>> for PrettyRenderer {
30    fn visit(&self, value: &SimpleDurationConstraint) -> RcDoc<'static> {
31        match value {
32            SimpleDurationConstraint::Op(op, dv) => RcDoc::text("(")
33                .append(op.accept(self))
34                .append(" ?duration ")
35                .append(dv.accept(self))
36                .append(")"),
37            SimpleDurationConstraint::At(ts, inner) => {
38                let inner_doc = self.visit(inner.as_ref());
39                RcDoc::text("(at ")
40                    .append(ts.accept(self))
41                    .append(" ")
42                    .append(inner_doc)
43                    .append(")")
44            }
45        }
46    }
47}
48
49impl Visitor<DurationConstraint, RcDoc<'static>> for PrettyRenderer {
50    fn visit(&self, value: &DurationConstraint) -> RcDoc<'static> {
51        match value {
52            DurationConstraint::Single(sdc) => sdc.accept(self),
53            DurationConstraint::All(sdcs) => RcDoc::text("(and")
54                .append(RcDoc::softline())
55                .append(RcDoc::intersperse(
56                    sdcs.iter().map(|sdc| sdc.accept(self)),
57                    RcDoc::softline(),
58                ))
59                .nest(4)
60                .group()
61                .append(")"),
62        }
63    }
64}
65
66impl Visitor<TimedGoalDescription, RcDoc<'static>> for PrettyRenderer {
67    fn visit(&self, value: &TimedGoalDescription) -> RcDoc<'static> {
68        match value {
69            TimedGoalDescription::At(ts, gd) => RcDoc::text("(at ")
70                .append(ts.accept(self))
71                .append(" ")
72                .append(gd.accept(self))
73                .append(")"),
74            TimedGoalDescription::Over(interval, gd) => RcDoc::text("(over ")
75                .append(interval.accept(self))
76                .append(" ")
77                .append(gd.accept(self))
78                .append(")"),
79        }
80    }
81}
82
83impl Visitor<PreferenceTimedGoalDescription, RcDoc<'static>> for PrettyRenderer {
84    fn visit(&self, value: &PreferenceTimedGoalDescription) -> RcDoc<'static> {
85        match value {
86            PreferenceTimedGoalDescription::Required(tgd) => tgd.accept(self),
87            PreferenceTimedGoalDescription::Preference(name, tgd) => {
88                let children: Vec<RcDoc<'static>> = match name {
89                    Some(n) => vec![n.accept(self), self.visit(tgd)],
90                    None => vec![self.visit(tgd)],
91                };
92                self.sexpr("preference", children)
93            }
94        }
95    }
96}
97
98impl Visitor<TimedEffect, RcDoc<'static>> for PrettyRenderer {
99    fn visit(&self, value: &TimedEffect) -> RcDoc<'static> {
100        match value {
101            TimedEffect::Conditional(ts, ce) => RcDoc::text("(at ")
102                .append(ts.accept(self))
103                .append(" ")
104                .append(ce.accept(self))
105                .append(")"),
106            TimedEffect::NumericFluent(ts, fassign) => RcDoc::text("(at ")
107                .append(ts.accept(self))
108                .append(" ")
109                .append(fassign.accept(self))
110                .append(")"),
111            TimedEffect::ContinuousEffect(op, head, exp) => RcDoc::text("(")
112                .append(op.accept(self))
113                .append(" ")
114                .append(head.accept(self))
115                .append(" ")
116                .append(exp.accept(self))
117                .append(")"),
118        }
119    }
120}
121
122impl Visitor<DurativeActionGoalDescription, RcDoc<'static>> for PrettyRenderer {
123    fn visit(&self, value: &DurativeActionGoalDescription) -> RcDoc<'static> {
124        match value {
125            DurativeActionGoalDescription::Timed(ptgd) => ptgd.accept(self),
126            DurativeActionGoalDescription::And(items) => {
127                if items.is_empty() {
128                    RcDoc::text("(and)")
129                } else {
130                    self.sexpr("and", items.iter().map(|dagd| self.visit(dagd)))
131                }
132            }
133            DurativeActionGoalDescription::Forall(vars, body) => self.sexpr(
134                "forall",
135                [
136                    RcDoc::text("(").append(vars.accept(self)).append(")"),
137                    self.visit(body.as_ref()),
138                ],
139            ),
140        }
141    }
142}
143
144impl Visitor<DurativeActionEffect, RcDoc<'static>> for PrettyRenderer {
145    fn visit(&self, value: &DurativeActionEffect) -> RcDoc<'static> {
146        match value {
147            DurativeActionEffect::Timed(te) => te.accept(self),
148            DurativeActionEffect::All(effects) => RcDoc::text("(and")
149                .append(RcDoc::softline())
150                .append(RcDoc::intersperse(
151                    effects.iter().map(|e| self.visit(e)),
152                    RcDoc::softline(),
153                ))
154                .nest(4)
155                .group()
156                .append(")"),
157            DurativeActionEffect::Forall(vars, body) => self.sexpr(
158                "forall",
159                [
160                    RcDoc::text("(").append(vars.accept(self)).append(")"),
161                    self.visit(body.as_ref()),
162                ],
163            ),
164            DurativeActionEffect::When(condition, effect) => RcDoc::text("(at ")
165                .append(self.visit(condition))
166                .append(" ")
167                .append(self.visit(effect))
168                .append(")"),
169        }
170    }
171}
172
173#[cfg(test)]
174mod tests {
175    use super::*;
176    use crate::pretty_print::prettify;
177    use crate::visitor::Accept;
178    use crate::{DurationOperator, PreferenceTimedGoalDescription};
179    use assert2::check;
180
181    #[test]
182    fn duration_value_number() {
183        let dv = DurationValue::number(10);
184        check!(prettify!(dv, 20) == "10");
185    }
186
187    #[test]
188    fn simple_duration_constraint_op() {
189        let sdc = SimpleDurationConstraint::op(DurationOperator::Equal, DurationValue::number(10));
190        check!(prettify!(sdc, 30) == "(= ?duration 10)");
191    }
192
193    #[test]
194    fn duration_value_fexp() {
195        let exp = crate::FluentExpression::number(crate::Number::from(5));
196        let dv = DurationValue::fluent_expression(exp);
197        check!(prettify!(dv, 20) == "5");
198    }
199
200    #[test]
201    fn simple_duration_constraint_at() {
202        let inner =
203            SimpleDurationConstraint::op(DurationOperator::Equal, DurationValue::number(10));
204        let sdc = SimpleDurationConstraint::at(crate::TimeSpecifier::Start, inner);
205        check!(prettify!(sdc, 30) == "(at start (= ?duration 10))");
206    }
207
208    #[test]
209    fn duration_constraint_single() {
210        let sdc = SimpleDurationConstraint::op(DurationOperator::Equal, DurationValue::number(10));
211        let dc = DurationConstraint::new(sdc);
212        check!(prettify!(dc, 30) == "(= ?duration 10)");
213    }
214
215    #[test]
216    fn duration_constraint_all() {
217        let sdc1 = SimpleDurationConstraint::op(DurationOperator::Equal, DurationValue::number(10));
218        let sdc2 = SimpleDurationConstraint::op(
219            DurationOperator::GreaterOrEqual,
220            DurationValue::number(5),
221        );
222        let dc = DurationConstraint::all([sdc1, sdc2]);
223        let out = prettify!(dc, 30);
224        check!(out.starts_with("(and"));
225        check!(out.contains("(= ?duration 10)"));
226        check!(out.contains("(>= ?duration 5)"));
227    }
228
229    #[test]
230    fn timed_gd_at() {
231        let gd = crate::GoalDescription::and(Vec::<crate::GoalDescription>::new());
232        let tgd = TimedGoalDescription::at(crate::TimeSpecifier::Start, gd);
233        check!(prettify!(tgd, 30) == "(at start (and))");
234    }
235
236    #[test]
237    fn timed_gd_over() {
238        use crate::Interval;
239        let gd = crate::GoalDescription::and(Vec::<crate::GoalDescription>::new());
240        let tgd = TimedGoalDescription::over(Interval::All, gd);
241        check!(prettify!(tgd, 30) == "(over all (and))");
242    }
243
244    #[test]
245    fn pref_timed_gd_required() {
246        let gd = crate::GoalDescription::and(Vec::<crate::GoalDescription>::new());
247        let tgd = TimedGoalDescription::at(crate::TimeSpecifier::Start, gd);
248        let ptgd = PreferenceTimedGoalDescription::required(tgd);
249        check!(prettify!(ptgd, 30) == "(at start (and))");
250    }
251
252    #[test]
253    fn pref_timed_gd_preference_with_name() {
254        use crate::PreferenceName;
255        let gd = crate::GoalDescription::and(Vec::<crate::GoalDescription>::new());
256        let tgd = TimedGoalDescription::at(crate::TimeSpecifier::Start, gd);
257        let ptgd =
258            PreferenceTimedGoalDescription::preference(Some(PreferenceName::string("p1")), tgd);
259        check!(prettify!(ptgd, 40) == "(preference p1 (at start (and)))");
260    }
261
262    #[test]
263    fn pref_timed_gd_preference_no_name() {
264        let gd = crate::GoalDescription::and(Vec::<crate::GoalDescription>::new());
265        let tgd = TimedGoalDescription::at(crate::TimeSpecifier::Start, gd);
266        let ptgd = PreferenceTimedGoalDescription::preference(None, tgd);
267        check!(prettify!(ptgd, 40) == "(preference (at start (and)))");
268    }
269
270    #[test]
271    fn timed_effect_conditional() {
272        use crate::EffectCondition;
273        let ce = EffectCondition::all(Vec::new());
274        let te = TimedEffect::conditional(crate::TimeSpecifier::Start, ce);
275        check!(prettify!(te, 30) == "(at start (and))");
276    }
277
278    #[test]
279    fn timed_effect_numeric_fluent() {
280        use crate::{
281            AssignOp, DurativeActionFluentExpression, DurativeActionFunctionAssignment,
282            FluentExpression, FunctionHead, FunctionSymbol, Number,
283        };
284        let head = FunctionHead::Simple(FunctionSymbol::from("x"));
285        let expr = DurativeActionFluentExpression::fluent_expression(FluentExpression::number(
286            Number::from(5),
287        ));
288        let fassign = DurativeActionFunctionAssignment::new(AssignOp::Increase, head, expr);
289        let te = TimedEffect::fluent(crate::TimeSpecifier::End, fassign);
290        check!(prettify!(te, 30) == "(at end (increase x 5))");
291    }
292
293    #[test]
294    fn timed_effect_continuous() {
295        use crate::{
296            FluentExpression, FunctionHead, FunctionSymbol, Number, TimedAssignOperator,
297            TimedFluentExpression,
298        };
299        let head = FunctionHead::Simple(FunctionSymbol::from("x"));
300        let exp = FluentExpression::number(Number::from(1));
301        let te = TimedEffect::continuous(
302            TimedAssignOperator::Increase,
303            head,
304            TimedFluentExpression::Scaled(exp),
305        );
306        check!(prettify!(te, 30) == "(increase x (* #t 1))");
307    }
308
309    #[test]
310    fn durative_action_goal_timed() {
311        let gd = crate::GoalDescription::and(Vec::<crate::GoalDescription>::new());
312        let tgd = TimedGoalDescription::at(crate::TimeSpecifier::Start, gd);
313        let ptgd = PreferenceTimedGoalDescription::required(tgd);
314        let dagd = DurativeActionGoalDescription::timed(ptgd);
315        check!(prettify!(dagd, 30) == "(at start (and))");
316    }
317
318    #[test]
319    fn durative_action_goal_and_empty() {
320        let dagd = DurativeActionGoalDescription::and(Vec::new());
321        check!(prettify!(dagd, 30) == "(and)");
322    }
323
324    #[test]
325    fn durative_action_goal_and_non_empty() {
326        let gd = crate::GoalDescription::and(Vec::<crate::GoalDescription>::new());
327        let tgd = TimedGoalDescription::at(crate::TimeSpecifier::Start, gd);
328        let ptgd = PreferenceTimedGoalDescription::required(tgd);
329        let dagd = DurativeActionGoalDescription::and([DurativeActionGoalDescription::timed(ptgd)]);
330        check!(prettify!(dagd, 40) == "(and (at start (and)))");
331    }
332
333    #[test]
334    fn durative_action_goal_forall() {
335        use crate::{ToTyped, Type, TypedVariables, Variable};
336        let vars: TypedVariables = vec![Variable::string("x").to_typed(Type::OBJECT)].into();
337        let inner = DurativeActionGoalDescription::and(Vec::new());
338        let dagd = DurativeActionGoalDescription::forall(vars, inner);
339        check!(prettify!(dagd, 40) == "(forall (?x) (and))");
340    }
341
342    #[test]
343    fn durative_action_effect_timed() {
344        use crate::EffectCondition;
345        let ce = EffectCondition::all(Vec::new());
346        let te = TimedEffect::conditional(crate::TimeSpecifier::Start, ce);
347        let dae = DurativeActionEffect::timed(te);
348        check!(prettify!(dae, 30) == "(at start (and))");
349    }
350
351    #[test]
352    fn durative_action_effect_all() {
353        use crate::EffectCondition;
354        let ce = EffectCondition::all(Vec::new());
355        let te = TimedEffect::conditional(crate::TimeSpecifier::Start, ce);
356        let dae = DurativeActionEffect::and([DurativeActionEffect::timed(te)]);
357        check!(prettify!(dae, 40) == "(and (at start (and)))");
358    }
359
360    #[test]
361    fn durative_action_effect_forall() {
362        use crate::{EffectCondition, ToTyped, Type, TypedVariables, Variable};
363        let vars: TypedVariables = vec![Variable::string("x").to_typed(Type::OBJECT)].into();
364        let ce = EffectCondition::all(Vec::new());
365        let inner =
366            DurativeActionEffect::timed(TimedEffect::conditional(crate::TimeSpecifier::Start, ce));
367        let dae = DurativeActionEffect::forall(vars, inner);
368        check!(prettify!(dae, 40) == "(forall (?x) (at start (and)))");
369    }
370
371    #[test]
372    fn durative_action_effect_when() {
373        use crate::{EffectCondition, GoalDescription};
374        let gd = GoalDescription::and(Vec::<GoalDescription>::new());
375        let ce = EffectCondition::all(Vec::new());
376        let te = TimedEffect::conditional(crate::TimeSpecifier::Start, ce);
377        let dae = DurativeActionEffect::when(
378            DurativeActionGoalDescription::timed(PreferenceTimedGoalDescription::required(
379                TimedGoalDescription::at(crate::TimeSpecifier::Start, gd),
380            )),
381            te,
382        );
383        check!(prettify!(dae, 40) == "(at (at start (and)) (at start (and)))");
384    }
385}