pddl_core/parsers/actions/
p_effect.rs1use crate::parsers::error::R;
4use crate::parsers::sexpr::Node;
5use crate::parsers::{
6 alt, atomic_formula, parens, parse_assign_op, parse_f_exp, parse_f_head, parse_function_term,
7 parse_term, prefix,
8};
9use crate::types::PrimitiveEffect;
10
11pub(crate) fn parse_p_effect(node: &Node) -> R<PrimitiveEffect> {
15 alt(
16 node,
17 &[
18 &|n| {
19 prefix(n, "not", |args| atomic_formula(args.next()?, parse_term))
20 .map(PrimitiveEffect::not)
21 },
22 &|n| {
23 prefix(n, "assign", |args| {
24 let term = parse_function_term(args.next()?)?;
25 args.expect("undefined")?;
26 Ok(term)
27 })
28 .map(|term| PrimitiveEffect::object_fluent(term, None))
29 },
30 &|n| {
31 prefix(n, "assign", |args| {
32 Ok((
33 parse_function_term(args.next()?)?,
34 parse_term(args.next()?)?,
35 ))
36 })
37 .map(|(f_term, term)| PrimitiveEffect::object_fluent(f_term, Some(term)))
38 },
39 &|n| {
40 parens(n, |args| {
41 Ok((
42 parse_assign_op(args.next()?)?,
43 parse_f_head(args.next()?)?,
44 parse_f_exp(args.next()?)?,
45 ))
46 })
47 .map(|(op, head, exp)| PrimitiveEffect::numeric_fluent(op, head, exp))
48 },
49 &|n| atomic_formula(n, parse_term).map(PrimitiveEffect::new),
50 ],
51 )
52}
53
54crate::parsers::impl_parser!(PrimitiveEffect => parse_p_effect);
55
56#[cfg(test)]
57mod tests {
58 use crate::parsers::UnwrapValue;
59 use assert2::check;
60
61 use crate::*;
62
63 #[test]
64 fn it_works() {
65 let input = "(can-move ?from-waypoint ?to-waypoint)";
66 let _effect = PrimitiveEffect::from_str(input).unwrap();
67 }
68
69 #[test]
70 fn not_works() {
71 let input = "(not (at B ?m))";
72 check!(let Ok(PrimitiveEffect::NotAtomicFormula(_)) = PrimitiveEffect::from_str(input));
73 }
74
75 #[test]
76 fn test_parse() {
77 check!(
78 PrimitiveEffect::from_str("(= x y)").is_value(PrimitiveEffect::AtomicFormula(
79 AtomicFormula::Equality(EqualityAtomicFormula::new(
80 Term::Name("x".into()),
81 Term::Name("y".into())
82 ))
83 ))
84 );
85
86 check!(PrimitiveEffect::from_str("(not (= ?a B))").is_value(
87 PrimitiveEffect::NotAtomicFormula(AtomicFormula::Equality(EqualityAtomicFormula::new(
88 Term::Variable("a".into()),
89 Term::Name("B".into())
90 )))
91 ));
92
93 check!(PrimitiveEffect::from_str("(assign fun-sym 1.23)").is_value(
94 PrimitiveEffect::numeric_fluent(
95 AssignOp::Assign,
96 FunctionHead::new(FunctionSymbol::string("fun-sym")),
97 FluentExpression::number(1.23)
98 )
99 ));
100
101 check!(PrimitiveEffect::from_str("(assign fun-sym 1.23)").is_value(
102 PrimitiveEffect::numeric_fluent(
103 AssignOp::Assign,
104 FunctionHead::new(FunctionSymbol::string("fun-sym")),
105 FluentExpression::number(1.23)
106 )
107 ));
108
109 check!(
110 PrimitiveEffect::from_str("(assign (fun-sym) undefined)").is_value(
111 PrimitiveEffect::object_fluent(
112 FunctionTerm::new(FunctionSymbol::string("fun-sym"), []),
113 None
114 )
115 )
116 );
117
118 check!(
119 PrimitiveEffect::from_str("(assign (fun-sym) something)").is_value(
120 PrimitiveEffect::object_fluent(
121 FunctionTerm::new(FunctionSymbol::string("fun-sym"), []),
122 Some(Term::Name("something".into()))
123 )
124 )
125 );
126 }
127
128 #[test]
129 fn doc_example_0() {
130 check!(
131 PrimitiveEffect::from_str("(= x y)").is_value(PrimitiveEffect::AtomicFormula(
132 AtomicFormula::Equality(EqualityAtomicFormula::new(
133 Term::Name("x".into()),
134 Term::Name("y".into())
135 ))
136 ))
137 );
138
139 check!(PrimitiveEffect::from_str("(not (= ?a B))").is_value(
140 PrimitiveEffect::NotAtomicFormula(AtomicFormula::Equality(EqualityAtomicFormula::new(
141 Term::Variable("a".into()),
142 Term::Name("B".into())
143 )))
144 ));
145
146 check!(PrimitiveEffect::from_str("(assign fun-sym 1.23)").is_value(
147 PrimitiveEffect::numeric_fluent(
148 AssignOp::Assign,
149 FunctionHead::new(FunctionSymbol::string("fun-sym")),
150 FluentExpression::number(1.23)
151 )
152 ));
153
154 check!(PrimitiveEffect::from_str("(assign fun-sym 1.23)").is_value(
155 PrimitiveEffect::numeric_fluent(
156 AssignOp::Assign,
157 FunctionHead::new(FunctionSymbol::string("fun-sym")),
158 FluentExpression::number(1.23)
159 )
160 ));
161
162 check!(
163 PrimitiveEffect::from_str("(assign (fun-sym) undefined)").is_value(
164 PrimitiveEffect::object_fluent(
165 FunctionTerm::new(FunctionSymbol::string("fun-sym"), []),
166 None
167 )
168 )
169 );
170
171 check!(
172 PrimitiveEffect::from_str("(assign (fun-sym) something)").is_value(
173 PrimitiveEffect::object_fluent(
174 FunctionTerm::new(FunctionSymbol::string("fun-sym"), []),
175 Some(Term::Name("something".into()))
176 )
177 )
178 );
179 }
180}