jia-parse 1.0.0

Command-line and library parser for PDDL and Jia model files.
Documentation
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
//! Recursive-descent parser for PDDL domain and problem files.
//!
//! Consumes a token stream from the [`crate::lexer`] and produces the AST defined in
//! [`crate::ast`]. The parser handles all PDDL features encountered in IPC benchmarks
//! (1998--2014), including:
//!
//! - Typing and type hierarchies (`:types`, `either`)
//! - Durative actions with temporal conditions/effects
//! - Numeric fluents, action costs, and metrics
//! - ADL quantifiers (`forall`, `exists`) and conditional effects (`when`)
//! - PDDL3 trajectory constraints and preferences
//! - Legacy PDDL 1.2 `:vars` in actions
//!
//! ## Entry points
//!
//! | Function | Input | Output |
//! |---|---|---|
//! | [`parse_domain_str`] | raw PDDL string | [`Domain`] |
//! | [`parse_problem_str`] | raw PDDL string | [`Problem`] |
//! | [`parse_domain`] | pre-tokenized `&[Token]` | [`Domain`] |
//! | [`parse_problem`] | pre-tokenized `&[Token]` | [`Problem`] |

mod condition;
mod cursor;
mod domain;
mod effect;
mod expr;
mod problem;
mod terms;
mod utils;

use crate::ast::{Domain, Problem};
use crate::error::ParseError;
use crate::lexer::Token;

// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------

/// Parse a pre-tokenized PDDL domain.
///
/// Prefer [`parse_domain_str`] unless you need to inspect or reuse the token stream.
///
/// # Arguments
///
/// * `tokens` - Token stream produced by [`crate::lexer::tokenize`]
///
/// # Returns
///
/// A [`Domain`] AST on success.
///
/// # Errors
///
/// Returns [`ParseError`] if the token stream does not represent a valid PDDL domain.
pub fn parse_domain(tokens: &[Token]) -> Result<Domain, ParseError> {
    let mut p = cursor::Parser::new(tokens);
    let mut domain = domain::parse_domain_def(&mut p)?;
    domain.sort_alphabetically();
    Ok(domain)
}

/// Parse a pre-tokenized PDDL problem.
///
/// Prefer [`parse_problem_str`] unless you need to inspect or reuse the token stream.
///
/// # Arguments
///
/// * `tokens` - Token stream produced by [`crate::lexer::tokenize`]
///
/// # Returns
///
/// A [`Problem`] AST on success.
///
/// # Errors
///
/// Returns [`ParseError`] if the token stream does not represent a valid PDDL problem.
pub fn parse_problem(tokens: &[Token]) -> Result<Problem, ParseError> {
    let mut p = cursor::Parser::new(tokens);
    let mut problem = problem::parse_problem_def(&mut p)?;
    problem.sort_alphabetically();
    Ok(problem)
}

/// Tokenize and parse a PDDL domain from a raw source string.
///
/// This is the primary convenience entry point for domain parsing. It chains
/// [`crate::lexer::tokenize`] and [`parse_domain`] in one call.
///
/// # Arguments
///
/// * `input` - The raw PDDL domain source text
///
/// # Returns
///
/// A [`Domain`] AST on success.
///
/// # Errors
///
/// Returns [`ParseError`] on tokenization failures (e.g. unexpected characters)
/// or parse failures (e.g. malformed S-expressions, unknown sections).
pub fn parse_domain_str(input: &str) -> Result<Domain, ParseError> {
    let tokens = crate::lexer::tokenize(input)?;
    parse_domain(&tokens)
}

/// Tokenize and parse a PDDL problem from a raw source string.
///
/// This is the primary convenience entry point for problem parsing. It chains
/// [`crate::lexer::tokenize`] and [`parse_problem`] in one call.
///
/// # Arguments
///
/// * `input` - The raw PDDL problem source text
///
/// # Returns
///
/// A [`Problem`] AST on success.
///
/// # Errors
///
/// Returns [`ParseError`] on tokenization failures or parse failures.
pub fn parse_problem_str(input: &str) -> Result<Problem, ParseError> {
    let tokens = crate::lexer::tokenize(input)?;
    parse_problem(&tokens)
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ast::{AssignOp, Condition, DurationConstraint, Effect, Optimization};

    #[test]
    fn test_parse_minimal_domain() {
        let input = r#"
(define (domain test)
  (:requirements :strips :typing)
  (:types block - object)
  (:predicates (on ?x - block ?y - block) (clear ?x - block))
)
"#;
        let domain = parse_domain_str(input).unwrap();
        assert_eq!(domain.name, "test");
        assert_eq!(domain.requirements.len(), 2);
        assert_eq!(domain.predicates.len(), 2);
        assert_eq!(domain.predicates[0].name, "clear");
        assert_eq!(domain.predicates[1].name, "on");
    }

    #[test]
    fn test_parse_durative_action() {
        let input = r#"
(define (domain test)
  (:durative-action move
    :parameters (?x - obj)
    :duration (= ?duration 5)
    :condition (and
      (at start (clear ?x))
      (over all (safe ?x))
    )
    :effect (and
      (at start (not (clear ?x)))
      (at end (moved ?x))
    )
  )
)
"#;
        let domain = parse_domain_str(input).unwrap();
        assert_eq!(domain.durative_actions.len(), 1);
        let da = &domain.durative_actions[0];
        assert_eq!(da.name, "move");
    }

    #[test]
    fn test_parse_minimal_problem() {
        let input = r#"
(define (problem test-prob)
  (:domain test)
  (:objects a b - block)
  (:init (on a b) (clear a) (= (cost) 0))
  (:goal (and (on b a)))
)
"#;
        let problem = parse_problem_str(input).unwrap();
        assert_eq!(problem.name, "test-prob");
        assert_eq!(problem.domain_name, "test");
        assert_eq!(problem.init.len(), 3);
    }

    #[test]
    fn test_parse_metric() {
        let input = r#"
(define (problem test)
  (:domain d)
  (:init)
  (:goal (and))
  (:metric minimize (total-time))
)
"#;
        let problem = parse_problem_str(input).unwrap();
        assert!(problem.metric.is_some());
        let m = problem.metric.unwrap();
        assert_eq!(m.optimization, Optimization::Minimize);
    }

    #[test]
    fn test_parse_functions_derived_and_duration_inequalities() {
        let input = r#"
(define (domain test)
  (:requirements :typing :numeric-fluents :durative-actions :derived-predicates)
  (:predicates (ready ?x - obj))
  (:functions (fuel ?x - obj) (total-cost) - number)
  (:derived (available ?x - obj) (ready ?x))
  (:durative-action wait
    :parameters (?x - obj)
    :duration (and (>= ?duration 1) (<= ?duration 5))
    :condition (and (at start (ready ?x)))
    :effect (and))
)
"#;

        let domain = parse_domain_str(input).unwrap();

        assert_eq!(domain.functions.len(), 2);
        assert_eq!(domain.derived_predicates.len(), 1);
        assert!(matches!(
            domain.durative_actions[0].duration,
            DurationConstraint::And(_)
        ));
    }

    #[test]
    fn test_parse_numeric_precondition() {
        let input = r#"
(define (domain test)
  (:durative-action act
    :parameters (?d - driver)
    :duration (= ?duration 10)
    :condition (and
      (at start (>= (time_available ?d) 10))
    )
    :effect (and
      (at start (decrease (time_available ?d) 10))
    )
  )
)
"#;
        let domain = parse_domain_str(input).unwrap();
        let da = &domain.durative_actions[0];
        assert!(da.condition.is_some());
        assert!(da.effect.is_some());
    }

    #[test]
    fn test_parse_broad_condition_variants() {
        let input = r#"
(define (problem test)
  (:domain d)
  (:init)
  (:goal
    (and
      (or (ready a) (ready b))
      (not (blocked a))
      (imply (ready a) (ready b))
      (forall (?x - obj) (ready ?x))
      (exists (?x - obj) (ready ?x))
      (preference (ready a))
      (at a loc)
      (over a b)
      (= a b)
      (= (cost) 0)
      (< (cost) 10)
      (> (cost) 0)
      (always (ready a))
      (sometime (ready b))
      (at-most-once (ready a))
      (within 5 (ready a))
      (sometime-before (ready a) (ready b))
      (sometime-after (ready a) (ready b))
      (always-within 3 (ready a) (ready b))
      (hold-during 1 2 (ready a))
      (hold-after 4 (ready a))
    ))
)
"#;

        let problem = parse_problem_str(input).unwrap();
        let conditions = match &problem.goal {
            Condition::And(conditions) => Some(conditions),
            _ => None,
        }
        .unwrap();

        assert!(conditions.iter().any(|c| matches!(c, Condition::Or(_))));
        assert!(conditions.iter().any(|c| matches!(c, Condition::Not(_))));
        assert!(conditions
            .iter()
            .any(|c| matches!(c, Condition::Imply(_, _))));
        assert!(conditions
            .iter()
            .any(|c| matches!(c, Condition::Forall { .. })));
        assert!(conditions
            .iter()
            .any(|c| matches!(c, Condition::Exists { .. })));
        assert!(conditions
            .iter()
            .any(|c| matches!(c, Condition::Preference { name: None, .. })));
        assert!(conditions.iter().any(|c| matches!(
            c,
            Condition::Predicate(predicate) if predicate.name == "at"
        )));
        assert!(conditions.iter().any(|c| matches!(
            c,
            Condition::Predicate(predicate) if predicate.name == "over"
        )));
        assert!(conditions
            .iter()
            .any(|c| matches!(c, Condition::Equals(_, _))));
        assert!(conditions
            .iter()
            .any(|c| matches!(c, Condition::NumericComparison { .. })));
        assert!(conditions.iter().any(|c| matches!(c, Condition::Always(_))));
        assert!(conditions
            .iter()
            .any(|c| matches!(c, Condition::Sometime(_))));
        assert!(conditions
            .iter()
            .any(|c| matches!(c, Condition::AtMostOnce(_))));
        assert!(conditions
            .iter()
            .any(|c| matches!(c, Condition::Within(_, _))));
        assert!(conditions
            .iter()
            .any(|c| matches!(c, Condition::SometimeBefore(_, _))));
        assert!(conditions
            .iter()
            .any(|c| matches!(c, Condition::SometimeAfter(_, _))));
        assert!(conditions
            .iter()
            .any(|c| matches!(c, Condition::AlwaysWithin(_, _, _))));
        assert!(conditions
            .iter()
            .any(|c| matches!(c, Condition::HoldDuring(_, _, _))));
        assert!(conditions
            .iter()
            .any(|c| matches!(c, Condition::HoldAfter(_, _))));
    }

    #[test]
    fn test_parse_broad_effect_variants() {
        let input = r#"
(define (domain test)
  (:action a
    :parameters (?x - obj)
    :precondition (and)
    :effect
      (and
        (at ?x loc)
        (assign (cost) 1)
        (increase (cost) 2)
        (scale-up (cost) 3)
        (when (ready ?x) (decrease (cost) 1))))
  (:durative-action d
    :parameters (?x - obj)
    :duration (= ?duration 1)
    :condition (and)
    :effect
      (and
        (at start (at ?x loc))
        (at end (scale-down (cost) 2))))
)
"#;

        let domain = parse_domain_str(input).unwrap();
        let effects = domain.actions[0]
            .effect
            .as_ref()
            .and_then(|effect| match effect {
                Effect::And(effects) => Some(effects),
                _ => None,
            })
            .unwrap();

        assert!(effects.iter().any(|e| matches!(
            e,
            Effect::Predicate(predicate) if predicate.name == "at"
        )));
        assert!(effects.iter().any(|e| matches!(
            e,
            Effect::NumericAssign {
                op: AssignOp::Assign,
                ..
            }
        )));
        assert!(effects.iter().any(|e| matches!(
            e,
            Effect::NumericAssign {
                op: AssignOp::Increase,
                ..
            }
        )));
        assert!(effects.iter().any(|e| matches!(
            e,
            Effect::NumericAssign {
                op: AssignOp::ScaleUp,
                ..
            }
        )));
        assert!(effects.iter().any(|e| matches!(e, Effect::When { .. })));

        let durative_effects = domain.durative_actions[0]
            .effect
            .as_ref()
            .and_then(|effect| match effect {
                Effect::And(effects) => Some(effects),
                _ => None,
            })
            .unwrap();
        assert!(durative_effects
            .iter()
            .any(|e| matches!(e, Effect::AtStart(_))));
        assert!(durative_effects
            .iter()
            .any(|e| matches!(e, Effect::AtEnd(_))));
    }

    #[test]
    fn parser_skips_unknown_domain_and_problem_sections() {
        let domain = parse_domain_str(
            r#"
(define (domain test)
  (:requirements :strips)
  (:unknown-section (nested value))
  (legacy-section (ignored value))
  (:action a
    :parameters ()
    :precondition (and)
    :unknown-action-field (ignored value)
    :effect (and))
  (:durative-action d
    :parameters ()
    :duration (= ?duration 1)
    :unknown-durative-field (ignored value)
    :condition (and)
    :effect (and))
)
"#,
        )
        .unwrap();
        assert_eq!(domain.actions.len(), 1);
        assert_eq!(domain.durative_actions.len(), 1);

        let problem = parse_problem_str(
            r#"
(define (problem p)
  (:domain test)
  (:unknown-section (nested value))
  (legacy-section (ignored value))
  (:init)
  (:goal (and))
)
"#,
        )
        .unwrap();
        assert_eq!(problem.domain_name, "test");
    }

    #[test]
    fn parser_skips_unknown_action_fields_with_sexp_values() {
        let domain = parse_domain_str(
            r#"
(define (domain test)
  (:action a
    :parameters ()
    :precondition (and)
    :unknown-action-field (ignored value)
    :effect (and))
  (:durative-action d
    :parameters ()
    :duration (= ?duration 1)
    :condition (and)
    :unknown-durative-field (ignored value)
    :effect (and))
)
"#,
        )
        .unwrap();

        assert_eq!(domain.actions.len(), 1);
        assert_eq!(domain.durative_actions.len(), 1);
    }

    #[test]
    fn parser_reports_malformed_conditions_effects_and_metrics() {
        for input in [
            "(define (problem p) (:domain d) (:goal 42))",
            "(define (problem p) (:domain d) (:goal (42)))",
            "(define (problem p) (:domain d) (:goal (> ?x 1)))",
            "(define (problem p) (:domain d) (:goal (at",
            "(define (problem p) (:domain d) (:goal (over",
            "(define (problem p) (:domain d) (:goal (and)) (:metric fastest 1))",
            "(define (problem p) (:domain d) (:goal (and)) (:metric minimize ?x))",
            "(define (problem p) (:domain d) (:goal (and)) (:metric minimize (42)))",
        ] {
            assert!(parse_problem_str(input).is_err(), "{input}");
        }

        for input in [
            "(define (domain d) (:action a :parameters () :precondition (and) :effect (42)))",
            "(define (domain d) (:action a :parameters () :precondition (and) :effect (at",
            "(define (domain d) (:action a :parameters () :precondition (and) :effect (increase (cost) ?x)))",
        ] {
            assert!(parse_domain_str(input).is_err(), "{input}");
        }
    }
}