41 #include <moveit/task_constructor/container.h>
42 #include <moveit/macros/class_forward.h>
45 #include <boost/bimap.hpp>
46 #include <boost/bimap/unordered_set_of.hpp>
47 #include <boost/bimap/unordered_multiset_of.hpp>
54 MOVEIT_CLASS_FORWARD(JointModelGroup);
55 MOVEIT_CLASS_FORWARD(RobotState);
60 namespace task_constructor {
76 class ContainerBasePrivate :
public StagePrivate
78 friend class ContainerBase;
79 friend class ConnectingPrivate;
82 using container_type = StagePrivate::container_type;
83 using iterator = container_type::iterator;
84 using const_iterator = container_type::const_iterator;
85 using NonConstStageCallback = std::function<bool(Stage&,
int)>;
87 inline const container_type& children()
const {
return children_; }
93 const_iterator childByIndex(
int index,
bool for_insert =
false)
const;
96 Stage::pointer remove(ContainerBasePrivate::const_iterator pos);
100 unsigned int max_depth)
const;
103 bool traverseStages(
const NonConstStageCallback& processor,
unsigned int cur_depth,
unsigned int max_depth) {
104 const auto& const_processor = [&processor](
const Stage& stage,
unsigned int depth) {
105 return processor(
const_cast<Stage&
>(stage), depth);
107 return const_cast<const ContainerBasePrivate*
>(
this)->traverseStages(const_processor, cur_depth, max_depth);
110 void validateConnectivity()
const override;
114 InterfaceFlags requiredInterface()
const override {
return required_interface_; }
117 bool canCompute()
const override;
118 void compute()
override;
121 InterfacePtr pendingBackward()
const {
return pending_backward_; }
122 InterfacePtr pendingForward()
const {
return pending_forward_; }
130 inline const auto& internalToExternalMap()
const {
return internal_external_.by<INTERNAL>(); }
131 inline const auto& externalToInternalMap()
const {
return internal_external_.by<EXTERNAL>(); }
134 virtual void onNewFailure(
const Stage& child,
const InterfaceState* from,
const InterfaceState* to);
137 ContainerBasePrivate(ContainerBase* me,
const std::string& name);
138 ContainerBasePrivate& operator=(ContainerBasePrivate&& other);
141 inline void setChildsPushBackwardInterface(StagePrivate* child) {
142 InterfaceFlags required = child->requiredInterface();
143 bool allowed = (required & WRITES_PREV_END);
144 child->setPrevEnds(allowed ? pending_backward_ : InterfacePtr());
146 inline void setChildsPushForwardInterface(StagePrivate* child) {
147 InterfaceFlags required = child->requiredInterface();
148 bool allowed = (required & WRITES_NEXT_START);
149 child->setNextStarts(allowed ? pending_forward_ : InterfacePtr());
153 template <Interface::Direction dir>
154 void setStatus(
const Stage* creator,
const InterfaceState* source,
const InterfaceState* target,
155 InterfaceState::Status status);
158 template <Interface::Direction>
159 void copyState(Interface::iterator external,
const InterfacePtr& target, Interface::UpdateFlags updated);
161 void copyState(Interface::Direction dir, Interface::iterator external,
const InterfacePtr& target,
162 Interface::UpdateFlags updated);
165 void liftSolution(
const SolutionBasePtr& solution,
const InterfaceState* internal_from,
166 const InterfaceState* internal_to);
169 inline auto& internalToExternalMap() {
return internal_external_.by<INTERNAL>(); }
170 inline auto& externalToInternalMap() {
return internal_external_.by<EXTERNAL>(); }
173 InterfaceFlags required_interface_;
176 container_type children_;
179 boost::bimap<boost::bimaps::unordered_set_of<boost::bimaps::tagged<const InterfaceState*, INTERNAL>>,
180 boost::bimaps::unordered_multiset_of<boost::bimaps::tagged<const InterfaceState*, EXTERNAL>>>
186 InterfacePtr pending_backward_;
188 InterfacePtr pending_forward_;
190 PIMPL_FUNCTIONS(ContainerBase)
197 class SerialContainerPrivate :
public ContainerBasePrivate
199 friend class SerialContainer;
202 SerialContainerPrivate(SerialContainer* me,
const std::string& name);
205 void resolveInterface(InterfaceFlags expected)
override;
207 void validateConnectivity()
const override;
213 void connect(StagePrivate& stage1, StagePrivate& stage2);
216 template <
unsigned int mask>
217 void validateInterface(
const StagePrivate& child, InterfaceFlags required)
const;
219 PIMPL_FUNCTIONS(SerialContainer)
221 class ParallelContainerBasePrivate :
public ContainerBasePrivate
223 friend class ParallelContainerBase;
226 ParallelContainerBasePrivate(ParallelContainerBase* me,
const std::string& name);
229 void resolveInterface(InterfaceFlags expected)
override;
231 void validateConnectivity()
const override;
234 void validateInterfaces(
const StagePrivate& child, InterfaceFlags& external,
bool first =
false)
const;
238 template <
typename Interface::Direction>
239 void propagateStateToAllChildren(Interface::iterator external, Interface::UpdateFlags updated);
242 virtual void initializeExternalInterfaces();
244 PIMPL_FUNCTIONS(ParallelContainerBase)
253 class FallbacksPrivate :
public ParallelContainerBasePrivate
256 FallbacksPrivate(Fallbacks* me,
const std::string& name);
257 FallbacksPrivate(FallbacksPrivate&& other);
259 void initializeExternalInterfaces() final;
260 void onNewFailure(const Stage& child, const InterfaceState* from, const InterfaceState* to) override;
263 virtual
void onNewSolution(const SolutionBase& s);
264 virtual
void reset() {}
266 PIMPL_FUNCTIONS(Fallbacks)
276 inline void nextChild();
280 void reset()
override;
281 bool canCompute()
const override;
282 void compute()
override;
284 container_type::const_iterator current_;
298 void reset()
override;
302 Interface::Direction dir_;
304 bool job_has_solutions_;
311 void reset()
override;
312 bool canCompute()
const override;
313 void compute()
override;
316 template <Interface::Direction dir>
319 mutable container_type::const_iterator active_;
322 class WrapperBasePrivate :
public ParallelContainerBasePrivate
327 WrapperBasePrivate(
WrapperBase* me,
const std::string& name);
329 PIMPL_FUNCTIONS(WrapperBase)
331 class MergerPrivate :
public ParallelContainerBasePrivate
335 moveit::core::JointModelGroupPtr jmg_merged_;
336 using ChildSolutionList = std::vector<const SubTrajectory*>;
337 using ChildSolutionMap = std::map<const Stage*, ChildSolutionList>;
339 std::map<const InterfaceState*, ChildSolutionMap> source_state_to_solutions_;
342 using Spawner = std::function<void(SubTrajectory&&)>;
343 MergerPrivate(Merger* me,
const std::string& name);
345 void resolveInterface(InterfaceFlags expected)
override;
347 void onNewPropagateSolution(
const SolutionBase& s);
348 void onNewGeneratorSolution(
const SolutionBase& s);
349 void mergeAnyCombination(
const ChildSolutionMap& all_solutions,
const SolutionBase& current,
350 const planning_scene::PlanningSceneConstPtr& start_scene,
const Spawner& spawner);
351 void merge(
const ChildSolutionList& sub_solutions,
const planning_scene::PlanningSceneConstPtr& start_scene,
352 const Spawner& spawner);
354 void sendForward(SubTrajectory&& t,
const InterfaceState* from);
355 void sendBackward(SubTrajectory&& t,
const InterfaceState* to);
357 PIMPL_FUNCTIONS(Merger)
std::function< bool(const Stage &, unsigned int)> StageCallback
Definition: container.h:65
Definition: container_p.h:271
virtual bool nextJob()=0
Advance to the next job, assuming that the current child is exhausted on the current job.
Definition: storage.h:180
abstract base class for solutions (primitive and sequences)
Definition: storage.h:269
Definition: container.h:214
Fallbacks implementation for CONNECT interface.
Definition: container_p.h:309
Fallbacks implementation for GENERATOR interface.
Definition: container_p.h:289
bool nextJob() override
Advance to the next job, assuming that the current child is exhausted on the current job.
Definition: container.cpp:959
Fallbacks implementation for FORWARD or BACKWARD interface.
Definition: container_p.h:296
bool nextJob() override
Advance to the next job, assuming that the current child is exhausted on the current job.
Definition: container.cpp:1004