pddl_core/parsers/actions/
c_effect.rs1use crate::parsers::error::R;
4use crate::parsers::sexpr::Node;
5use crate::parsers::{
6 alt, parse_effect, parse_effect_condition, parse_gd, parse_p_effect, prefix, typed_variables,
7};
8use crate::types::{ConditionalEffect, ForallConditionalEffect, WhenConditionalEffect};
9
10#[allow(deprecated)]
13pub(crate) fn parse_c_effect(node: &Node) -> R<ConditionalEffect> {
14 alt(
15 node,
16 &[
17 &|n| parse_forall_c_effect(n).map(ConditionalEffect::from),
18 &|n| parse_when_c_effect(n).map(ConditionalEffect::from),
19 &|n| parse_p_effect(n).map(ConditionalEffect::from),
20 ],
21 )
22}
23
24pub(crate) fn parse_forall_c_effect(node: &Node) -> R<ForallConditionalEffect> {
29 prefix(node, "forall", |args| {
30 Ok((typed_variables(args.next()?)?, parse_effect(args.next()?)?))
31 })
32 .map(ForallConditionalEffect::from)
33}
34
35#[allow(deprecated)]
40pub(crate) fn parse_when_c_effect(node: &Node) -> R<WhenConditionalEffect> {
41 prefix(node, "when", |args| {
42 Ok((
43 parse_gd(args.next()?)?,
44 parse_effect_condition(args.next()?)?,
45 ))
46 })
47 .map(WhenConditionalEffect::from)
48}
49
50crate::parsers::impl_parser!(
51 ConditionalEffect => parse_c_effect,
52 ForallConditionalEffect => parse_forall_c_effect,
53 WhenConditionalEffect => parse_when_c_effect,
54);
55
56#[cfg(test)]
57#[allow(deprecated)]
58mod tests {
59 use crate::parsers::UnwrapValue;
60 use assert2::check;
61
62 use crate::*;
63
64 #[test]
65 fn test_parse() {
66 let value = ConditionalEffect::from_str("(= x y)").unwrap();
67 check!(
68 value
69 == ConditionalEffect::new_primitive_effect(
70 PrimitiveEffect::from_str("(= x y)").unwrap()
71 )
72 );
73
74 let value = ConditionalEffect::from_str("(not (= ?a B))").unwrap();
75 check!(
76 value
77 == ConditionalEffect::new_primitive_effect(
78 PrimitiveEffect::from_str("(not (= ?a B))").unwrap()
79 )
80 );
81
82 let value = ConditionalEffect::from_str("(forall (?a ?b) (= ?a ?b))").unwrap();
83 check!(
84 value
85 == ConditionalEffect::new_forall(
86 TypedList::from_iter([
87 Typed::new_object(Variable::new_string("a")),
88 Typed::new_object(Variable::new_string("b")),
89 ]),
90 Effects::from_str("(= ?a ?b)").unwrap()
91 )
92 );
93
94 let input = r"(when
95 (and (has-hot-chocolate ?p ?c) (has-marshmallows ?c))
96 (and (person-is-happy ?p)))";
97 let value = ConditionalEffect::from_str(input).unwrap();
98 check!(
99 value
100 == ConditionalEffect::new_when(
101 GoalDescription::from_str(
102 "(and (has-hot-chocolate ?p ?c) (has-marshmallows ?c))"
103 )
104 .unwrap(),
105 EffectCondition::from_str("(and (person-is-happy ?p))").unwrap()
106 )
107 );
108 }
109
110 #[test]
111 fn test_parse_forall() {
112 let value = ForallConditionalEffect::from_str("(forall (?a ?b) (= ?a ?b))").unwrap();
113 check!(
114 value
115 == ForallConditionalEffect::new(
116 TypedList::from_iter([
117 Typed::new_object(Variable::new_string("a")),
118 Typed::new_object(Variable::new_string("b")),
119 ]),
120 Effects::from_str("(= ?a ?b)").unwrap()
121 )
122 );
123 }
124
125 #[test]
126 fn test_parse_when() {
127 let input = r"(when
128 (and (has-hot-chocolate ?p ?c) (has-marshmallows ?c))
129 (and (person-is-happy ?p)))";
130
131 let value = WhenConditionalEffect::from_str(input).unwrap();
132 check!(
133 value
134 == WhenConditionalEffect::new(
135 GoalDescription::from_str(
136 "(and (has-hot-chocolate ?p ?c) (has-marshmallows ?c))"
137 )
138 .unwrap(),
139 EffectCondition::from_str("(and (person-is-happy ?p))").unwrap()
140 )
141 );
142 }
143
144 #[test]
145 fn doc_example_0() {
146 check!(
147 ConditionalEffect::from_str("(= x y)").is_value(ConditionalEffect::Effect(
148 PrimitiveEffect::AtomicFormula(AtomicFormula::Equality(
149 EqualityAtomicFormula::new(Term::Name("x".into()), Term::Name("y".into()))
150 ))
151 ))
152 );
153 check!(
154 ConditionalEffect::from_str("(not (= ?a B))").is_value(ConditionalEffect::Effect(
155 PrimitiveEffect::NotAtomicFormula(AtomicFormula::Equality(
156 EqualityAtomicFormula::new(Term::Variable("a".into()), Term::Name("B".into()))
157 ))
158 ))
159 );
160
161 check!(
162 ConditionalEffect::from_str("(forall (?a ?b) (= ?a ?b))").is_value(
163 ConditionalEffect::forall(
164 TypedList::from_iter([
165 Typed::object(Variable::new_string("a")),
166 Typed::object(Variable::new_string("b")),
167 ]),
168 Effects::new(ConditionalEffect::Effect(PrimitiveEffect::AtomicFormula(
169 AtomicFormula::Equality(EqualityAtomicFormula::new(
170 Term::Variable("a".into()),
171 Term::Variable("b".into())
172 ))
173 )))
174 )
175 )
176 );
177
178 let input = r"(when
179 (and (has-hot-chocolate ?p ?c) (has-marshmallows ?c))
180 (and (person-is-happy ?p)))";
181 check!(
182 ConditionalEffect::from_str(input).is_value(ConditionalEffect::when(
183 GoalDescription::new_and([
184 GoalDescription::new_atomic_formula(AtomicFormula::new_predicate(
185 Predicate::new("has-hot-chocolate".into()),
186 [Term::Variable("p".into()), Term::Variable("c".into())]
187 )),
188 GoalDescription::new_atomic_formula(AtomicFormula::new_predicate(
189 Predicate::new("has-marshmallows".into()),
190 [Term::Variable("c".into())]
191 )),
192 ]),
193 EffectCondition::from_iter([PrimitiveEffect::new(AtomicFormula::new_predicate(
194 Predicate::new("person-is-happy".into()),
195 [Term::Variable("p".into())]
196 ))])
197 ))
198 );
199 }
200
201 #[test]
202 fn doc_example_1() {
203 check!(
204 ForallConditionalEffect::from_str("(forall (?a ?b) (= ?a ?b))").is_value(
205 ForallConditionalEffect::new(
206 TypedList::from_iter([
207 Typed::object(Variable::new_string("a")),
208 Typed::object(Variable::new_string("b")),
209 ]),
210 Effects::new(ConditionalEffect::Effect(PrimitiveEffect::AtomicFormula(
211 AtomicFormula::Equality(EqualityAtomicFormula::new(
212 Term::Variable("a".into()),
213 Term::Variable("b".into())
214 ))
215 )))
216 )
217 )
218 );
219 }
220
221 #[test]
222 fn doc_example_2() {
223 let input = r"(when
224 (and (has-hot-chocolate ?p ?c) (has-marshmallows ?c))
225 (and (person-is-happy ?p)))";
226
227 check!(
228 WhenConditionalEffect::from_str(input).is_value(WhenConditionalEffect::new(
229 GoalDescription::new_and([
230 GoalDescription::new_atomic_formula(AtomicFormula::new_predicate(
231 Predicate::new("has-hot-chocolate".into()),
232 [Term::Variable("p".into()), Term::Variable("c".into())]
233 )),
234 GoalDescription::new_atomic_formula(AtomicFormula::new_predicate(
235 Predicate::new("has-marshmallows".into()),
236 [Term::Variable("c".into())]
237 )),
238 ]),
239 EffectCondition::from_iter([PrimitiveEffect::new(AtomicFormula::new_predicate(
240 Predicate::new("person-is-happy".into()),
241 [Term::Variable("p".into())]
242 ))])
243 ))
244 );
245 }
246
247 #[test]
248 fn doc_example_3() {
249 let value = ConditionalEffect::from_str("(= x y)").unwrap();
250 check!(
251 value
252 == ConditionalEffect::new_primitive_effect(
253 PrimitiveEffect::from_str("(= x y)").unwrap()
254 )
255 );
256
257 let value = ConditionalEffect::from_str("(not (= ?a B))").unwrap();
258 check!(
259 value
260 == ConditionalEffect::new_primitive_effect(
261 PrimitiveEffect::from_str("(not (= ?a B))").unwrap()
262 )
263 );
264
265 let value = ConditionalEffect::from_str("(forall (?a ?b) (= ?a ?b))").unwrap();
266 check!(
267 value
268 == ConditionalEffect::forall(
269 TypedList::from_iter([
270 Typed::object(Variable::new_string("a")),
271 Typed::object(Variable::new_string("b")),
272 ]),
273 Effects::from_str("(= ?a ?b)").unwrap()
274 )
275 );
276
277 let input = r"(when
278 (and (has-hot-chocolate ?p ?c) (has-marshmallows ?c))
279 (and (person-is-happy ?p)))";
280 let value = ConditionalEffect::from_str(input).unwrap();
281 check!(
282 value
283 == ConditionalEffect::when(
284 GoalDescription::from_str(
285 "(and (has-hot-chocolate ?p ?c) (has-marshmallows ?c))"
286 )
287 .unwrap(),
288 EffectCondition::from_str("(and (person-is-happy ?p))").unwrap()
289 )
290 );
291 }
292
293 #[test]
294 fn doc_example_4() {
295 let value = ForallConditionalEffect::from_str("(forall (?a ?b) (= ?a ?b))").unwrap();
296 check!(
297 value
298 == ForallConditionalEffect::new(
299 TypedList::from_iter([
300 Typed::object(Variable::new_string("a")),
301 Typed::object(Variable::new_string("b")),
302 ]),
303 Effects::from_str("(= ?a ?b)").unwrap()
304 )
305 );
306 }
307
308 #[test]
309 fn doc_example_5() {
310 let input = r"(when
311 (and (has-hot-chocolate ?p ?c) (has-marshmallows ?c))
312 (and (person-is-happy ?p)))";
313
314 let value = WhenConditionalEffect::from_str(input).unwrap();
315 check!(
316 value
317 == WhenConditionalEffect::new(
318 GoalDescription::from_str(
319 "(and (has-hot-chocolate ?p ?c) (has-marshmallows ?c))"
320 )
321 .unwrap(),
322 EffectCondition::from_str("(and (person-is-happy ?p))").unwrap()
323 )
324 );
325 }
326}