Skip to content

Commit

Permalink
removed additional parsing context, moved error handler only to the r…
Browse files Browse the repository at this point in the history
…oot of a rule
  • Loading branch information
drexlerd committed Oct 21, 2023
1 parent bb52c02 commit 1630638
Show file tree
Hide file tree
Showing 18 changed files with 404 additions and 211 deletions.
1 change: 1 addition & 0 deletions api/python/src/dlplan/policy/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class PolicyFactory:
def make_bot_effect(self, numerical: Numerical) -> BaseEffect: ...
def make_rule(self, conditions: MutableSet[BaseCondition], effects: MutableSet[BaseEffect]) -> Rule: ...
def make_policy(self, rules: MutableSet[Rule]) -> Policy: ...
def parse_policy(self, description: str, filename: str = "") -> Policy: ...


class PolicyMinimizer:
Expand Down
22 changes: 9 additions & 13 deletions examples/policy/policy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,9 @@ static std::string read_from_file(const std::string& filename) {
int main() {
auto vocabulary = construct_vocabulary_info();
auto instance = construct_instance_info(vocabulary);

auto element_factory = std::make_shared<dlplan::core::SyntacticElementFactory>(vocabulary);

std::cout << "Constructing policy:" << std::endl;
// boolean_1 represents whether the hand is empty or not
auto boolean_1 = element_factory->parse_boolean("b_nullary(arm-empty)");
// numerical_1 representes the number of blocks on top of another block
Expand Down Expand Up @@ -122,22 +123,17 @@ int main() {
assert(!policy->evaluate(state_2, state_0, caches));
assert(!policy->evaluate(state_2, state_1, caches));

std::cout << "Write policy:" << std::endl;
std::cout << policy->compute_repr() << std::endl << std::endl;
std::cout << policy->str() << std::endl << std::endl;

write_to_file("policy.txt", policy->str());

std::string pol =
"(:policy\n"
"(:booleans )\n"
"(:numericals (n0 \"n_count(c_primitive(holding,0))\") (n1 \"n_count(c_equal(r_primitive(at,0,1),r_primitive(at_g,0,1)))\") (n2 \"n_count(r_and(r_primitive(at,0,1),r_primitive(at_g,0,1)))\"))\n"
"(:rule (:conditions (:c_n_gt n0) (:c_n_gt n1) (:c_n_gt n2)) (:effects (:e_n_inc n0) (:e_n_bot n1) (:e_n_bot n2)))\n"
std::cout << "Parsing policy:" << std::endl;
std::string policy_str =
"(:policy"
"(:booleans (b0 \"b_nullary(arm-empty)\"))"
"(:numericals (n0 \"n_count(r_primitive(on,0,1))\"))"
"(:rule (:conditions (:c_b_pos b0) (:c_n_gt n0)) (:effects (:e_b_bot b42) (:e_n_dec n0)))"
")";
// auto policy_in = policy_factory.parse_policy(read_from_file("policy.txt"));
auto policy_in = policy_factory.parse_policy(pol);

std::cout << "Read policy:" << std::endl;
auto policy_in = policy_factory.parse_policy(policy_str);
std::cout << policy_in->compute_repr() << std::endl << std::endl;
std::cout << policy_in->str() << std::endl << std::endl;

Expand Down
35 changes: 18 additions & 17 deletions examples/policy/policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from dlplan.core import VocabularyInfo, InstanceInfo, State, SyntacticElementFactory, \
DenotationsCaches
from dlplan.policy import PolicyBuilder, PolicyWriter, PolicyReader
from dlplan.policy import PolicyFactory


def construct_vocabulary_info():
Expand Down Expand Up @@ -51,23 +51,24 @@ def construct_instance_info(vocabulary):
def main():
vocabulary = construct_vocabulary_info()
instance = construct_instance_info(vocabulary)

element_factory = SyntacticElementFactory(vocabulary)

print("Constructing policy:")
# boolean_1 represents whether the hand is empty or not
boolean_1 = element_factory.parse_boolean("b_nullary(arm-empty)")
# numerical_1 representes the number of blocks on top of another block
numerical_1 = element_factory.parse_numerical("n_count(r_primitive(on,0,1))")

policy_builder = PolicyBuilder(element_factory)
b_pos_condition_0 = policy_builder.make_pos_condition(boolean_1)
b_bot_effect_0 = policy_builder.make_bot_effect(boolean_1)
n_gt_condition_0 = policy_builder.make_gt_condition(numerical_1)
n_dec_effect_0 = policy_builder.make_dec_effect(numerical_1)
rule_1 = policy_builder.make_rule(
policy_factory = PolicyFactory(element_factory)
b_pos_condition_0 = policy_factory.make_pos_condition(boolean_1)
b_bot_effect_0 = policy_factory.make_bot_effect(boolean_1)
n_gt_condition_0 = policy_factory.make_gt_condition(numerical_1)
n_dec_effect_0 = policy_factory.make_dec_effect(numerical_1)
rule_1 = policy_factory.make_rule(
{b_pos_condition_0, n_gt_condition_0},
{b_bot_effect_0, n_dec_effect_0}
)
policy = policy_builder.make_policy({rule_1})
policy = policy_factory.make_policy({rule_1})

atoms = instance.get_atoms()
atom_0 = atoms[0]
Expand All @@ -84,25 +85,25 @@ def main():

caches = DenotationsCaches()
assert policy.evaluate(state_0, state_2, caches)
assert not policy.evaluate(state_1, state_2, caches)
assert policy.evaluate(state_1, state_2, caches)
assert not policy.evaluate(state_2, state_0, caches)
assert not policy.evaluate(state_2, state_1, caches)

print("Write policy:")
print(repr(policy))
print(str(policy))
print()
with open("policy.txt", "w", encoding="iso8859-1") as file:
file.write(PolicyWriter().write(policy))

print("Read policy:")
with open("policy.txt", "r", encoding="iso8859-1") as file:
policy_in = PolicyReader().read("\n".join(file.readlines()), policy_builder, element_factory)
print("Parsing policy:")
policy_str = "(:policy" \
"(:booleans (b0 \"b_nullary(arm-empty)\"))" \
"(:numericals (n0 \"n_count(r_primitive(on,0,1))\"))" \
"(:rule (:conditions (:c_b_pos b0) (:c_n_gt n0)) (:effects (:e_b_bot b42) (:e_n_dec n0)))" \
")"
policy_in = policy_factory.parse_policy(policy_str)
print(repr(policy_in))
print(str(policy_in))
print()



if __name__ == "__main__":
main()
20 changes: 3 additions & 17 deletions include/dlplan/common/parsers/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

/// @brief Defines types of our parsers.
/// The configuration is relevant when reusing the parsers instantiated by the library.
/// The additional parsing context can be modified if additional information is needed.
namespace dlplan::common::parsers
{
namespace x3 = boost::spirit::x3;
Expand All @@ -26,29 +25,16 @@ namespace dlplan::common::parsers
typedef error_handler<iterator_type> error_handler_type;


/* In case we need to provide context in the future. */
struct parsing_context_tag;

struct parsing_context_type {
bool error_reported = false;
};

/* The phrase parse context */
typedef
x3::phrase_parse_context<x3::ascii::space_type>::type
phrase_context_type;
x3::phrase_parse_context<x3::ascii::space_type>::type phrase_context_type;


/* Combined error handler, parsing, and phrase parse Context */
/* Combined error handler and phrase parse Context */
typedef x3::context<
error_handler_tag,
std::reference_wrapper<error_handler_type>,
x3::context<
parsing_context_tag,
std::reference_wrapper<parsing_context_type>,
phrase_context_type>>
phrase_context_type>
context_type;

}

#endif
36 changes: 8 additions & 28 deletions include/dlplan/common/parsers/error_handler.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef DLPLAN_INCLUDE_DLPLAN_CORE_PARSERS_ELEMENTS_COMMON_ERROR_HANDLER_HPP_
#define DLPLAN_INCLUDE_DLPLAN_CORE_PARSERS_ELEMENTS_COMMON_ERROR_HANDLER_HPP_
#ifndef DLPLAN_INCLUDE_DLPLAN_CORE_COMMON_PARSERS_ERROR_HANDLER_HPP_
#define DLPLAN_INCLUDE_DLPLAN_CORE_COMMON_PARSERS_ERROR_HANDLER_HPP_

#include <map>
#include <iostream>
Expand All @@ -12,40 +12,24 @@ namespace dlplan::common::parsers
namespace x3 = boost::spirit::x3;

////////////////////////////////////////////////////////////////////////////
// Our error handler
// Our error handler
////////////////////////////////////////////////////////////////////////////

/// @brief The error_handler_base only print error message for the first occurence
/// The expectation failures of the parent grammar rules are not printed.
/// We do not throw an exception because, as a general rule,
/// the caller should have the option to recover from a failed parse.
struct error_handler_base
{
std::string error_message;
std::unordered_map<std::string, std::string> id_map;

// Spirit calls the default constructor
error_handler_base() : error_message("") { }

error_handler_base(const std::string& message) : error_message(message) { }

virtual ~error_handler_base() { }
error_handler_base() { }

template <typename Iterator, typename Exception, typename Context>
x3::error_handler_result on_error(
Iterator& /*first*/, Iterator const& /*last*/
, Exception const& x, Context const& context) {
{
auto& parsing_context = x3::get<parsing_context_tag>(context).get();
if (parsing_context.error_reported) {
// We only print the first occurence of an error
return x3::error_handler_result::fail;
}
parsing_context.error_reported = true;

std::string which = x.which();
// Use our message if defined
if (error_message != "") {
which = error_message;
auto iter = id_map.find(which);
if (iter != id_map.end()) {
which = iter->second;
}

std::string message = "Error! Expecting: " + which + " here:";
Expand All @@ -56,10 +40,6 @@ namespace dlplan::common::parsers
}
}
};




}

#endif
70 changes: 70 additions & 0 deletions include/dlplan/core/parsers/elements/stage_1/error_handler.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#ifndef DLPLAN_INCLUDE_DLPLAN_CORE_PARSERS_ELEMENTS_STAGE_1_ERROR_HANDLER_HPP_
#define DLPLAN_INCLUDE_DLPLAN_CORE_PARSERS_ELEMENTS_STAGE_1_ERROR_HANDLER_HPP_

#include <map>
#include <iostream>

#include "include/dlplan/common/parsers/error_handler.hpp"


namespace dlplan::core::parsers::elements::stage_1
{
namespace x3 = boost::spirit::x3;

struct error_handler_core : dlplan::common::parsers::error_handler_base {
error_handler_core() : dlplan::common::parsers::error_handler_base() {
// TODO: add more precise error messages.
id_map["name"] = "name";
id_map["constant"] = "constant";
id_map["predicate"] = "predicate";
id_map["position"] = "position";
id_map["boolean_inner"] = "boolean_inner";
id_map["concept_inner"] = "concept_inner";
id_map["numerical_inner"] = "numerical_inner";
id_map["role_inner"] = "role_inner";
id_map["element_inner"] = "element_inner";
id_map["concept_or_role_inner"] = "concept_or_role_inner";
id_map["concept_or_role"] = "concept_or_role";
id_map["empty_boolean"] = "empty_boolean";
id_map["inclusion_boolean"] = "inclusion_boolean";
id_map["nullary_boolean"] = "nullary_boolean";
id_map["all_concept"] = "all_concept";
id_map["and_concept"] = "and_concept";
id_map["bot_concept"] = "bot_concept";
id_map["diff_concept"] = "diff_concept";
id_map["equal_concept"] = "equal_concept";
id_map["not_concept"] = "not_concept";
id_map["one_of_concept"] = "one_of_concept";
id_map["or_concept"] = "or_concept";
id_map["primitive_concept"] = "primitive_concept";
id_map["projection_concept"] = "projection_concept";
id_map["some_concept"] = "some_concept";
id_map["subset_concept"] = "subset_concept";
id_map["top_concept"] = "top_concept";
id_map["concept_distance_numerical"] = "concept_distance_numerical";
id_map["count_numerical"] = "count_numerical";
id_map["role_distance_numerical"] = "role_distance_numerical";
id_map["sum_concept_distance_numerical"] = "sum_concept_distance_numerical";
id_map["sum_role_distance_numerical"] = "sum_role_distance_numerical";
id_map["and_role"] = "and_role";
id_map["compose_role"] = "compose_role";
id_map["diff_role"] = "diff_role";
id_map["identity_role"] = "identity_role";
id_map["inverse_role"] = "inverse_role";
id_map["not_role"] = "not_role";
id_map["or_role"] = "or_role";
id_map["primitive_role"] = "primitive_role";
id_map["restrict_role"] = "restrict_role";
id_map["top_role"] = "top_role";
id_map["transitive_closure_role"] = "transitive_closure_role";
id_map["transitive_reflexive_closure_role"] = "transitive_reflexive_closure_role";
id_map["boolean"] = "boolean";
id_map["numerical"] = "numerical";
id_map["concept"] = "concept";
id_map["role"] = "role";
id_map["element"] = "element";
}
};
}

#endif
22 changes: 22 additions & 0 deletions include/dlplan/policy/parsers/policy/stage_1/error_handler.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef DLPLAN_INCLUDE_DLPLAN_POLICY_PARSERS_POLICY_STAGE_1_ERROR_HANDLER_HPP_
#define DLPLAN_INCLUDE_DLPLAN_POLICY_PARSERS_POLICY_STAGE_1_ERROR_HANDLER_HPP_

#include <map>
#include <iostream>

#include "include/dlplan/core/parsers/elements/stage_1/error_handler.hpp"


namespace dlplan::policy::parsers::policy::stage_1
{
namespace x3 = boost::spirit::x3;

struct error_handler_policy : dlplan::core::parsers::elements::stage_1::error_handler_core {
error_handler_policy() : dlplan::core::parsers::elements::stage_1::error_handler_core() {
// TODO: add more precise error messages.

}
};
}

#endif
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ add_subdirectory(novelty)
add_subdirectory(policy)
add_subdirectory(serialization)
add_subdirectory(state_space)
# add_subdirectory(weisfeiler_lehman)
#add_subdirectory(weisfeiler_lehman)
Loading

0 comments on commit 1630638

Please sign in to comment.