-
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.
temp: work in progress: builder pattern for validation
- Loading branch information
1 parent
6ba0257
commit e9cd01d
Showing
13 changed files
with
427 additions
and
331 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,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 builder classes for PDDL domains and 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,74 @@ | ||
# | ||
# 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 includes the base classes for the PDDL builders.""" | ||
|
||
from abc import ABC, abstractmethod | ||
from typing import AbstractSet, Generic, Type, TypeVar | ||
|
||
from pddl.builders.types_def import TypesDef | ||
from pddl.core import Domain, Problem | ||
from pddl.exceptions import PDDLValidationError | ||
from pddl.helpers.base import assert_ | ||
from pddl.requirements import Requirements | ||
|
||
T = TypeVar("T", Domain, Problem) | ||
|
||
|
||
class BaseBuilder(ABC, Generic[T]): | ||
"""A base class for the PDDL builders.""" | ||
|
||
@abstractmethod | ||
def build(self) -> T: | ||
"""Build the PDDL object.""" | ||
|
||
|
||
class _NoDuplicateList(list): | ||
"""A list that does not allow duplicates.""" | ||
|
||
def __init__( | ||
self, item_name: str, exception_cls: Type[Exception] = PDDLValidationError | ||
) -> None: | ||
"""Initialize the list.""" | ||
super().__init__() | ||
self.__item_name = item_name | ||
self.__exception_cls = exception_cls | ||
# this is for O(1) lookup | ||
self.__elements = set() | ||
|
||
def append(self, item) -> None: | ||
"""Append an item to the list.""" | ||
if item in self.__elements: | ||
raise PDDLValidationError(f"duplicate {self.__item_name}: '{item}'") | ||
super().append(item) | ||
self.__elements.add(item) | ||
|
||
def extend(self, iterable) -> None: | ||
"""Extend the list with an iterable.""" | ||
for item in iterable: | ||
self.append(item) | ||
|
||
def __contains__(self, item): | ||
"""Check if the list contains an item.""" | ||
return item in self.__elements | ||
|
||
|
||
class _Definition: | ||
"""Abstract class for a PDDL definition.""" | ||
|
||
def __init__( | ||
self, requirements: AbstractSet[Requirements], types: TypesDef | ||
) -> None: | ||
"""Initialize the PDDL definition.""" | ||
assert_(type(self) is not _Definition) | ||
self._requirements = requirements | ||
self._types = 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,36 @@ | ||
# | ||
# 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, Sequence, cast | ||
|
||
from pddl.builders.terms_list import TermsValidator | ||
from pddl.logic import Constant | ||
|
||
|
||
class ConstantsDef: | ||
"""A set of constants of a PDDL domain.""" | ||
|
||
def __init__(self) -> None: | ||
"""Initialize the PDDL constants section validator.""" | ||
self._terms_validator = TermsValidator( | ||
no_duplicates=True, must_be_instances_of=Constant | ||
) | ||
|
||
def add_constant(self, c: Constant) -> None: | ||
"""Add a constant.""" | ||
self._terms_validator.add_term(c) | ||
|
||
@property | ||
def constants(self) -> AbstractSet[Constant]: | ||
"""Get the constants.""" | ||
return frozenset(cast(Sequence[Constant], self._terms_validator.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
from typing import Callable, Collection, List, Optional | ||
|
||
from pddl.builders.base import BaseBuilder, _NoDuplicateList | ||
from pddl.builders.constants_def import ConstantsDef | ||
from pddl.builders.types_def import MutableTypesDef | ||
from pddl.custom_types import namelike | ||
from pddl.exceptions import PDDLValidationError | ||
from pddl.logic import Constant | ||
from pddl.logic.terms import Term | ||
from pddl.requirements import Requirements | ||
|
||
|
||
class DomainBuilder(BaseBuilder): | ||
"""A builder for PDDL domains.""" | ||
|
||
def __init__(self, name: str): | ||
"""Initialize the domain builder.""" | ||
self.__name = name | ||
self.__requirements: List[Requirements] = _NoDuplicateList("requirement") | ||
self.__types_def = MutableTypesDef() | ||
self.__constants_def: ConstantsDef = ConstantsDef() | ||
|
||
@property | ||
def has_typing(self) -> bool: | ||
"""Check if the typing requirement is specified.""" | ||
return Requirements.TYPING in self.__requirements | ||
|
||
def add_requirement(self, requirement: Requirements) -> "DomainBuilder": | ||
"""Add a requirement to the domain.""" | ||
self.__requirements.append(requirement) | ||
return self | ||
|
||
def add_type( | ||
self, child_type: namelike, parent_type: Optional[namelike] = None | ||
) -> "DomainBuilder": | ||
"""Add a type to the domain.""" | ||
self._check_typing_requirement_for_types(child_type, parent_type) | ||
self.__types_def.add_type(child_type, parent_type) | ||
return self | ||
|
||
def add_constant(self, constant: Constant) -> "DomainBuilder": | ||
"""Add a constant to the domain.""" | ||
self._check_typing_requirement_for_term(constant) | ||
self._check_types_are_available(constant) | ||
self.__constants_def.add_constant(constant) | ||
return self | ||
|
||
def build(self) -> "T": | ||
pass | ||
|
||
# def build(self) -> Domain: | ||
# """Build the domain.""" | ||
# return Domain( | ||
# name=self.__name, | ||
# requirements=self.__requirements, | ||
# types=self.types, | ||
# constants=self.__constants, | ||
# predicates=self.predicates, | ||
# functions=self.functions, | ||
# actions=self.actions, | ||
# axioms=self.axioms, | ||
# ) | ||
|
||
def _check_typing_requirement_for_types( | ||
self, child_type: namelike, parent_type: Optional[namelike] = None | ||
) -> None: | ||
"""Check that the typing requirement is specified.""" | ||
if not self.has_typing: | ||
raise PDDLValidationError( | ||
f"typing requirement is not specified, but the following types were used: {child_type}" | ||
+ (f" -> {parent_type}" if parent_type else "") | ||
) | ||
|
||
def _check_typing_requirement_for_term(self, term: Term) -> None: | ||
"""Check that the typing requirement is specified.""" | ||
if not self.has_typing and len(term.type_tags) > 0: | ||
raise PDDLValidationError( | ||
f"typing requirement is not specified, but the following types for term '{term}' were used: {term.type_tags}" | ||
) | ||
|
||
def _check_types_are_available(self, term: Term) -> None: | ||
"""Check that the types of a term are available in the domain.""" | ||
if not self.__types_def.are_types_available(term.type_tags): | ||
raise PDDLValidationError( | ||
f"types {sorted(term.type_tags)} of term '{term}' are not in available types {self.__types_def.sorted_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
Oops, something went wrong.