Skip to content

Commit

Permalink
refactoring: separated events and processes in pddl writer
Browse files Browse the repository at this point in the history
  • Loading branch information
Samuel Gobbi committed Dec 5, 2024
1 parent 8a8a44b commit 7bd6be1
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 65 deletions.
127 changes: 63 additions & 64 deletions unified_planning/io/pddl_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -704,73 +704,72 @@ def _write_domain(self, out: IO[str]):
out.write(")\n")
else:
raise NotImplementedError
for nt in self.problem.natural_transitions:
if isinstance(nt, up.model.Event):
if any(p.simplify().is_false() for p in nt.preconditions):
continue
out.write(f" (:event {self._get_mangled_name(nt)}")
out.write(f"\n :parameters (")
self._write_parameters(out, nt)
for proc in self.problem.processes:

if any(p.simplify().is_false() for p in proc.preconditions):
continue

Check warning on line 710 in unified_planning/io/pddl_writer.py

View check run for this annotation

Codecov / codecov/patch

unified_planning/io/pddl_writer.py#L710

Added line #L710 was not covered by tests
out.write(f" (:process {self._get_mangled_name(proc)}")
out.write(f"\n :parameters (")
self._write_parameters(out, proc)
out.write(")")
if len(proc.preconditions) > 0:
precond_str = []
for p in (c.simplify() for c in proc.preconditions):
if not p.is_true():
if p.is_and():
precond_str.extend(map(converter.convert, p.args))

Check warning on line 720 in unified_planning/io/pddl_writer.py

View check run for this annotation

Codecov / codecov/patch

unified_planning/io/pddl_writer.py#L720

Added line #L720 was not covered by tests
else:
precond_str.append(converter.convert(p))
out.write(f'\n :precondition (and {" ".join(precond_str)})')
elif len(proc.preconditions) == 0 and self.empty_preconditions:
out.write(f"\n :precondition ()")

Check warning on line 725 in unified_planning/io/pddl_writer.py

View check run for this annotation

Codecov / codecov/patch

unified_planning/io/pddl_writer.py#L724-L725

Added lines #L724 - L725 were not covered by tests
if len(proc.effects) > 0:
out.write("\n :effect (and")
for e in proc.effects:
_write_derivative(
e,
out,
converter,
)
out.write(")")
if len(nt.preconditions) > 0:
precond_str = []
for p in (c.simplify() for c in nt.preconditions):
if not p.is_true():
if p.is_and():
precond_str.extend(map(converter.convert, p.args))
else:
precond_str.append(converter.convert(p))
out.write(f'\n :precondition (and {" ".join(precond_str)})')
elif len(nt.preconditions) == 0 and self.empty_preconditions:
out.write(f"\n :precondition ()")
if len(nt.effects) > 0:
out.write("\n :effect (and")
for e in nt.effects:
_write_effect(
e,
None,
out,
converter,
self.rewrite_bool_assignments,
self._get_mangled_name,
)
out.write(")\n")
for eve in self.problem.events:

if nt in costs:
out.write(
f" (increase (total-cost) {converter.convert(costs[nt])})"
)
out.write(")")
out.write(")\n")
elif isinstance(nt, up.model.Process):
if any(p.simplify().is_false() for p in nt.preconditions):
continue
out.write(f" (:process {self._get_mangled_name(nt)}")
out.write(f"\n :parameters (")
self._write_parameters(out, nt)
if any(p.simplify().is_false() for p in eve.preconditions):
continue

Check warning on line 739 in unified_planning/io/pddl_writer.py

View check run for this annotation

Codecov / codecov/patch

unified_planning/io/pddl_writer.py#L739

Added line #L739 was not covered by tests
out.write(f" (:event {self._get_mangled_name(eve)}")
out.write(f"\n :parameters (")
self._write_parameters(out, eve)
out.write(")")
if len(eve.preconditions) > 0:
precond_str = []
for p in (c.simplify() for c in eve.preconditions):
if not p.is_true():
if p.is_and():
precond_str.extend(map(converter.convert, p.args))

Check warning on line 749 in unified_planning/io/pddl_writer.py

View check run for this annotation

Codecov / codecov/patch

unified_planning/io/pddl_writer.py#L749

Added line #L749 was not covered by tests
else:
precond_str.append(converter.convert(p))
out.write(f'\n :precondition (and {" ".join(precond_str)})')
elif len(eve.preconditions) == 0 and self.empty_preconditions:
out.write(f"\n :precondition ()")

Check warning on line 754 in unified_planning/io/pddl_writer.py

View check run for this annotation

Codecov / codecov/patch

unified_planning/io/pddl_writer.py#L753-L754

Added lines #L753 - L754 were not covered by tests
if len(eve.effects) > 0:
out.write("\n :effect (and")
for e in eve.effects:
_write_effect(
e,
None,
out,
converter,
self.rewrite_bool_assignments,
self._get_mangled_name,
)

if eve in costs:
out.write(

Check warning on line 768 in unified_planning/io/pddl_writer.py

View check run for this annotation

Codecov / codecov/patch

unified_planning/io/pddl_writer.py#L768

Added line #L768 was not covered by tests
f" (increase (total-cost) {converter.convert(costs[eve])})"
)
out.write(")")
if len(nt.preconditions) > 0:
precond_str = []
for p in (c.simplify() for c in nt.preconditions):
if not p.is_true():
if p.is_and():
precond_str.extend(map(converter.convert, p.args))
else:
precond_str.append(converter.convert(p))
out.write(f'\n :precondition (and {" ".join(precond_str)})')
elif len(nt.preconditions) == 0 and self.empty_preconditions:
out.write(f"\n :precondition ()")
if len(nt.effects) > 0:
out.write("\n :effect (and")
for e in nt.effects:
_write_derivative(
e,
out,
converter,
)
out.write(")")
out.write(")\n")
else:
raise NotImplementedError
out.write(")\n")
out.write(")\n")

def _write_problem(self, out: IO[str]):
Expand Down
2 changes: 1 addition & 1 deletion unified_planning/test/test_pddl_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import os
import tempfile
import pytest # type: ignore
import pytest
from typing import cast
import unified_planning
from unified_planning.shortcuts import *
Expand Down

0 comments on commit 7bd6be1

Please sign in to comment.