From 25bacb0220716cf668efdfea4a9e4a9a7a76221a Mon Sep 17 00:00:00 2001 From: Marco Favorito Date: Tue, 3 Oct 2023 00:14:56 +0200 Subject: [PATCH] temp --- plan4past/compiler.py | 49 ++-- plan4past/helpers/compilation_helper.py | 48 +++- plan4past/utils/derived_visitor.py | 259 +++++++++--------- plan4past/utils/predicates_visitor.py | 168 ++++++------ plan4past/utils/val_predicates_visitor.py | 194 ++++++------- tests/test_compiler/base.py | 1 - tests/test_compiler/test_blocksworld_det.py | 4 +- tests/test_utils/test_derived_visitor.py | 110 ++++---- tests/test_utils/test_predicates_visitor.py | 54 ++-- .../test_utils/test_val_predicates_visitor.py | 96 ++++--- 10 files changed, 539 insertions(+), 444 deletions(-) diff --git a/plan4past/compiler.py b/plan4past/compiler.py index 7d27af3d..737f60d7 100644 --- a/plan4past/compiler.py +++ b/plan4past/compiler.py @@ -45,7 +45,11 @@ TRUE_PREDICATE, ) from plan4past.exceptions import ProblemUnsolvableException -from plan4past.helpers.compilation_helper import CompilationManager, YesterdayAtom +from plan4past.helpers.compilation_helper import ( + CompilationManager, + PredicateMapping, + YesterdayAtom, +) from plan4past.helpers.utils import ( add_val_prefix, check_, @@ -55,13 +59,13 @@ ) from plan4past.helpers.yesterday_atom_helper import QUOTED_ATOM from plan4past.utils.atoms_visitor import find_atoms -from plan4past.utils.derived_visitor import derived_predicates +from plan4past.utils.derived_visitor import DerivedPredicatesVisitor from plan4past.utils.dnf_visitor import dnf from plan4past.utils.nnf_visitor import nnf -from plan4past.utils.predicates_visitor import predicates +from plan4past.utils.predicates_visitor import PredicatesVisitor from plan4past.utils.pylogics2pddl import Pylogics2PddlTranslator from plan4past.utils.rewrite_formula_visitor import rewrite -from plan4past.utils.val_predicates_visitor import val_predicates +from plan4past.utils.val_predicates_visitor import ValPredicatesVisitor class Compiler: @@ -93,6 +97,7 @@ def __init__( check_(self.formula.logic == Logic.PLTL, "only PPLTL is supported!") + self._predicate_mapping = PredicateMapping() self._executed: bool = False self._result_domain: Optional[Domain] = None self._result_problem: Optional[Problem] = None @@ -134,18 +139,27 @@ def result(self) -> Tuple[Domain, Problem]: def compile(self): """Compute the new domain and the new problem.""" - if not self._executed: - self._compile_domain() - self._compile_problem() - self._executed = True + if self._executed: + return + + self._compile_domain() + self._compile_problem() + + self._executed = True def _compile_domain(self): """Compute the new domain.""" - new_predicates = predicates(self.formula).union(val_predicates(self.formula)) - new_derived_predicates = derived_predicates( - self.formula, self.from_atoms_to_fluent + subformula_predicates_set = PredicatesVisitor(self._predicate_mapping).visit( + self.formula + ) + val_predicates_set = ValPredicatesVisitor(self._predicate_mapping).visit( + self.formula ) - new_whens = _compute_whens(self.formula) + new_predicates = subformula_predicates_set.union(val_predicates_set) + new_derived_predicates = DerivedPredicatesVisitor( + self._predicate_mapping, self.from_atoms_to_fluent + ).visit(self.formula) + new_whens = _compute_whens(subformula_predicates_set) domain_actions = _update_domain_actions_det(self.domain.actions, new_whens) self._result_domain = Domain( @@ -174,26 +188,25 @@ def _compile_problem(self): else set(self.problem.init) ) + goal_predicate = self._predicate_mapping.get_predicate(self.formula) self._result_problem = Problem( name=self.problem.name, domain_name=self.domain.name, requirements=self.problem.requirements, objects=[*self.problem.objects], init=new_init, - goal=And( - Predicate(add_val_prefix(replace_symbols(to_string(self.formula)))) - ), + goal=And(Predicate(add_val_prefix(goal_predicate.name))), ) -def _compute_whens(formula: Formula) -> Set[When]: +def _compute_whens(predicates: Set[Predicate]) -> Set[When]: """Compute conditional effects for formula progression.""" return { When(Predicate(add_val_prefix(remove_yesterday_prefix(p.name))), p) - for p in predicates(formula) + for p in predicates }.union( When(Not(Predicate(add_val_prefix(remove_yesterday_prefix(p.name)))), Not(p)) - for p in predicates(formula) + for p in predicates ) diff --git a/plan4past/helpers/compilation_helper.py b/plan4past/helpers/compilation_helper.py index 0f0ffba5..ba661977 100644 --- a/plan4past/helpers/compilation_helper.py +++ b/plan4past/helpers/compilation_helper.py @@ -20,8 +20,9 @@ # along with Plan4Past. If not, see . # """This module contains the class that manages the compilation of a PPLTL formula.""" -from typing import List, Tuple +from typing import Dict, List, Tuple +from pddl.logic import Predicate from pylogics.syntax.base import Formula, Not from pylogics.syntax.pltl import ( Atomic, @@ -33,7 +34,7 @@ ) from plan4past.helpers.utils import check_ -from plan4past.helpers.yesterday_atom_helper import YesterdayAtom +from plan4past.helpers.yesterday_atom_helper import QUOTED_ATOM, YesterdayAtom from plan4past.utils.ppnf_visitor import ppnf from plan4past.utils.yesterday_generator_visitor import get_quoted_dictionary @@ -108,3 +109,46 @@ def get_problem_extension(self) -> Tuple[List[Formula], List, Formula]: conditional_effects.append((ppnf(yesterday_atom.formula), yesterday_atom)) return fresh_atoms, conditional_effects, goal + + +class PredicateMapping: + """Class that manages the mapping of the predicates.""" + + def __init__(self): + """Initialize the predicate mapping.""" + self.mapping: Dict[Formula, Predicate] = {} + self.inverse_mapping: Dict[Predicate, Formula] = {} + self.id = 0 + + def add_predicate(self, formula: Formula) -> None: + """ + Add a predicate to the mapping. + + :param formula: the formula to be added + """ + if self.mapping.get(formula) is None: + self.mapping[formula] = Predicate(f"{QUOTED_ATOM}_{self.id}") + self.inverse_mapping[self.mapping[formula]] = formula + self.id += 1 + + def get_predicate(self, formula: Formula) -> Predicate: + """ + Get the predicate from the mapping. + + :param formula: the formula to be added + :return: the predicate + """ + result = self.mapping.get(formula) + if result is None: + self.add_predicate(formula) + return self.mapping[formula] + + def get_formula(self, predicate: Predicate) -> Formula: + """ + Get the formula from the inverse mapping. + + :param predicate: the predicate to be added + :return: the formula + """ + check_(predicate in self.inverse_mapping, f"predicate {predicate} not found") + return self.inverse_mapping[predicate] diff --git a/plan4past/utils/derived_visitor.py b/plan4past/utils/derived_visitor.py index d459f13c..35b3b975 100644 --- a/plan4past/utils/derived_visitor.py +++ b/plan4past/utils/derived_visitor.py @@ -21,6 +21,7 @@ # """Derived Predicates visitor.""" +import functools from functools import singledispatch from typing import Dict, Set @@ -39,134 +40,136 @@ ) from pylogics.utils.to_string import to_string +from plan4past.helpers.compilation_helper import PredicateMapping from plan4past.helpers.utils import add_val_prefix, replace_symbols -@singledispatch -def derived_predicates( - formula: object, atoms_to_fluents: Dict[PLTLAtomic, Predicate] -) -> Set[DerivedPredicate]: - """Compute the derived predicate for a formula.""" - raise NotImplementedError(f"handler not implemented for object {type(formula)}") - - -@derived_predicates.register -def derived_predicates_true( - _formula: PropositionalTrue, _atoms_to_fluents: Dict[PLTLAtomic, Predicate] -) -> Set[DerivedPredicate]: - """Compute the derived predicate for a true formula.""" - val = Predicate(add_val_prefix("true")) - return {DerivedPredicate(val, And())} - - -@derived_predicates.register -def derived_predicates_false( - _formula: PropositionalFalse, _atoms_to_fluents: Dict[PLTLAtomic, Predicate] -) -> Set[DerivedPredicate]: - """Compute the derived predicate for a false formula.""" - val = Predicate(add_val_prefix("false")) - return {DerivedPredicate(val, Or())} - - -@derived_predicates.register -def derived_predicates_atomic( - formula: PLTLAtomic, atoms_to_fluents: Dict[PLTLAtomic, Predicate] -) -> Set[DerivedPredicate]: - """Compute the derived predicate for an atomic formula.""" - val = Predicate(add_val_prefix(formula.name)) - condition = atoms_to_fluents[formula] - return {DerivedPredicate(val, condition)} - - -@derived_predicates.register -def derived_predicates_and( - formula: PLTLAnd, atoms_to_fluents: Dict[PLTLAtomic, Predicate] -) -> Set[DerivedPredicate]: - """Compute the derived predicate for a PPLTL And formula.""" - formula_name = to_string(formula) - val = Predicate(add_val_prefix(replace_symbols(formula_name))) - val_ops = [ - Predicate(add_val_prefix(replace_symbols(to_string(op)))) - for op in formula.operands - ] - condition = And(*val_ops) - der_pred_ops = [derived_predicates(op, atoms_to_fluents) for op in formula.operands] - return {DerivedPredicate(val, condition)}.union(*der_pred_ops) - - -@derived_predicates.register -def derived_predicates_or( - formula: PLTLOr, atoms_to_fluents: Dict[PLTLAtomic, Predicate] -) -> Set[DerivedPredicate]: - """Compute the derived predicate for a PPLTL Or formula.""" - formula_name = to_string(formula) - val = Predicate(add_val_prefix(replace_symbols(formula_name))) - val_ops = [ - Predicate(add_val_prefix(replace_symbols(to_string(op)))) - for op in formula.operands - ] - condition = Or(*val_ops) - der_pred_ops = [derived_predicates(op, atoms_to_fluents) for op in formula.operands] - return {DerivedPredicate(val, condition)}.union(*der_pred_ops) - - -@derived_predicates.register -def derived_predicates_not( - formula: PLTLNot, atoms_to_fluents: Dict[PLTLAtomic, Predicate] -) -> Set[DerivedPredicate]: - """Compute the derived predicate for a PPLTL Not formula.""" - formula_name = to_string(formula) - val = Predicate(add_val_prefix(replace_symbols(formula_name))) - condition = Not( - Predicate(add_val_prefix(replace_symbols(to_string(formula.argument)))) - ) - der_pred_arg = derived_predicates(formula.argument, atoms_to_fluents) - return {DerivedPredicate(val, condition)}.union(der_pred_arg) - - -@derived_predicates.register -def derived_predicates_yesterday( - formula: Before, atoms_to_fluents: Dict[PLTLAtomic, Predicate] -) -> Set[DerivedPredicate]: - """Compute the derived predicate for a Before formula.""" - formula_name = to_string(formula) - val = Predicate(add_val_prefix(replace_symbols(formula_name))) - condition = Predicate(replace_symbols(to_string(formula))) - der_pred_arg = derived_predicates(formula.argument, atoms_to_fluents) - return {DerivedPredicate(val, condition)}.union(der_pred_arg) - - -@derived_predicates.register -def derived_predicates_since( - formula: Since, atoms_to_fluents: Dict[PLTLAtomic, Predicate] -) -> Set[DerivedPredicate]: - """Compute the derived predicate for a Since formula.""" - if len(formula.operands) != 2: - head = formula.operands[0] - tail = Since(*formula.operands[1:]) - return derived_predicates(Since(head, tail), atoms_to_fluents) - formula_name = to_string(formula) - val = Predicate(add_val_prefix(replace_symbols(formula_name))) - op_or_1 = Predicate(add_val_prefix(replace_symbols(to_string(formula.operands[1])))) - op_or_2 = And( - Predicate(add_val_prefix(replace_symbols(to_string(formula.operands[0])))), - Predicate(f"Y-{replace_symbols(to_string(formula))}"), - ) - condition = Or(op_or_1, op_or_2) - der_pred_ops = [derived_predicates(op, atoms_to_fluents) for op in formula.operands] - return {DerivedPredicate(val, condition)}.union(*der_pred_ops) - - -@derived_predicates.register -def derived_predicates_once( - formula: Once, atoms_to_fluents: Dict[PLTLAtomic, Predicate] -) -> Set[DerivedPredicate]: - """Compute the derived predicate for a Once formula.""" - formula_name = to_string(formula) - val = Predicate(add_val_prefix(replace_symbols(formula_name))) - condition = Or( - Predicate(add_val_prefix(replace_symbols(to_string(formula.argument)))), - Predicate(f"Y-{replace_symbols(to_string(formula))}"), - ) - der_pred_arg = derived_predicates(formula.argument, atoms_to_fluents) - return {DerivedPredicate(val, condition)}.union(der_pred_arg) +class DerivedPredicatesVisitor: + """Visitor for computing derived predicates.""" + + def __init__( + self, + predicate_mapping: PredicateMapping, + from_atoms_to_fluent: Dict[PLTLAtomic, Predicate], + ): + """Initialize the visitor.""" + self._predicate_mapping = predicate_mapping + self._from_atoms_to_fluent = from_atoms_to_fluent + + @functools.singledispatchmethod + def visit(self, formula: object) -> Set[Predicate]: + """Compute the value predicate for a formula.""" + raise NotImplementedError( + f"handler not implemented for object of type {type(formula)}" + ) + + @visit.register + def derived_predicates_true( + self, + _formula: PropositionalTrue, + ) -> Set[DerivedPredicate]: + """Compute the derived predicate for a true formula.""" + predicate = self._predicate_mapping.get_predicate(_formula) + val = Predicate(add_val_prefix(predicate.name)) + return {DerivedPredicate(val, And())} + + @visit.register + def derived_predicates_false( + self, + _formula: PropositionalFalse, + ) -> Set[DerivedPredicate]: + """Compute the derived predicate for a false formula.""" + predicate = self._predicate_mapping.get_predicate(_formula) + val = Predicate(add_val_prefix(predicate.name)) + return {DerivedPredicate(val, Or())} + + @visit.register + def derived_predicates_atomic(self, formula: PLTLAtomic) -> Set[DerivedPredicate]: + """Compute the derived predicate for an atomic formula.""" + predicate = self._predicate_mapping.get_predicate(formula) + val = Predicate(add_val_prefix(predicate.name)) + condition = self._from_atoms_to_fluent[formula] + return {DerivedPredicate(val, condition)} + + @visit.register + def derived_predicates_and(self, formula: PLTLAnd) -> Set[DerivedPredicate]: + """Compute the derived predicate for a PPLTL And formula.""" + predicate = self._predicate_mapping.get_predicate(formula) + val = Predicate(add_val_prefix(predicate.name)) + val_ops = [ + Predicate(add_val_prefix(self._predicate_mapping.get_predicate(op).name)) + for op in formula.operands + ] + condition = And(*val_ops) + der_pred_ops = [self.visit(op) for op in formula.operands] + return {DerivedPredicate(val, condition)}.union(*der_pred_ops) + + @visit.register + def derived_predicates_or(self, formula: PLTLOr) -> Set[DerivedPredicate]: + """Compute the derived predicate for a PPLTL Or formula.""" + predicate = self._predicate_mapping.get_predicate(formula) + val = Predicate(add_val_prefix(predicate.name)) + val_ops = [ + Predicate(add_val_prefix(self._predicate_mapping.get_predicate(op).name)) + for op in formula.operands + ] + condition = Or(*val_ops) + der_pred_ops = [self.visit(op) for op in formula.operands] + return {DerivedPredicate(val, condition)}.union(*der_pred_ops) + + @visit.register + def derived_predicates_not(self, formula: PLTLNot) -> Set[DerivedPredicate]: + """Compute the derived predicate for a PPLTL Not formula.""" + predicate = self._predicate_mapping.get_predicate(formula) + val = Predicate(add_val_prefix(predicate.name)) + condition = Not( + Predicate( + add_val_prefix( + self._predicate_mapping.get_predicate(formula.argument).name + ) + ) + ) + der_pred_arg = self.visit(formula.argument) + return {DerivedPredicate(val, condition)}.union(der_pred_arg) + + @visit.register + def derived_predicates_yesterday(self, formula: Before) -> Set[DerivedPredicate]: + """Compute the derived predicate for a Before formula.""" + predicate = self._predicate_mapping.get_predicate(formula) + val = Predicate(add_val_prefix(predicate.name)) + condition = predicate + der_pred_arg = self.visit(formula.argument) + return {DerivedPredicate(val, condition)}.union(der_pred_arg) + + @visit.register + def derived_predicates_since(self, formula: Since) -> Set[DerivedPredicate]: + """Compute the derived predicate for a Since formula.""" + if len(formula.operands) != 2: + head = formula.operands[0] + tail = Since(*formula.operands[1:]) + return self.visit(Since(head, tail)) + predicate = self._predicate_mapping.get_predicate(formula) + val = Predicate(add_val_prefix(predicate.name)) + op_or_1_predicate = self._predicate_mapping.get_predicate(formula.operands[1]) + op_or_1 = Predicate(add_val_prefix(op_or_1_predicate.name)) + op_or_2_predicate = self._predicate_mapping.get_predicate(formula.operands[0]) + op_or_2 = And( + Predicate(add_val_prefix(op_or_2_predicate.name)), + Predicate(f"Y-{predicate.name}"), + ) + condition = Or(op_or_1, op_or_2) + der_pred_ops = [self.visit(op) for op in formula.operands] + return {DerivedPredicate(val, condition)}.union(*der_pred_ops) + + @visit.register + def derived_predicates_once(self, formula: Once) -> Set[DerivedPredicate]: + """Compute the derived predicate for a Once formula.""" + predicate = self._predicate_mapping.get_predicate(formula) + val = Predicate(add_val_prefix(predicate.name)) + arg_predicate = self._predicate_mapping.get_predicate(formula.argument) + condition = Or( + Predicate(add_val_prefix(arg_predicate.name)), + Predicate(f"Y-{predicate.name}"), + ) + der_pred_arg = self.visit(formula.argument) + return {DerivedPredicate(val, condition)}.union(der_pred_arg) diff --git a/plan4past/utils/predicates_visitor.py b/plan4past/utils/predicates_visitor.py index d642f51f..3d71e0cf 100644 --- a/plan4past/utils/predicates_visitor.py +++ b/plan4past/utils/predicates_visitor.py @@ -22,7 +22,6 @@ """Predicates visitor.""" import functools -from functools import singledispatch from typing import Set from pddl.logic.predicates import Predicate @@ -38,90 +37,85 @@ PropositionalTrue, Since, ) -from pylogics.utils.to_string import to_string -from plan4past.helpers.utils import replace_symbols - - -def predicates_binaryop(formula: _BinaryOp): - """Compute predicate for a binary operator.""" - return set(functools.reduce(set.union, map(predicates, formula.operands))) # type: ignore[arg-type] - - -def predicates_unaryop(formula: _UnaryOp): - """Compute predicate for a unary operator.""" - return predicates(formula.argument) - - -@singledispatch -def predicates(formula: object) -> Set[Predicate]: - """Compute predicate for a formula.""" - raise NotImplementedError( - f"handler not implemented for object of type {type(formula)}" - ) - - -@predicates.register -def predicates_true(_formula: PropositionalTrue) -> Set[Predicate]: - """Compute predicate for a true formula.""" - return set() - - -@predicates.register -def predicates_false(_formula: PropositionalFalse) -> Set[Predicate]: - """Compute predicate for a false formula.""" - return set() - - -@predicates.register -def predicates_atomic(_formula: PLTLAtomic) -> Set[Predicate]: - """Compute predicate for an atomic formula.""" - return set() - - -@predicates.register -def predicates_and(formula: PLTLAnd) -> Set[Predicate]: - """Compute predicate for an And formula.""" - return predicates_binaryop(formula) - - -@predicates.register -def predicates_or(formula: PLTLOr) -> Set[Predicate]: - """Compute predicate for an Or formula.""" - return predicates_binaryop(formula) - - -@predicates.register -def predicates_not(formula: PLTLNot) -> Set[Predicate]: - """Compute predicate for a Not formula.""" - return predicates(formula.argument) - - -@predicates.register -def predicates_yesterday(formula: Before) -> Set[Predicate]: - """Compute predicate for a Before (Yesterday) formula.""" - quoted = Predicate(replace_symbols(to_string(formula))) - sub = predicates_unaryop(formula) - return sub.union({quoted}) - - -@predicates.register -def predicates_since(formula: Since) -> Set[Predicate]: - """Compute predicate for a Since formula.""" - if len(formula.operands) != 2: - head = formula.operands[0] - tail = Since(*formula.operands[1:]) - return predicates(Since(head, tail)) - formula_name = replace_symbols(to_string(formula)) - quoted = Predicate(f"Y-{formula_name}") - subsinces = predicates_binaryop(formula) - return {quoted}.union(subsinces) - - -@predicates.register -def predicates_once(formula: Once) -> Set[Predicate]: - """Compute predicate for a Once formula.""" - formula_name = replace_symbols(to_string(formula)) - quoted = Predicate(f"Y-{formula_name}") - sub = predicates_unaryop(formula) - return sub.union({quoted}) +from plan4past.helpers.compilation_helper import PredicateMapping + + +class PredicatesVisitor: + """Visitor for computing predicates.""" + + def __init__(self, predicate_mapping: PredicateMapping): + """Initialize the visitor.""" + self._predicate_mapping = predicate_mapping + + @functools.singledispatchmethod + def visit(self, formula: object) -> Set[Predicate]: + """Compute predicate for a formula.""" + raise NotImplementedError( + f"handler not implemented for object of type {type(formula)}" + ) + + def predicates_binaryop(self, formula: _BinaryOp): + """Compute predicate for a binary operator.""" + return set(functools.reduce(set.union, map(self.visit, formula.operands))) # type: ignore[arg-type] + + def predicates_unaryop(self, formula: _UnaryOp): + """Compute predicate for a unary operator.""" + return self.visit(formula.argument) + + @visit.register + def predicates_true(self, _formula: PropositionalTrue) -> Set[Predicate]: + """Compute predicate for a true formula.""" + return set() + + @visit.register + def predicates_false(self, _formula: PropositionalFalse) -> Set[Predicate]: + """Compute predicate for a false formula.""" + return set() + + @visit.register + def predicates_atomic(self, _formula: PLTLAtomic) -> Set[Predicate]: + """Compute predicate for an atomic formula.""" + return set() + + @visit.register + def predicates_and(self, formula: PLTLAnd) -> Set[Predicate]: + """Compute predicate for an And formula.""" + return self.predicates_binaryop(formula) + + @visit.register + def predicates_or(self, formula: PLTLOr) -> Set[Predicate]: + """Compute predicate for an Or formula.""" + return self.predicates_binaryop(formula) + + @visit.register + def predicates_not(self, formula: PLTLNot) -> Set[Predicate]: + """Compute predicate for a Not formula.""" + return self.visit(formula.argument) + + @visit.register + def predicates_yesterday(self, formula: Before) -> Set[Predicate]: + """Compute predicate for a Before (Yesterday) formula.""" + quoted = self._predicate_mapping.get_predicate(formula) + sub = self.predicates_unaryop(formula) + return sub.union({quoted}) + + @visit.register + def predicates_since(self, formula: Since) -> Set[Predicate]: + """Compute predicate for a Since formula.""" + if len(formula.operands) != 2: + head = formula.operands[0] + tail = Since(*formula.operands[1:]) + return self.visit(Since(head, tail)) + predicate = self._predicate_mapping.get_predicate(formula) + quoted = Predicate(f"Y-{predicate.name}") + subsinces = self.predicates_binaryop(formula) + return {quoted}.union(subsinces) + + @visit.register + def predicates_once(self, formula: Once) -> Set[Predicate]: + """Compute predicate for a Once formula.""" + predicate = self._predicate_mapping.get_predicate(formula) + quoted = Predicate(f"Y-{predicate.name}") + sub = self.predicates_unaryop(formula) + return sub.union({quoted}) diff --git a/plan4past/utils/val_predicates_visitor.py b/plan4past/utils/val_predicates_visitor.py index bb40edcb..30fe25b3 100644 --- a/plan4past/utils/val_predicates_visitor.py +++ b/plan4past/utils/val_predicates_visitor.py @@ -22,7 +22,6 @@ """Value predicates for val predicates visitor.""" import functools -from functools import singledispatch from typing import Set from pddl.logic.predicates import Predicate @@ -38,100 +37,101 @@ PropositionalTrue, Since, ) -from pylogics.utils.to_string import to_string -from plan4past.helpers.utils import add_val_prefix, replace_symbols - - -def val_predicates_binaryop(formula: _BinaryOp): - """Compute the value predicate for a binary operator formula.""" - return set(functools.reduce(set.union, map(val_predicates, formula.operands))) # type: ignore[arg-type] - - -def val_predicates_unaryop(formula: _UnaryOp): - """Compute the value predicate for a unary operator formula.""" - return val_predicates(formula.argument) - - -@singledispatch -def val_predicates(formula: object) -> Set[Predicate]: - """Compute the value predicate for a formula.""" - raise NotImplementedError( - f"handler not implemented for object of type {type(formula)}" - ) - - -@val_predicates.register -def val_predicates_true(_formula: PropositionalTrue) -> Set[Predicate]: - """Compute the value predicate for a true formula.""" - return {Predicate(add_val_prefix("true"))} - - -@val_predicates.register -def val_predicates_false(_formula: PropositionalFalse) -> Set[Predicate]: - """Compute the value predicate for a false formula.""" - return {Predicate(add_val_prefix("false"))} - - -@val_predicates.register -def val_predicates_atomic(formula: PLTLAtomic) -> Set[Predicate]: - """Compute the value predicate for an atomic formula.""" - return {Predicate(add_val_prefix(formula.name))} - - -@val_predicates.register -def val_predicates_and(formula: PLTLAnd) -> Set[Predicate]: - """Compute the value predicate for an And formula.""" - formula_name = replace_symbols(to_string(formula)) - value = Predicate(add_val_prefix(formula_name)) - subands = val_predicates_binaryop(formula) - return {value}.union(subands) - - -@val_predicates.register -def val_predicates_or(formula: PLTLOr) -> Set[Predicate]: - """Compute the value predicate for an Or formula.""" - formula_name = replace_symbols(to_string(formula)) - value = Predicate(add_val_prefix(formula_name)) - subors = val_predicates_binaryop(formula) - return {value}.union(subors) - - -@val_predicates.register -def val_predicates_not(formula: PLTLNot) -> Set[Predicate]: - """Compute the value predicate for a Not formula.""" - formula_name = replace_symbols(to_string(formula)) - value = Predicate(add_val_prefix(formula_name)) - sub = val_predicates_unaryop(formula) - return sub.union({value}) - - -@val_predicates.register -def val_predicates_yesterday(formula: Before) -> Set[Predicate]: - """Compute the value predicate for a Before (Yesterday) formula.""" - formula_name = replace_symbols(to_string(formula)) - value = Predicate(add_val_prefix(formula_name)) - sub = val_predicates_unaryop(formula) - return sub.union({value}) - - -@val_predicates.register -def val_predicates_since(formula: Since) -> Set[Predicate]: - """Compute the value predicate for a Since formula.""" - if len(formula.operands) != 2: - head = formula.operands[0] - tail = Since(*formula.operands[1:]) - return val_predicates(Since(head, tail)) - formula_name = replace_symbols(to_string(formula)) - value = Predicate(add_val_prefix(formula_name)) - subsinces = val_predicates_binaryop(formula) - return {value}.union(subsinces) - - -@val_predicates.register -def val_predicates_once(formula: Once) -> Set[Predicate]: - """Compute the value predicate for a Once formula.""" - formula_name = replace_symbols(to_string(formula)) - value = Predicate(add_val_prefix(formula_name)) - sub = val_predicates_unaryop(formula) - return sub.union({value}) +from plan4past.helpers.compilation_helper import PredicateMapping +from plan4past.helpers.utils import add_val_prefix + + +class ValPredicatesVisitor: + """Visitor for computing value predicates.""" + + def __init__(self, predicate_mapping: PredicateMapping): + """Initialize the visitor.""" + self._predicate_mapping = predicate_mapping + + @functools.singledispatchmethod + def visit(self, formula: object) -> Set[Predicate]: + """Compute the value predicate for a formula.""" + raise NotImplementedError( + f"handler not implemented for object of type {type(formula)}" + ) + + @visit.register + def val_predicates_binaryop(self, formula: _BinaryOp): + """Compute the value predicate for a binary operator formula.""" + return set(functools.reduce(set.union, map(self.visit, formula.operands))) # type: ignore[arg-type] + + @visit.register + def val_predicates_unaryop(self, formula: _UnaryOp): + """Compute the value predicate for a unary operator formula.""" + return self.visit(formula.argument) + + @visit.register + def val_predicates_false(self, formula: PropositionalFalse) -> Set[Predicate]: + """Compute the value predicate for an atomic formula.""" + predicate = self._predicate_mapping.get_predicate(formula) + return {Predicate(add_val_prefix(predicate.name))} + + @visit.register + def val_predicates_true(self, formula: PropositionalTrue) -> Set[Predicate]: + """Compute the value predicate for an atomic formula.""" + predicate = self._predicate_mapping.get_predicate(formula) + return {Predicate(add_val_prefix(predicate.name))} + + @visit.register + def val_predicates_atomic(self, formula: PLTLAtomic) -> Set[Predicate]: + """Compute the value predicate for an atomic formula.""" + predicate = self._predicate_mapping.get_predicate(formula) + return {Predicate(add_val_prefix(predicate.name))} + + @visit.register + def val_predicates_and(self, formula: PLTLAnd) -> Set[Predicate]: + """Compute the value predicate for an And formula.""" + predicate = self._predicate_mapping.get_predicate(formula) + value = Predicate(add_val_prefix(predicate.name)) + subands = self.val_predicates_binaryop(formula) + return {value}.union(subands) + + @visit.register + def val_predicates_or(self, formula: PLTLOr) -> Set[Predicate]: + """Compute the value predicate for an Or formula.""" + predicate = self._predicate_mapping.get_predicate(formula) + value = Predicate(add_val_prefix(predicate.name)) + subors = self.val_predicates_binaryop(formula) + return {value}.union(subors) + + @visit.register + def val_predicates_not(self, formula: PLTLNot) -> Set[Predicate]: + """Compute the value predicate for a Not formula.""" + predicate = self._predicate_mapping.get_predicate(formula) + value = Predicate(add_val_prefix(predicate.name)) + sub = self.val_predicates_unaryop(formula) + return sub.union({value}) + + @visit.register + def val_predicates_yesterday(self, formula: Before) -> Set[Predicate]: + """Compute the value predicate for a Before (Yesterday) formula.""" + predicate = self._predicate_mapping.get_predicate(formula) + value = Predicate(add_val_prefix(predicate.name)) + sub = self.val_predicates_unaryop(formula) + return sub.union({value}) + + @visit.register + def val_predicates_since(self, formula: Since) -> Set[Predicate]: + """Compute the value predicate for a Since formula.""" + if len(formula.operands) != 2: + head = formula.operands[0] + tail = Since(*formula.operands[1:]) + return self.visit(Since(head, tail)) + predicate = self._predicate_mapping.get_predicate(formula) + value = Predicate(add_val_prefix(predicate.name)) + subsinces = self.val_predicates_binaryop(formula) + return {value}.union(subsinces) + + @visit.register + def val_predicates_once(self, formula: Once) -> Set[Predicate]: + """Compute the value predicate for a Once formula.""" + predicate = self._predicate_mapping.get_predicate(formula) + value = Predicate(add_val_prefix(predicate.name)) + sub = self.val_predicates_unaryop(formula) + return sub.union({value}) diff --git a/tests/test_compiler/base.py b/tests/test_compiler/base.py index a582080c..043d90af 100644 --- a/tests/test_compiler/base.py +++ b/tests/test_compiler/base.py @@ -81,7 +81,6 @@ def _test_instance( compiler = Compiler(domain, problem, goal) compiler.compile() compiled_domain, compiled_problem = compiler.result - check_compilation( compiled_domain, compiled_problem, diff --git a/tests/test_compiler/test_blocksworld_det.py b/tests/test_compiler/test_blocksworld_det.py index 6dcde8cf..06c33300 100644 --- a/tests/test_compiler/test_blocksworld_det.py +++ b/tests/test_compiler/test_blocksworld_det.py @@ -35,8 +35,8 @@ class TestBlocksworldDetSimpleSequence(BaseCompilerTest): PATH_TO_DOMAINS_DIR = BLOCKSWORLD_DIR PATH_TO_INSTANCES_DIR = BLOCKSWORLD_DIR - MIN_INSTANCE_ID = 10 - MAX_INSTANCE_ID = 15 + MIN_INSTANCE_ID = 3 + MAX_INSTANCE_ID = 3 def make_formula(self, instance_id: int, domain: Path, problem: Path) -> str: """ diff --git a/tests/test_utils/test_derived_visitor.py b/tests/test_utils/test_derived_visitor.py index 3702d786..5b1781c7 100644 --- a/tests/test_utils/test_derived_visitor.py +++ b/tests/test_utils/test_derived_visitor.py @@ -36,8 +36,23 @@ Since, ) +from plan4past.helpers.compilation_helper import PredicateMapping from plan4past.helpers.utils import add_val_prefix -from plan4past.utils.derived_visitor import derived_predicates +from plan4past.utils.derived_visitor import DerivedPredicatesVisitor + +a = PLTLAtomic("a") +b = PLTLAtomic("b") +c = PLTLAtomic("c") +condition_a = Predicate("p", Constant("a")) +condition_b = Predicate("p", Constant("b")) +condition_c = Predicate("p", Constant("c")) + + +@pytest.fixture(scope="function") +def derived_predicates_visitor(): + """Return a val predicates visitor.""" + m = PredicateMapping() + return DerivedPredicatesVisitor(m, {a: condition_a, b: condition_b, c: condition_c}) def _eq_pred(a_pred: DerivedPredicate, b_pred: DerivedPredicate) -> bool: @@ -57,44 +72,45 @@ def _eq(a: Set[DerivedPredicate], b: Set[DerivedPredicate]) -> bool: return True -def test_derived_predicates_visitor_not_implemented_fail(): +def test_derived_predicates_visitor_not_implemented_fail(derived_predicates_visitor): """Test the derived predicates visitor when the input argument is not supported.""" with pytest.raises( - NotImplementedError, match="handler not implemented for object " + NotImplementedError, + match="handler not implemented for object of type ", ): - derived_predicates(1, {}) + derived_predicates_visitor.visit(1) -def test_derived_predicates_visitor_true(): +def test_derived_predicates_visitor_true(derived_predicates_visitor): """Test the derived predicates visitor for the propositional true.""" - val = Predicate(add_val_prefix("true")) + val = Predicate(add_val_prefix("quoted_0")) expected = {DerivedPredicate(val, And())} - actual = derived_predicates(PropositionalTrue(), {}) + actual = derived_predicates_visitor.visit(PropositionalTrue()) assert _eq(actual, expected) -def test_derived_predicates_visitor_false(): +def test_derived_predicates_visitor_false(derived_predicates_visitor): """Test the derived predicates visitor for the propositional false.""" - val = Predicate(add_val_prefix("false")) + val = Predicate(add_val_prefix("quoted_0")) expected = {DerivedPredicate(val, Or())} - actual = derived_predicates(PropositionalFalse(), {}) + actual = derived_predicates_visitor.visit(PropositionalFalse()) assert _eq(actual, expected) -def test_derived_predicates_visitor_atomic(): +def test_derived_predicates_visitor_atomic(derived_predicates_visitor): """Test the derived predicates visitor for the atomic formula.""" a = PLTLAtomic("a") - val = Predicate(add_val_prefix("a")) + val = Predicate(add_val_prefix("quoted_0")) condition = Predicate("p", Constant("a")) expected = {DerivedPredicate(val, condition)} - actual = derived_predicates(a, {a: condition}) + actual = derived_predicates_visitor.visit(a) assert _eq(actual, expected) -def test_derived_predicates_visitor_and(): +def test_derived_predicates_visitor_and(derived_predicates_visitor): """Test the derived predicates visitor for the and formula.""" a = PLTLAtomic("a") b = PLTLAtomic("b") @@ -102,9 +118,9 @@ def test_derived_predicates_visitor_and(): condition_a = Predicate("p", Constant("a")) condition_b = Predicate("p", Constant("b")) - val = Predicate(add_val_prefix("a-and-b")) - val_a = Predicate(add_val_prefix("a")) - val_b = Predicate(add_val_prefix("b")) + val = Predicate(add_val_prefix("quoted_0")) + val_a = Predicate(add_val_prefix("quoted_1")) + val_b = Predicate(add_val_prefix("quoted_2")) condition = And(val_a, val_b) @@ -114,11 +130,11 @@ def test_derived_predicates_visitor_and(): DerivedPredicate(val_b, condition_b), } - actual = derived_predicates(a & b, {a: condition_a, b: condition_b}) + actual = derived_predicates_visitor.visit(a & b) assert _eq(actual, expected) -def test_derived_predicates_visitor_or(): +def test_derived_predicates_visitor_or(derived_predicates_visitor): """Test the derived predicates visitor for the or formula.""" a = PLTLAtomic("a") b = PLTLAtomic("b") @@ -126,9 +142,9 @@ def test_derived_predicates_visitor_or(): condition_a = Predicate("p", Constant("a")) condition_b = Predicate("p", Constant("b")) - val = Predicate(add_val_prefix("a-or-b")) - val_a = Predicate(add_val_prefix("a")) - val_b = Predicate(add_val_prefix("b")) + val = Predicate(add_val_prefix("quoted_0")) + val_a = Predicate(add_val_prefix("quoted_1")) + val_b = Predicate(add_val_prefix("quoted_2")) condition = Or(val_a, val_b) @@ -138,59 +154,59 @@ def test_derived_predicates_visitor_or(): DerivedPredicate(val_b, condition_b), } - actual = derived_predicates(a | b, {a: condition_a, b: condition_b}) + actual = derived_predicates_visitor.visit(a | b) assert _eq(actual, expected) -def test_derived_predicates_visitor_not(): +def test_derived_predicates_visitor_not(derived_predicates_visitor): """Test the derived predicates visitor for the not formula.""" a = PLTLAtomic("a") condition_a = Predicate("p", Constant("a")) - val = Predicate(add_val_prefix("not-a")) - val_a = Predicate(add_val_prefix("a")) + val = Predicate(add_val_prefix("quoted_0")) + val_a = Predicate(add_val_prefix("quoted_1")) condition = Not(val_a) expected = {DerivedPredicate(val, condition), DerivedPredicate(val_a, condition_a)} - actual = derived_predicates(~a, {a: condition_a}) + actual = derived_predicates_visitor.visit(~a) assert _eq(actual, expected) -def test_derived_predicates_visitor_yesterday(): +def test_derived_predicates_visitor_yesterday(derived_predicates_visitor): """Test the derived predicates visitor for the yesterday formula.""" a = PLTLAtomic("a") Ya = Before(a) condition_a = Predicate("p", Constant("a")) - val = Predicate(add_val_prefix("Ya")) - val_a = Predicate(add_val_prefix("a")) + val = Predicate(add_val_prefix("quoted_0")) + val_a = Predicate(add_val_prefix("quoted_1")) - condition = Predicate("Ya") + condition = Predicate("quoted_0") expected = {DerivedPredicate(val, condition), DerivedPredicate(val_a, condition_a)} - actual = derived_predicates(Ya, {a: condition_a}) + actual = derived_predicates_visitor.visit(Ya) assert _eq(actual, expected) -def test_derived_predicates_visitor_since(): +def test_derived_predicates_visitor_since(derived_predicates_visitor): """Test the derived predicates visitor for the since formula.""" a = PLTLAtomic("a") b = PLTLAtomic("b") c = PLTLAtomic("c") a_since_b_since_c = Since(a, b, c) - val_a = Predicate(add_val_prefix("a")) - val_b = Predicate(add_val_prefix("b")) - val_c = Predicate(add_val_prefix("c")) - val_a_since_b_since_c = Predicate(add_val_prefix("a-S-b-S-c")) - val_b_since_c = Predicate(add_val_prefix("b-S-c")) - Y_a_since_b_since_c = Predicate("Y-a-S-b-S-c") - Y_b_since_c = Predicate("Y-b-S-c") + val_a = Predicate(add_val_prefix("quoted_2")) + val_b = Predicate(add_val_prefix("quoted_4")) + val_c = Predicate(add_val_prefix("quoted_3")) + val_a_since_b_since_c = Predicate(add_val_prefix("quoted_0")) + val_b_since_c = Predicate(add_val_prefix("quoted_1")) + Y_a_since_b_since_c = Predicate("Y-quoted_0") + Y_b_since_c = Predicate("Y-quoted_1") condition_a = Predicate("p", Constant("a")) condition_b = Predicate("p", Constant("b")) @@ -206,20 +222,18 @@ def test_derived_predicates_visitor_since(): DerivedPredicate(val_c, condition_c), } - actual = derived_predicates( - a_since_b_since_c, {a: condition_a, b: condition_b, c: condition_c} - ) + actual = derived_predicates_visitor.visit(a_since_b_since_c) assert _eq(actual, expected) -def test_derived_predicates_visitor_once(): +def test_derived_predicates_visitor_once(derived_predicates_visitor): """Test the derived predicates visitor for the once formula.""" a = PLTLAtomic("a") once_a = Once(a) - val_a = Predicate(add_val_prefix("a")) - val_once_a = Predicate(add_val_prefix("Oa")) - Y_once_a = Predicate("Y-Oa") + val_a = Predicate(add_val_prefix("quoted_1")) + val_once_a = Predicate(add_val_prefix("quoted_0")) + Y_once_a = Predicate("Y-quoted_0") condition_a = Predicate("p", Constant("a")) condition_once_a = Or(val_a, Y_once_a) @@ -229,5 +243,5 @@ def test_derived_predicates_visitor_once(): DerivedPredicate(val_once_a, condition_once_a), } - actual = derived_predicates(once_a, {a: condition_a}) + actual = derived_predicates_visitor.visit(once_a) assert _eq(actual, expected) diff --git a/tests/test_utils/test_predicates_visitor.py b/tests/test_utils/test_predicates_visitor.py index 82df6b46..2c26fbec 100644 --- a/tests/test_utils/test_predicates_visitor.py +++ b/tests/test_utils/test_predicates_visitor.py @@ -33,74 +33,82 @@ Since, ) -from plan4past.utils.predicates_visitor import predicates +from plan4past.helpers.compilation_helper import PredicateMapping +from plan4past.utils.predicates_visitor import PredicatesVisitor -def test_predicates_visitor_not_implemented_fail(): +@pytest.fixture(scope="function") +def predicates_visitor(): + """Return a val predicates visitor.""" + m = PredicateMapping() + return PredicatesVisitor(m) + + +def test_predicates_visitor_not_implemented_fail(predicates_visitor): """Test the predicates visitor when the input argument is not supported.""" with pytest.raises( NotImplementedError, match="handler not implemented for object of type ", ): - predicates(1) + predicates_visitor.visit(1) -def test_predicates_visitor_true(): +def test_predicates_visitor_true(predicates_visitor): """Test the predicates visitor for the propositional true.""" - assert predicates(PropositionalTrue()) == set() + assert predicates_visitor.visit(PropositionalTrue()) == set() -def test_predicates_visitor_false(): +def test_predicates_visitor_false(predicates_visitor): """Test the predicates visitor for the propositional false.""" - assert predicates(PropositionalFalse()) == set() + assert predicates_visitor.visit(PropositionalFalse()) == set() -def test_predicates_visitor_atomic(): +def test_predicates_visitor_atomic(predicates_visitor): """Test the predicates visitor for the atomic formula.""" - assert predicates(PLTLAtomic("a")) == set() + assert predicates_visitor.visit(PLTLAtomic("a")) == set() -def test_predicates_visitor_and(): +def test_predicates_visitor_and(predicates_visitor): """Test the predicates visitor for the and formula.""" a = PLTLAtomic("a") b = PLTLAtomic("b") - assert predicates(a & b) == set() + assert predicates_visitor.visit(a & b) == set() -def test_predicates_visitor_or(): +def test_predicates_visitor_or(predicates_visitor): """Test the predicates visitor for the or formula.""" a = PLTLAtomic("a") b = PLTLAtomic("b") - assert predicates(a | b) == set() + assert predicates_visitor.visit(a | b) == set() -def test_predicates_visitor_not(): +def test_predicates_visitor_not(predicates_visitor): """Test the predicates visitor for the not formula.""" a = PLTLAtomic("a") - assert predicates(~a) == set() + assert predicates_visitor.visit(~a) == set() -def test_predicates_visitor_yesterday(): +def test_predicates_visitor_yesterday(predicates_visitor): """Test the predicates visitor for the yesterday formula.""" a = PLTLAtomic("a") Ya = Before(a) - assert predicates(Ya) == {Predicate("Ya")} + assert predicates_visitor.visit(Ya) == {Predicate("quoted_0")} -def test_predicates_visitor_since(): +def test_predicates_visitor_since(predicates_visitor): """Test the predicates visitor for the since formula.""" a = PLTLAtomic("a") b = PLTLAtomic("b") c = PLTLAtomic("c") a_since_b_since_c = Since(a, b, c) - assert predicates(a_since_b_since_c) == { - Predicate("Y-b-S-c"), - Predicate("Y-a-S-b-S-c"), + assert predicates_visitor.visit(a_since_b_since_c) == { + Predicate("Y-quoted_0"), + Predicate("Y-quoted_1"), } -def test_predicates_visitor_once(): +def test_predicates_visitor_once(predicates_visitor): """Test the predicates visitor for the once formula.""" a = PLTLAtomic("a") once_a = Once(a) - assert predicates(once_a) == {Predicate("Y-Oa")} + assert predicates_visitor.visit(once_a) == {Predicate("Y-quoted_0")} diff --git a/tests/test_utils/test_val_predicates_visitor.py b/tests/test_utils/test_val_predicates_visitor.py index fe8d521a..138d3176 100644 --- a/tests/test_utils/test_val_predicates_visitor.py +++ b/tests/test_utils/test_val_predicates_visitor.py @@ -32,94 +32,114 @@ Since, ) +from plan4past.helpers.compilation_helper import PredicateMapping from plan4past.helpers.utils import add_val_prefix -from plan4past.utils.val_predicates_visitor import val_predicates +from plan4past.utils.val_predicates_visitor import ValPredicatesVisitor -def test_val_predicates_visitor_not_implemented_fail(): +@pytest.fixture(scope="function") +def val_predicates_visitor(): + """Return a val predicates visitor.""" + m = PredicateMapping() + return ValPredicatesVisitor(m) + + +def test_val_predicates_visitor_not_implemented_fail(val_predicates_visitor): """Test the val predicates visitor when the input argument is not supported.""" with pytest.raises( NotImplementedError, match="handler not implemented for object of type ", ): - val_predicates(1) + val_predicates_visitor.visit(1) -def test_val_predicates_visitor_true(): +def test_val_predicates_visitor_true(val_predicates_visitor): """Test the val predicates visitor for the propositional true.""" - assert val_predicates(PropositionalTrue()) == {Predicate(add_val_prefix("true"))} + assert val_predicates_visitor.visit(PropositionalTrue()) == { + Predicate(add_val_prefix("quoted_0")) + } -def test_val_predicates_visitor_false(): +def test_val_predicates_visitor_false(val_predicates_visitor): """Test the val predicates visitor for the propositional false.""" - assert val_predicates(PropositionalFalse()) == {Predicate(add_val_prefix("false"))} + assert val_predicates_visitor.visit(PropositionalFalse()) == { + Predicate(add_val_prefix("quoted_0")) + } -def test_val_predicates_visitor_atomic(): +def test_val_predicates_visitor_atomic(val_predicates_visitor): """Test the val predicates visitor for the atomic formula.""" a = PLTLAtomic("a") - assert val_predicates(a) == {Predicate(add_val_prefix(a.name))} + assert val_predicates_visitor.visit(a) == {Predicate(add_val_prefix("quoted_0"))} -def test_val_predicates_visitor_and(): +def test_val_predicates_visitor_and(val_predicates_visitor): """Test the val predicates visitor for the and formula.""" a = PLTLAtomic("a") b = PLTLAtomic("b") - val_a_and_b = Predicate(add_val_prefix("a-and-b")) - val_a = Predicate(add_val_prefix(a.name)) - val_b = Predicate(add_val_prefix(b.name)) + actual_set = val_predicates_visitor.visit(a & b) - assert val_predicates(a & b) == {val_a_and_b, val_a, val_b} + val_a_and_b = Predicate(add_val_prefix("quoted_0")) + val_a = Predicate(add_val_prefix("quoted_1")) + val_b = Predicate(add_val_prefix("quoted_2")) + expected_set = {val_a_and_b, val_a, val_b} -def test_val_predicates_visitor_or(): + assert actual_set == expected_set + + +def test_val_predicates_visitor_or(val_predicates_visitor): """Test the val predicates visitor for the or formula.""" a = PLTLAtomic("a") b = PLTLAtomic("b") - val_a_or_b = Predicate(add_val_prefix("a-or-b")) - val_a = Predicate(add_val_prefix(a.name)) - val_b = Predicate(add_val_prefix(b.name)) + actual_set = val_predicates_visitor.visit(a | b) + + val_a_or_b = Predicate(add_val_prefix("quoted_0")) + val_a = Predicate(add_val_prefix("quoted_1")) + val_b = Predicate(add_val_prefix("quoted_2")) + + expected_set = {val_a_or_b, val_a, val_b} - assert val_predicates(a | b) == {val_a_or_b, val_a, val_b} + assert actual_set == expected_set -def test_val_predicates_visitor_not(): +def test_val_predicates_visitor_not(val_predicates_visitor): """Test the val predicates visitor for the not formula.""" a = PLTLAtomic("a") - val_not_a = Predicate(add_val_prefix("not-a")) - val_a = Predicate(add_val_prefix(a.name)) + val_not_a = Predicate(add_val_prefix("quoted_0")) + val_a = Predicate(add_val_prefix("quoted_1")) - assert val_predicates(~a) == {val_not_a, val_a} + assert val_predicates_visitor.visit(~a) == {val_not_a, val_a} -def test_val_predicates_visitor_yesterday(): +def test_val_predicates_visitor_yesterday(val_predicates_visitor): """Test the val predicates visitor for the yesterday formula.""" a = PLTLAtomic("a") yesterday_a = Before(a) - val_yesterday_a = Predicate(add_val_prefix("Ya")) - val_a = Predicate(add_val_prefix(a.name)) + val_yesterday_a = Predicate(add_val_prefix("quoted_0")) + val_a = Predicate(add_val_prefix("quoted_1")) - assert val_predicates(yesterday_a) == {val_yesterday_a, val_a} + assert val_predicates_visitor.visit(yesterday_a) == {val_yesterday_a, val_a} -def test_val_predicates_visitor_since(): +def test_val_predicates_visitor_since(val_predicates_visitor): """Test the val predicates visitor for the yesterday formula.""" a = PLTLAtomic("a") b = PLTLAtomic("b") c = PLTLAtomic("c") a_since_b_since_c = Since(a, b, c) - val_a_since_b_since_c = Predicate(add_val_prefix("a-S-b-S-c")) - val_b_since_c = Predicate(add_val_prefix("b-S-c")) - val_a = Predicate(add_val_prefix(a.name)) - val_b = Predicate(add_val_prefix(b.name)) - val_c = Predicate(add_val_prefix(c.name)) + val_a_since_b_since_c = Predicate(add_val_prefix("quoted_0")) + val_b_since_c = Predicate(add_val_prefix("quoted_1")) + val_a = Predicate(add_val_prefix("quoted_2")) + val_b = Predicate(add_val_prefix("quoted_3")) + val_c = Predicate(add_val_prefix("quoted_4")) - assert val_predicates(a_since_b_since_c) == { + assert val_predicates_visitor.visit(a_since_b_since_c) == { val_a_since_b_since_c, val_b_since_c, val_a, @@ -128,12 +148,12 @@ def test_val_predicates_visitor_since(): } -def test_val_predicates_visitor_once(): +def test_val_predicates_visitor_once(val_predicates_visitor): """Test the val predicates visitor for the once formula.""" a = PLTLAtomic("a") once_a = Once(a) - val_once_a = Predicate(add_val_prefix("Oa")) - val_a = Predicate(add_val_prefix(a.name)) + val_once_a = Predicate(add_val_prefix("quoted_0")) + val_a = Predicate(add_val_prefix("quoted_1")) - assert val_predicates(once_a) == {val_once_a, val_a} + assert val_predicates_visitor.visit(once_a) == {val_once_a, val_a}