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
//! Provides parsers for preference goal definitions.
use nom::branch::alt;
use nom::character::complete::multispace1;
use nom::combinator::{map, opt};
use nom::sequence::preceded;
use nom::Parser;
use crate::parsers::{parse_gd, parse_pref_name};
use crate::parsers::{prefix_expr, ParseResult, Span};
use crate::types::{Preference, PreferenceGoalDefinition};
/// Parser for goal definitions.
///
/// ## Examples
/// ```
/// # use pddl::parsers::{parse_pref_gd, preamble::*};
/// # use pddl::{AtomicFormula, EqualityAtomicFormula, GoalDefinition, Literal, Preference, PreferenceName, PreferenceGoalDefinition, Term, Variable};
/// // Simple goal definition.
/// assert!(parse_pref_gd("(= x y)").is_value(
/// PreferenceGoalDefinition::Goal(
/// GoalDefinition::AtomicFormula(
/// AtomicFormula::new_equality(
/// Term::Name("x".into()),
/// Term::Name("y".into())
/// )
/// )
/// )
/// ));
///
/// // Named preference.
/// assert!(parse_pref_gd("(preference p (= x y))").is_value(
/// PreferenceGoalDefinition::Preference(
/// Preference::new(
/// Some(PreferenceName::from("p")),
/// GoalDefinition::AtomicFormula(
/// AtomicFormula::new_equality(
/// Term::Name("x".into()),
/// Term::Name("y".into())
/// )
/// )
/// )
/// )
/// ));
///
/// // Unnamed preference.
/// assert!(parse_pref_gd("(preference (= x y))").is_value(
/// PreferenceGoalDefinition::Preference(
/// Preference::new(
/// None,
/// GoalDefinition::AtomicFormula(
/// AtomicFormula::new_equality(
/// Term::Name("x".into()),
/// Term::Name("y".into())
/// )
/// )
/// )
/// )
/// ));
/// ```
pub fn parse_pref_gd<'a, T: Into<Span<'a>>>(input: T) -> ParseResult<'a, PreferenceGoalDefinition> {
// :preferences
let pref_named = map(
prefix_expr(
"preference",
(opt(parse_pref_name), preceded(multispace1, parse_gd)),
),
|(pref, gd)| PreferenceGoalDefinition::from_preference(Preference::new(pref, gd)),
);
let pref_unnamed = map(prefix_expr("preference", parse_gd), |gd| {
PreferenceGoalDefinition::from_preference(Preference::new(None, gd))
});
let gd = map(parse_gd, PreferenceGoalDefinition::from_gd);
alt((pref_named, pref_unnamed, gd)).parse(input.into())
}
impl crate::parsers::Parser for PreferenceGoalDefinition {
type Item = PreferenceGoalDefinition;
/// See [`parse_pref_gd`].
fn parse<'a, S: Into<Span<'a>>>(input: S) -> ParseResult<'a, Self::Item> {
parse_pref_gd(input)
}
}
#[cfg(test)]
mod tests {
use crate::parsers::preamble::*;
use crate::{
AtomicFormula, GoalDefinition, Preference, PreferenceGoalDefinition, PreferenceName, Term,
};
#[test]
fn test_parse() {
// Simple goal definition.
assert!(PreferenceGoalDefinition::parse("(= x y)").is_value(
PreferenceGoalDefinition::Goal(GoalDefinition::AtomicFormula(
AtomicFormula::new_equality(Term::Name("x".into()), Term::Name("y".into()))
))
));
// Named preference.
assert!(
PreferenceGoalDefinition::parse("(preference p (= x y))").is_value(
PreferenceGoalDefinition::Preference(Preference::new(
Some(PreferenceName::from("p")),
GoalDefinition::AtomicFormula(AtomicFormula::new_equality(
Term::Name("x".into()),
Term::Name("y".into())
))
))
)
);
// Unnamed preference.
assert!(
PreferenceGoalDefinition::parse("(preference (= x y))").is_value(
PreferenceGoalDefinition::Preference(Preference::new(
None,
GoalDefinition::AtomicFormula(AtomicFormula::new_equality(
Term::Name("x".into()),
Term::Name("y".into())
))
))
)
);
}
}