Skip to main content

pddl_core/pretty_print/
formula_skeleton.rs

1//! Pretty-printing of predicate and function declarations.
2
3use crate::pretty_print::{sealed, PrettyRenderer};
4use crate::types::{AtomicFormulaSkeleton, AtomicFunctionSkeleton};
5use crate::visitor::{Accept, Visitor};
6use pretty::RcDoc;
7
8impl sealed::Sealed for AtomicFormulaSkeleton {}
9impl sealed::Sealed for AtomicFunctionSkeleton {}
10
11impl Visitor<AtomicFormulaSkeleton, RcDoc<'static>> for PrettyRenderer {
12    fn visit(&self, value: &AtomicFormulaSkeleton) -> RcDoc<'static> {
13        if value.variables().is_empty() {
14            return RcDoc::text("(")
15                .append(value.predicate().accept(self))
16                .append(")");
17        }
18        RcDoc::text("(")
19            .append(value.predicate().accept(self))
20            .append(RcDoc::softline())
21            .append(value.variables().accept(self))
22            .nest(4)
23            .group()
24            .append(")")
25    }
26}
27
28impl Visitor<AtomicFunctionSkeleton, RcDoc<'static>> for PrettyRenderer {
29    fn visit(&self, value: &AtomicFunctionSkeleton) -> RcDoc<'static> {
30        if value.variables().is_empty() {
31            return RcDoc::text("(")
32                .append(value.symbol().accept(self))
33                .append(")");
34        }
35        RcDoc::text("(")
36            .append(value.symbol().accept(self))
37            .append(RcDoc::softline())
38            .append(value.variables().accept(self))
39            .nest(4)
40            .group()
41            .append(")")
42    }
43}
44
45#[cfg(test)]
46mod tests {
47    use super::*;
48    use crate::pretty_print::prettify;
49    use crate::visitor::Accept;
50    use crate::{AtomicFormulaSkeleton, FunctionSymbol, Predicate, ToTyped, Type, Variable};
51    use assert2::check;
52
53    #[test]
54    fn atomic_formula_skeleton_no_vars() {
55        let x = AtomicFormulaSkeleton::new(Predicate::new_string("block"), vec![].into());
56        check!(prettify!(x, 20) == "(block)");
57    }
58
59    #[test]
60    fn atomic_formula_skeleton_with_vars() {
61        let x = AtomicFormulaSkeleton::new(
62            Predicate::new_string("at"),
63            vec![
64                Variable::new_string("x").to_typed(Type::new_exactly("physob")),
65                Variable::new_string("y").to_typed(Type::new_exactly("location")),
66            ]
67            .into(),
68        );
69        check!(prettify!(x, 40) == "(at ?x - physob ?y - location)");
70    }
71
72    #[test]
73    fn atomic_function_skeleton() {
74        let x = AtomicFunctionSkeleton::new(
75            FunctionSymbol::from("battery-level"),
76            vec![Variable::new_string("r").to_typed(Type::new_exactly("rover"))].into(),
77        );
78        check!(prettify!(x, 40) == "(battery-level ?r - rover)");
79    }
80}