Skip to content

Commit

Permalink
[qr] add back QDDL!
Browse files Browse the repository at this point in the history
  • Loading branch information
vacancy committed Apr 16, 2023
1 parent 932488e commit 3632ac9
Show file tree
Hide file tree
Showing 5 changed files with 501 additions and 11 deletions.
18 changes: 16 additions & 2 deletions lisdf/components/pddl.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,18 @@ def _to_pddl(self, ctx: StringifyContext) -> str:
return f"{self.pddl_name} - {self.parent.to_pddl(ctx)}"


@dataclass
class PDDLObjectType(PDDLType):
url: Optional[str] = None

def __init__(self, identifier: str, url: str) -> None:
super().__init__(identifier)
self.url = url

def _to_pddl(self, ctx: StringifyContext) -> str:
return f'({self.pddl_name} {self.url})'


@dataclass
class PDDLVariable(PDDLStringConfigurable):
name: str
Expand Down Expand Up @@ -165,7 +177,7 @@ def _to_pddl(self, ctx: StringifyContext) -> str:
class PDDLSDFObject(PDDLStringConfigurable):
model_name: str
name: Optional[str]
sdf_type: PDDLType
sdf_type: Optional[PDDLType] = None

@property
def pddl_name(self) -> str:
Expand Down Expand Up @@ -230,6 +242,7 @@ def _to_pddl(self, ctx: StringifyContext) -> str:
class PDDLDomain(PDDLStringConfigurable):
name: str
types: Dict[str, PDDLType] = field(default_factory=dict)
object_types: Dict[str, PDDLObjectType] = field(default_factory=dict)
constants: Dict[str, PDDLObject] = field(default_factory=dict)
predicates: Dict[str, PDDLPredicate] = field(default_factory=dict)
operators: Dict[str, PDDLOperator] = field(default_factory=dict)
Expand Down Expand Up @@ -272,7 +285,7 @@ class PDDLProblem(PDDLStringConfigurable):
domain: PDDLDomain
objects: Dict[str, PDDLObject] = field(default_factory=dict)
init: List[PDDLProposition] = field(default_factory=list)
conjunctive_goal: List[PDDLPredicate] = field(default_factory=list)
conjunctive_goal: List[PDDLProposition] = field(default_factory=list)

def _to_pddl(self, ctx: StringifyContext) -> str:
fmt = f"(define (problem {self.name})\n"
Expand Down Expand Up @@ -319,3 +332,4 @@ class PDDLFunctionCall(PDDLStringConfigurable):
def _to_pddl(self, ctx: StringifyContext) -> str:
arguments_str = " ".join(a.to_pddl(ctx) for a in self.arguments)
return f"({self.op_name} {arguments_str})"

9 changes: 0 additions & 9 deletions lisdf/parsing/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +0,0 @@
# A large chunk of the code in the `lisdf.parsing` modules and submodules were
# modified from https://github.com/ros/urdf_parser_py to support recursion.


from .all import load_all # noqa: F401
from .mjcf import load_mjcf # noqa: F401
from .pddl_j import load_pddl, load_pddl_string # noqa: F401
from .sdf_j import load_sdf, load_sdf_string # noqa: F401
from .urdf_j import load_urdf, load_urdf_string # noqa: F401
69 changes: 69 additions & 0 deletions lisdf/parsing/qddl-builtins-v2.0.pddl
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
(define
(domain lispddl-builtins)
(:types
qr::object
qr::value
qr::link - qr::object
qr::joint - qr::object
qr::chain - qr::object
qr::frame - qr::object
qr::body - qr::object
qr::joint-conf - qr::value
qr::chain-conf - qr::value
qr::color - qr::value ; (r, g, b, a)

qr::pose - qr::value ; (x, y, z, roll, pitch, yaw)
)

(:object-types
; a built-in "world-type". Used to create the world frame.
(qr::world-type "")
(qrgeom::box-type "")
)

(:predicates
; predicates for downstream parsers that do not support typing.
(is-qr-link ?link)
(is-qr-joint ?joint)
(is-qr-chain ?chain)
(is-qr-frame ?frame)
(is-qr-body ?body)
(is-qr-joint-conf ?pose)
(is-qr-chain-conf ?pose)
(is-qr-body-pose ?pose)

; ?body: a free body
; ?pose: (x, y, z, roll, pitch, yaw)
(body-pose ?body - qr::body ?pose - qr::pose)

; ?body: a body
; ?scale: a single valuekj
(body-scale ?body - qr::body ?scale - qr::value)

; ?parent-frame: an object::frame_name of the parent
; ?child-frame: an object::frame_name of the child
; ?X_PC: the pose of the child relative to the parent, in (x, y, z, roll, pitch, yaw)
(weld ?parent-frame - qr::frame ?child-frame - qr::frame ?X_PC - qr::pose)

; ?x: a joint of a robot
; ?c: a value
(joint-conf ?x - qr::joint ?c - qr::joint-conf)

; ?x: a kinematic chain
; ?c: a list of values
(chain-conf ?x - qr::chain ?c - qr::chain-conf)

; ?x: a body
; ?y: a link of the robot
; ?g: a grasp, specified as a transform from ?y to ?x, in (x, y, z, roll, pitch, yaw)
(holding ?x - qr::body ?y - qr::link ?g - qr::pose)

; ?x: the name of the body
; ?size: the size of the box, as a tuple of (length_x, length_y, length_z)
(qrgeom::box-shape ?x - qr::body ?size - qr::value)

; ?x: the name of the body
; ?color: the color of the box, as a tuple of (r, g, b, a)
(qrgeom::box-color ?x - qr::body ?color - qr::color)
)
)
132 changes: 132 additions & 0 deletions lisdf/parsing/qddl-v2.0.grammar
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
start: definition | expr

definition: "(" "define" definition_decl supp_definitions ")"

definition_decl: "(" definition_type definition_name ")"
?definition_type: VARNAME
?definition_name: VARNAME

supp_definitions: supp_definition* supp_definition
?supp_definition: domain_definition
| requirements_definition
| types_definition
| object_types_definition
| constants_definition
| predicates_definition
| objects_definition
| init_definition
| goal_definition

// domain
domain_definition: "(" ":" "domain" domain_name ")"
domain_name: VARNAME

// requirements
requirements_definition: "(" ":" "requirements" requirement_name* ")"
requirement_name: ":" VARNAME

// types
types_definition: "(" ":" "types" type_definition* ")"
type_definition: object_type_name+
| object_type_name+ "-" parent_type_name
parent_type_name: object_type_name

// types
object_types_definition: "(" ":" "object-types" object_type_definition* ")"
object_type_definition: "(" object_type_name string ")"

// constants
constants_definition: "(" ":" "constants" constant_definition* ")"
constant_definition: allconstant

// predicates
predicates_definition: "(" ":" "predicates" predicate_definition* ")"
predicate_definition: "(" predicate_name allvariable* ")"
?predicate_name: function_name | method_name

// objects
objects_definition: "(" ":" "objects" object_definition* ")"
object_definition: allconstant

// init
init_definition: "(" ":" "init" init_definition_item* ")"
init_definition_item: function_call

// goal
goal_definition: "(" ":" "goal" function_call ")"

// variable name
%import common.LETTER
%import common.DIGIT
VARNAME: LETTER ("_"|"-"|LETTER|DIGIT)*
CONSTNAME: (LETTER|"@"|"_") ("_"|"-"|":"|"#"|LETTER|DIGIT)*

// variables and constants
VARIABLE: "?" VARNAME
variable: VARIABLE
CONSTANT: CONSTNAME
constant: CONSTANT
typedvariable: variable "-" object_type_name
typedconstant: constant "-" object_type_name

?allconstant: constant | typedconstant
?allvariable: variable | typedvariable

// type name
type_name: CONSTANT
object_type_name: type_name

// literal types
?literal: bool | number | string | list
TRUE: "true"
FALSE: "false"
bool: TRUE | FALSE

// numbers
INT: DIGIT+
SIGNED_INT: ["+"|"-"] INT
// DECIMAL: INT "." INT? | "." INT
DECIMAL: INT "." INT? | "." INT

// float = /-?\d+(\.\d+)?([eE][+-]?\d+)?/
_EXP: ("e"|"E") SIGNED_INT
FLOAT: INT _EXP | DECIMAL _EXP?
SIGNED_FLOAT: ["+"|"-"] FLOAT

NUMBER: FLOAT | INT
SIGNED_NUMBER: ["+"|"-"] NUMBER

int: INT | SIGNED_INT
float: NUMBER | SIGNED_NUMBER
?number: float

%import common.ESCAPED_STRING
string: ESCAPED_STRING
list: "[" "]"
| "[" literal ("," literal)* "]"
| "(" literal ("," literal)* ")"

named_literal: CONSTNAME "=" literal

// expression
?expr_function_call: function_call | quantified_function_call
?expr: variable | constant | literal | named_literal | expr_function_call

function_call: "(" (function_name|method_name) expr* ")"
function_name: VARNAME
method_name: VARNAME SCOPE_SEP VARNAME
SCOPE_SEP: "::"

EXISTS: "exists"
FORALL: "forall"
IOTA: "iota"
?quantifier: EXISTS | FORALL | IOTA
quantified_function_call: "(" quantifier "(" typedvariable ")" expr ")"

%import common.WS
%ignore WS

%import common.NEWLINE
COMMENT: ";" /(.)+/ NEWLINE
| "#" /(.)+/ NEWLINE
%ignore COMMENT
Loading

0 comments on commit 3632ac9

Please sign in to comment.