Skip to content

Commit 25bacb0

Browse files
committed
temp
1 parent ce48e1e commit 25bacb0

10 files changed

+539
-444
lines changed

plan4past/compiler.py

+31-18
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@
4545
TRUE_PREDICATE,
4646
)
4747
from plan4past.exceptions import ProblemUnsolvableException
48-
from plan4past.helpers.compilation_helper import CompilationManager, YesterdayAtom
48+
from plan4past.helpers.compilation_helper import (
49+
CompilationManager,
50+
PredicateMapping,
51+
YesterdayAtom,
52+
)
4953
from plan4past.helpers.utils import (
5054
add_val_prefix,
5155
check_,
@@ -55,13 +59,13 @@
5559
)
5660
from plan4past.helpers.yesterday_atom_helper import QUOTED_ATOM
5761
from plan4past.utils.atoms_visitor import find_atoms
58-
from plan4past.utils.derived_visitor import derived_predicates
62+
from plan4past.utils.derived_visitor import DerivedPredicatesVisitor
5963
from plan4past.utils.dnf_visitor import dnf
6064
from plan4past.utils.nnf_visitor import nnf
61-
from plan4past.utils.predicates_visitor import predicates
65+
from plan4past.utils.predicates_visitor import PredicatesVisitor
6266
from plan4past.utils.pylogics2pddl import Pylogics2PddlTranslator
6367
from plan4past.utils.rewrite_formula_visitor import rewrite
64-
from plan4past.utils.val_predicates_visitor import val_predicates
68+
from plan4past.utils.val_predicates_visitor import ValPredicatesVisitor
6569

6670

6771
class Compiler:
@@ -93,6 +97,7 @@ def __init__(
9397

9498
check_(self.formula.logic == Logic.PLTL, "only PPLTL is supported!")
9599

100+
self._predicate_mapping = PredicateMapping()
96101
self._executed: bool = False
97102
self._result_domain: Optional[Domain] = None
98103
self._result_problem: Optional[Problem] = None
@@ -134,18 +139,27 @@ def result(self) -> Tuple[Domain, Problem]:
134139

135140
def compile(self):
136141
"""Compute the new domain and the new problem."""
137-
if not self._executed:
138-
self._compile_domain()
139-
self._compile_problem()
140-
self._executed = True
142+
if self._executed:
143+
return
144+
145+
self._compile_domain()
146+
self._compile_problem()
147+
148+
self._executed = True
141149

142150
def _compile_domain(self):
143151
"""Compute the new domain."""
144-
new_predicates = predicates(self.formula).union(val_predicates(self.formula))
145-
new_derived_predicates = derived_predicates(
146-
self.formula, self.from_atoms_to_fluent
152+
subformula_predicates_set = PredicatesVisitor(self._predicate_mapping).visit(
153+
self.formula
154+
)
155+
val_predicates_set = ValPredicatesVisitor(self._predicate_mapping).visit(
156+
self.formula
147157
)
148-
new_whens = _compute_whens(self.formula)
158+
new_predicates = subformula_predicates_set.union(val_predicates_set)
159+
new_derived_predicates = DerivedPredicatesVisitor(
160+
self._predicate_mapping, self.from_atoms_to_fluent
161+
).visit(self.formula)
162+
new_whens = _compute_whens(subformula_predicates_set)
149163
domain_actions = _update_domain_actions_det(self.domain.actions, new_whens)
150164

151165
self._result_domain = Domain(
@@ -174,26 +188,25 @@ def _compile_problem(self):
174188
else set(self.problem.init)
175189
)
176190

191+
goal_predicate = self._predicate_mapping.get_predicate(self.formula)
177192
self._result_problem = Problem(
178193
name=self.problem.name,
179194
domain_name=self.domain.name,
180195
requirements=self.problem.requirements,
181196
objects=[*self.problem.objects],
182197
init=new_init,
183-
goal=And(
184-
Predicate(add_val_prefix(replace_symbols(to_string(self.formula))))
185-
),
198+
goal=And(Predicate(add_val_prefix(goal_predicate.name))),
186199
)
187200

188201

189-
def _compute_whens(formula: Formula) -> Set[When]:
202+
def _compute_whens(predicates: Set[Predicate]) -> Set[When]:
190203
"""Compute conditional effects for formula progression."""
191204
return {
192205
When(Predicate(add_val_prefix(remove_yesterday_prefix(p.name))), p)
193-
for p in predicates(formula)
206+
for p in predicates
194207
}.union(
195208
When(Not(Predicate(add_val_prefix(remove_yesterday_prefix(p.name)))), Not(p))
196-
for p in predicates(formula)
209+
for p in predicates
197210
)
198211

199212

plan4past/helpers/compilation_helper.py

+46-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@
2020
# along with Plan4Past. If not, see <https://www.gnu.org/licenses/>.
2121
#
2222
"""This module contains the class that manages the compilation of a PPLTL formula."""
23-
from typing import List, Tuple
23+
from typing import Dict, List, Tuple
2424

25+
from pddl.logic import Predicate
2526
from pylogics.syntax.base import Formula, Not
2627
from pylogics.syntax.pltl import (
2728
Atomic,
@@ -33,7 +34,7 @@
3334
)
3435

3536
from plan4past.helpers.utils import check_
36-
from plan4past.helpers.yesterday_atom_helper import YesterdayAtom
37+
from plan4past.helpers.yesterday_atom_helper import QUOTED_ATOM, YesterdayAtom
3738
from plan4past.utils.ppnf_visitor import ppnf
3839
from plan4past.utils.yesterday_generator_visitor import get_quoted_dictionary
3940

@@ -108,3 +109,46 @@ def get_problem_extension(self) -> Tuple[List[Formula], List, Formula]:
108109
conditional_effects.append((ppnf(yesterday_atom.formula), yesterday_atom))
109110

110111
return fresh_atoms, conditional_effects, goal
112+
113+
114+
class PredicateMapping:
115+
"""Class that manages the mapping of the predicates."""
116+
117+
def __init__(self):
118+
"""Initialize the predicate mapping."""
119+
self.mapping: Dict[Formula, Predicate] = {}
120+
self.inverse_mapping: Dict[Predicate, Formula] = {}
121+
self.id = 0
122+
123+
def add_predicate(self, formula: Formula) -> None:
124+
"""
125+
Add a predicate to the mapping.
126+
127+
:param formula: the formula to be added
128+
"""
129+
if self.mapping.get(formula) is None:
130+
self.mapping[formula] = Predicate(f"{QUOTED_ATOM}_{self.id}")
131+
self.inverse_mapping[self.mapping[formula]] = formula
132+
self.id += 1
133+
134+
def get_predicate(self, formula: Formula) -> Predicate:
135+
"""
136+
Get the predicate from the mapping.
137+
138+
:param formula: the formula to be added
139+
:return: the predicate
140+
"""
141+
result = self.mapping.get(formula)
142+
if result is None:
143+
self.add_predicate(formula)
144+
return self.mapping[formula]
145+
146+
def get_formula(self, predicate: Predicate) -> Formula:
147+
"""
148+
Get the formula from the inverse mapping.
149+
150+
:param predicate: the predicate to be added
151+
:return: the formula
152+
"""
153+
check_(predicate in self.inverse_mapping, f"predicate {predicate} not found")
154+
return self.inverse_mapping[predicate]

0 commit comments

Comments
 (0)