-
Notifications
You must be signed in to change notification settings - Fork 1
/
cond_effect.rs
94 lines (85 loc) · 3.04 KB
/
cond_effect.rs
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
//! Provides parsers for conditional effects.
use nom::branch::alt;
use nom::combinator::map;
use crate::parsers::{parse_p_effect, ParseResult, Span};
use crate::parsers::{prefix_expr, space_separated_list0};
use crate::types::ConditionalEffect;
/// Parses conditional effects.
///
/// ## Example
/// ```
/// # use pddl::parsers::{parse_cond_effect, preamble::*};
/// # use pddl::{AtomicFormula, CEffect, ConditionalEffect, EqualityAtomicFormula, PEffect, Term};
/// assert!(parse_cond_effect("(= x y)").is_value(
/// ConditionalEffect::Single(
/// PEffect::AtomicFormula(AtomicFormula::Equality(
/// EqualityAtomicFormula::new(
/// Term::Name("x".into()),
/// Term::Name("y".into()))
/// )
/// )
/// )
/// ));
///
/// assert!(parse_cond_effect("(and (= x y) (not (= ?a B)))").is_value(
/// ConditionalEffect::All(vec![
/// PEffect::AtomicFormula(AtomicFormula::Equality(
/// EqualityAtomicFormula::new(
/// Term::Name("x".into()),
/// Term::Name("y".into()))
/// )
/// ),
/// PEffect::NotAtomicFormula(AtomicFormula::Equality(
/// EqualityAtomicFormula::new(
/// Term::Variable("a".into()),
/// Term::Name("B".into()))
/// )
/// )
/// ])
/// ));
/// ```
pub fn parse_cond_effect<'a, T: Into<Span<'a>>>(input: T) -> ParseResult<'a, ConditionalEffect> {
let exactly = map(parse_p_effect, ConditionalEffect::from);
let all = map(
prefix_expr("and", space_separated_list0(parse_p_effect)),
ConditionalEffect::from,
);
alt((all, exactly))(input.into())
}
impl crate::parsers::Parser for ConditionalEffect {
type Item = ConditionalEffect;
/// See [`parse_cond_effect`].
fn parse<'a, S: Into<Span<'a>>>(input: S) -> ParseResult<'a, Self::Item> {
parse_cond_effect(input)
}
}
#[cfg(test)]
mod tests {
use crate::parsers::UnwrapValue;
use crate::{AtomicFormula, ConditionalEffect, EqualityAtomicFormula, PEffect, Parser, Term};
#[test]
fn test_parse() {
assert!(
ConditionalEffect::parse("(= x y)").is_value(ConditionalEffect::Single(
PEffect::AtomicFormula(AtomicFormula::Equality(EqualityAtomicFormula::new(
Term::Name("x".into()),
Term::Name("y".into())
)))
))
);
assert!(
ConditionalEffect::parse("(and (= x y) (not (= ?a B)))").is_value(
ConditionalEffect::All(vec![
PEffect::AtomicFormula(AtomicFormula::Equality(EqualityAtomicFormula::new(
Term::Name("x".into()),
Term::Name("y".into())
))),
PEffect::NotAtomicFormula(AtomicFormula::Equality(EqualityAtomicFormula::new(
Term::Variable("a".into()),
Term::Name("B".into())
)))
])
)
);
}
}