1use crate::pretty_print::{sealed, PrettyRenderer};
4use crate::types::{
5 BinaryComparison, BinaryOp, DurativeActionFluentExpression, DurativeActionFunctionAssignment,
6 FluentComparison, FluentExpression, FunctionHead, MetricFluentExpression, MultiOp, Number,
7 TimedFluentExpression,
8};
9use crate::visitor::{Accept, Visitor};
10use pretty::RcDoc;
11
12impl sealed::Sealed for FluentExpression {}
13impl sealed::Sealed for FunctionHead {}
14impl sealed::Sealed for FluentComparison {}
15impl sealed::Sealed for TimedFluentExpression {}
16impl sealed::Sealed for DurativeActionFluentExpression {}
17impl sealed::Sealed for DurativeActionFunctionAssignment {}
18impl sealed::Sealed for MetricFluentExpression {}
19
20impl PrettyRenderer {
21 pub(super) fn f_exp_number(&self, number: Number) -> RcDoc<'static> {
24 if *number < 0.0 {
25 self.sexpr("-", [Number::new(-*number).accept(self)])
26 } else {
27 number.accept(self)
28 }
29 }
30
31 fn multi_op(
33 &self,
34 op: &'static str,
35 first: RcDoc<'static>,
36 rest: impl ExactSizeIterator<Item = RcDoc<'static>>,
37 ) -> RcDoc<'static> {
38 if rest.len() == 0 {
39 return first;
40 }
41
42 self.sexpr(op, std::iter::once(first).chain(rest))
43 }
44}
45
46impl Visitor<FluentExpression, RcDoc<'static>> for PrettyRenderer {
47 fn visit(&self, value: &FluentExpression) -> RcDoc<'static> {
48 match value {
49 FluentExpression::Number(n) => self.f_exp_number(*n),
50 FluentExpression::Function(head) => head.accept(self),
51 FluentExpression::Negative(e) => RcDoc::text("(")
52 .append("-")
53 .append(RcDoc::softline())
54 .append(self.visit(&**e))
55 .nest(4)
56 .group()
57 .append(")"),
58 FluentExpression::BinaryOp(op, a, b) => {
59 let op_str = match op {
60 BinaryOp::Addition => "+",
61 BinaryOp::Subtraction => "-",
62 BinaryOp::Multiplication => "*",
63 BinaryOp::Division => "/",
64 };
65 self.sexpr(op_str, [self.visit(&**a), self.visit(&**b)])
66 }
67 FluentExpression::MultiOp(op, first, rest) => {
68 let op_str = match op {
69 MultiOp::Addition => "+",
70 MultiOp::Multiplication => "*",
71 };
72 self.multi_op(
73 op_str,
74 self.visit(&**first),
75 rest.iter().map(|e| self.visit(e)),
76 )
77 }
78 }
79 }
80}
81
82impl Visitor<FunctionHead, RcDoc<'static>> for PrettyRenderer {
83 fn visit(&self, value: &FunctionHead) -> RcDoc<'static> {
84 match value {
85 FunctionHead::Simple(sym) => sym.accept(self),
86 FunctionHead::WithTerms(sym, terms) => RcDoc::text("(")
87 .append(sym.accept(self))
88 .append(RcDoc::softline())
89 .append(RcDoc::intersperse(
90 terms.iter().map(|t| t.accept(self)),
91 RcDoc::softline(),
92 ))
93 .nest(4)
94 .group()
95 .append(")"),
96 }
97 }
98}
99
100impl Visitor<FluentComparison, RcDoc<'static>> for PrettyRenderer {
101 fn visit(&self, value: &FluentComparison) -> RcDoc<'static> {
102 let op_str = match value.comparison() {
103 BinaryComparison::GreaterThan => ">",
104 BinaryComparison::LessThan => "<",
105 BinaryComparison::Equal => "=",
106 BinaryComparison::GreaterOrEqual => ">=",
107 BinaryComparison::LessThanOrEqual => "<=",
108 };
109 self.sexpr(
110 op_str,
111 [self.visit(value.first()), self.visit(value.second())],
112 )
113 }
114}
115
116impl Visitor<TimedFluentExpression, RcDoc<'static>> for PrettyRenderer {
117 fn visit(&self, value: &TimedFluentExpression) -> RcDoc<'static> {
118 match value {
119 TimedFluentExpression::Now => RcDoc::text("#t"),
120 TimedFluentExpression::Scaled(e) => self.sexpr("*", [RcDoc::text("#t"), self.visit(e)]),
121 }
122 }
123}
124
125impl Visitor<DurativeActionFluentExpression, RcDoc<'static>> for PrettyRenderer {
126 fn visit(&self, value: &DurativeActionFluentExpression) -> RcDoc<'static> {
127 match value {
128 DurativeActionFluentExpression::Duration => RcDoc::text("?duration"),
129 DurativeActionFluentExpression::FluentExpression(e) => self.visit(e),
130 DurativeActionFluentExpression::Negative(e) => RcDoc::text("(")
131 .append("-")
132 .append(RcDoc::softline())
133 .append(self.visit(&**e))
134 .nest(4)
135 .group()
136 .append(")"),
137 DurativeActionFluentExpression::BinaryOp(op, a, b) => {
138 let op_str = match op {
139 BinaryOp::Addition => "+",
140 BinaryOp::Subtraction => "-",
141 BinaryOp::Multiplication => "*",
142 BinaryOp::Division => "/",
143 };
144 self.sexpr(op_str, [self.visit(&**a), self.visit(&**b)])
145 }
146 DurativeActionFluentExpression::MultiOp(op, first, rest) => {
147 let op_str = match op {
148 MultiOp::Addition => "+",
149 MultiOp::Multiplication => "*",
150 };
151 self.multi_op(
152 op_str,
153 self.visit(&**first),
154 rest.iter().map(|e| self.visit(e)),
155 )
156 }
157 DurativeActionFluentExpression::Assign(op, head, expr) => {
158 self.sexpr(op.as_str(), [self.visit(head), self.visit(&**expr)])
159 }
160 }
161 }
162}
163
164impl Visitor<DurativeActionFunctionAssignment, RcDoc<'static>> for PrettyRenderer {
165 fn visit(&self, value: &DurativeActionFunctionAssignment) -> RcDoc<'static> {
166 self.sexpr(
167 value.operation().as_str(),
168 [
169 self.visit(value.function()),
170 self.visit(value.function_expr()),
171 ],
172 )
173 }
174}
175
176impl Visitor<MetricFluentExpression, RcDoc<'static>> for PrettyRenderer {
177 fn visit(&self, value: &MetricFluentExpression) -> RcDoc<'static> {
178 match value {
179 MetricFluentExpression::Number(n) => self.f_exp_number(*n),
180 MetricFluentExpression::Function(sym, names) => {
181 if names.is_empty() {
182 sym.accept(self)
183 } else {
184 RcDoc::text("(")
185 .append(sym.accept(self))
186 .append(RcDoc::softline())
187 .append(RcDoc::intersperse(
188 names.iter().map(|n| n.accept(self)),
189 RcDoc::softline(),
190 ))
191 .nest(4)
192 .group()
193 .append(")")
194 }
195 }
196 MetricFluentExpression::TotalTime => RcDoc::text("total-time"),
197 MetricFluentExpression::IsViolated(pref) => RcDoc::text("(")
198 .append("is-violated")
199 .append(RcDoc::softline())
200 .append(pref.accept(self))
201 .nest(4)
202 .group()
203 .append(")"),
204 MetricFluentExpression::Negative(e) => RcDoc::text("(")
205 .append("-")
206 .append(RcDoc::softline())
207 .append(self.visit(&**e))
208 .nest(4)
209 .group()
210 .append(")"),
211 MetricFluentExpression::BinaryOp(op, a, b) => {
212 let op_str = match op {
213 BinaryOp::Addition => "+",
214 BinaryOp::Subtraction => "-",
215 BinaryOp::Multiplication => "*",
216 BinaryOp::Division => "/",
217 };
218 self.sexpr(op_str, [self.visit(&**a), self.visit(&**b)])
219 }
220 MetricFluentExpression::MultiOp(op, first, rest) => {
221 let op_str = match op {
222 MultiOp::Addition => "+",
223 MultiOp::Multiplication => "*",
224 };
225 self.multi_op(
226 op_str,
227 self.visit(&**first),
228 rest.iter().map(|e| self.visit(e)),
229 )
230 }
231 }
232 }
233}
234
235#[cfg(test)]
236mod tests {
237 use super::*;
238 use crate::pretty_print::prettify;
239 use crate::visitor::Accept;
240 use crate::{
241 AssignOp, BinaryComparison, BinaryOp, FunctionSymbol, MultiOp, Name, Number,
242 PreferenceName, Variable,
243 };
244 use assert2::check;
245
246 #[test]
247 fn fexp_number_works() {
248 let n = FluentExpression::number(Number::from(42));
249 check!(prettify!(n, 10) == "42");
250 }
251
252 #[test]
253 fn fexp_simple_function_works() {
254 let head = FunctionHead::Simple(FunctionSymbol::from("fuel"));
255 let e = FluentExpression::function(head);
256 check!(prettify!(e, 10) == "fuel");
257 }
258
259 #[test]
260 fn fexp_function_with_terms_works() {
261 let head = FunctionHead::WithTerms(
262 FunctionSymbol::from("fuel"),
263 vec![crate::Term::new_variable(Variable::from("r"))],
264 );
265 let e = FluentExpression::function(head);
266 check!(prettify!(e, 20) == "(fuel ?r)");
267 }
268
269 #[test]
270 fn fexp_negative_works() {
271 let inner = FluentExpression::number(Number::from(5));
272 let e = FluentExpression::negative(inner);
273 check!(prettify!(e, 10) == "(- 5)");
274 }
275
276 #[test]
277 fn negative_numbers_print_as_negations() {
278 let e = FluentExpression::number(Number::from(-5));
279 check!(prettify!(e, 10) == "(- 5)");
280 let m = MetricFluentExpression::number(Number::from(-2.5));
281 check!(prettify!(m, 10) == "(- 2.5)");
282 let zero = FluentExpression::number(Number::from(-0.0));
283 check!(prettify!(zero, 10) == "0");
284 }
285
286 #[test]
287 fn multi_op_without_extra_operands_prints_the_operand() {
288 let one = || Box::new(FluentExpression::number(1));
289 check!(
290 prettify!(
291 FluentExpression::MultiOp(MultiOp::Addition, one(), vec![]),
292 10
293 ) == "1"
294 );
295 let m = MetricFluentExpression::MultiOp(
296 MultiOp::Addition,
297 Box::new(MetricFluentExpression::total_time()),
298 vec![],
299 );
300 check!(prettify!(m, 20) == "total-time");
301 }
302
303 #[test]
304 fn fexp_binary_op_works() {
305 let a = FluentExpression::number(Number::from(3));
306 let b = FluentExpression::number(Number::from(4));
307 let e = FluentExpression::binary_op(BinaryOp::Addition, a, b);
308 check!(prettify!(e, 10) == "(+ 3 4)");
309 }
310
311 #[test]
312 fn fcomp_works() {
313 let a = FluentExpression::number(Number::from(3));
314 let b = FluentExpression::number(Number::from(4));
315 let fc = FluentComparison::new(BinaryComparison::GreaterThan, a, b);
316 check!(prettify!(fc, 10) == "(> 3 4)");
317 }
318
319 #[test]
320 fn fexp_t_now_works() {
321 let e = TimedFluentExpression::Now;
322 check!(prettify!(e, 10) == "#t");
323 }
324
325 #[test]
326 fn fexp_t_scaled_works() {
327 let inner = FluentExpression::number(Number::from(2));
328 let e = TimedFluentExpression::Scaled(inner);
329 check!(prettify!(e, 10) == "(* #t 2)");
330 }
331
332 #[test]
333 fn fexp_da_duration_works() {
334 let e = DurativeActionFluentExpression::duration();
335 check!(prettify!(e, 10) == "?duration");
336 }
337
338 #[test]
339 fn fexp_da_fexp_works() {
340 let inner = FluentExpression::number(Number::from(10));
341 let e = DurativeActionFluentExpression::fluent_expression(inner);
342 check!(prettify!(e, 10) == "10");
343 }
344
345 #[test]
346 fn f_assign_da_works() {
347 let head = FunctionHead::Simple(FunctionSymbol::from("fuel"));
348 let expr = DurativeActionFluentExpression::fluent_expression(FluentExpression::number(
349 Number::from(5),
350 ));
351 let assign = DurativeActionFunctionAssignment::new(AssignOp::Assign, head, expr);
352 check!(prettify!(assign, 20) == "(assign fuel 5)");
353 }
354
355 #[test]
356 fn metric_fexp_number_works() {
357 let m = MetricFluentExpression::number(Number::from(10));
358 check!(prettify!(m, 10) == "10");
359 }
360
361 #[test]
362 fn metric_fexp_total_time_works() {
363 let m = MetricFluentExpression::total_time();
364 check!(prettify!(m, 10) == "total-time");
365 }
366
367 #[test]
368 fn metric_fexp_function_works() {
369 let m =
370 MetricFluentExpression::function(FunctionSymbol::from("fuel"), vec![Name::new("r")]);
371 check!(prettify!(m, 20) == "(fuel r)");
372 }
373
374 #[test]
375 fn metric_fexp_is_violated_works() {
376 let m = MetricFluentExpression::is_violated(PreferenceName::string("p1"));
377 check!(prettify!(m, 20) == "(is-violated p1)");
378 }
379
380 #[test]
381 fn fexp_multi_op_addition_works() {
382 let a = FluentExpression::number(Number::from(1));
383 let b = FluentExpression::number(Number::from(2));
384 let c = FluentExpression::number(Number::from(3));
385 let e = FluentExpression::multi_op(MultiOp::Addition, a, [b, c]);
386 check!(prettify!(e, 20) == "(+ 1 2 3)");
387 }
388
389 #[test]
390 fn fexp_multi_op_multiplication_works() {
391 let a = FluentExpression::number(Number::from(2));
392 let b = FluentExpression::number(Number::from(3));
393 let c = FluentExpression::number(Number::from(4));
394 let e = FluentExpression::multi_op(MultiOp::Multiplication, a, [b, c]);
395 check!(prettify!(e, 20) == "(* 2 3 4)");
396 }
397
398 #[test]
399 fn fexp_binary_op_subtraction_works() {
400 let a = FluentExpression::number(Number::from(5));
401 let b = FluentExpression::number(Number::from(3));
402 let e = FluentExpression::binary_op(BinaryOp::Subtraction, a, b);
403 check!(prettify!(e, 10) == "(- 5 3)");
404 }
405
406 #[test]
407 fn fexp_binary_op_division_works() {
408 let a = FluentExpression::number(Number::from(10));
409 let b = FluentExpression::number(Number::from(2));
410 let e = FluentExpression::binary_op(BinaryOp::Division, a, b);
411 check!(prettify!(e, 10) == "(/ 10 2)");
412 }
413
414 #[test]
415 fn fexp_binary_op_multiplication_works() {
416 let a = FluentExpression::number(Number::from(3));
417 let b = FluentExpression::number(Number::from(4));
418 let e = FluentExpression::binary_op(BinaryOp::Multiplication, a, b);
419 check!(prettify!(e, 10) == "(* 3 4)");
420 }
421
422 #[test]
423 fn fexp_da_negative_works() {
424 let inner = DurativeActionFluentExpression::fluent_expression(FluentExpression::number(
425 Number::from(5),
426 ));
427 let e = DurativeActionFluentExpression::negative(inner);
428 check!(prettify!(e, 10) == "(- 5)");
429 }
430
431 #[test]
432 fn fexp_da_binary_op_subtraction_works() {
433 let a = DurativeActionFluentExpression::fluent_expression(FluentExpression::number(
434 Number::from(5),
435 ));
436 let b = DurativeActionFluentExpression::fluent_expression(FluentExpression::number(
437 Number::from(3),
438 ));
439 let e = DurativeActionFluentExpression::binary_op(BinaryOp::Subtraction, a, b);
440 check!(prettify!(e, 10) == "(- 5 3)");
441 }
442
443 #[test]
444 fn fexp_da_binary_op_division_works() {
445 let a = DurativeActionFluentExpression::fluent_expression(FluentExpression::number(
446 Number::from(10),
447 ));
448 let b = DurativeActionFluentExpression::fluent_expression(FluentExpression::number(
449 Number::from(2),
450 ));
451 let e = DurativeActionFluentExpression::binary_op(BinaryOp::Division, a, b);
452 check!(prettify!(e, 10) == "(/ 10 2)");
453 }
454
455 #[test]
456 fn fexp_da_binary_op_multiplication_works() {
457 let a = DurativeActionFluentExpression::fluent_expression(FluentExpression::number(
458 Number::from(3),
459 ));
460 let b = DurativeActionFluentExpression::fluent_expression(FluentExpression::number(
461 Number::from(4),
462 ));
463 let e = DurativeActionFluentExpression::binary_op(BinaryOp::Multiplication, a, b);
464 check!(prettify!(e, 10) == "(* 3 4)");
465 }
466
467 #[test]
468 fn fexp_da_multi_op_addition_works() {
469 let a = DurativeActionFluentExpression::fluent_expression(FluentExpression::number(
470 Number::from(1),
471 ));
472 let b = DurativeActionFluentExpression::fluent_expression(FluentExpression::number(
473 Number::from(2),
474 ));
475 let c = DurativeActionFluentExpression::fluent_expression(FluentExpression::number(
476 Number::from(3),
477 ));
478 let e = DurativeActionFluentExpression::multi_op(MultiOp::Addition, a, [b, c]);
479 check!(prettify!(e, 20) == "(+ 1 2 3)");
480 }
481
482 #[test]
483 fn fexp_da_multi_op_multiplication_works() {
484 let a = DurativeActionFluentExpression::fluent_expression(FluentExpression::number(
485 Number::from(2),
486 ));
487 let b = DurativeActionFluentExpression::fluent_expression(FluentExpression::number(
488 Number::from(3),
489 ));
490 let c = DurativeActionFluentExpression::fluent_expression(FluentExpression::number(
491 Number::from(4),
492 ));
493 let e = DurativeActionFluentExpression::multi_op(MultiOp::Multiplication, a, [b, c]);
494 check!(prettify!(e, 20) == "(* 2 3 4)");
495 }
496
497 #[test]
498 fn fexp_da_assign_works() {
499 use crate::FunctionHead;
500 let head = FunctionHead::Simple(FunctionSymbol::from("x"));
501 let expr = DurativeActionFluentExpression::fluent_expression(FluentExpression::number(
502 Number::from(10),
503 ));
504 let e = DurativeActionFluentExpression::Assign(AssignOp::Increase, head, Box::new(expr));
505 check!(prettify!(e, 20) == "(increase x 10)");
506 }
507
508 #[test]
509 fn fcomp_less_than_works() {
510 let a = FluentExpression::number(Number::from(3));
511 let b = FluentExpression::number(Number::from(4));
512 let fc = FluentComparison::new(BinaryComparison::LessThan, a, b);
513 check!(prettify!(fc, 10) == "(< 3 4)");
514 }
515
516 #[test]
517 fn fcomp_equal_works() {
518 let a = FluentExpression::number(Number::from(3));
519 let b = FluentExpression::number(Number::from(4));
520 let fc = FluentComparison::new(BinaryComparison::Equal, a, b);
521 check!(prettify!(fc, 10) == "(= 3 4)");
522 }
523
524 #[test]
525 fn fcomp_greater_or_equal_works() {
526 let a = FluentExpression::number(Number::from(3));
527 let b = FluentExpression::number(Number::from(4));
528 let fc = FluentComparison::new(BinaryComparison::GreaterOrEqual, a, b);
529 check!(prettify!(fc, 10) == "(>= 3 4)");
530 }
531
532 #[test]
533 fn fcomp_less_than_or_equal_works() {
534 let a = FluentExpression::number(Number::from(3));
535 let b = FluentExpression::number(Number::from(4));
536 let fc = FluentComparison::new(BinaryComparison::LessThanOrEqual, a, b);
537 check!(prettify!(fc, 10) == "(<= 3 4)");
538 }
539
540 #[test]
541 fn metric_fexp_negative_works() {
542 let inner = MetricFluentExpression::number(Number::from(5));
543 let m = MetricFluentExpression::negative(inner);
544 check!(prettify!(m, 10) == "(- 5)");
545 }
546
547 #[test]
548 fn metric_fexp_binary_op_subtraction_works() {
549 let a = MetricFluentExpression::number(Number::from(5));
550 let b = MetricFluentExpression::number(Number::from(3));
551 let m = MetricFluentExpression::binary_op(BinaryOp::Subtraction, a, b);
552 check!(prettify!(m, 10) == "(- 5 3)");
553 }
554
555 #[test]
556 fn metric_fexp_binary_op_multiplication_works() {
557 let a = MetricFluentExpression::number(Number::from(3));
558 let b = MetricFluentExpression::number(Number::from(4));
559 let m = MetricFluentExpression::binary_op(BinaryOp::Multiplication, a, b);
560 check!(prettify!(m, 10) == "(* 3 4)");
561 }
562
563 #[test]
564 fn metric_fexp_multi_op_addition_works() {
565 let a = MetricFluentExpression::number(Number::from(1));
566 let b = MetricFluentExpression::number(Number::from(2));
567 let c = MetricFluentExpression::number(Number::from(3));
568 let m = MetricFluentExpression::multi_op(MultiOp::Addition, a, [b, c]);
569 check!(prettify!(m, 20) == "(+ 1 2 3)");
570 }
571
572 #[test]
573 fn metric_fexp_multi_op_multiplication_works() {
574 let a = MetricFluentExpression::number(Number::from(2));
575 let b = MetricFluentExpression::number(Number::from(3));
576 let c = MetricFluentExpression::number(Number::from(4));
577 let m = MetricFluentExpression::multi_op(MultiOp::Multiplication, a, [b, c]);
578 check!(prettify!(m, 20) == "(* 2 3 4)");
579 }
580}