knowledge_reasoning.cpp
Go to the documentation of this file.
00001 
00002 #include "actions/ActionFactory.h"
00003 
00004 #include "actasp/reasoners/Reasoner.h"
00005 #include <actasp/QueryGenerator.h>
00006 #include <actasp/reasoners/Clingo4_2.h>
00007 #include "actasp/action_utils.h"
00008 
00009 #include "msgs_utils.h"
00010 #include "bwi_kr_execution/UpdateFluents.h"
00011 #include "bwi_kr_execution/CurrentStateQuery.h"
00012 #include "bwi_kr_execution/ComputePlan.h"
00013 #include "bwi_kr_execution/ComputeAllPlans.h"
00014 #include "bwi_kr_execution/IsPlanValid.h"
00015 
00016 #include <ros/ros.h>
00017 #include <ros/package.h>
00018 #include <std_srvs/Empty.h>
00019 
00020 
00021 #include <boost/filesystem.hpp>
00022 
00023 using namespace actasp;
00024 using namespace std;
00025 using namespace ros;
00026 using namespace bwi_krexec;
00027 
00028 const int MAX_N = 20;
00029 const std::string queryDirectory("/tmp/bwi_kr_execution/");
00030 
00031 
00032 
00033 bool updateFluents(bwi_kr_execution::UpdateFluents::Request  &req,
00034                    bwi_kr_execution::UpdateFluents::Response &res) throw();
00035 
00036 bool currentStateQuery(bwi_kr_execution::CurrentStateQuery::Request  &req,
00037                        bwi_kr_execution::CurrentStateQuery::Response &res) throw();
00038 
00039 bool computePlan(bwi_kr_execution::ComputePlan::Request  &req,
00040                  bwi_kr_execution::ComputePlan::Response &res);
00041 
00042 bool computeAllPlans(bwi_kr_execution::ComputeAllPlans::Request  &req,
00043                      bwi_kr_execution::ComputeAllPlans::Response &res);
00044 
00045 bool isPlanvalid(bwi_kr_execution::IsPlanValid::Request  &req,
00046                  bwi_kr_execution::IsPlanValid::Response &res);
00047 
00048 bool resetState(std_srvs::Empty::Request &,
00049                 std_srvs::Empty::Response &);
00050 
00051 actasp::AspKR *reasoner;
00052 
00053 int main(int argc, char **argv) {
00054 
00055   ros::init(argc, argv, "bwi_kr");
00056   ros::NodeHandle n;
00057 
00058   if (ros::console::set_logger_level(ROSCONSOLE_DEFAULT_NAME, ros::console::levels::Debug)) {
00059     ros::console::notifyLoggerLevelsChanged();
00060   }
00061 
00062   ros::NodeHandle privateNode("~");
00063   
00064   string domainDirectory;
00065   n.param<std::string>("bwi_kr_execution/domain_directory", domainDirectory, ros::package::getPath("bwi_kr_execution")+"/domain/");
00066   
00067   if(domainDirectory.at(domainDirectory.size()-1) != '/')
00068     domainDirectory += '/';
00069   
00070   bool simulating;
00071   privateNode.param<bool>("simulation",simulating,false);
00072 
00073   ActionFactory::setSimulation(simulating);
00074 
00075   boost::filesystem::create_directories(queryDirectory);
00076 
00077   QueryGenerator* generator = new Clingo4_2("n",queryDirectory,domainDirectory,actionMapToSet(ActionFactory::actions()));
00078   reasoner = new Reasoner(generator, MAX_N,actionMapToSet(ActionFactory::actions()));
00079   reasoner->resetCurrentState();
00080 
00081   ros::ServiceServer update_fluents = n.advertiseService("update_fluents", updateFluents);
00082   ros::ServiceServer current_state_query = n.advertiseService("current_state_query", currentStateQuery);
00083   ros::ServiceServer compute_plan = n.advertiseService("compute_plan", computePlan);
00084   ros::ServiceServer compute_all_plans = n.advertiseService("compute_all_plans", computeAllPlans);
00085   ros::ServiceServer is_plan_valid = n.advertiseService("is_plan_valid", isPlanvalid);
00086   ros::ServiceServer reset_state = n.advertiseService("reset_state", resetState);
00087 
00088 
00089   //TODO make sure clingo can be executed concurrently, or create multiple instances
00090 // ros::MultiThreadedSpinner m(2); //we don't really want to potentially block all the available cores
00091 
00092   ros::spin();
00093 
00094   delete reasoner;
00095   delete generator;
00096 
00097   return 0;
00098 }
00099 
00100 bool updateFluents(bwi_kr_execution::UpdateFluents::Request  &req,
00101                    bwi_kr_execution::UpdateFluents::Response &res) throw() {
00102 
00103   vector<AspFluent> fluents;
00104   transform(req.fluents.begin(),req.fluents.end(),back_inserter(fluents),TranslateFluent());
00105 
00106   res.consistent = reasoner->updateFluents(fluents);
00107 
00108   return true;
00109 }
00110 
00111 bool currentStateQuery(bwi_kr_execution::CurrentStateQuery::Request  &req,
00112                        bwi_kr_execution::CurrentStateQuery::Response &res) throw() {
00113 
00114   vector<AspRule> rules;
00115   transform(req.query.begin(),req.query.end(),back_inserter(rules),TranslateRule());
00116 
00117   AnswerSet answer = reasoner->currentStateQuery(rules);
00118 
00119   res.answer.satisfied = answer.isSatisfied();
00120   transform(answer.getFluents().begin(),answer.getFluents().end(),back_inserter(res.answer.fluents),TranslateFluent());
00121 
00122   return true;
00123 }
00124 
00125 
00126 bool computePlan(bwi_kr_execution::ComputePlan::Request  &req,
00127                  bwi_kr_execution::ComputePlan::Response &res) {
00128   vector<AspRule> goal;
00129   transform(req.goal.begin(),req.goal.end(),back_inserter(goal),TranslateRule());
00130 
00131   //TODO catch exception
00132   AnswerSet answer = reasoner->computePlan(goal);
00133 
00134   res.plan.satisfied = answer.isSatisfied();
00135   transform(answer.getFluents().begin(),answer.getFluents().end(),back_inserter(res.plan.fluents),TranslateFluent());
00136 
00137   return true;
00138 }
00139 
00140 bool computeAllPlans(bwi_kr_execution::ComputeAllPlans::Request& req, bwi_kr_execution::ComputeAllPlans::Response& res) {
00141 
00142   vector<AspRule> goal;
00143   transform(req.goal.begin(),req.goal.end(),back_inserter(goal),TranslateRule());
00144 
00145   //TODO catch exception
00146   vector<actasp::AnswerSet> answers = reasoner->computeAllPlans(goal,req.suboptimality);
00147 
00148   transform(answers.begin(),answers.end(),back_inserter(res.plans),TranslateAnswerSet());
00149 
00150   return true;
00151 }
00152 
00153 
00154 bool isPlanvalid(bwi_kr_execution::IsPlanValid::Request& req, bwi_kr_execution::IsPlanValid::Response& res) {
00155 
00156   vector<AspRule> goal;
00157   transform(req.goal.begin(),req.goal.end(),back_inserter(goal),TranslateRule());
00158 
00159   res.valid = reasoner->isPlanValid(TranslateAnswerSet()(req.plan), goal);
00160 
00161   return true;
00162 }
00163 
00164 bool resetState(std_srvs::Empty::Request &, std_srvs::Empty::Response &) {
00165   reasoner->resetCurrentState();
00166 }


bwi_kr_execution
Author(s): Matteo Leonetti, Piyush Khandelwal
autogenerated on Thu Jun 6 2019 17:57:37