symbols.cpp
Go to the documentation of this file.
00001 #include <symbols.h>
00002 
00003 bool compare_vector_strings(std::vector<std::string> v1, std::vector<std::string> v2) {
00004     if (v1.size() != v2.size())
00005         return false;
00006     std::set<std::string> s1(v1.begin(), v1.end());
00007     std::set<std::string> s2(v2.begin(), v2.end());
00008     std::vector<std::string> v3;
00009     std::set_intersection(s1.begin(), s1.end(), s2.begin(), s2.end(), std::back_inserter(v3));
00010     if (v1.size() == v3.size())
00011         return true;
00012     else
00013         return false;
00014 }
00015 
00016 
00017 char choose_variable_separator(std::string str)
00018 {
00019         // get params separated by ,
00020         char separator;
00021         size_t found = str.find(',');
00022         if (found!=std::string::npos) {
00023                 separator=',';
00024         }
00025         else {
00026                 separator=' ';
00027         }
00028         return separator;
00029 }
00030 
00031 std::string trim(const std::string& str,
00032                  const std::string& whitespace)
00033 {
00034     const size_t strBegin = str.find_first_not_of(whitespace);
00035     if (strBegin == std::string::npos)
00036         return ""; // no content
00037 
00038     const size_t strEnd = str.find_last_not_of(whitespace);
00039     const size_t strRange = strEnd - strBegin + 1;
00040 
00041     return str.substr(strBegin, strRange);
00042 }
00043 
00044 
00045 // OPERATORS
00046 bool operator== (Symbol &s1, Symbol &s2)
00047 {
00048     return ((s1.name_.compare(s2.name_) == 0) && compare_vector_strings(s1.param_names_, s2.param_names_));
00049 }
00050 
00051 std::ostream& operator<<(std::ostream &out, const Symbol &s)
00052 {
00053     out << s.name_ << "(";
00054     for ( size_t i = 0; i < s.param_names_.size(); i++ ) {
00055         out << s.param_names_[i];
00056         if ( (i+1) < s.param_names_.size() )
00057             out << ",";
00058     }
00059     out << ")";
00060     return out;
00061 }
00062 
00063 
00064 // Symbol
00065 Symbol::Symbol(std::string line):
00066         name_(get_symbol_name_from_string(line)), 
00067         param_names_(get_symbol_param_names_from_string(line))
00068 {
00069 }
00070 
00071 bool Symbol::has_name(std::string name)
00072 {
00073         if (name_.compare(name) == 0) {
00074                 ROS_DEBUG_STREAM("Name comparison is true: " << name_ );
00075                 return true;
00076         }
00077         else
00078                 return false;
00079 }
00080 
00081 bool Symbol::has_param_name(std::string param)
00082 {
00083         bool result = false;
00084         for ( size_t i = 0; i < param_names_.size(); i++ ) {
00085                 if (param_names_[i] == param) {
00086                         result = true;
00087                 }
00088         }
00089         return result;
00090 }
00091 
00092 bool Symbol::has_param_names_in(std::vector<std::string> params)
00093 {
00094         bool result = true;
00095         for ( size_t i = 0; i < param_names_.size(); i++ ) {
00096                 bool present = false;
00097                 for (size_t j = 0; j < params.size(); j++) {
00098                         if (has_name("traversed2") && (i == 1)) { // TODO ugly
00099                                 present = true;
00100                         }
00101                         if (param_names_[i] == params[j]) {
00102                                 present = true;
00103                         }
00104                 }
00105                 if (!present) {
00106                         result = false;
00107                 }
00108         }
00109         return result;
00110 }
00111 
00112 bool Symbol::exists()
00113 {
00114         return (name_.compare("") != 0);
00115 }
00116 
00117 
00118 Symbol get_symbol_from_string(std::string line)
00119 {
00120 //     line = line.substr(line.find_first_not_of(" \t"));
00121     line = trim(line);
00122     Symbol symbol(get_symbol_name_from_string(line), get_symbol_param_names_from_string(line));
00123     return symbol;
00124 }
00125 
00126 std::string get_symbol_name_from_string(std::string line)
00127 {
00128     std::stringstream ss(line);
00129     std::string item;
00130     
00131     if(std::getline(ss, item, '(')) {
00132         //std::cout << "Symbol name: " << item << std::endl;
00133     }
00134     
00135     return item;
00136 }
00137 
00138 std::vector< std::string > get_symbol_param_names_from_string(std::string line)
00139 {
00140     std::stringstream ss(line);
00141     std::string item;
00142     std::vector< std::string> param_names;
00143     
00144     // get string between ()
00145     std::getline(ss, item, '(');
00146     std::getline(ss, item, ')');
00147     ss.str(item);
00148     
00149     char separator = choose_variable_separator(item);
00150     while ( std::getline(ss, item, separator) ) {
00151         // remove spaces and empty strings
00152         item.erase(remove_if(item.begin(), item.end(), isspace), item.end());
00153         if (item.compare("") != 0) {
00154             param_names.push_back(item);
00155             //std::cout << "Param names: " << item << std::endl;
00156         }
00157     }
00158     
00159     return param_names;
00160 }


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