21 #include "clips_pddl_parser_feature.h"
23 #include "effect_visitor.h"
24 #include "precondition_visitor.h"
26 #include <core/threading/mutex_locker.h>
27 #include <logging/logger.h>
28 #include <pddl_parser/pddl_parser.h>
34 using namespace pddl_parser;
45 : CLIPSFeature(
"pddl-parser"), logger_(logger)
57 envs_[env_name] = clips;
59 clips->add_function(
"parse-pddl-domain",
60 sigc::slot<void, string>(
61 sigc::bind<0>(sigc::mem_fun(*
this, &PDDLCLIPSFeature::parse_domain),
71 envs_.erase(env_name);
81 PDDLCLIPSFeature::parse_domain(std::string env_name, std::string domain_file)
84 CLIPS::Environment &env = **(envs_[env_name]);
87 ifstream df(domain_file);
90 domain = PddlParser::parseDomain(buffer.str());
92 logger_->
log_error((
"PDDLCLIPS|" + env_name).c_str(),
93 "Failed to parse domain: %s",
97 for (
auto &type : domain.
types) {
98 string super_type =
"";
99 if (!type.second.empty()) {
100 super_type =
"(super-type " + type.second +
")";
102 env.assert_fact(
"(domain-object-type "
104 + type.first +
")" + super_type +
")");
107 string param_string =
"";
108 string type_string =
"";
109 for (
auto ¶m : predicate.second) {
110 param_string +=
" " + param.first;
111 type_string +=
" " + param.second;
113 env.assert_fact(
"(domain-predicate"
126 for (
auto &action : domain.
actions) {
127 string params_string =
"(param-names";
128 for (
auto ¶m_pair : action.action_params) {
129 string param_name = param_pair.first;
130 string param_type = param_pair.second;
131 params_string +=
" " + param_name;
132 env.assert_fact(
"(domain-operator-parameter"
144 params_string +=
")";
145 env.assert_fact(
"(domain-operator (name " + action.name +
")" + params_string +
")");
146 vector<string> precondition_facts =
148 action.precondition);
149 for (
auto &fact : precondition_facts) {
150 env.assert_fact(fact);
152 vector<string> effect_facts =
154 for (
auto &fact : effect_facts) {
155 env.assert_fact(fact);
Translate a PDDL effect into CLIPS facts.
PDDLCLIPSFeature(fawkes::Logger *logger)
Initialize the CLIPS feature.
virtual void clips_context_destroyed(const std::string &env_name)
Clean up a context.
virtual void clips_context_init(const std::string &env_name, fawkes::LockPtr< CLIPS::Environment > &clips)
Initialize the context and add a parse-pddl-domain CLIPS function.
Translate a PDDL precondition into CLIPS facts.
virtual const char * what_no_backtrace() const
Get primary string (does not implicitly print the back trace).
virtual void log_error(const char *component, const char *format,...)=0
Log error message.
Exception thrown by the parser if an error occurs during parsing.
A structured representation of a PDDL domain.
std::vector< Action > actions
A list of actions defined in the domain.
pairs_type types
A list of types with their super types.
std::vector< predicate_type > predicates
A list of predicate names in the domain, including the types of their arguments.