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
//! Provides parsers for initial state list elements.
use nom::branch::alt;
use nom::character::complete::multispace1;
use nom::combinator::map;
use nom::sequence::{preceded, tuple};
use crate::parsers::{
literal, parse_basic_function_term, parse_name, parse_number, prefix_expr, ParseResult, Span,
};
use crate::types::InitElement;
/// Parses initial state list elements.
///
/// ## Example
/// ```
/// # use pddl::parsers::{parse_init_el, preamble::*};
/// # use pddl::{AtomicFormula, BasicFunctionTerm, InitElement, Name, NameLiteral, Number, Predicate};
/// assert!(parse_init_el("(train-not-in-use train1)").is_value(
/// InitElement::new_literal(
/// NameLiteral::new(
/// AtomicFormula::new_predicate(
/// "train-not-in-use".into(),
/// ["train1".into()]
/// )
/// )
/// )
/// ));
///
/// assert!(parse_init_el("(at 10 (train-not-in-use train2))").is_value(
/// InitElement::new_at(
/// Number::from(10),
/// NameLiteral::new(
/// AtomicFormula::new_predicate(
/// "train-not-in-use".into(),
/// ["train2".into()]
/// )
/// )
/// )
/// ));
///
/// assert!(parse_init_el("(= (battery rover) 100)").is_value(
/// InitElement::new_is_value(
/// BasicFunctionTerm::new("battery".into(), ["rover".into()]),
/// Number::from(100)
/// )
/// ));
///
/// assert!(parse_init_el("(= (location rover) base)").is_value(
/// InitElement::new_is_object(
/// BasicFunctionTerm::new("location".into(), ["rover".into()]),
/// Name::from("base")
/// )
/// ));
/// ```
pub fn parse_init_el<'a, T: Into<Span<'a>>>(input: T) -> ParseResult<'a, InitElement> {
let literal_ = map(literal(parse_name), InitElement::new_literal);
// :timed-initial-literals
let at = map(
prefix_expr(
"at",
tuple((parse_number, preceded(multispace1, literal(parse_name)))),
),
|(time, name)| InitElement::new_at(time, name),
);
// :numeric-fluents
let is_numeric = map(
prefix_expr(
"=",
tuple((
parse_basic_function_term,
preceded(multispace1, parse_number),
)),
),
|(fun, value)| InitElement::new_is_value(fun, value),
);
// :object-fluents
let is_object = map(
prefix_expr(
"=",
tuple((parse_basic_function_term, preceded(multispace1, parse_name))),
),
|(fun, name)| InitElement::new_is_object(fun, name),
);
alt((literal_, at, is_numeric, is_object))(input.into())
}
impl crate::parsers::Parser for InitElement {
type Item = InitElement;
/// See [`parse_init_el`].
fn parse<'a, S: Into<Span<'a>>>(input: S) -> ParseResult<'a, Self::Item> {
parse_init_el(input)
}
}
#[cfg(test)]
mod tests {
use crate::parsers::UnwrapValue;
use crate::{AtomicFormula, BasicFunctionTerm, InitElement, Name, NameLiteral, Number, Parser};
#[test]
fn test_parse() {
assert!(InitElement::parse("(train-not-in-use train1)").is_value(
InitElement::new_literal(NameLiteral::new(AtomicFormula::new_predicate(
"train-not-in-use".into(),
["train1".into()]
)))
));
assert!(
InitElement::parse("(at 10 (train-not-in-use train2))").is_value(InitElement::new_at(
Number::from(10),
NameLiteral::new(AtomicFormula::new_predicate(
"train-not-in-use".into(),
["train2".into()]
))
))
);
assert!(
InitElement::parse("(= (battery rover) 100)").is_value(InitElement::new_is_value(
BasicFunctionTerm::new("battery".into(), ["rover".into()]),
Number::from(100)
))
);
assert!(InitElement::parse("(= (location rover) base)").is_value(
InitElement::new_is_object(
BasicFunctionTerm::new("location".into(), ["rover".into()]),
Name::from("base")
)
));
}
}