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
//! Provides parsers for (preferred) timed goal definitions.
use nom::branch::alt;
use nom::character::complete::multispace1;
use nom::combinator::{map, opt};
use nom::sequence::{terminated, tuple};
use crate::parsers::{parse_pref_name, parse_timed_gd};
use crate::parsers::{prefix_expr, ParseResult, Span};
use crate::types::PrefTimedGD;
/// Parser for (preferred) timed goal definitions.
///
/// ## Examples
/// ```
/// # use pddl::parsers::{parse_pref_timed_gd, preamble::*};
/// # use pddl::{AtomicFormula, GoalDefinition, Interval, PrefTimedGD, Term, TimedGD, TimeSpecifier};
/// assert!(parse_pref_timed_gd("(at start (= x y))").is_value(
/// PrefTimedGD::Required(
/// TimedGD::new_at(
/// TimeSpecifier::Start,
/// GoalDefinition::AtomicFormula(
/// AtomicFormula::new_equality(
/// Term::Name("x".into()),
/// Term::Name("y".into())
/// )
/// )
/// )
/// )
/// ));
///
///
/// assert!(parse_pref_timed_gd("(preference (over all (= x y)))").is_value(
/// PrefTimedGD::Preference(
/// None,
/// TimedGD::new_over(
/// Interval::All,
/// GoalDefinition::AtomicFormula(
/// AtomicFormula::new_equality(
/// Term::Name("x".into()),
/// Term::Name("y".into())
/// )
/// )
/// )
/// )
/// ));
///
/// assert!(parse_pref_timed_gd("(preference pref-name (over all (= x y)))").is_value(
/// PrefTimedGD::Preference(
/// Some("pref-name".into()),
/// TimedGD::new_over(
/// Interval::All,
/// GoalDefinition::AtomicFormula(
/// AtomicFormula::new_equality(
/// Term::Name("x".into()),
/// Term::Name("y".into())
/// )
/// )
/// )
/// )
/// ));
/// ```
pub fn parse_pref_timed_gd<'a, T: Into<Span<'a>>>(input: T) -> ParseResult<'a, PrefTimedGD> {
let required = map(parse_timed_gd, PrefTimedGD::from);
// :preferences
let preference = map(
prefix_expr(
"preference",
tuple((
opt(terminated(parse_pref_name, multispace1)),
parse_timed_gd,
)),
),
PrefTimedGD::from,
);
alt((preference, required))(input.into())
}
impl crate::parsers::Parser for PrefTimedGD {
type Item = PrefTimedGD;
/// See [`parse_pref_timed_gd`].
fn parse<'a, S: Into<Span<'a>>>(input: S) -> ParseResult<'a, Self::Item> {
parse_pref_timed_gd(input)
}
}
#[cfg(test)]
mod tests {
use crate::parsers::preamble::*;
use crate::{
AtomicFormula, GoalDefinition, Interval, PrefTimedGD, Term, TimeSpecifier, TimedGD,
};
#[test]
fn test_parse() {
assert!(
PrefTimedGD::parse("(at start (= x y))").is_value(PrefTimedGD::Required(
TimedGD::new_at(
TimeSpecifier::Start,
GoalDefinition::AtomicFormula(AtomicFormula::new_equality(
Term::Name("x".into()),
Term::Name("y".into())
))
)
))
);
assert!(
PrefTimedGD::parse("(preference (over all (= x y)))").is_value(
PrefTimedGD::Preference(
None,
TimedGD::new_over(
Interval::All,
GoalDefinition::AtomicFormula(AtomicFormula::new_equality(
Term::Name("x".into()),
Term::Name("y".into())
))
)
)
)
);
assert!(
PrefTimedGD::parse("(preference pref-name (over all (= x y)))").is_value(
PrefTimedGD::Preference(
Some("pref-name".into()),
TimedGD::new_over(
Interval::All,
GoalDefinition::AtomicFormula(AtomicFormula::new_equality(
Term::Name("x".into()),
Term::Name("y".into())
))
)
)
)
);
}
}