MTC
container.h
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2017, Bielefeld University
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 Bielefeld University 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: Robert Haschke
36  Desc: Container Stages to combine multiple planning Stage in different ways
37 */
38 
39 #pragma once
40 
41 #include "stage.h"
42 
43 namespace moveit {
44 namespace task_constructor {
45 
46 class ContainerBasePrivate;
48 class ContainerBase : public Stage
49 {
50 public:
51  PRIVATE_CLASS(ContainerBase)
52  using pointer = std::unique_ptr<ContainerBase>;
53 
55  void setPruning(bool pruning) { setProperty("pruning", pruning); }
56  bool pruning() const { return properties().get<bool>("pruning"); }
57 
58  size_t numChildren() const;
59  Stage* findChild(const std::string& name) const;
60  Stage* operator[](int index) const;
61 
65  using StageCallback = std::function<bool(const Stage&, unsigned int)>;
67  bool traverseChildren(const StageCallback& processor) const;
69  bool traverseRecursively(const StageCallback& processor) const;
70 
71  void add(Stage::pointer&& stage);
72 
73  virtual void insert(Stage::pointer&& stage, int before = -1);
74  virtual Stage::pointer remove(int pos);
75  virtual Stage::pointer remove(Stage* child);
76  virtual void clear();
77 
78  void reset() override;
79  void init(const moveit::core::RobotModelConstPtr& robot_model) override;
80 
81  virtual bool canCompute() const = 0;
82  virtual void compute() = 0;
83  void explainFailure(std::ostream& os) const override;
84 
86  virtual void onNewSolution(const SolutionBase& s) = 0;
87 
88 protected:
89  ContainerBase(ContainerBasePrivate* impl);
90 };
91 std::ostream& operator<<(std::ostream& os, const ContainerBase& stage);
92 
93 class SerialContainerPrivate;
96 {
97 public:
98  PRIVATE_CLASS(SerialContainer)
99  SerialContainer(const std::string& name = "serial container");
100 
101  bool canCompute() const override;
102  void compute() override;
103 
104 protected:
105  void onNewSolution(const SolutionBase& s) override;
106 
107  SerialContainer(SerialContainerPrivate* impl);
108 };
109 
110 class ParallelContainerBasePrivate;
120 {
121 public:
122  PRIVATE_CLASS(ParallelContainerBase)
123  ParallelContainerBase(const std::string& name = "parallel container");
124 
125 protected:
126  ParallelContainerBase(ParallelContainerBasePrivate* impl);
127 
129  inline void liftSolution(const SolutionBase& solution) { liftSolution(solution, solution.cost()); }
131  void liftSolution(const SolutionBase& solution, double cost) { liftSolution(solution, cost, solution.comment()); }
132 
134  void liftSolution(const SolutionBase& solution, double cost, std::string comment);
135 
137  void spawn(InterfaceState&& state, SubTrajectory&& trajectory);
139  void sendForward(const InterfaceState& from, InterfaceState&& to, SubTrajectory&& trajectory);
141  void sendBackward(InterfaceState&& from, const InterfaceState& to, SubTrajectory&& trajectory);
142 };
143 
149 {
150 public:
151  Alternatives(const std::string& name = "alternatives") : ParallelContainerBase(name) {}
152 
153  bool canCompute() const override;
154  void compute() override;
155 
156  void onNewSolution(const SolutionBase& s) override;
157 };
158 
159 class FallbacksPrivate;
167 {
168  inline void replaceImpl();
169 
170 public:
171  PRIVATE_CLASS(Fallbacks);
172  Fallbacks(const std::string& name = "fallbacks");
173 
174  void reset() override;
175  void init(const moveit::core::RobotModelConstPtr& robot_model) override;
176 
177 protected:
178  Fallbacks(FallbacksPrivate* impl);
179  void onNewSolution(const SolutionBase& s) override;
180 
181 private:
182  // not needed, we directly use corresponding virtual methods of FallbacksPrivate
183  bool canCompute() const final { return false; }
184  void compute() final {}
185 };
186 
187 class MergerPrivate;
190 {
191 public:
192  PRIVATE_CLASS(Merger)
193  Merger(const std::string& name = "merger");
194 
195  void reset() override;
196  void init(const core::RobotModelConstPtr& robot_model) override;
197  bool canCompute() const override;
198  void compute() override;
199 
200 protected:
201  Merger(MergerPrivate* impl);
202  void onNewSolution(const SolutionBase& s) override;
203 };
204 
205 class WrapperBasePrivate;
214 {
215 public:
216  PRIVATE_CLASS(WrapperBase)
217  WrapperBase(const std::string& name = "wrapper", Stage::pointer&& child = Stage::pointer());
218 
220  void insert(Stage::pointer&& stage, int before = -1) override;
221 
223  Stage* wrapped();
224  inline const Stage* wrapped() const { return const_cast<WrapperBase*>(this)->wrapped(); }
225 
226  bool canCompute() const override;
227  void compute() override;
228 
229 protected:
230  WrapperBase(WrapperBasePrivate* impl, Stage::pointer&& child = Stage::pointer());
231 };
232 } // namespace task_constructor
233 } // namespace moveit
Definition: container.h:149
void onNewSolution(const SolutionBase &s) override
called by a (direct) child when a new solution becomes available
Definition: container.cpp:858
Definition: container.h:49
void reset() override
reset stage, clearing all solutions, interfaces, inherited properties.
void init(const moveit::core::RobotModelConstPtr &robot_model) override
bool traverseRecursively(const StageCallback &processor) const
traverse all children of this container recursively
std::function< bool(const Stage &, unsigned int)> StageCallback
Definition: container.h:65
bool traverseChildren(const StageCallback &processor) const
traverse direct children of this container, calling the callback for each of them
void setPruning(bool pruning)
Explicitly enable/disable pruning.
Definition: container.h:55
virtual void onNewSolution(const SolutionBase &s)=0
called by a (direct) child when a new solution becomes available
Definition: container.h:167
void init(const moveit::core::RobotModelConstPtr &robot_model) override
Definition: container.cpp:871
void reset() override
reset stage, clearing all solutions, interfaces, inherited properties.
Definition: container.cpp:866
void onNewSolution(const SolutionBase &s) override
called by a (direct) child when a new solution becomes available
Definition: container.cpp:876
Definition: container.h:190
void onNewSolution(const SolutionBase &s) override
called by a (direct) child when a new solution becomes available
Definition: container.cpp:1147
void reset() override
reset stage, clearing all solutions, interfaces, inherited properties.
Definition: container.cpp:1121
void liftSolution(const SolutionBase &solution, double cost)
lift child solution to external interface, adapting the costs
Definition: container.h:131
void sendBackward(InterfaceState &&from, const InterfaceState &to, SubTrajectory &&trajectory)
propagate a solution backwards
Definition: container.cpp:811
void liftSolution(const SolutionBase &solution)
lift unmodified child solution (useful for simple filtering)
Definition: container.h:129
void sendForward(const InterfaceState &from, InterfaceState &&to, SubTrajectory &&trajectory)
propagate a solution forwards
Definition: container.cpp:807
void spawn(InterfaceState &&state, SubTrajectory &&trajectory)
spawn a new solution with given state as start and end
Definition: container.cpp:803
const boost::any & get(const std::string &name) const
Get the value of a property. Throws undeclared if unknown name.
Definition: properties.cpp:254
void onNewSolution(const SolutionBase &s) override
called by a (direct) child when a new solution becomes available
Definition: container.cpp:511
abstract base class for solutions (primitive and sequences)
Definition: storage.h:269
Definition: stage.h:147
PropertyMap & properties()
Get the stage's property map.
Definition: stage.cpp:444
Stage(StagePrivate *impl)
Stage can only be instantiated through derived classes.
Definition: stage.cpp:309
void setProperty(const std::string &name, const boost::any &value)
Set a previously declared property to a new value.
Definition: stage.cpp:448
SubTrajectory connects interface states of ComputeStages.
Definition: storage.h:348
Definition: container.h:214
Stage * wrapped()
access the single wrapped child, NULL if still empty
Definition: container.cpp:833
void insert(Stage::pointer &&stage, int before=-1) override
insertion is only allowed if children() is empty
Definition: container.cpp:826