transitions.cpp
Go to the documentation of this file.
00001 #include <transitions.h>
00002 
00003 
00004 std::ostream& operator<<(std::ostream &out, const TransitionList &t)
00005 {
00006         for ( TransitionList::const_iterator transition=t.begin() ; transition != t.end(); ++transition ) {
00007                 out << transition->prev_state_ << std::endl;
00008                 out << transition->action_ << std::endl;
00009                 
00010                 PredicateGroup prec_group_next_state(transition->next_state_);
00011                 prec_group_next_state.add_positive_differences(PredicateGroup(transition->prev_state_));
00012                 out << prec_group_next_state << std::endl;
00013                 
00014                 out << "1" << std::endl;
00015         }
00016         return out;
00017 }
00018 
00019 std::ostream& operator<<(std::ostream &out, const Transition &t)
00020 {
00021     out << t.prev_state_ << std::endl;
00022     out << t.action_ << std::endl;
00023     out << t.next_state_ << std::endl;
00024     return out;
00025 }
00026 
00027 void write_transitions_to_file(TransitionList transitions, std::string file_path)
00028 {
00029         std::ofstream transitions_file(file_path.c_str());
00030         if (transitions_file.is_open())
00031         {
00032                 transitions_file << transitions;
00033         }
00034         else
00035                 ROS_ERROR_STREAM("Couldn't write transitions to " << file_path);
00036         transitions_file.close();
00037 }
00038 
00039 
00040 TransitionList read_transitions_from_file(std::string file_path)
00041 {
00042         TransitionList transitions;
00043         std::ifstream transitions_file(file_path.c_str());
00044         if (transitions_file.is_open())
00045         {
00046                 std::string line_prev, line_action, line_next, line_aux;
00047                 
00048                 while ( transitions_file.good() )
00049                 {
00050                         getline (transitions_file,line_prev);
00051                         getline (transitions_file,line_action);
00052                         getline (transitions_file,line_next);
00053                         getline (transitions_file,line_aux);
00054                         
00055                         Transition transition(line_prev, 
00056                                               get_symbol_from_string(line_action), 
00057                                               line_next);
00058                         
00059                         // avoid adding twice last transition
00060                         if (transitions_file.good()) {
00061                                 transitions.push_back(transition);
00062                         }
00063                 }
00064         }
00065         else {
00066                 ROS_ERROR_STREAM("Couldn't read transitions from " << file_path);
00067         }
00068         transitions_file.close();
00069         return transitions;
00070 }
00071 
00072 void Transition::remove_unused_predicates()
00073 {
00074         std::stringstream ss;
00075         PredicateGroup pred_group_prev_state(this->prev_state_);
00076         pred_group_prev_state.remove_variables_not_present(this->action_.param_names_);
00077         ss << pred_group_prev_state;
00078         this->prev_state_ = ss.str();
00079         
00080         PredicateGroup pred_group_next_state(this->next_state_);
00081         pred_group_next_state.remove_variables_not_present(this->action_.param_names_);
00082         ss.str("");
00083         ss << pred_group_next_state;
00084         this->next_state_ = ss.str();
00085 }
00086 
00087 TransitionList remove_unused_predicates(const TransitionList transitions)
00088 {
00089         TransitionList result(transitions);
00090         for ( TransitionList::iterator transition=result.begin() ; transition != result.end(); ++transition ) {
00091                 transition->remove_unused_predicates();
00092         }
00093         return result;
00094 }


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