query.cpp
Go to the documentation of this file.
00001 //=================================================================================================
00002 // Copyright (c) 2013, Johannes Meyer, TU Darmstadt
00003 // All rights reserved.
00004 
00005 // Redistribution and use in source and binary forms, with or without
00006 // modification, are permitted provided that the following conditions are met:
00007 //     * Redistributions of source code must retain the above copyright
00008 //       notice, this list of conditions and the following disclaimer.
00009 //     * Redistributions in binary form must reproduce the above copyright
00010 //       notice, this list of conditions and the following disclaimer in the
00011 //       documentation and/or other materials provided with the distribution.
00012 //     * Neither the name of the Flight Systems and Automatic Control group,
00013 //       TU Darmstadt, nor the names of its contributors may be used to
00014 //       endorse or promote products derived from this software without
00015 //       specific prior written permission.
00016 
00017 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00018 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00019 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00020 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
00021 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00022 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00023 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00024 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00026 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027 //=================================================================================================
00028 
00029 #include <rosmatlab/rosbag/query.h>
00030 
00031 #include <rosmatlab/options.h>
00032 #include <rosmatlab/connection_header.h>
00033 #include <rosmatlab/conversion.h>
00034 #include <rosmatlab/log.h>
00035 
00036 #include <ros/forwards.h>
00037 #include <rosbag/structures.h>
00038 
00039 #include <introspection/message.h>
00040 
00041 #include <boost/algorithm/string/replace.hpp>
00042 
00043 
00044 namespace rosmatlab {
00045 namespace rosbag {
00046 
00047 Query::Query()
00048   : start_time_(ros::TIME_MIN)
00049   , end_time_(ros::TIME_MAX)
00050 {
00051 }
00052 
00053 Query::Query(int nrhs, const mxArray *prhs[])
00054   : start_time_(ros::TIME_MIN)
00055   , end_time_(ros::TIME_MAX)
00056 {
00057   init(Options(nrhs, prhs, true));
00058 }
00059 
00060 Query::Query(const Options &options)
00061   : start_time_(ros::TIME_MIN)
00062   , end_time_(ros::TIME_MAX)
00063 {
00064   init(options);
00065 }
00066 
00067 Query::Query(const std::string &topic)
00068   : start_time_(ros::TIME_MIN)
00069   , end_time_(ros::TIME_MAX)
00070 {
00071   topics_.insert(topic);
00072 }
00073 
00074 Query::~Query()
00075 {
00076 }
00077 
00078 void Query::init(const Options &options)
00079 {
00080   if (options.hasKey("start"))    start_time_ = ros::Time(options.getDouble("start"));
00081   if (options.hasKey("stop"))     end_time_   = ros::Time(options.getDouble("stop"));
00082   if (options.hasKey("topic"))    topics_.insert(options.getStrings("topic").begin(), options.getStrings("topic").end());
00083   if (options.hasKey("datatype")) datatypes_.insert(options.getStrings("datatype").begin(), options.getStrings("datatype").end());
00084   if (options.hasKey("md5sum"))   md5sums_.insert(options.getStrings("md5sum").begin(), options.getStrings("md5sum").end());
00085 
00086   // default option are topics
00087   if (options.hasKey(""))         topics_.insert(options.getStrings("").begin(), options.getStrings("").end());
00088 
00089   options.throwOnUnused();
00090 }
00091 
00092 bool Query::operator()(const ConnectionInfo *info)
00093 {
00094   if (topics_.size() > 0    && topics_.count(info->topic) == 0) return false;
00095   if (datatypes_.size() > 0 && datatypes_.count(info->datatype) == 0) return false;
00096   if (md5sums_.size() > 0   && md5sums_.count(info->md5sum) == 0) return false;
00097   return true;
00098 }
00099 
00100 ros::Time const& Query::getStartTime() const { return start_time_; }
00101 ros::Time const& Query::getEndTime()   const { return end_time_;   }
00102 
00103 mxArray *Query::toMatlab(const std::vector<boost::shared_ptr<Query> > &queries) {
00104   mxArray *result;
00105   static const char *fieldnames[] = { "Topic", "DataType", "MD5Sum", "StartTime", "EndTime" };
00106   result = mxCreateStructMatrix(queries.size(), 1, 5, fieldnames);
00107 
00108   for(mwIndex i = 0; i < queries.size(); ++i) {
00109     const Query &query = *queries[i];
00110 
00111     {
00112       mxArray *cell = mxCreateCellMatrix(1, query.topics_.size());
00113       mwIndex j = 0;
00114       for(Filter::const_iterator it = query.topics_.begin(); it != query.topics_.end(); ++it)
00115         mxSetCell(cell, j++, mxCreateString(it->c_str()));
00116       mxSetField(result, i, "Topic", cell);
00117     }
00118 
00119     {
00120       mxArray *cell = mxCreateCellMatrix(1, query.datatypes_.size());
00121       mwIndex j = 0;
00122       for(Filter::const_iterator it = query.datatypes_.begin(); it != query.datatypes_.end(); ++it)
00123         mxSetCell(cell, j++, mxCreateString(it->c_str()));
00124       mxSetField(result, i, "DataType", cell);
00125     }
00126 
00127     {
00128       mxArray *cell = mxCreateCellMatrix(1, query.md5sums_.size());
00129       mwIndex j = 0;
00130       for(Filter::const_iterator it = query.md5sums_.begin(); it != query.md5sums_.end(); ++it)
00131         mxSetCell(cell, j++, mxCreateString(it->c_str()));
00132       mxSetField(result, i, "MD5Sum", cell);
00133     }
00134 
00135     mxSetField(result, i, "StartTime", mxCreateTime(query.getStartTime()));
00136     mxSetField(result, i, "EndTime",   mxCreateTime(query.getEndTime()));
00137   }
00138 
00139   return result;
00140 }
00141 
00142 TopicFilterQuery::TopicFilterQuery(::rosbag::Query& base, const std::string& topic)
00143   : base_(base)
00144   , topic_(topic)
00145 {
00146 }
00147 
00148 TopicFilterQuery::~TopicFilterQuery()
00149 {
00150 }
00151 
00152 bool TopicFilterQuery::operator()(const ConnectionInfo *info) {
00153   return (base_.getQuery()(info)) && (info->topic == topic_);
00154 }
00155 
00156 
00157 } // namespace rosbag
00158 } // namespace rosmatlab
00159 


rosmatlab_rosbag
Author(s): Johannes Meyer
autogenerated on Fri Jul 25 2014 06:48:22