Skip to main content

pddl_core/serde/
expressions.rs

1//! Fluent expressions, comparisons and the operators in them.
2
3use crate::serde::{deserialize_from_pddl, formula, serialize_display, Head, Keyed};
4use crate::{
5    AssignOp, BinaryComparison, BinaryOp, FluentComparison, FluentExpression, FunctionHead,
6    MetricFluentExpression, MultiOp, Optimization, Term,
7};
8use ::serde::{Serialize, Serializer};
9use std::fmt::Display;
10
11serialize_display!(AssignOp, BinaryComparison, BinaryOp, MultiOp, Optimization);
12
13/// Serializes `{"op": operands}`, keyed by the operator's PDDL spelling.
14pub(crate) fn operation<S: Serializer, T: Serialize + ?Sized>(
15    serializer: S,
16    op: &impl Display,
17    operands: &T,
18) -> Result<S::Ok, S::Error> {
19    Keyed(&op.to_string(), operands).serialize(serializer)
20}
21
22/// The operands of a multi-operand expression as one list.
23pub(crate) fn operands<'a, E>(first: &'a E, rest: &'a [E]) -> Vec<&'a E> {
24    std::iter::once(first).chain(rest).collect()
25}
26
27impl Serialize for FluentComparison {
28    /// `{">=": [a, b]}`.
29    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
30        operation(
31            serializer,
32            self.comparison(),
33            &(self.first(), self.second()),
34        )
35    }
36}
37
38impl Serialize for FunctionHead {
39    /// `["symbol", terms...]`, also when there are no terms.
40    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
41        match self {
42            FunctionHead::Simple(symbol) => Head(symbol, &[] as &[Term]).serialize(serializer),
43            FunctionHead::WithTerms(symbol, terms) => Head(symbol, terms).serialize(serializer),
44        }
45    }
46}
47
48impl Serialize for FluentExpression {
49    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
50        match self {
51            FluentExpression::Number(number) => number.serialize(serializer),
52            FluentExpression::Function(head) => head.serialize(serializer),
53            FluentExpression::Negative(value) => Keyed("-", value).serialize(serializer),
54            FluentExpression::BinaryOp(op, a, b) => operation(serializer, op, &(a, b)),
55            FluentExpression::MultiOp(op, first, rest) => {
56                operation(serializer, op, &operands(&**first, rest))
57            }
58        }
59    }
60}
61
62impl Serialize for MetricFluentExpression {
63    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
64        type M = MetricFluentExpression;
65        match self {
66            M::BinaryOp(op, a, b) => operation(serializer, op, &(a, b)),
67            M::MultiOp(op, first, rest) => operation(serializer, op, &operands(&**first, rest)),
68            M::Negative(value) => Keyed("-", value).serialize(serializer),
69            M::Number(number) => number.serialize(serializer),
70            M::Function(symbol, names) => Head(symbol, names).serialize(serializer),
71            M::TotalTime => serializer.serialize_str("total-time"),
72            M::IsViolated(name) => Keyed("is-violated", name).serialize(serializer),
73        }
74    }
75}
76
77deserialize_from_pddl!(
78    AssignOp => formula,
79    BinaryComparison => formula,
80    BinaryOp => formula,
81    MultiOp => formula,
82    Optimization => formula,
83    FluentComparison => formula,
84    FunctionHead => formula,
85    FluentExpression => formula,
86    MetricFluentExpression => formula,
87);