state_storage.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2012, Willow Garage, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of Willow Garage nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *********************************************************************/
34 
35 /* Author: Ioan Sucan */
36 
38 
39 const std::string moveit_warehouse::RobotStateStorage::DATABASE_NAME = "moveit_robot_states";
40 
41 const std::string moveit_warehouse::RobotStateStorage::STATE_NAME = "state_id";
42 const std::string moveit_warehouse::RobotStateStorage::ROBOT_NAME = "robot_id";
43 
46 
48  : MoveItMessageStorage(conn)
49 {
51 }
52 
54 {
55  state_collection_ = conn_->openCollectionPtr<moveit_msgs::RobotState>(DATABASE_NAME, "robot_states");
56 }
57 
59 {
60  state_collection_.reset();
61  conn_->dropDatabase(DATABASE_NAME);
63 }
64 
65 void moveit_warehouse::RobotStateStorage::addRobotState(const moveit_msgs::RobotState& msg, const std::string& name,
66  const std::string& robot)
67 {
68  bool replace = false;
69  if (hasRobotState(name, robot))
70  {
71  removeRobotState(name, robot);
72  replace = true;
73  }
74  Metadata::Ptr metadata = state_collection_->createMetadata();
75  metadata->append(STATE_NAME, name);
76  metadata->append(ROBOT_NAME, robot);
77  state_collection_->insert(msg, metadata);
78  ROS_DEBUG("%s robot state '%s'", replace ? "Replaced" : "Added", name.c_str());
79 }
80 
81 bool moveit_warehouse::RobotStateStorage::hasRobotState(const std::string& name, const std::string& robot) const
82 {
83  Query::Ptr q = state_collection_->createQuery();
84  q->append(STATE_NAME, name);
85  if (!robot.empty())
86  q->append(ROBOT_NAME, robot);
87  std::vector<RobotStateWithMetadata> constr = state_collection_->queryList(q, true);
88  return !constr.empty();
89 }
90 
91 void moveit_warehouse::RobotStateStorage::getKnownRobotStates(const std::string& regex, std::vector<std::string>& names,
92  const std::string& robot) const
93 {
94  getKnownRobotStates(names, robot);
95  filterNames(regex, names);
96 }
97 
98 void moveit_warehouse::RobotStateStorage::getKnownRobotStates(std::vector<std::string>& names,
99  const std::string& robot) const
100 {
101  names.clear();
102  Query::Ptr q = state_collection_->createQuery();
103  if (!robot.empty())
104  q->append(ROBOT_NAME, robot);
105  std::vector<RobotStateWithMetadata> constr = state_collection_->queryList(q, true, STATE_NAME, true);
106  for (std::size_t i = 0; i < constr.size(); ++i)
107  if (constr[i]->lookupField(STATE_NAME))
108  names.push_back(constr[i]->lookupString(STATE_NAME));
109 }
110 
112  const std::string& robot) const
113 {
114  Query::Ptr q = state_collection_->createQuery();
115  q->append(STATE_NAME, name);
116  if (!robot.empty())
117  q->append(ROBOT_NAME, robot);
118  std::vector<RobotStateWithMetadata> constr = state_collection_->queryList(q, false);
119  if (constr.empty())
120  return false;
121  else
122  {
123  msg_m = constr.front();
124  return true;
125  }
126 }
127 
128 void moveit_warehouse::RobotStateStorage::renameRobotState(const std::string& old_name, const std::string& new_name,
129  const std::string& robot)
130 {
131  Query::Ptr q = state_collection_->createQuery();
132  q->append(STATE_NAME, old_name);
133  if (!robot.empty())
134  q->append(ROBOT_NAME, robot);
135  Metadata::Ptr m = state_collection_->createMetadata();
136  m->append(STATE_NAME, new_name);
137  state_collection_->modifyMetadata(q, m);
138  ROS_DEBUG("Renamed robot state from '%s' to '%s'", old_name.c_str(), new_name.c_str());
139 }
140 
141 void moveit_warehouse::RobotStateStorage::removeRobotState(const std::string& name, const std::string& robot)
142 {
143  Query::Ptr q = state_collection_->createQuery();
144  q->append(STATE_NAME, name);
145  if (!robot.empty())
146  q->append(ROBOT_NAME, robot);
147  unsigned int rem = state_collection_->removeMessages(q);
148  ROS_DEBUG("Removed %u RobotState messages (named '%s')", rem, name.c_str());
149 }
void addRobotState(const moveit_msgs::RobotState &msg, const std::string &name, const std::string &robot="")
void filterNames(const std::string &regex, std::vector< std::string > &names) const
Keep only the names that match regex.
void getKnownRobotStates(std::vector< std::string > &names, const std::string &robot="") const
RobotStateStorage(warehouse_ros::DatabaseConnection::Ptr conn)
warehouse_ros::DatabaseConnection::Ptr conn_
This class provides the mechanism to connect to a database and reads needed ROS parameters when appro...
bool hasRobotState(const std::string &name, const std::string &robot="") const
static const std::string DATABASE_NAME
Definition: state_storage.h:54
static const std::string STATE_NAME
Definition: state_storage.h:56
bool getRobotState(RobotStateWithMetadata &msg_m, const std::string &name, const std::string &robot="") const
Get the constraints named name. Return false on failure.
RobotStateCollection state_collection_
Definition: state_storage.h:79
void renameRobotState(const std::string &old_name, const std::string &new_name, const std::string &robot="")
void removeRobotState(const std::string &name, const std::string &robot="")
static const std::string ROBOT_NAME
Definition: state_storage.h:57
#define ROS_DEBUG(...)


warehouse
Author(s): Ioan Sucan
autogenerated on Sun Oct 18 2020 13:18:37