-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add ConstantsDef definition and validation
- Loading branch information
1 parent
24a98cc
commit 3fb63de
Showing
9 changed files
with
183 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# | ||
# Copyright 2021-2023 WhiteMech | ||
# | ||
# ------------------------------ | ||
# | ||
# This file is part of pddl. | ||
# | ||
# Use of this source code is governed by an MIT-style | ||
# license that can be found in the LICENSE file or at | ||
# https://opensource.org/licenses/MIT. | ||
# | ||
|
||
"""This module implements the ConstantsDef class to handle the constants of a PDDL domain.""" | ||
from typing import AbstractSet, Collection, Optional | ||
|
||
from pddl.definitions.base import TypesDef, _Definition | ||
from pddl.helpers.base import ensure_set | ||
from pddl.logic import Constant | ||
from pddl.requirements import Requirements | ||
from pddl.validation.terms import TermsValidator | ||
|
||
|
||
class ConstantsDef(_Definition): | ||
"""A set of constants of a PDDL domain.""" | ||
|
||
def __init__( | ||
self, | ||
requirements: AbstractSet[Requirements], | ||
types: TypesDef, | ||
constants: Optional[Collection[Constant]], | ||
) -> None: | ||
"""Initialize the PDDL constants section validator.""" | ||
TermsValidator(requirements, types).check_terms(constants if constants else []) | ||
|
||
super().__init__(requirements, types) | ||
self._constants = ensure_set(constants) | ||
|
||
@property | ||
def constants(self) -> AbstractSet[Constant]: | ||
"""Get the constants.""" | ||
return self._constants |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# | ||
# Copyright 2021-2023 WhiteMech | ||
# | ||
# ------------------------------ | ||
# | ||
# This file is part of pddl. | ||
# | ||
# Use of this source code is governed by an MIT-style | ||
# license that can be found in the LICENSE file or at | ||
# https://opensource.org/licenses/MIT. | ||
# | ||
|
||
"""This package includes validation functions of PDDL domains/problems.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# | ||
# Copyright 2021-2023 WhiteMech | ||
# | ||
# ------------------------------ | ||
# | ||
# This file is part of pddl. | ||
# | ||
# Use of this source code is governed by an MIT-style | ||
# license that can be found in the LICENSE file or at | ||
# https://opensource.org/licenses/MIT. | ||
# | ||
|
||
"""Base module for validators.""" | ||
from typing import AbstractSet, Collection | ||
|
||
from pddl.custom_types import name as name_type | ||
from pddl.definitions.base import TypesDef | ||
from pddl.exceptions import PDDLValidationError | ||
from pddl.helpers.base import assert_ | ||
from pddl.requirements import Requirements | ||
|
||
|
||
class BaseValidator: | ||
"""Base class for validators.""" | ||
|
||
def __init__( | ||
self, requirements: AbstractSet[Requirements], types: TypesDef | ||
) -> None: | ||
"""Initialize the validator.""" | ||
assert_(type(self) is not BaseValidator) | ||
self._requirements = requirements | ||
self._types = types | ||
|
||
@property | ||
def has_typing(self) -> bool: | ||
"""Check if the typing requirement is specified.""" | ||
return Requirements.TYPING in self._requirements | ||
|
||
def _check_typing_requirement(self, type_tags: Collection[name_type]) -> None: | ||
"""Check that the typing requirement is specified.""" | ||
if not self.has_typing and len(type_tags) > 0: | ||
raise PDDLValidationError( | ||
f"typing requirement is not specified, but the following types were used: {type_tags}" | ||
) | ||
|
||
def _check_types_are_available( | ||
self, type_tags: Collection[name_type], what: str | ||
) -> None: | ||
"""Check that the types are available in the domain.""" | ||
if not self._types.all_types.issuperset(type_tags): | ||
raise PDDLValidationError( | ||
f"types {sorted(type_tags)} of {what} are not in available types {self._types.all_types}" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# | ||
# Copyright 2021-2023 WhiteMech | ||
# | ||
# ------------------------------ | ||
# | ||
# This file is part of pddl. | ||
# | ||
# Use of this source code is governed by an MIT-style | ||
# license that can be found in the LICENSE file or at | ||
# https://opensource.org/licenses/MIT. | ||
# | ||
|
||
"""Module for validator of terms.""" | ||
from typing import AbstractSet, Collection | ||
|
||
from pddl.definitions.base import TypesDef | ||
from pddl.logic.predicates import _TermsList | ||
from pddl.logic.terms import Term | ||
from pddl.requirements import Requirements | ||
from pddl.validation.base import BaseValidator | ||
|
||
|
||
class TermsValidator(BaseValidator): | ||
"""Class for validator of terms.""" | ||
|
||
def __init__( | ||
self, requirements: AbstractSet[Requirements], types: TypesDef | ||
) -> None: | ||
"""Initialize the validator.""" | ||
super().__init__(requirements, types) | ||
|
||
def check_terms(self, terms: Collection[Term]) -> None: | ||
"""Check the terms.""" | ||
terms_iter = _TermsList.check_no_duplicate_iterator(terms) | ||
for term in terms_iter: | ||
self._check_typing_requirement(term.type_tags) | ||
self._check_types_are_available(term.type_tags, "terms") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters