rule_learner_alg_node.cpp
Go to the documentation of this file.
00001 #include "rule_learner_alg_node.h"
00002 
00003 RuleLearnerAlgNode::RuleLearnerAlgNode(void) :
00004   algorithm_base::IriBaseAlgorithm<RuleLearnerAlgorithm>()
00005 {
00006   //init class attributes if necessary
00007   this->loop_rate_ = 0.2;//in [Hz]
00008 
00009   // [init publishers]
00010   
00011   // [init subscribers]
00012   
00013   // [init services]
00014   this->learn_with_pasula_server_ = this->public_node_handle_.advertiseService("learn_with_pasula", &RuleLearnerAlgNode::learn_with_pasulaCallback, this);
00015   this->add_state_server_ = this->public_node_handle_.advertiseService("add_state", &RuleLearnerAlgNode::add_stateCallback, this);
00016   this->add_action_server_ = this->public_node_handle_.advertiseService("add_action", &RuleLearnerAlgNode::add_actionCallback, this);
00017   
00018   // [init clients]
00019   learn_pasula_client_ = this->public_node_handle_.serviceClient<iri_learning_msgs::LearnRulesFromTransitions>("learn_pasula");
00020   representation_to_string_client_ = this->public_node_handle_.serviceClient<estirabot_msgs::RepresentationToString>("representation_to_string");
00021   get_state_changes_client_ = this->public_node_handle_.serviceClient<estirabot_msgs::StateRepresentationChanges>("get_state_changes");
00022   
00023   // [init action servers]
00024   
00025   // [init action clients]
00026   
00027   pasula_time_ = ros::Duration(0);
00028   pasula_iter_ = 0;
00029 }
00030 
00031 RuleLearnerAlgNode::~RuleLearnerAlgNode(void)
00032 {
00033   // [free dynamic memory]
00034 }
00035 
00036 void RuleLearnerAlgNode::mainNodeThread(void)
00037 {
00038   // [fill msg structures]
00039   
00040   // [fill srv structure and make request to the server]
00041   
00042   // [fill action structure and make request to the action server]
00043 
00044   // [publish messages]
00045         std::vector<std::string> actions = this->alg_.actions_needing_pasula_learning();
00046         if (actions.size() > 0) {
00047                 ROS_INFO("RuleLearnerAlgNode:: Requesting pasula rules."); 
00048                 
00049                 this->alg_.lock();
00050                 TransitionList transitions = this->alg_.get_transitions_with_actions(actions);
00051                 this->alg_.write_transitions(remove_unused_predicates(transitions));
00052                 this->alg_.reset_new_transitions();
00053                 this->alg_.unlock();
00054                 
00055                 ros::Time begin = ros::Time::now();
00056                 // learn_pasula_srv_.request.data = my_var; 
00057                 if (learn_pasula_client_.call(learn_pasula_srv_)) 
00058                 { 
00059                         ROS_INFO_STREAM("RuleLearnerAlgNode:: Response: " << learn_pasula_srv_.response.ok); 
00060                 } 
00061                 else 
00062                 { 
00063                         ROS_ERROR("RuleLearnerAlgNode:: Failed to Call Server on topic learn_pasula "); 
00064                         return;
00065                 }
00066                 this->alg_.lock();
00067                 std::vector<int> number_rules = this->alg_.update_with_pasula_rules(actions, transitions);
00068                 this->alg_.unlock();
00069                 
00070                 // ugly logging
00071                 pasula_time_ += ros::Time::now() - begin;
00072                 pasula_iter_++;
00073                 
00074                 std::string pasula_logs_file_path = "/tmp/pasula_logs.txt";
00075                 std::ofstream pasula_logs_file(pasula_logs_file_path.c_str());
00076                 if (pasula_logs_file.is_open())
00077                 {
00078                         pasula_logs_file << pasula_time_ << std::endl;
00079                         pasula_logs_file << pasula_iter_ << std::endl;
00080                         pasula_logs_file << number_rules[2] << std::endl;
00081                         pasula_logs_file << number_rules[0] << std::endl;
00082                         pasula_logs_file << number_rules[1] << std::endl;
00083                         
00084                         
00085                 }
00086                 else
00087                         ROS_ERROR_STREAM("Couldn't write pasula logs to " << pasula_logs_file_path);
00088                 pasula_logs_file.close();
00089         }
00090         
00091 }
00092 
00093 /*  [subscriber callbacks] */
00094 
00095 /*  [service callbacks] */
00096 /*bool RuleLearnerAlgNode::learn_with_pasulaCallback(iri_rule_learner::LearnWithPasula::Request &req, iri_rule_learner::LearnWithPasula::Response &res) 
00097 { 
00098         ROS_INFO("RuleLearnerAlgNode::learn_with_pasulaCallback: New Request Received!"); */
00099 /*
00100         std::vector<std::string> actions = this->alg_.actions_needing_pasula_learning();
00101         if (actions.size() > 0) {
00102                 ROS_INFO("RuleLearnerAlgNode:: Requesting pasula rules."); 
00103                 
00104                 this->alg_.lock();
00105                 TransitionList transitions = this->alg_.get_transitions_with_actions(actions);
00106                 this->alg_.write_transitions(remove_unused_predicates(transitions));
00107                 this->alg_.reset_new_transitions();
00108                 this->alg_.unlock();
00109                 
00110                 ros::Time begin = ros::Time::now();
00111                 // learn_pasula_srv_.request.data = my_var; 
00112                 if (learn_pasula_client_.call(learn_pasula_srv_)) 
00113                 { 
00114                         ROS_INFO_STREAM("RuleLearnerAlgNode:: Response: " << learn_pasula_srv_.response.ok); 
00115                 } 
00116                 else 
00117                 { 
00118                         ROS_ERROR("RuleLearnerAlgNode:: Failed to Call Server on topic learn_pasula "); 
00119                         return false;
00120                 }
00121                 this->alg_.lock();
00122                 std::vector<int> number_rules = this->alg_.update_with_pasula_rules(actions, transitions);
00123                 this->alg_.unlock();
00124                 res.num_pasula_rules = number_rules[0];
00125                 res.num_learned_rules = number_rules[1];
00126                 res.num_total_rules = number_rules[2];
00127                 res.time = ros::Time::now() - begin;
00128         }
00129 
00130         return true; */
00131 //}
00132 bool RuleLearnerAlgNode::add_stateCallback(estirabot_msgs::AddState::Request &req, estirabot_msgs::AddState::Response &res) 
00133 { 
00134         ROS_DEBUG("RuleLearnerAlgNode::add_stateCallback: New Request Received!"); 
00135         
00136         //use appropiate mutex to shared variables if necessary 
00137         //this->alg_.lock(); 
00138         //this->add_state_mutex_.enter(); 
00139 
00140         res.rules_updated = this->alg_.add_state(req.state);
00141 
00142         //unlock previously blocked shared variables 
00143         //this->alg_.unlock(); 
00144         //this->add_state_mutex_.exit(); 
00145         
00146         return true; 
00147 }
00148 
00149 
00150 bool RuleLearnerAlgNode::add_actionCallback(estirabot_msgs::AddAction::Request &req, estirabot_msgs::AddAction::Response &res) 
00151 { 
00152         ROS_DEBUG("RuleLearnerAlgNode::add_actionCallback: New Request Received!"); 
00153         
00154         //use appropiate mutex to shared variables if necessary 
00155         //this->alg_.lock(); 
00156         //this->add_action_mutex_.enter(); 
00157         
00158         res.needs_learning = this->alg_.add_action(req.action, req.target_dirty_areas.indexes);
00159         
00160         //unlock previously blocked shared variables 
00161         //this->alg_.unlock(); 
00162         //this->add_action_mutex_.exit(); 
00163         
00164         return true; 
00165 }
00166 
00167 /*  [action callbacks] */
00168 
00169 /*  [action requests] */
00170 
00171 void RuleLearnerAlgNode::node_config_update(Config &config, uint32_t level)
00172 {
00173         this->alg_.lock();
00174 
00175         this->alg_.unlock();
00176 }
00177 
00178 void RuleLearnerAlgNode::addNodeDiagnostics(void)
00179 {
00180 }
00181 
00182 /* main function */
00183 int main(int argc,char *argv[])
00184 {
00185         return algorithm_base::main<RuleLearnerAlgNode>(argc, argv, "rule_learner_alg_node");
00186 }


iri_rule_learner
Author(s): dmartinez
autogenerated on Fri Dec 6 2013 20:43:48