1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//! Provides parsers for action definitions.
use nom::bytes::complete::tag;
use nom::character::complete::multispace1;
use nom::combinator::{map, opt};
use nom::sequence::{preceded, tuple};
use crate::parsers::{empty_or, parens, prefix_expr, typed_list, ws, ParseResult, Span};
use crate::parsers::{parse_action_symbol, parse_effect, parse_pre_gd, parse_variable};
use crate::types::ActionDefinition;
/// Parses an action definition.
///
/// ## Example
/// ```
/// # use pddl::{ActionDefinition, ActionSymbol, AtomicFormula, CEffect, Effects, GoalDefinition, Name, PEffect, Predicate, PreferenceGD, PreconditionGoalDefinitions, PreconditionGoalDefinition, Term, ToTyped, TypedList, Variable};
/// # use pddl::parsers::{parse_action_def, Span, UnwrapValue};
/// let input = r#"(:action take-out
/// :parameters (?x - physob)
/// :precondition (not (= ?x B))
/// :effect (not (in ?x))
/// )"#;
///
/// let action = parse_action_def(Span::new(input));
///
/// assert!(action.is_value(
/// ActionDefinition::new(
/// ActionSymbol::from_str("take-out"),
/// TypedList::from_iter([
/// Variable::from_str("x").to_typed("physob")
/// ]),
/// PreconditionGoalDefinitions::from(
/// PreconditionGoalDefinition::Preference(PreferenceGD::from_gd(
/// GoalDefinition::new_not(
/// GoalDefinition::AtomicFormula(
/// AtomicFormula::new_equality(
/// Term::Variable(Variable::from_str("x")),
/// Term::Name(Name::new("B"))
/// )
/// )
/// )
/// )
/// )),
/// Some(Effects::new(CEffect::new_p_effect(
/// PEffect::NotAtomicFormula(
/// AtomicFormula::new_predicate(
/// Predicate::from_str("in"),
/// vec![Term::Variable(Variable::from_str("x"))]
/// )
/// )
/// )))
/// )
/// ));
/// ```
pub fn parse_action_def<'a, T: Into<Span<'a>>>(input: T) -> ParseResult<'a, ActionDefinition> {
let precondition = preceded(
tag(":precondition"),
preceded(multispace1, empty_or(parse_pre_gd)),
);
let effect = preceded(
tag(":effect"),
preceded(multispace1, empty_or(parse_effect)),
);
let action_def_body = tuple((opt(ws(precondition)), opt(ws(effect))));
let parameters = preceded(
tag(":parameters"),
preceded(multispace1, parens(typed_list(parse_variable))),
);
let action_def = prefix_expr(
":action",
tuple((
parse_action_symbol,
preceded(multispace1, parameters),
ws(action_def_body),
)),
);
map(action_def, |(symbol, params, (preconditions, effects))| {
ActionDefinition::new(
symbol,
params,
preconditions.flatten().into(),
effects.flatten(),
)
})(input.into())
}
impl crate::parsers::Parser for ActionDefinition {
type Item = ActionDefinition;
/// Parses an action definition.
///
/// ## Example
/// ```
/// # use pddl::{ActionDefinition, ActionSymbol, AtomicFormula, CEffect, Effects, GoalDefinition, Name, PEffect, Predicate, PreferenceGD, PreconditionGoalDefinitions, PreconditionGoalDefinition, Term, ToTyped, TypedList, Variable, Parser};
/// # use pddl::parsers::{parse_action_def, Span, UnwrapValue};
/// let input = r#"(:action take-out
/// :parameters (?x - physob)
/// :precondition (not (= ?x B))
/// :effect (not (in ?x))
/// )"#;
///
/// let (_, action) = ActionDefinition::parse(input).unwrap();
///
/// assert_eq!(action,
/// ActionDefinition::new(
/// ActionSymbol::from_str("take-out"),
/// TypedList::from_iter([
/// Variable::from_str("x").to_typed("physob")
/// ]),
/// PreconditionGoalDefinitions::from_str("(not (= ?x B))").unwrap(),
/// Some(Effects::from_str("(not (in ?x))").unwrap())
/// )
/// );
/// ```
///
/// ## See also
/// See [`parse_action_def`].
fn parse<'a, S: Into<Span<'a>>>(input: S) -> ParseResult<'a, Self::Item> {
parse_action_def(input)
}
}
#[cfg(test)]
mod tests {
use crate::{
ActionDefinition, ActionSymbol, Effects, Parser, PreconditionGoalDefinitions, ToTyped,
TypedList, Variable,
};
#[test]
fn test_parse() {
let input = r#"(:action take-out
:parameters (?x - physob)
:precondition (not (= ?x B))
:effect (not (in ?x))
)"#;
let (_, action) = ActionDefinition::parse(input).unwrap();
assert_eq!(
action,
ActionDefinition::new(
ActionSymbol::from_str("take-out"),
TypedList::from_iter([Variable::from_str("x").to_typed("physob")]),
PreconditionGoalDefinitions::from_str("(not (= ?x B))").unwrap(),
Some(Effects::from_str("(not (in ?x))").unwrap())
)
);
}
}