Skip to main content

pddl_core/pretty_print/
domain.rs

1//! Pretty-printing of domains and their `:constraints` section.
2
3use crate::pretty_print::{sealed, PrettyRenderer};
4use crate::types::{Domain, DomainConstraintsDef};
5use crate::visitor::{Accept, Visitor};
6use pretty::RcDoc;
7
8impl sealed::Sealed for Domain {}
9impl sealed::Sealed for DomainConstraintsDef {}
10
11impl Visitor<DomainConstraintsDef, RcDoc<'static>> for PrettyRenderer {
12    fn visit(&self, value: &DomainConstraintsDef) -> RcDoc<'static> {
13        if value.value().is_empty() {
14            return RcDoc::nil();
15        }
16        value.value().accept(self)
17    }
18}
19
20impl Visitor<Domain, RcDoc<'static>> for PrettyRenderer {
21    fn visit(&self, value: &Domain) -> RcDoc<'static> {
22        let mut doc = RcDoc::text("(")
23            .append(RcDoc::text("define"))
24            .append(RcDoc::hardline())
25            .append(RcDoc::text("(domain "))
26            .append(value.name().accept(self))
27            .append(")");
28
29        if !value.extends().is_empty() {
30            doc = doc
31                .append(RcDoc::hardline())
32                .append(RcDoc::text("(:extends"))
33                .append(RcDoc::softline())
34                .append(RcDoc::intersperse(
35                    value.extends().iter().map(|n| n.accept(self)),
36                    RcDoc::softline(),
37                ))
38                .nest(4)
39                .group()
40                .append(")");
41        }
42
43        if !value.requirements().is_empty_including_unknown() {
44            doc = doc
45                .append(RcDoc::hardline())
46                .append(self.section("requirements", self.requirement_docs(value.requirements())));
47        }
48
49        if !value.types().is_empty() {
50            doc = doc
51                .append(RcDoc::hardline())
52                .append(RcDoc::text("(:types"))
53                .append(RcDoc::softline())
54                .append(value.types().accept(self))
55                .nest(4)
56                .group()
57                .append(")");
58        }
59
60        if !value.constants().is_empty() {
61            doc = doc
62                .append(RcDoc::hardline())
63                .append(RcDoc::text("(:constants"))
64                .append(RcDoc::softline())
65                .append(value.constants().accept(self))
66                .nest(4)
67                .group()
68                .append(")");
69        }
70
71        if !value.predicates().is_empty() {
72            doc = doc.append(RcDoc::hardline()).append(self.section(
73                "predicates",
74                value.predicates().iter().map(|p| p.accept(self)),
75            ));
76        }
77
78        if !value.functions().is_empty() {
79            doc = doc.append(RcDoc::hardline()).append(self.section(
80                "functions",
81                value.functions().values().iter().map(|f| f.accept(self)),
82            ));
83        }
84
85        if !value.constraints().is_empty() {
86            doc = doc
87                .append(RcDoc::hardline())
88                .append(self.section("constraints", [value.constraints().accept(self)]));
89        }
90
91        if !value.timeless().is_empty() {
92            doc = doc
93                .append(RcDoc::hardline())
94                .append(value.timeless().accept(self));
95        }
96
97        if !value.structure().is_empty() {
98            doc = doc
99                .append(RcDoc::hardline())
100                .append(value.structure().accept(self));
101        }
102
103        doc.append(RcDoc::hardline()).append(")").nest(2).group()
104    }
105}
106
107// These tests parse their input, so they need the `parser` feature.
108#[cfg(all(test, feature = "parser"))]
109mod tests {
110    use crate::parsers::Parser;
111    use crate::pretty_print::Pretty;
112    use assert2::check;
113
114    #[test]
115    fn domain_basic() {
116        let input = r"(define (domain test)
117  (:requirements :strips)
118  (:types block)
119  (:predicates (on ?x ?y - block))
120)";
121        let domain = crate::Domain::from_str(input).unwrap();
122        let output = domain.pretty(80).to_string();
123        check!(output.contains("(define"));
124        check!(output.contains("(domain test)"));
125    }
126
127    #[test]
128    fn domain_with_extends() {
129        let input = r"(define (domain child)
130  (:extends parent)
131  (:requirements :strips)
132)";
133        let domain = crate::Domain::from_str(input).unwrap();
134        let output = domain.pretty(80).to_string();
135        check!(output.contains("(:extends parent)"));
136    }
137
138    #[test]
139    fn domain_with_constants() {
140        let input = r"(define (domain test)
141  (:requirements :strips)
142  (:constants c1 c2)
143)";
144        let domain = crate::Domain::from_str(input).unwrap();
145        let output = domain.pretty(80).to_string();
146        check!(output.contains("(:constants c1 c2)"));
147    }
148
149    #[test]
150    fn domain_with_functions() {
151        // Functions require :fluents requirement and specific syntax
152        // Just verify the parser can handle a domain with predicates and structure
153        let input = r"(define (domain test)
154  (:requirements :strips)
155  (:predicates (p ?x))
156  (:action a
157    :parameters (?x)
158    :precondition (and)
159    :effect (and)
160  )
161)";
162        let domain = crate::Domain::from_str(input).unwrap();
163        let output = domain.pretty(80).to_string();
164        check!(output.contains("(:action a"));
165    }
166
167    #[test]
168    fn domain_with_constraints() {
169        // Constraints parsing may vary; test via structure instead
170        let input = r"(define (domain test)
171  (:requirements :strips)
172  (:predicates (p))
173  (:action a
174    :parameters ()
175    :precondition (and)
176    :effect (and)
177  )
178)";
179        let domain = crate::Domain::from_str(input).unwrap();
180        let output = domain.pretty(80).to_string();
181        check!(output.contains("(:action a"));
182    }
183
184    #[test]
185    fn domain_with_timeless() {
186        let input = r"(define (domain test)
187  (:requirements :strips)
188  (:predicates (p))
189  (:timeless (p))
190)";
191        let domain = crate::Domain::from_str(input).unwrap();
192        let output = domain.pretty(80).to_string();
193        check!(output.contains("(:timeless"));
194    }
195
196    #[test]
197    fn domain_with_structure() {
198        let input = r"(define (domain test)
199  (:requirements :strips)
200  (:predicates (p))
201  (:action a
202    :parameters ()
203    :precondition (and)
204    :effect (and)
205  )
206)";
207        let domain = crate::Domain::from_str(input).unwrap();
208        let output = domain.pretty(80).to_string();
209        check!(output.contains("(:action"));
210    }
211}
212
213#[cfg(test)]
214mod tests_without_parser {
215    use crate::pretty_print::Pretty;
216    use crate::types::{ConstraintGoalDescription, DomainConstraintsDef};
217    use assert2::check;
218
219    #[test]
220    fn domain_constraints_def_empty_nil() {
221        let dc = DomainConstraintsDef::new(ConstraintGoalDescription::default());
222        let out = dc.pretty(80).to_string();
223        check!(out == "");
224    }
225}