transaction.cpp
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2018, Locus Robotics
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 the copyright holder 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 HOLDER 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 #include <fuse_core/transaction.h>
35 
36 #include <fuse_core/constraint.h>
37 #include <fuse_core/uuid.h>
38 #include <fuse_core/variable.h>
39 #include <ros/time.h>
40 
41 #include <boost/iterator/transform_iterator.hpp>
42 #include <boost/range/empty.hpp>
43 
44 #include <algorithm>
45 #include <ostream>
46 #include <utility>
47 
48 
49 namespace fuse_core
50 {
51 
53 {
54  if (involved_stamps_.empty())
55  {
56  return stamp_;
57  }
58  else
59  {
60  return std::min(*involved_stamps_.begin(), stamp_);
61  }
62 }
63 
65 {
66  if (involved_stamps_.empty())
67  {
68  return stamp_;
69  }
70  else
71  {
72  return std::max(*involved_stamps_.rbegin(), stamp_);
73  }
74 }
75 
77 {
78  involved_stamps_.insert(stamp);
79 }
80 
82 {
83  auto to_constraint_ref = +[](const Constraint::SharedPtr& constraint) -> const Constraint&
84  {
85  return *constraint;
86  };
87 
89  boost::make_transform_iterator(added_constraints_.cbegin(), to_constraint_ref),
90  boost::make_transform_iterator(added_constraints_.cend(), to_constraint_ref));
91 }
92 
93 void Transaction::addConstraint(Constraint::SharedPtr constraint, bool overwrite)
94 {
95  // If the constraint being added is in the 'removed' container, then delete it from
96  // the 'removed' container instead of adding it to the 'added' container.
97  UUID constraint_uuid = constraint->uuid();
98  auto removed_constraints_iter = std::find(removed_constraints_.begin(), removed_constraints_.end(), constraint_uuid);
99  if (removed_constraints_iter != removed_constraints_.end())
100  {
101  removed_constraints_.erase(removed_constraints_iter);
102  return;
103  }
104 
105  // Also don't add the same constraint twice
106  auto is_constraint_added = [&constraint_uuid](const Constraint::SharedPtr& added_constraint)
107  {
108  return constraint_uuid == added_constraint->uuid();
109  };
110  auto added_constraints_iter = std::find_if(added_constraints_.begin(), added_constraints_.end(), is_constraint_added);
111  if (added_constraints_iter == added_constraints_.end())
112  {
113  added_constraints_.push_back(std::move(constraint));
114  }
115  else if (overwrite)
116  {
117  *added_constraints_iter = std::move(constraint);
118  }
119 }
120 
121 void Transaction::removeConstraint(const UUID& constraint_uuid)
122 {
123  // If the constraint being removed is in the 'added' container, then delete it from
124  // the 'added' container instead of adding it to the 'removed' container.
125  auto is_constraint_added = [&constraint_uuid](const Constraint::SharedPtr& added_constraint)
126  {
127  return constraint_uuid == added_constraint->uuid();
128  };
129  auto added_constraints_iter = std::find_if(added_constraints_.begin(), added_constraints_.end(), is_constraint_added);
130  if (added_constraints_iter != added_constraints_.end())
131  {
132  added_constraints_.erase(added_constraints_iter);
133  return;
134  }
135  // Also don't remove the same constraint twice
136  auto removed_constraints_iter = std::find(removed_constraints_.begin(), removed_constraints_.end(), constraint_uuid);
137  if (removed_constraints_iter == removed_constraints_.end())
138  {
139  removed_constraints_.push_back(constraint_uuid);
140  return;
141  }
142 }
143 
145 {
146  auto to_variable_ref = +[](const Variable::SharedPtr& variable) -> const Variable&
147  {
148  return *variable;
149  };
150 
151  return const_variable_range(
152  boost::make_transform_iterator(added_variables_.cbegin(), to_variable_ref),
153  boost::make_transform_iterator(added_variables_.cend(), to_variable_ref));
154 }
155 
156 bool Transaction::empty() const
157 {
158  return boost::empty(added_variables_) && boost::empty(removed_variables_) &&
159  boost::empty(added_constraints_) && boost::empty(removed_constraints_) && involved_stamps_.empty();
160 }
161 
162 void Transaction::addVariable(Variable::SharedPtr variable, bool overwrite)
163 {
164  // If the variable being added is in the 'removed' container, then delete it from
165  // the 'removed' container instead of adding it to the 'added' container.
166 
167  UUID variable_uuid = variable->uuid();
168  auto removed_variables_iter = std::find(removed_variables_.begin(), removed_variables_.end(), variable_uuid);
169  if (removed_variables_iter != removed_variables_.end())
170  {
171  removed_variables_.erase(removed_variables_iter);
172  return;
173  }
174 
175  // Also don't add the same variable twice
176  auto is_variable_added = [&variable_uuid](const Variable::SharedPtr& added_variable)
177  {
178  return variable_uuid == added_variable->uuid();
179  };
180  auto added_variables_iter = std::find_if(added_variables_.begin(), added_variables_.end(), is_variable_added);
181  if (added_variables_iter == added_variables_.end())
182  {
183  added_variables_.push_back(std::move(variable));
184  }
185  else if (overwrite)
186  {
187  *added_variables_iter = std::move(variable);
188  }
189 }
190 
191 void Transaction::removeVariable(const UUID& variable_uuid)
192 {
193  // If the variable being removed is in the 'added' container, then delete it from
194  // the 'added' container instead of adding it to the 'removed' container.
195  auto is_variable_added = [&variable_uuid](const Variable::SharedPtr& added_variable)
196  {
197  return variable_uuid == added_variable->uuid();
198  };
199  auto added_variables_iter = std::find_if(added_variables_.begin(), added_variables_.end(), is_variable_added);
200  if (added_variables_iter != added_variables_.end())
201  {
202  added_variables_.erase(added_variables_iter);
203  return;
204  }
205 
206  // Also don't remove the same variable twice
207  auto removed_variables_iter = std::find(removed_variables_.begin(), removed_variables_.end(), variable_uuid);
208  if (removed_variables_iter == removed_variables_.end())
209  {
210  removed_variables_.push_back(variable_uuid);
211  return;
212  }
213 }
214 
215 void Transaction::merge(const Transaction& other, bool overwrite)
216 {
217  stamp_ = std::max(stamp_, other.stamp_);
218  involved_stamps_.insert(other.involved_stamps_.begin(), other.involved_stamps_.end());
219  for (const auto& added_constraint : other.added_constraints_)
220  {
221  addConstraint(added_constraint, overwrite);
222  }
223  for (const auto& removed_constraint : other.removed_constraints_)
224  {
225  removeConstraint(removed_constraint);
226  }
227  for (const auto& added_variable : other.added_variables_)
228  {
229  addVariable(added_variable, overwrite);
230  }
231  for (const auto& removed_variable : other.removed_variables_)
232  {
233  removeVariable(removed_variable);
234  }
235 }
236 
237 void Transaction::print(std::ostream& stream) const
238 {
239  stream << "Stamp: " << stamp_ << "\n";
240  stream << "Involved Timestamps:\n";
241  for (const auto& involved_stamp : involved_stamps_)
242  {
243  stream << " - " << involved_stamp << "\n";
244  }
245  stream << "Added Variables:\n";
246  for (const auto& added_variable : added_variables_)
247  {
248  stream << " - " << *added_variable << "\n";
249  }
250  stream << "Added Constraints:\n";
251  for (const auto& added_constraint : added_constraints_)
252  {
253  stream << " - " << *added_constraint << "\n";
254  }
255  stream << "Removed Variables:\n";
256  for (const auto& removed_variable : removed_variables_)
257  {
258  stream << " - " << removed_variable << "\n";
259  }
260  stream << "Removed Constraints:\n";
261  for (const auto& removed_constraint : removed_constraints_)
262  {
263  stream << " - " << removed_constraint << "\n";
264  }
265 }
266 
267 Transaction::UniquePtr Transaction::clone() const
268 {
269  return Transaction::make_unique(*this);
270 }
271 
273 {
274  archive << *this;
275 }
276 
278 {
279  archive << *this;
280 }
281 
283 {
284  archive >> *this;
285 }
286 
288 {
289  archive >> *this;
290 }
291 
292 std::ostream& operator <<(std::ostream& stream, const Transaction& transaction)
293 {
294  transaction.print(stream);
295  return stream;
296 }
297 
298 } // namespace fuse_core
boost::any_range< const Variable, boost::forward_traversal_tag > const_variable_range
A range of Variable::SharedPtr objects.
Definition: transaction.h:105
void merge(const Transaction &other, bool overwrite=false)
Merge the contents of another transaction into this one.
The Variable interface definition.
Definition: variable.h:188
ros::Time stamp_
The transaction message timestamp.
Definition: transaction.h:285
boost::uuids::uuid UUID
Definition: uuid.h:51
boost::archive::text_oarchive TextOutputArchive
Definition: serialization.h:63
void serialize(fuse_core::BinaryOutputArchive &) const
Serialize this Constraint into the provided binary archive.
std::set< ros::Time > involved_stamps_
The set of timestamps involved in this transaction.
Definition: transaction.h:288
void deserialize(fuse_core::BinaryInputArchive &)
Deserialize data from the provided binary archive into this Constraint.
const_variable_range addedVariables() const
Read-only access to the added variables.
void print(std::ostream &stream=std::cout) const
Print a human-readable description of the transaction to the provided stream.
std::vector< Constraint::SharedPtr > added_constraints_
The constraints to be added.
Definition: transaction.h:286
const ros::Time & minStamp() const
Read-only access to the minimum (oldest), timestamp among the transaction&#39;s stamp and all involved ti...
Definition: transaction.cpp:52
const ros::Time & stamp() const
Read-only access to this transaction&#39;s timestamp.
Definition: transaction.h:110
bool empty() const
Check if the transaction is empty, i.e. it has no added or removed constraints or variables...
const ros::Time & maxStamp() const
Read-only access to the maximum (newest) timestamp among the transaction&#39;s stamp and all involved tim...
Definition: transaction.cpp:64
std::vector< Variable::SharedPtr > added_variables_
The variables to be added.
Definition: transaction.h:287
boost::archive::binary_oarchive BinaryOutputArchive
Definition: serialization.h:61
void addInvolvedStamp(const ros::Time &stamp)
Add a timestamp to the "involved stamps" collection.
Definition: transaction.cpp:76
The Constraint interface definition.
Definition: constraint.h:193
boost::archive::binary_iarchive BinaryInputArchive
Definition: serialization.h:60
Transaction::UniquePtr clone() const
Perform a deep copy of the Transaction and return a unique pointer to the copy.
boost::archive::text_iarchive TextInputArchive
Definition: serialization.h:62
std::vector< UUID > removed_constraints_
The constraint UUIDs to be removed.
Definition: transaction.h:289
const_constraint_range addedConstraints() const
Read-only access to the added constraints.
Definition: transaction.cpp:81
A transaction is a group of variable and constraint additions and subtractions that should all be pro...
Definition: transaction.h:66
boost::any_range< const Constraint, boost::forward_traversal_tag > const_constraint_range
A range of Constraint::SharedPtr objects.
Definition: transaction.h:78
std::vector< UUID > removed_variables_
The variable UUIDs to be removed.
Definition: transaction.h:290
std::ostream & operator<<(std::ostream &stream, const Constraint &constraint)
Definition: constraint.cpp:53
void removeVariable(const UUID &variable_uuid)
Remove the variable from this transaction if it was previously added, or mark the variable for remova...
void removeConstraint(const UUID &constraint_uuid)
Remove a constraint from this transaction if it was previously added, or mark the constraint for remo...
void addConstraint(Constraint::SharedPtr constraint, bool overwrite=false)
Add a constraint to this transaction.
Definition: transaction.cpp:93
void addVariable(Variable::SharedPtr variable, bool overwrite=false)
Add a variable to this transaction.


fuse_core
Author(s): Stephen Williams
autogenerated on Wed Jul 5 2023 02:14:08