use crate::parsers::error::R;
use crate::parsers::sexpr::Node;
use crate::parsers::{
alt, parse_effect, parse_effect_condition, parse_gd, parse_p_effect, prefix, typed_variables,
};
use crate::types::{ConditionalEffect, ForallConditionalEffect, WhenConditionalEffect};
#[allow(deprecated)]
pub(crate) fn parse_c_effect(node: &Node) -> R<ConditionalEffect> {
alt(
node,
&[
&|n| parse_forall_c_effect(n).map(ConditionalEffect::from),
&|n| parse_when_c_effect(n).map(ConditionalEffect::from),
&|n| parse_p_effect(n).map(ConditionalEffect::from),
],
)
}
pub(crate) fn parse_forall_c_effect(node: &Node) -> R<ForallConditionalEffect> {
prefix(node, "forall", |args| {
Ok((typed_variables(args.next()?)?, parse_effect(args.next()?)?))
})
.map(ForallConditionalEffect::from)
}
#[allow(deprecated)]
pub(crate) fn parse_when_c_effect(node: &Node) -> R<WhenConditionalEffect> {
prefix(node, "when", |args| {
Ok((
parse_gd(args.next()?)?,
parse_effect_condition(args.next()?)?,
))
})
.map(WhenConditionalEffect::from)
}
crate::parsers::impl_parser!(
ConditionalEffect => parse_c_effect,
ForallConditionalEffect => parse_forall_c_effect,
WhenConditionalEffect => parse_when_c_effect,
);
#[cfg(test)]
#[allow(deprecated)]
mod tests {
use crate::parsers::UnwrapValue;
use assert2::check;
use crate::*;
#[test]
fn test_parse() {
let value = ConditionalEffect::from_str("(= x y)").unwrap();
check!(
value
== ConditionalEffect::new_primitive_effect(
PrimitiveEffect::from_str("(= x y)").unwrap()
)
);
let value = ConditionalEffect::from_str("(not (= ?a B))").unwrap();
check!(
value
== ConditionalEffect::new_primitive_effect(
PrimitiveEffect::from_str("(not (= ?a B))").unwrap()
)
);
let value = ConditionalEffect::from_str("(forall (?a ?b) (= ?a ?b))").unwrap();
check!(
value
== ConditionalEffect::new_forall(
TypedList::from_iter([
Typed::new_object(Variable::new_string("a")),
Typed::new_object(Variable::new_string("b")),
]),
Effects::from_str("(= ?a ?b)").unwrap()
)
);
let input = r"(when
(and (has-hot-chocolate ?p ?c) (has-marshmallows ?c))
(and (person-is-happy ?p)))";
let value = ConditionalEffect::from_str(input).unwrap();
check!(
value
== ConditionalEffect::new_when(
GoalDescription::from_str(
"(and (has-hot-chocolate ?p ?c) (has-marshmallows ?c))"
)
.unwrap(),
EffectCondition::from_str("(and (person-is-happy ?p))").unwrap()
)
);
}
#[test]
fn test_parse_forall() {
let value = ForallConditionalEffect::from_str("(forall (?a ?b) (= ?a ?b))").unwrap();
check!(
value
== ForallConditionalEffect::new(
TypedList::from_iter([
Typed::new_object(Variable::new_string("a")),
Typed::new_object(Variable::new_string("b")),
]),
Effects::from_str("(= ?a ?b)").unwrap()
)
);
}
#[test]
fn test_parse_when() {
let input = r"(when
(and (has-hot-chocolate ?p ?c) (has-marshmallows ?c))
(and (person-is-happy ?p)))";
let value = WhenConditionalEffect::from_str(input).unwrap();
check!(
value
== WhenConditionalEffect::new(
GoalDescription::from_str(
"(and (has-hot-chocolate ?p ?c) (has-marshmallows ?c))"
)
.unwrap(),
EffectCondition::from_str("(and (person-is-happy ?p))").unwrap()
)
);
}
#[test]
fn doc_example_0() {
check!(
ConditionalEffect::from_str("(= x y)").is_value(ConditionalEffect::Effect(
PrimitiveEffect::AtomicFormula(AtomicFormula::Equality(
EqualityAtomicFormula::new(Term::Name("x".into()), Term::Name("y".into()))
))
))
);
check!(
ConditionalEffect::from_str("(not (= ?a B))").is_value(ConditionalEffect::Effect(
PrimitiveEffect::NotAtomicFormula(AtomicFormula::Equality(
EqualityAtomicFormula::new(Term::Variable("a".into()), Term::Name("B".into()))
))
))
);
check!(
ConditionalEffect::from_str("(forall (?a ?b) (= ?a ?b))").is_value(
ConditionalEffect::forall(
TypedList::from_iter([
Typed::object(Variable::new_string("a")),
Typed::object(Variable::new_string("b")),
]),
Effects::new(ConditionalEffect::Effect(PrimitiveEffect::AtomicFormula(
AtomicFormula::Equality(EqualityAtomicFormula::new(
Term::Variable("a".into()),
Term::Variable("b".into())
))
)))
)
)
);
let input = r"(when
(and (has-hot-chocolate ?p ?c) (has-marshmallows ?c))
(and (person-is-happy ?p)))";
check!(
ConditionalEffect::from_str(input).is_value(ConditionalEffect::when(
GoalDescription::new_and([
GoalDescription::new_atomic_formula(AtomicFormula::new_predicate(
Predicate::new("has-hot-chocolate".into()),
[Term::Variable("p".into()), Term::Variable("c".into())]
)),
GoalDescription::new_atomic_formula(AtomicFormula::new_predicate(
Predicate::new("has-marshmallows".into()),
[Term::Variable("c".into())]
)),
]),
EffectCondition::from_iter([PrimitiveEffect::new(AtomicFormula::new_predicate(
Predicate::new("person-is-happy".into()),
[Term::Variable("p".into())]
))])
))
);
}
#[test]
fn doc_example_1() {
check!(
ForallConditionalEffect::from_str("(forall (?a ?b) (= ?a ?b))").is_value(
ForallConditionalEffect::new(
TypedList::from_iter([
Typed::object(Variable::new_string("a")),
Typed::object(Variable::new_string("b")),
]),
Effects::new(ConditionalEffect::Effect(PrimitiveEffect::AtomicFormula(
AtomicFormula::Equality(EqualityAtomicFormula::new(
Term::Variable("a".into()),
Term::Variable("b".into())
))
)))
)
)
);
}
#[test]
fn doc_example_2() {
let input = r"(when
(and (has-hot-chocolate ?p ?c) (has-marshmallows ?c))
(and (person-is-happy ?p)))";
check!(
WhenConditionalEffect::from_str(input).is_value(WhenConditionalEffect::new(
GoalDescription::new_and([
GoalDescription::new_atomic_formula(AtomicFormula::new_predicate(
Predicate::new("has-hot-chocolate".into()),
[Term::Variable("p".into()), Term::Variable("c".into())]
)),
GoalDescription::new_atomic_formula(AtomicFormula::new_predicate(
Predicate::new("has-marshmallows".into()),
[Term::Variable("c".into())]
)),
]),
EffectCondition::from_iter([PrimitiveEffect::new(AtomicFormula::new_predicate(
Predicate::new("person-is-happy".into()),
[Term::Variable("p".into())]
))])
))
);
}
#[test]
fn doc_example_3() {
let value = ConditionalEffect::from_str("(= x y)").unwrap();
check!(
value
== ConditionalEffect::new_primitive_effect(
PrimitiveEffect::from_str("(= x y)").unwrap()
)
);
let value = ConditionalEffect::from_str("(not (= ?a B))").unwrap();
check!(
value
== ConditionalEffect::new_primitive_effect(
PrimitiveEffect::from_str("(not (= ?a B))").unwrap()
)
);
let value = ConditionalEffect::from_str("(forall (?a ?b) (= ?a ?b))").unwrap();
check!(
value
== ConditionalEffect::forall(
TypedList::from_iter([
Typed::object(Variable::new_string("a")),
Typed::object(Variable::new_string("b")),
]),
Effects::from_str("(= ?a ?b)").unwrap()
)
);
let input = r"(when
(and (has-hot-chocolate ?p ?c) (has-marshmallows ?c))
(and (person-is-happy ?p)))";
let value = ConditionalEffect::from_str(input).unwrap();
check!(
value
== ConditionalEffect::when(
GoalDescription::from_str(
"(and (has-hot-chocolate ?p ?c) (has-marshmallows ?c))"
)
.unwrap(),
EffectCondition::from_str("(and (person-is-happy ?p))").unwrap()
)
);
}
#[test]
fn doc_example_4() {
let value = ForallConditionalEffect::from_str("(forall (?a ?b) (= ?a ?b))").unwrap();
check!(
value
== ForallConditionalEffect::new(
TypedList::from_iter([
Typed::object(Variable::new_string("a")),
Typed::object(Variable::new_string("b")),
]),
Effects::from_str("(= ?a ?b)").unwrap()
)
);
}
#[test]
fn doc_example_5() {
let input = r"(when
(and (has-hot-chocolate ?p ?c) (has-marshmallows ?c))
(and (person-is-happy ?p)))";
let value = WhenConditionalEffect::from_str(input).unwrap();
check!(
value
== WhenConditionalEffect::new(
GoalDescription::from_str(
"(and (has-hot-chocolate ?p ?c) (has-marshmallows ?c))"
)
.unwrap(),
EffectCondition::from_str("(and (person-is-happy ?p))").unwrap()
)
);
}
}