From 2262740d7d8766d5c1eca92acc912d4086c68325 Mon Sep 17 00:00:00 2001 From: Gustavo Date: Mon, 2 Sep 2024 12:19:43 -0300 Subject: [PATCH] add test problem_expert.exist_precidate --- .../test/pddl/domain_simple_derived.pddl | 91 +++++++++++++++++++ .../test/unit/problem_expert_test.cpp | 66 ++++++++++++++ 2 files changed, 157 insertions(+) create mode 100644 plansys2_problem_expert/test/pddl/domain_simple_derived.pddl diff --git a/plansys2_problem_expert/test/pddl/domain_simple_derived.pddl b/plansys2_problem_expert/test/pddl/domain_simple_derived.pddl new file mode 100644 index 00000000..6a2a9880 --- /dev/null +++ b/plansys2_problem_expert/test/pddl/domain_simple_derived.pddl @@ -0,0 +1,91 @@ +(define (domain simple) +(:requirements :strips :typing :adl :fluents :durative-actions :derived-predicates) + +;; Types ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +(:types +person - object +message - object +robot - object +room - object +room_with_teleporter - room +);; end Types ;;;;;;;;;;;;;;;;;;;;;;;;; + +;; Predicates ;;;;;;;;;;;;;;;;;;;;;;;;; +(:predicates + +(robot_talk ?r - robot ?m - message ?p - person) +(robot_near_person ?r - robot ?p - person) +(robot_at ?r - robot ?ro - room) +(person_at ?p - person ?ro - room) +(is_teleporter_enabled ?r - room_with_teleporter) +(is_teleporter_destination ?r - room) +(inferred-robot_at ?r - robot ?ro - room) +(inferred-person_at ?p - person ?ro - room) +);; end Predicates ;;;;;;;;;;;;;;;;;;;; +;; Functions ;;;;;;;;;;;;;;;;;;;;;;;;; +(:functions + (room_distance ?r1 - room ?r2 - room) +);; end Functions ;;;;;;;;;;;;;;;;;;;; + +;; Derived predicates ;;;;;;;;;;;;;;;;;;;;;;;;;;;; +(:derived (inferred-robot_at ?r - robot ?ro - room) + (and + (robot_at ?r ?ro) + ) +) +(:derived (inferred-person_at ?p - person ?ro - room) + (and + (person_at ?p ?ro) + ) +) + +;; Actions ;;;;;;;;;;;;;;;;;;;;;;;;;;;; +(:durative-action move + :parameters (?r - robot ?r1 ?r2 - room) + :duration ( = ?duration (* (room_distance ?r1 ?r2) 0.5)) + :condition (and + (at start(robot_at ?r ?r1))) + :effect (and + (at start(not(robot_at ?r ?r1))) + (at end(robot_at ?r ?r2)) + ) +) + +(:action teleport + :parameters (?r - robot ?r1 - room_with_teleporter ?r2 - room) + :precondition (and + (robot_at ?r ?r1) + (is_teleporter_enabled ?r1) + (is_teleporter_destination ?r2) + ) + :effect (and + (not(robot_at ?r ?r1)) + (robot_at ?r ?r2) + ) +) + +(:durative-action talk + :parameters (?r - robot ?from ?p - person ?m - message) + :duration ( = ?duration 5) + :condition (and + (over all(robot_near_person ?r ?p)) + ) + :effect (and + (at end(robot_talk ?r ?m ?p)) + ) +) + +(:durative-action approach + :parameters (?r - robot ?ro - room ?p - person) + :duration ( = ?duration 5) + :condition (and + (over all(robot_at ?r ?ro)) + (over all(person_at ?p ?ro)) + (at end(person_at ?p ?ro)) + ) + :effect (and + (at end(robot_near_person ?r ?p)) + ) +) + +);; end Domain ;;;;;;;;;;;;;;;;;;;;;;;; diff --git a/plansys2_problem_expert/test/unit/problem_expert_test.cpp b/plansys2_problem_expert/test/unit/problem_expert_test.cpp index 906a8fe3..0ffeea32 100644 --- a/plansys2_problem_expert/test/unit/problem_expert_test.cpp +++ b/plansys2_problem_expert/test/unit/problem_expert_test.cpp @@ -747,6 +747,72 @@ TEST(problem_expert, is_goal_satisfied) ASSERT_TRUE(problem_expert.isGoalSatisfied(goal)); } +TEST(problem_expert, exist_predicate) +{ + std::string pkgpath = ament_index_cpp::get_package_share_directory("plansys2_problem_expert"); + std::ifstream domain_ifs(pkgpath + "/pddl/domain_simple_derived.pddl"); + std::string domain_str(( + std::istreambuf_iterator(domain_ifs)), + std::istreambuf_iterator()); + + auto domain_expert = std::make_shared(domain_str); + plansys2::ProblemExpert problem_expert(domain_expert); + + std::ifstream problem_ifs(pkgpath + "/pddl/problem_simple_1.pddl"); + std::string problem_str(( + std::istreambuf_iterator(problem_ifs)), + std::istreambuf_iterator()); + ASSERT_TRUE(problem_expert.addProblem(problem_str)); + + ASSERT_TRUE( + problem_expert.existPredicate( + parser::pddl::fromStringPredicate( + "(robot_at leia kitchen)"))); + ASSERT_TRUE( + problem_expert.existPredicate( + parser::pddl::fromStringPredicate( + "(inferred-robot_at leia kitchen)"))); + ASSERT_TRUE( + problem_expert.existPredicate( + parser::pddl::fromStringPredicate( + "(person_at jack bedroom)"))); + ASSERT_TRUE( + problem_expert.existPredicate( + parser::pddl::fromStringPredicate( + "(inferred-person_at jack bedroom)"))); + ASSERT_FALSE( + problem_expert.existPredicate( + parser::pddl::fromStringPredicate( + "(inferred-person_at jack kitchen)"))); + ASSERT_FALSE( + problem_expert.existPredicate( + parser::pddl::fromStringPredicate( + "(inferred-robot_at leia bedroom)"))); + + problem_expert.removePredicate( + parser::pddl::fromStringPredicate("(robot_at leia kitchen)")); + problem_expert.removePredicate( + parser::pddl::fromStringPredicate("(person_at jack bedroom)")); + + ASSERT_FALSE( + problem_expert.existPredicate( + parser::pddl::fromStringPredicate( + "(inferred-person_at jack bedroom)"))); + ASSERT_FALSE( + problem_expert.existPredicate( + parser::pddl::fromStringPredicate( + "(inferred-robot_at leia kitchen)"))); + ASSERT_FALSE( + problem_expert.existPredicate( + parser::pddl::fromStringPredicate( + "(person_at jack bedroom)"))); + ASSERT_FALSE( + problem_expert.existPredicate( + parser::pddl::fromStringPredicate( + "(robot_at leia kitchen)"))); + +} + int main(int argc, char ** argv) { testing::InitGoogleTest(&argc, argv);