pddl_parser/domain/
action.rs1use nom::branch::alt;
2use nom::combinator::map;
3use nom::IResult;
4use serde::{Deserialize, Serialize};
5
6use super::durative_action::DurativeAction;
7use super::expression::Expression;
8use super::simple_action::SimpleAction;
9use crate::domain::typed_parameter::TypedParameter;
10use crate::error::ParserError;
11use crate::lexer::TokenStream;
12
13#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
15pub enum Action {
16 Simple(SimpleAction),
18 Durative(DurativeAction),
20}
21
22impl From<SimpleAction> for Action {
23 fn from(action: SimpleAction) -> Self {
24 Self::Simple(action)
25 }
26}
27
28impl From<DurativeAction> for Action {
29 fn from(action: DurativeAction) -> Self {
30 Self::Durative(action)
31 }
32}
33
34impl Action {
35 pub fn name(&self) -> &str {
37 match self {
38 Self::Simple(action) => &action.name,
39 Self::Durative(action) => &action.name,
40 }
41 }
42
43 pub fn parameters(&self) -> &[TypedParameter] {
45 match self {
46 Self::Simple(action) => &action.parameters,
47 Self::Durative(action) => &action.parameters,
48 }
49 }
50
51 pub fn precondition(&self) -> Option<Expression> {
53 match self {
54 Self::Simple(action) => action.precondition.clone(),
55 Self::Durative(action) => action.condition.clone(),
56 }
57 }
58
59 pub fn effect(&self) -> Expression {
61 match self {
62 Self::Simple(action) => action.effect.clone(),
63 Self::Durative(action) => action.effect.clone(),
64 }
65 }
66
67 pub fn parse(input: TokenStream) -> IResult<TokenStream, Action, ParserError> {
69 alt((
70 map(SimpleAction::parse, Self::Simple),
71 map(DurativeAction::parse, Self::Durative),
72 ))(input)
73 }
74
75 pub fn to_pddl(&self) -> String {
77 match self {
78 Self::Simple(action) => action.to_pddl(),
79 Self::Durative(action) => action.to_pddl(),
80 }
81 }
82}