Skip to content

Commit

Permalink
add and fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
francescofuggitti committed Nov 8, 2022
1 parent d3a500d commit 119ee27
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 31 deletions.
28 changes: 17 additions & 11 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,28 +42,30 @@
FIXTURES_PDDL_FILES = FIXTURES_DIR / "pddl_files"
BLOCKSWORLD_FILES = FIXTURES_PDDL_FILES / "blocksworld-ipc08"
TRIANGLE_FILES = FIXTURES_PDDL_FILES / "triangle-tireworld"
BLOCKSWORLD_FOND_FILES = FIXTURES_PDDL_FILES / "blocksworld_fond"

# TODO once missing features are supported, uncomment this
# DOMAIN_FILES = [
# *FIXTURES_PDDL_FILES.glob("./**/domain.pddl")
# ]

DOMAIN_NAMES = [
"acrobatics",
"beam-walk",
"blocksworld-ipc08",
"doors",
# "acrobatics",
# "beam-walk",
# "blocksworld-ipc08",
# "doors",
# "earth_observation",
"elevators",
# "elevators",
# "faults-ipc08",
# "first-responders-ipc08",
"islands",
"miner",
"spiky-tireworld",
"tireworld",
"tireworld-truck",
"triangle-tireworld",
# "islands",
# "miner",
# "spiky-tireworld",
# "tireworld",
# "tireworld-truck",
# "triangle-tireworld",
# "zenotravel",
"blocksworld_fond"
]

DOMAIN_FILES = [
Expand Down Expand Up @@ -101,5 +103,9 @@ def markdown_parser():
triangle_tireworld_domain,
triangle_tireworld_problem_01,
)
from tests.fixtures.code_objects.blocksworld_fond import ( # noqa: E402, F401
blocksworld_fond_domain,
blocksworld_fond_01,
)

#################################################
120 changes: 120 additions & 0 deletions tests/fixtures/code_objects/blocksworld_fond.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
# -*- coding: utf-8 -*-
#
# Copyright 2021 WhiteMech
#
# ------------------------------
#
# This file is part of pddl.
#
# pddl is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# pddl is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with pddl. If not, see <https://www.gnu.org/licenses/>.
#

"""This test module contains the fixtures for 'blocksworld-ipc08' domain and problem."""
import pytest

from pddl.core import Action, Domain, Problem, Requirements
from pddl.logic import Constant
from pddl.logic.base import And, OneOf
from pddl.logic.effects import When, AndEffect
from pddl.logic.helpers import constants, variables
from pddl.logic.predicates import EqualTo, Predicate


@pytest.fixture(scope="session")
def blocksworld_fond_domain():
"""The 'blocksworld' FOND domain."""
# terms
x, y, z, b= variables("x y z b")

# constants:
table = Constant("Table")

# predicates
on = Predicate("on", x, y)
clear = Predicate("clear", x)
block = Predicate("block", b)
predicates = {on, clear, block}

# actions
# put-on
put_on_name = "puton"
put_on_parameters = [x, y, z]
put_on_precondition = on(x, z) & clear(x) & clear(y) & ~EqualTo(y, z) & ~EqualTo(x, z) & ~EqualTo(x, y) & ~EqualTo(
x, table)
put_on_effect = OneOf(
AndEffect(on(x, y), ~on(x, z),
When(~EqualTo(z, table), clear(z)), When(~EqualTo(y, table), ~clear(y))),
AndEffect(on(x, table),
When(~EqualTo(z, table), ~on(x, z) & clear(z)), When(~EqualTo(y, table), ~clear(y))),
)
put_on = Action(put_on_name, put_on_parameters, put_on_precondition, put_on_effect)

name = "blocks-world-domain"
requirements = {
Requirements.STRIPS,
Requirements.EQUALITY,
Requirements.NON_DETERMINISTIC,
Requirements.CONDITIONAL_EFFECTS
}
actions = {
put_on
}
domain = Domain(
name=name,
requirements=requirements,
constants={table},
predicates=predicates,
actions=actions,
)
return domain


@pytest.fixture(scope="session")
def blocksworld_fond_01():
"""Blocksworld FOND problem 01."""
# objects
objects = [A, B, C] = constants("A B C")

Table = Constant("Table")

# predicates
block = Predicate("block", A)
on = Predicate("on", C, A)
clear = Predicate("clear", B)

init = {
block(A),
block(B),
block(C),
block(Table),
on(C, A),
on(A, Table),
on(B, Table),
clear(C),
clear(B),
clear(Table)
}

goal = (And())

problem_name = "sussman-anomaly"

problem = Problem(
problem_name,
domain_name="blocks-world-domain",
objects=objects,
init=init,
goal=goal,
)
return problem
21 changes: 11 additions & 10 deletions tests/fixtures/code_objects/blocksworld_ipc08.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

from pddl.core import Action, Domain, Problem, Requirements
from pddl.logic.base import And, OneOf
from pddl.logic.effects import AndEffect
from pddl.logic.helpers import constants, variables
from pddl.logic.predicates import EqualTo, Predicate

Expand Down Expand Up @@ -55,8 +56,8 @@ def blocksworld_domain():
pick_up_parameters = [b1, b2]
pick_up_precondition = ~EqualTo(b1, b2) & emptyhand & clear(b1) & on(b1, b2)
pick_up_effect = OneOf(
holding(b1) & clear(b2) & ~emptyhand & ~clear(b1) & ~on(b1, b2),
clear(b2) & on_table(b1) & ~on(b1, b2),
AndEffect(holding(b1), clear(b2), ~emptyhand, ~clear(b1), ~on(b1, b2)),
AndEffect(clear(b2), on_table(b1), ~on(b1, b2)),
)
pick_up = Action(
pick_up_name, pick_up_parameters, pick_up_precondition, pick_up_effect
Expand All @@ -66,7 +67,7 @@ def blocksworld_domain():
pick_up_from_table_name = "pick-up-from-table"
pick_up_from_table_parameters = [b]
pick_up_from_table_precondition = emptyhand & clear(b) & on_table(b)
pick_up_from_table_effect = OneOf(And(), holding(b) & ~emptyhand & ~on_table(b))
pick_up_from_table_effect = OneOf(AndEffect(), AndEffect(holding(b), ~emptyhand, ~on_table(b)))
pick_up_from_table = Action(
pick_up_from_table_name,
pick_up_from_table_parameters,
Expand All @@ -79,8 +80,8 @@ def blocksworld_domain():
put_on_block_parameters = [b1, b2]
put_on_block_precondition = holding(b1) & clear(b2)
put_on_block_effect = OneOf(
on(b1, b2) & emptyhand & clear(b1) & ~holding(b1) & ~clear(b2),
on_table(b1) & emptyhand & clear(b1) & ~holding(b1),
AndEffect(on(b1, b2), emptyhand, clear(b1), ~holding(b1), ~clear(b2)),
AndEffect(on_table(b1), emptyhand, clear(b1), ~holding(b1)),
)
put_on_block = Action(
put_on_block_name,
Expand All @@ -93,15 +94,15 @@ def blocksworld_domain():
put_down_name = "put-down"
put_down_parameters = [b]
put_down_precondition = holding(b)
put_down_effect = on_table(b) & emptyhand & clear(b) & ~holding(b)
put_down_effect = AndEffect(on_table(b), emptyhand, clear(b), ~holding(b))
put_down = Action(
put_down_name, put_down_parameters, put_down_precondition, put_down_effect
)
# pick-tower
pick_tower_name = "pick-tower"
pick_tower_parameters = [b1, b2, b3]
pick_tower_precondition = emptyhand & on(b1, b2) & on(b2, b3)
pick_tower_effect = OneOf(And(), holding(b2) & clear(b3) & ~emptyhand & ~on(b2, b3))
pick_tower_effect = OneOf(AndEffect(), AndEffect(holding(b2), clear(b3), ~emptyhand, ~on(b2, b3)))
pick_tower = Action(
pick_tower_name,
pick_tower_parameters,
Expand All @@ -114,8 +115,8 @@ def blocksworld_domain():
put_tower_on_block_parameters = [b1, b2, b3]
put_tower_on_block_precondition = holding(b2) & on(b1, b2) & clear(b3)
put_tower_on_block_effect = OneOf(
on(b2, b3) & emptyhand & ~holding(b2) & ~clear(b3),
on_table(b2) & emptyhand & ~holding(b2),
AndEffect(on(b2, b3), emptyhand, ~holding(b2), ~clear(b3)),
AndEffect(on_table(b2), emptyhand, ~holding(b2)),
)
put_tower_on_block = Action(
put_tower_on_block_name,
Expand All @@ -128,7 +129,7 @@ def blocksworld_domain():
put_tower_down_name = "put-tower-down"
put_tower_down_parameters = [b1, b2]
put_tower_down_precondition = holding(b2) & on(b1, b2)
put_tower_down_effect = on_table(b2) & emptyhand & ~holding(b2)
put_tower_down_effect = AndEffect(on_table(b2), emptyhand, ~holding(b2))
put_tower_down = Action(
put_tower_down_name,
put_tower_down_parameters,
Expand Down
9 changes: 5 additions & 4 deletions tests/fixtures/code_objects/triangle_tireworld.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

from pddl.core import Action, Domain, Problem, Requirements
from pddl.logic.base import And, OneOf
from pddl.logic.effects import AndEffect
from pddl.logic.helpers import constants, variables
from pddl.logic.predicates import Predicate

Expand Down Expand Up @@ -53,10 +54,10 @@ def triangle_tireworld_domain():
move_car_name = "move-car"
move_car_parameters = [from_, to]
move_car_precondition = vehicleat(from_) & road(from_, to) & not_flattire
move_car_effect = And(
move_car_effect = AndEffect(
OneOf(
vehicleat(to) & ~vehicleat(from_),
vehicleat(to) & ~vehicleat(from_) & ~not_flattire,
AndEffect(vehicleat(to), ~vehicleat(from_)),
AndEffect(vehicleat(to), ~vehicleat(from_), ~not_flattire),
)
)
move_car = Action(
Expand All @@ -67,7 +68,7 @@ def triangle_tireworld_domain():
changetire_name = "changetire"
changetire_parameters = [loc]
changetire_precondition = spare_in(loc) & vehicleat(loc)
changetire_effect = ~spare_in(loc) & not_flattire
changetire_effect = AndEffect(~spare_in(loc), not_flattire)
changetire = Action(
changetire_name,
changetire_parameters,
Expand Down
2 changes: 1 addition & 1 deletion tests/test_docs/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def compile_and_exec(code: str, locals_dict: Dict = None) -> Dict:


class BaseTestMarkdownDocs:
"""Base test class for testing Markdown documents."""
"""Base test class for blocksworld_fond Markdown documents."""

MD_FILE: Optional[Path] = None
code_blocks: List[Dict] = []
Expand Down
14 changes: 9 additions & 5 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@
import pytest
from pytest import lazy_fixture # type: ignore # noqa

# from pddl.core import Domain, Problem
from pddl.core import Domain, Problem
from tests.conftest import (
BLOCKSWORLD_FILES,
DOMAIN_FILES,
PROBLEM_FILES,
TRIANGLE_FILES,
BLOCKSWORLD_FOND_FILES
)


Expand All @@ -53,6 +53,7 @@ def test_problem_parser(problem_parser, pddl_file: Path):
[
(BLOCKSWORLD_FILES / "domain.pddl", lazy_fixture("blocksworld_domain")),
(TRIANGLE_FILES / "domain.pddl", lazy_fixture("triangle_tireworld_domain")),
(BLOCKSWORLD_FOND_FILES / "domain.pddl", lazy_fixture("blocksworld_fond_domain")),
],
)
def test_check_domain_parser_output(domain_parser, pddl_file: Path, expected_domain):
Expand All @@ -65,11 +66,14 @@ def test_check_domain_parser_output(domain_parser, pddl_file: Path, expected_dom

@pytest.mark.parametrize(
"pddl_file,expected_problem",
[(BLOCKSWORLD_FILES / "p01.pddl", lazy_fixture("blocksworld_problem_01"))],
[
(BLOCKSWORLD_FILES / "p01.pddl", lazy_fixture("blocksworld_problem_01")),
(BLOCKSWORLD_FOND_FILES / "p01.pddl", lazy_fixture("blocksworld_fond_01"))
]
)
def test_check_problem_parser_output(problem_parser, pddl_file: Path, expected_problem):
"""Test problem parsing."""
problem = problem_parser(pddl_file.read_text())
actual_problem = problem_parser(pddl_file.read_text())

assert isinstance(problem, Problem)
assert problem == expected_problem
assert isinstance(actual_problem, Problem)
assert actual_problem == expected_problem

0 comments on commit 119ee27

Please sign in to comment.