From 8670753ecbb2182ee689a6231ee5b2444cfdec2f Mon Sep 17 00:00:00 2001 From: rpgoldman Date: Thu, 16 Nov 2023 13:49:54 -0600 Subject: [PATCH] Fix alternative HDDL plan formats. (#12) * Fix alternative HDDL plan formats. HDDL permits tasks to be entered in three different variant syntaxes. Previously, only the one emitted by PANDA was tested. Now the s-expression and comma-separated notations are also working. * Fix tests for CCL. --- .../tests/example-plan-comma-separated.hddl | 20 +++++++ hddl-utils/tests/example-plan-sexp.hddl | 20 +++++++ hddl-utils/tests/hddl-data.lisp | 59 +++++++++++++++++-- hddl-utils/tests/tests.lisp | 22 +++++++ hddl/hddl-pprint.lisp | 26 ++++++-- version.lisp-expr | 2 +- 6 files changed, 139 insertions(+), 10 deletions(-) create mode 100644 hddl-utils/tests/example-plan-comma-separated.hddl create mode 100644 hddl-utils/tests/example-plan-sexp.hddl diff --git a/hddl-utils/tests/example-plan-comma-separated.hddl b/hddl-utils/tests/example-plan-comma-separated.hddl new file mode 100644 index 0000000..aeb7067 --- /dev/null +++ b/hddl-utils/tests/example-plan-comma-separated.hddl @@ -0,0 +1,20 @@ +==> +0 drive truck-0, city-loc-2, city-loc-1 +1 pick-up truck-0, city-loc-1, package-0, capacity-0, capacity-1 +2 drive truck-0, city-loc-1, city-loc-0 +3 drop truck-0, city-loc-0, package-0, capacity-0, capacity-1 +4 drive truck-0, city-loc-0, city-loc-1 +5 pick-up truck-0, city-loc-1, package-1, capacity-0, capacity-1 +6 drive truck-0, city-loc-1, city-loc-2 +7 drop truck-0, city-loc-2, package-1, capacity-0, capacity-1 +root 15 14 +8 load truck-0, city-loc-1, package-0 -> m-load 1 +9 unload truck-0, city-loc-0, package-0 -> m-unload 3 +10 get-to truck-0, city-loc-1, -> m-drive-to 0 +11 unload truck-0, city-loc-2, package-1 -> m-unload 7 +12 get-to truck-0, city-loc-0 -> m-drive-to 2 +13 load truck-0, city-loc-1, package-1 -> m-load 5 +14 deliver package-0, city-loc-0 -> m-deliver 10 8 12 9 +15 deliver package-1, city-loc-2 -> m-deliver 16 13 17 11 +16 get-to truck-0, city-loc-1 -> m-drive-to 4 +17 get-to truck-0, city-loc-2 -> m-drive-to 6 diff --git a/hddl-utils/tests/example-plan-sexp.hddl b/hddl-utils/tests/example-plan-sexp.hddl new file mode 100644 index 0000000..23a736c --- /dev/null +++ b/hddl-utils/tests/example-plan-sexp.hddl @@ -0,0 +1,20 @@ +==> +0 (drive truck-0 city-loc-2 city-loc-1) +1 (pick-up truck-0 city-loc-1 package-0 capacity-0 capacity-1) +2 (drive truck-0 city-loc-1 city-loc-0) +3 (drop truck-0 city-loc-0 package-0 capacity-0 capacity-1) +4 (drive truck-0 city-loc-0 city-loc-1) +5 (pick-up truck-0 city-loc-1 package-1 capacity-0 capacity-1) +6 (drive truck-0 city-loc-1 city-loc-2) +7 (drop truck-0 city-loc-2 package-1 capacity-0 capacity-1) +root 15 14 +8 (load truck-0 city-loc-1 package-0) -> m-load 1 +9 (unload truck-0 city-loc-0 package-0) -> m-unload 3 +10 (get-to truck-0 city-loc-1) -> m-drive-to 0 +11 (unload truck-0 city-loc-2 package-1) -> m-unload 7 +12 (get-to truck-0 city-loc-0) -> m-drive-to 2 +13 (load truck-0 city-loc-1 package-1) -> m-load 5 +14 (deliver package-0 city-loc-0) -> m-deliver 10 8 12 9 +15 (deliver package-1 city-loc-2) -> m-deliver 16 13 17 11 +16 (get-to truck-0 city-loc-1) -> m-drive-to 4 +17 (get-to truck-0 city-loc-2) -> m-drive-to 6 diff --git a/hddl-utils/tests/hddl-data.lisp b/hddl-utils/tests/hddl-data.lisp index 7b1a5da..368a0f6 100644 --- a/hddl-utils/tests/hddl-data.lisp +++ b/hddl-utils/tests/hddl-data.lisp @@ -9,16 +9,67 @@ (cl:defparameter hddl-utils-tests::*method* '(:task get-to :parameters (?l - location))) -#| +(cl:defparameter hddl-utils-tests::*parsed-plan* + '(:HDDL-PLAN :ACTIONS + ((0 DRIVE TRUCK-0 CITY-LOC-2 + CITY-LOC-1) + (1 PICK-UP TRUCK-0 CITY-LOC-1 + PACKAGE-0 CAPACITY-0 CAPACITY-1) + (2 DRIVE TRUCK-0 CITY-LOC-1 + CITY-LOC-0) + (3 DROP TRUCK-0 CITY-LOC-0 + PACKAGE-0 CAPACITY-0 CAPACITY-1) + (4 DRIVE TRUCK-0 CITY-LOC-0 + CITY-LOC-1) + (5 PICK-UP TRUCK-0 CITY-LOC-1 + PACKAGE-1 CAPACITY-0 CAPACITY-1) + (6 DRIVE TRUCK-0 CITY-LOC-1 + CITY-LOC-2) + (7 DROP TRUCK-0 CITY-LOC-2 + PACKAGE-1 CAPACITY-0 CAPACITY-1)) + :ROOTS (15 14) :DECOMPOSITIONS + ((8 + (LOAD TRUCK-0 CITY-LOC-1 + PACKAGE-0) + M-LOAD 1) + (9 + (UNLOAD TRUCK-0 CITY-LOC-0 + PACKAGE-0) + M-UNLOAD 3) + (10 (GET-TO TRUCK-0 CITY-LOC-1) + M-DRIVE-TO 0) + (11 + (UNLOAD TRUCK-0 CITY-LOC-2 + PACKAGE-1) + M-UNLOAD 7) + (12 (GET-TO TRUCK-0 CITY-LOC-0) + M-DRIVE-TO 2) + (13 + (LOAD TRUCK-0 CITY-LOC-1 + PACKAGE-1) + M-LOAD 5) + (14 (DELIVER PACKAGE-0 CITY-LOC-0) + M-DELIVER 10 8 12 9) + (15 (DELIVER PACKAGE-1 CITY-LOC-2) + M-DELIVER 16 13 17 11) + (16 (GET-TO TRUCK-0 CITY-LOC-1) + M-DRIVE-TO 4) + (17 (GET-TO TRUCK-0 CITY-LOC-2) + M-DRIVE-TO 6)))) + +(cl:defparameter hddl-utils-tests::*expected-task* + '(drive truck-0 city-loc-2 city-loc-1)) + + #| (cl:defparameter pddl-utils-tests::*pddl-keywords* - '(:adl :strips - :typing + '(:adl :strips + :typing :negative-preconditions :disjunctive-preconditions :equality :existential-preconditions :universal-preconditions - :quantified-preconditions + :quantified-preconditions :existential-preconditions :universal-preconditions :conditional-effects diff --git a/hddl-utils/tests/tests.lisp b/hddl-utils/tests/tests.lisp index dcf235b..ffbc0cc 100644 --- a/hddl-utils/tests/tests.lisp +++ b/hddl-utils/tests/tests.lisp @@ -220,3 +220,25 @@ (is (equalp goal-expr (hddl-utils:problem-goal problem))) ) ) + + + +(test check-reading-plan + (let ((plan (hddl-utils:read-hddl-plan-file + (asdf:system-relative-pathname "hddl-utils" "hddl-utils/tests/example-plan.hddl")))) + (is (equalp *parsed-plan* plan)))) + +(test check-reading-plan-sexp + (let ((plan (hddl-utils:read-hddl-plan-file + (asdf:system-relative-pathname "hddl-utils" "hddl-utils/tests/example-plan-sexp.hddl")))) + (is (equalp *parsed-plan* plan)))) + +(test check-comma-separated-task + (let ((string "drive truck-0, city-loc-2, city-loc-1")) + (is (equalp *expected-task* + (hddl-pprinter::comma-separated-task->sexp string))))) + +(test check-reading-plan-comma-separated + (let ((plan (hddl-utils:read-hddl-plan-file + (asdf:system-relative-pathname "hddl-utils" "hddl-utils/tests/example-plan-comma-separated.hddl")))) + (is (equalp *parsed-plan* plan)))) diff --git a/hddl/hddl-pprint.lisp b/hddl/hddl-pprint.lisp index 515ff3c..f90b192 100644 --- a/hddl/hddl-pprint.lisp +++ b/hddl/hddl-pprint.lisp @@ -80,6 +80,17 @@ in the form of a list of actions." (with-open-file (str filename) (read-HDDL-plan str))) +(defun comma-separated-task->sexp (raw-string) + (let* ((string (string-upcase raw-string)) + (task-name-pos (or (position #\space string) + (error "Unable to parse task from \"~a\"" raw-string))) + (task-name (string-right-trim (list #\space) (subseq string 0 task-name-pos))) + (parameters + (mapcar #'(lambda (x) (string-right-trim (list #\space) + (string-left-trim (list #\space) x))) + (cl-ppcre:split "," string :start (1+ task-name-pos))))) + (mapcar #'hddl-symbol (cons task-name parameters)))) + (defun read-HDDL-plan (stream) ;; FIXME: rewrite the docstring with return value "Read a HDDL plan from STREAM and return it @@ -115,11 +126,9 @@ in the form of a list of actions." (cons id (ecase format (:comma-separated - (let ((components - (cl-ppcre:split "," line :start (1+ pos)))) - (mapcar #'hddl-symbol components))) + (comma-separated-task->sexp (subseq (string-upcase line) (1+ pos)))) (:s-expression - (let ((*package* *hddl-package*)) + (let ((*package* (find-package *hddl-package*))) (read-from-string line t nil :start (1+ pos)))) (:default (space-separated-string->hddl-list (subseq line pos)) @@ -144,7 +153,14 @@ in the form of a list of actions." (partition-method-line (subseq line (1+ pos))) (declare (type string task-string method-id subtasks)) (collecting `(,id - ,(space-separated-string->hddl-list task-string) + ,(ecase format + (:default + (space-separated-string->hddl-list (string-upcase task-string))) + (:s-expression + (let ((*package* (find-package *hddl-package*))) + (read-from-string task-string))) + (:comma-separated + (comma-separated-task->sexp (string-upcase task-string)))) . (,(intern (string-upcase method-id) *hddl-package*) ,@(read-integer-list subtasks)))))))) diff --git a/version.lisp-expr b/version.lisp-expr index e6c3369..b6d7788 100644 --- a/version.lisp-expr +++ b/version.lisp-expr @@ -1 +1 @@ -"3.1.0" +"3.1.1"