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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
//! Errors in the domain or problem, found before any plan runs.
use alloc::string::String;
use core::fmt;
/// The domain or problem is malformed, so no plan can be validated against it.
///
/// [`Validator::new`](crate::Validator::new) checks every name and arity up front, so a
/// plan's verdict never depends on a typo in the domain.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct ModelError {
/// Where the problem is, such as ``action `drive` `` or `goal`.
pub location: String,
/// What is wrong.
pub kind: ModelErrorKind,
}
impl ModelError {
/// Builds an error at `location`.
pub(crate) fn new(location: impl Into<String>, kind: ModelErrorKind) -> Self {
Self {
location: location.into(),
kind,
}
}
}
/// The kinds of [`ModelError`]. All names are in ASCII lowercase.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum ModelErrorKind {
/// The problem names another domain in `(:domain ...)`.
DomainMismatch {
/// The name of the domain given to the validator.
domain: String,
/// The domain the problem names.
problem_domain: String,
},
/// A type is used but not declared.
UnknownType(String),
/// A predicate is used but not declared.
UnknownPredicate(String),
/// A function is used but not declared.
UnknownFunction(String),
/// An object or constant is used but not declared.
UnknownObject(String),
/// A variable is used outside the parameters and quantifiers that bind it.
UnboundVariable(String),
/// A predicate or function is applied to the wrong number of arguments.
Arity {
/// The predicate or function.
symbol: String,
/// The number of parameters it is declared with.
expected: usize,
/// The number of arguments it is applied to.
found: usize,
},
/// Two actions share a name.
DuplicateAction(String),
/// One parameter or quantifier list declares a variable twice, such as `(?x ?x)`.
DuplicateVariable(String),
/// An action effect or the initial state sets a derived predicate, which only its
/// rule may define.
DerivedPredicateSet(String),
/// An effect or the initial state sets an equality `(= a b)`.
EqualitySet,
/// Two timed initial literals at the same time make one atom both true and false,
/// such as `(at 1 (q))` and `(at 1 (not (q)))`.
ContradictoryTimedLiterals {
/// The ground atom, such as `(at r1 base)`.
atom: String,
/// The time of the first of the two literals, formatted as a decimal number.
time: String,
},
}
impl fmt::Display for ModelError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}: {}", self.location, self.kind)
}
}
impl fmt::Display for ModelErrorKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::DomainMismatch {
domain,
problem_domain,
} => write!(
f,
"the problem is for domain `{problem_domain}`, not `{domain}`"
),
Self::UnknownType(name) => write!(f, "unknown type `{name}`"),
Self::UnknownPredicate(name) => write!(f, "unknown predicate `{name}`"),
Self::UnknownFunction(name) => write!(f, "unknown function `{name}`"),
Self::UnknownObject(name) => write!(f, "unknown object `{name}`"),
Self::UnboundVariable(name) => write!(f, "unbound variable `?{name}`"),
Self::Arity {
symbol,
expected,
found,
} => write!(
f,
"`{symbol}` takes {expected} arguments but is given {found}"
),
Self::DuplicateAction(name) => write!(f, "action `{name}` is defined twice"),
Self::DuplicateVariable(name) => write!(f, "variable `?{name}` is declared twice"),
Self::DerivedPredicateSet(name) => {
write!(f, "derived predicate `{name}` cannot be set directly")
}
Self::EqualitySet => f.write_str("`=` cannot be set"),
Self::ContradictoryTimedLiterals { atom, time } => {
write!(
f,
"timed literals make `{atom}` both true and false at {time}"
)
}
}
}
}
impl core::error::Error for ModelError {}
#[cfg(test)]
mod tests {
//! Pins the exact text of every model error, as users read it.
use alloc::string::ToString;
use super::*;
#[test]
fn model_errors_display_location_and_kind() {
let name = || "drive".to_string();
let cases = [
(
ModelErrorKind::DomainMismatch {
domain: "trucks".to_string(),
problem_domain: "boats".to_string(),
},
"the problem is for domain `boats`, not `trucks`",
),
(ModelErrorKind::UnknownType(name()), "unknown type `drive`"),
(
ModelErrorKind::UnknownPredicate(name()),
"unknown predicate `drive`",
),
(
ModelErrorKind::UnknownFunction(name()),
"unknown function `drive`",
),
(
ModelErrorKind::UnknownObject(name()),
"unknown object `drive`",
),
(
ModelErrorKind::UnboundVariable(name()),
"unbound variable `?drive`",
),
(
ModelErrorKind::Arity {
symbol: name(),
expected: 2,
found: 3,
},
"`drive` takes 2 arguments but is given 3",
),
(
ModelErrorKind::DuplicateAction(name()),
"action `drive` is defined twice",
),
(
ModelErrorKind::DuplicateVariable(name()),
"variable `?drive` is declared twice",
),
(
ModelErrorKind::DerivedPredicateSet(name()),
"derived predicate `drive` cannot be set directly",
),
(ModelErrorKind::EqualitySet, "`=` cannot be set"),
(
ModelErrorKind::ContradictoryTimedLiterals {
atom: "(open shop)".to_string(),
time: "1.5".to_string(),
},
"timed literals make `(open shop)` both true and false at 1.5",
),
];
for (kind, text) in cases {
let error = ModelError::new("action `drive`", kind);
assert_eq!(error.to_string(), alloc::format!("action `drive`: {text}"));
}
}
}