1use crate::pretty_print::{sealed, PrettyRenderer};
4use crate::types::{
5 ConditionalEffect, EffectCondition, Effects, ForallConditionalEffect, PrimitiveEffect,
6 WhenConditionalEffect,
7};
8use crate::visitor::{Accept, Visitor};
9use pretty::RcDoc;
10
11impl sealed::Sealed for PrimitiveEffect {}
12impl sealed::Sealed for ConditionalEffect {}
13impl sealed::Sealed for EffectCondition {}
14impl sealed::Sealed for ForallConditionalEffect {}
15impl sealed::Sealed for WhenConditionalEffect {}
16impl sealed::Sealed for Effects {}
17
18impl Visitor<PrimitiveEffect, RcDoc<'static>> for PrettyRenderer {
19 fn visit(&self, value: &PrimitiveEffect) -> RcDoc<'static> {
20 match value {
21 PrimitiveEffect::AtomicFormula(af) => af.accept(self),
22 PrimitiveEffect::NotAtomicFormula(af) => {
23 RcDoc::text("(not ").append(af.accept(self)).append(")")
24 }
25 PrimitiveEffect::AssignNumericFluent(op, head, exp) => RcDoc::text("(")
26 .append(op.accept(self))
27 .append(" ")
28 .append(head.accept(self))
29 .append(" ")
30 .append(exp.accept(self))
31 .append(")"),
32 PrimitiveEffect::AssignObjectFluent(ft, term) => match term {
33 Some(t) => RcDoc::text("(assign ")
34 .append(ft.accept(self))
35 .append(" ")
36 .append(t.accept(self))
37 .append(")"),
38 None => RcDoc::text("(assign ")
39 .append(ft.accept(self))
40 .append(" undefined)"),
41 },
42 }
43 }
44}
45
46impl Visitor<ConditionalEffect, RcDoc<'static>> for PrettyRenderer {
47 fn visit(&self, value: &ConditionalEffect) -> RcDoc<'static> {
48 match value {
49 ConditionalEffect::Effect(pe) => pe.accept(self),
50 ConditionalEffect::Forall(fc) => fc.accept(self),
51 ConditionalEffect::When(wc) => wc.accept(self),
52 }
53 }
54}
55
56impl Visitor<ForallConditionalEffect, RcDoc<'static>> for PrettyRenderer {
57 fn visit(&self, value: &ForallConditionalEffect) -> RcDoc<'static> {
58 RcDoc::text("(forall (")
59 .append(value.variables.accept(self))
60 .append(") ")
61 .append(value.effects.accept(self))
62 .append(")")
63 }
64}
65
66impl Visitor<WhenConditionalEffect, RcDoc<'static>> for PrettyRenderer {
67 fn visit(&self, value: &WhenConditionalEffect) -> RcDoc<'static> {
68 RcDoc::text("(when ")
69 .append(value.condition.accept(self))
70 .append(" ")
71 .append(value.effect.accept(self))
72 .append(")")
73 }
74}
75
76impl Visitor<EffectCondition, RcDoc<'static>> for PrettyRenderer {
77 fn visit(&self, value: &EffectCondition) -> RcDoc<'static> {
78 match value {
79 EffectCondition::Single(pe) => pe.accept(self),
80 EffectCondition::All(pes) => {
81 if pes.is_empty() {
82 RcDoc::text("(and)")
83 } else {
84 RcDoc::text("(and")
85 .append(RcDoc::softline())
86 .append(RcDoc::intersperse(
87 pes.iter().map(|pe| pe.accept(self)),
88 RcDoc::softline(),
89 ))
90 .nest(4)
91 .group()
92 .append(")")
93 }
94 }
95 }
96 }
97}
98
99impl Visitor<Effects, RcDoc<'static>> for PrettyRenderer {
100 fn visit(&self, value: &Effects) -> RcDoc<'static> {
101 RcDoc::text("(and")
102 .append(RcDoc::softline())
103 .append(RcDoc::intersperse(
104 value.iter().map(|ce| ce.accept(self)),
105 RcDoc::softline(),
106 ))
107 .nest(4)
108 .group()
109 .append(")")
110 }
111}
112
113#[cfg(test)]
114mod tests {
115 use super::*;
116 use crate::pretty_print::prettify;
117 use crate::visitor::Accept;
118 use crate::{
119 AtomicFormula, ConditionalEffect, EffectCondition, FunctionSymbol, GoalDescription, Name,
120 Predicate, PrimitiveEffect, Term,
121 };
122 use assert2::check;
123
124 #[test]
125 fn p_effect_atomic() {
126 let af = AtomicFormula::predicate(
127 Predicate::string("at"),
128 vec![
129 Term::new_name(Name::new("B")),
130 Term::new_name(Name::new("home")),
131 ],
132 );
133 let pe = PrimitiveEffect::new(af);
134 check!(prettify!(pe, 30) == "(at B home)");
135 }
136
137 #[test]
138 fn p_effect_not() {
139 let af = AtomicFormula::predicate(
140 Predicate::string("at"),
141 vec![Term::new_name(Name::new("B"))],
142 );
143 let pe = PrimitiveEffect::not(af);
144 check!(prettify!(pe, 30) == "(not (at B))");
145 }
146
147 #[test]
148 fn p_effect_assign() {
149 let pe = PrimitiveEffect::numeric_fluent(
150 crate::AssignOp::Assign,
151 crate::FunctionHead::new(FunctionSymbol::from("battery")),
152 crate::FluentExpression::number(10),
153 );
154 check!(prettify!(pe, 30) == "(assign battery 10)");
155 }
156
157 #[test]
158 fn effects_single() {
159 let af = AtomicFormula::predicate(
160 Predicate::string("at"),
161 vec![Term::new_name(Name::new("B"))],
162 );
163 let pe = PrimitiveEffect::new(af);
164 let ce = ConditionalEffect::new_primitive_effect(pe);
165 let effects = Effects::new(ce);
166 check!(prettify!(effects, 30) == "(and (at B))");
167 }
168
169 #[test]
170 fn p_effect_assign_object_fluent_with_term() {
171 use crate::{FunctionSymbol, FunctionTerm};
172 let ft = FunctionTerm::new(
173 FunctionSymbol::from("loc"),
174 vec![Term::new_name(Name::new("x"))],
175 );
176 let pe = PrimitiveEffect::object_fluent(ft, Some(Term::new_name(Name::new("y"))));
177 check!(prettify!(pe, 30) == "(assign (loc x) y)");
178 }
179
180 #[test]
181 fn p_effect_assign_object_fluent_without_term() {
182 use crate::{FunctionSymbol, FunctionTerm};
183 let ft = FunctionTerm::new(FunctionSymbol::from("loc"), vec![]);
184 let pe = PrimitiveEffect::object_fluent(ft, None);
185 check!(prettify!(pe, 30) == "(assign (loc) undefined)");
186 }
187
188 #[test]
189 fn c_effect_forall() {
190 use crate::{ToTyped, Type, TypedVariables, Variable};
191 let vars: TypedVariables = vec![Variable::string("x").to_typed(Type::OBJECT)].into();
192 let af = AtomicFormula::predicate(
193 Predicate::string("p"),
194 vec![Term::new_variable(Variable::from("x"))],
195 );
196 let pe = PrimitiveEffect::new(af);
197 let effects = Effects::new(ConditionalEffect::new_primitive_effect(pe));
198 let ce = ConditionalEffect::forall(vars, effects);
199 check!(prettify!(ce, 40) == "(forall (?x) (and (p ?x)))");
200 }
201
202 #[test]
203 fn c_effect_when() {
204 let cond = GoalDescription::and(Vec::<GoalDescription>::new());
205 let ce_inner = EffectCondition::all(Vec::new());
206 let ce = ConditionalEffect::when(cond, ce_inner);
207 check!(prettify!(ce, 30) == "(when (and) (and))");
208 }
209
210 #[test]
211 fn conditional_effect_all_empty() {
212 let ce = EffectCondition::all(Vec::new());
213 check!(prettify!(ce, 20) == "(and)");
214 }
215
216 #[test]
217 fn conditional_effect_all_non_empty() {
218 let af = AtomicFormula::predicate(
219 Predicate::string("at"),
220 vec![Term::new_name(Name::new("x"))],
221 );
222 let pe1 = PrimitiveEffect::new(af.clone());
223 let pe2 = PrimitiveEffect::new(af);
224 let ce = EffectCondition::all(vec![pe1, pe2]);
225 check!(prettify!(ce, 30) == "(and (at x) (at x))");
226 }
227
228 #[test]
229 fn effects_multi_effect() {
230 let af1 = AtomicFormula::predicate(
231 Predicate::string("at"),
232 vec![Term::new_name(Name::new("x"))],
233 );
234 let af2 = AtomicFormula::predicate(
235 Predicate::string("at"),
236 vec![Term::new_name(Name::new("y"))],
237 );
238 let ce1 = ConditionalEffect::new_primitive_effect(PrimitiveEffect::new(af1));
239 let ce2 = ConditionalEffect::new_primitive_effect(PrimitiveEffect::new(af2));
240 let effects = Effects::and(vec![ce1, ce2]);
241 check!(prettify!(effects, 40) == "(and (at x) (at y))");
242 }
243}