pddl_core/pretty_print/
problem.rs1use crate::pretty_print::{sealed, PrettyRenderer};
4use crate::types::{Problem, ProblemConstraintsDef};
5use crate::visitor::{Accept, Visitor};
6use pretty::RcDoc;
7
8impl sealed::Sealed for Problem {}
9impl sealed::Sealed for ProblemConstraintsDef {}
10
11impl Visitor<ProblemConstraintsDef, RcDoc<'static>> for PrettyRenderer {
12 fn visit(&self, value: &ProblemConstraintsDef) -> RcDoc<'static> {
13 if value.value().is_empty() {
14 return RcDoc::nil();
15 }
16 value.value().accept(self)
17 }
18}
19
20impl Visitor<Problem, RcDoc<'static>> for PrettyRenderer {
21 fn visit(&self, value: &Problem) -> RcDoc<'static> {
22 let mut doc = RcDoc::text("(")
23 .append(RcDoc::text("define"))
24 .append(RcDoc::hardline())
25 .append(RcDoc::text("(problem "))
26 .append(value.name().accept(self))
27 .append(")")
28 .append(RcDoc::hardline())
29 .append(RcDoc::text("(:domain "))
30 .append(value.domain().accept(self))
31 .append(")");
32
33 if !value.requirements().is_empty_including_unknown() {
34 doc = doc
35 .append(RcDoc::hardline())
36 .append(self.section("requirements", self.requirement_docs(value.requirements())));
37 }
38
39 if !value.objects().is_empty() {
40 doc = doc
41 .append(RcDoc::hardline())
42 .append(RcDoc::text("(:objects"))
43 .append(RcDoc::softline())
44 .append(value.objects().accept(self))
45 .nest(4)
46 .group()
47 .append(")");
48 }
49
50 doc = doc
51 .append(RcDoc::hardline())
52 .append(self.section("init", value.init().iter().map(|ie| ie.accept(self))));
53
54 doc = doc
55 .append(RcDoc::hardline())
56 .append(self.section("goal", [value.goals().accept(self)]));
57
58 if !value.constraints().is_empty() {
59 doc = doc.append(RcDoc::hardline()).append(self.section(
60 "constraints",
61 value.constraints().iter().map(|c| c.accept(self)),
62 ));
63 }
64
65 if let Some(metric) = value.metric_spec() {
66 doc = doc
67 .append(RcDoc::hardline())
68 .append(self.section("metric", [metric.accept(self)]));
69 }
70
71 if let Some(length) = value.length_spec() {
72 doc = doc.append(RcDoc::hardline()).append(length.accept(self));
73 }
74
75 doc.append(RcDoc::hardline()).append(")").nest(2).group()
76 }
77}
78
79#[cfg(all(test, feature = "parser"))]
81mod tests {
82 use crate::parsers::Parser;
83 use crate::pretty_print::Pretty;
84 use assert2::check;
85
86 #[test]
87 fn problem_basic() {
88 let input = r"(define (problem test-prob)
89 (:domain test)
90 (:init (block a) (block b))
91 (:goal (and))
92)";
93 let problem = crate::Problem::from_str(input).unwrap();
94 let output = problem.pretty(80).to_string();
95 check!(output.contains("(define"));
96 check!(output.contains("(problem test-prob)"));
97 check!(output.contains("(:domain test)"));
98 }
99
100 #[test]
101 fn problem_with_requirements() {
102 let input = r"(define (problem test-prob)
103 (:domain test)
104 (:requirements :strips)
105 (:init)
106 (:goal (and))
107)";
108 let problem = crate::Problem::from_str(input).unwrap();
109 let output = problem.pretty(80).to_string();
110 check!(output.contains("(:requirements :strips)"));
111 }
112
113 #[test]
114 fn problem_with_objects() {
115 let input = r"(define (problem test-prob)
116 (:domain test)
117 (:objects a b)
118 (:init)
119 (:goal (and))
120)";
121 let problem = crate::Problem::from_str(input).unwrap();
122 let output = problem.pretty(80).to_string();
123 check!(output.contains("(:objects a b)"));
124 }
125
126 #[test]
127 fn problem_with_constraints() {
128 let input = r"(define (problem test-prob)
129 (:domain test)
130 (:init)
131 (:goal (and))
132 (:constraints (always (p)))
133 (:requirements :constraints)
134)";
135 let problem = crate::Problem::from_str(input).unwrap();
136 let output = problem.pretty(80).to_string();
137 check!(output.contains("(:constraints"), "{output}");
138 }
139
140 #[test]
141 fn problem_with_metric() {
142 let input = r"(define (problem test-prob)
143 (:domain test)
144 (:init)
145 (:goal (and))
146 (:metric minimize total-time)
147)";
148 let problem = crate::Problem::from_str(input).unwrap();
149 let output = problem.pretty(80).to_string();
150 check!(output.contains("(:metric minimize total-time)"));
151 }
152
153 #[test]
154 fn problem_with_length_spec() {
155 let input = r"(define (problem test-prob)
156 (:domain test)
157 (:init)
158 (:goal (and))
159 (:length (:serial 10))
160)";
161 let problem = crate::Problem::from_str(input).unwrap();
162 let output = problem.pretty(80).to_string();
163 check!(output.contains("(:length (:serial 10))"));
164 }
165}
166
167#[cfg(test)]
168mod tests_without_parser {
169 use crate::pretty_print::Pretty;
170 use crate::types::{PreferenceConstraintGoalDescriptions, ProblemConstraintsDef};
171 use assert2::check;
172
173 #[test]
174 fn problem_constraints_def_empty_nil() {
175 let dc = ProblemConstraintsDef::new(PreferenceConstraintGoalDescriptions::new(Vec::new()));
176 let out = dc.pretty(80).to_string();
177 check!(out == "");
178 }
179}