Skip to content

Commit 986d3c8

Browse files
committed
FallbacksPrivateCommon: shared between Generator + Propagator
1 parent b82b70e commit 986d3c8

File tree

3 files changed

+64
-41
lines changed

3 files changed

+64
-41
lines changed

core/include/moveit/task_constructor/container.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,12 +167,15 @@ class Fallbacks : public ParallelContainerBase
167167

168168
void reset() override;
169169
void init(const moveit::core::RobotModelConstPtr& robot_model) override;
170-
bool canCompute() const override;
171-
void compute() override;
172170

173171
protected:
174172
Fallbacks(FallbacksPrivate* impl);
175173
void onNewSolution(const SolutionBase& s) override;
174+
175+
private:
176+
// not needed, we directly use corresponding virtual methods of FallbacksPrivate
177+
bool canCompute() const final { return false; }
178+
void compute() final {}
176179
};
177180

178181
class MergerPrivate;

core/include/moveit/task_constructor/container_p.h

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -254,38 +254,52 @@ class FallbacksPrivate : public ParallelContainerBasePrivate
254254
FallbacksPrivate(Fallbacks* me, const std::string& name);
255255
FallbacksPrivate(FallbacksPrivate&& other);
256256

257-
// methods common to all variants
258257
void initializeExternalInterfaces() final;
259258
void onNewFailure(const Stage& child, const InterfaceState* from, const InterfaceState* to) override;
260-
void nextChild(); /// << Advance to next child
261259

262260
// virtual methods specific to each variant
261+
virtual void onNewSolution(const SolutionBase& s);
262+
virtual void reset() {}
263+
};
264+
PIMPL_FUNCTIONS(Fallbacks)
265+
266+
/* Class shared between FallbacksPrivateGenerator and FallbacksPrivatePropagator,
267+
which both have the notion of a currently active child stage */
268+
class FallbacksPrivateCommon : public FallbacksPrivate
269+
{
270+
public:
271+
FallbacksPrivateCommon(FallbacksPrivate&& other) : FallbacksPrivate(std::move(other)) {}
272+
273+
/// Advance to next child
274+
inline void nextChild();
263275
/// Advance to the next job, assuming that the current child is exhausted on the current job.
264-
virtual bool nextJob() { return false; }
265-
/// Reset data structures
266-
virtual void reset();
276+
virtual bool nextJob() = 0;
267277

268-
container_type::const_iterator current_; // currently active child generator
269-
bool job_has_solutions_; // flag indicating whether the current job generated solutions
278+
void reset() override;
279+
bool canCompute() const override;
280+
void compute() override;
281+
282+
container_type::const_iterator current_; // currently active child
270283
};
271-
PIMPL_FUNCTIONS(Fallbacks)
272284

273285
/// Fallbacks implementation for GENERATOR interface
274-
struct FallbacksPrivateGenerator : FallbacksPrivate
286+
struct FallbacksPrivateGenerator : FallbacksPrivateCommon
275287
{
276288
FallbacksPrivateGenerator(FallbacksPrivate&& old);
277289
bool nextJob() override;
278290
};
279291

280292
/// Fallbacks implementation for FORWARD or BACKWARD interface
281-
struct FallbacksPrivatePropagator : FallbacksPrivate
293+
struct FallbacksPrivatePropagator : FallbacksPrivateCommon
282294
{
283295
FallbacksPrivatePropagator(FallbacksPrivate&& old);
284-
bool nextJob() override;
285296
void reset() override;
297+
void onNewSolution(const SolutionBase& s) override;
298+
bool nextJob() override;
286299

287300
Interface::Direction dir_; // propagation direction
288301
Interface::iterator job_; // pointer to currently processed external state
302+
bool job_has_solutions_; // flag indicating whether the current job generated solutions
289303
};
290304

291305
class WrapperBasePrivate : public ParallelContainerBasePrivate

core/src/container.cpp

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -861,25 +861,8 @@ void Fallbacks::init(const moveit::core::RobotModelConstPtr& robot_model) {
861861
pimpl()->reset();
862862
}
863863

864-
bool Fallbacks::canCompute() const {
865-
auto impl = const_cast<FallbacksPrivate*>(pimpl());
866-
867-
while(impl->current_ != impl->children().end() && // not completely exhausted
868-
!(*impl->current_)->pimpl()->canCompute()) // but current child cannot compute
869-
return impl->nextJob(); // advance to next job
870-
871-
// return value: current child is well defined and thus can compute?
872-
return impl->current_ != impl->children().end();
873-
}
874-
875-
void Fallbacks::compute() {
876-
(*pimpl()->current_)->pimpl()->runCompute();
877-
}
878-
879864
void Fallbacks::onNewSolution(const SolutionBase& s) {
880-
pimpl()->job_has_solutions_ = true;
881-
// printChildrenInterfaces(*this->pimpl(), true, *s.creator());
882-
liftSolution(s);
865+
pimpl()->onNewSolution(s);
883866
}
884867

885868
inline void Fallbacks::replaceImpl() {
@@ -909,17 +892,17 @@ FallbacksPrivate::FallbacksPrivate(FallbacksPrivate&& other)
909892
this->ParallelContainerBasePrivate::operator=(std::move(other));
910893
}
911894

912-
void FallbacksPrivate::reset() {
913-
current_ = children().begin();
914-
job_has_solutions_ = false;
915-
}
916-
917895
void FallbacksPrivate::initializeExternalInterfaces() {
918896
// Here we know the final interface of the container (and all its children)
919897
// Thus replace, this pimpl() with a new interface-specific one:
920898
static_cast<Fallbacks*>(me())->replaceImpl();
921899
}
922900

901+
void FallbacksPrivate::onNewSolution(const SolutionBase& s) {
902+
// printChildrenInterfaces(*this, true, *s.creator());
903+
static_cast<Fallbacks*>(me())->liftSolution(s);
904+
}
905+
923906
void FallbacksPrivate::onNewFailure(const Stage& child, const InterfaceState* /*from*/, const InterfaceState* /*to*/) {
924907
// This override is deliberately empty.
925908
// The method prunes solution paths when a child failed to find a valid solution for it,
@@ -929,20 +912,37 @@ void FallbacksPrivate::onNewFailure(const Stage& child, const InterfaceState* /*
929912
(void)child;
930913
}
931914

932-
void FallbacksPrivate::nextChild() {
915+
void FallbacksPrivateCommon::reset() {
916+
current_ = children().begin();
917+
}
918+
919+
bool FallbacksPrivateCommon::canCompute() const {
920+
while(current_ != children().end() && // not completely exhausted
921+
!(*current_)->pimpl()->canCompute()) // but current child cannot compute
922+
return const_cast<FallbacksPrivateCommon*>(this)->nextJob(); // advance to next job
923+
924+
// return value: current child is well defined and thus can compute?
925+
return current_ != children().end();
926+
}
927+
928+
void FallbacksPrivateCommon::compute() {
929+
(*current_)->pimpl()->runCompute();
930+
}
931+
932+
inline void FallbacksPrivateCommon::nextChild() {
933933
if (std::next(current_) != children().end())
934934
ROS_DEBUG_STREAM_NAMED("Fallbacks", "Child '" << (*current_)->name() << "' failed, trying next one.");
935935
++current_; // advance to next child
936936
}
937937

938938
FallbacksPrivateGenerator::FallbacksPrivateGenerator(FallbacksPrivate&& old)
939-
: FallbacksPrivate(std::move(old)) { reset(); }
939+
: FallbacksPrivateCommon(std::move(old)) { FallbacksPrivateCommon::reset(); }
940940

941941
bool FallbacksPrivateGenerator::nextJob() {
942942
assert(current_ != children().end() && !(*current_)->pimpl()->canCompute());
943943

944944
// don't advance to next child when we already produced solutions
945-
if (job_has_solutions_) {
945+
if (!solutions_.empty()) {
946946
current_ = children().end(); // indicate that we are exhausted
947947
return false;
948948
}
@@ -956,7 +956,7 @@ bool FallbacksPrivateGenerator::nextJob() {
956956

957957

958958
FallbacksPrivatePropagator::FallbacksPrivatePropagator(FallbacksPrivate&& old)
959-
: FallbacksPrivate(std::move(old)) {
959+
: FallbacksPrivateCommon(std::move(old)) {
960960
switch (requiredInterface()) {
961961
case PROPAGATE_FORWARDS:
962962
dir_ = Interface::FORWARD;
@@ -973,8 +973,14 @@ FallbacksPrivatePropagator::FallbacksPrivatePropagator(FallbacksPrivate&& old)
973973
}
974974

975975
void FallbacksPrivatePropagator::reset() {
976-
FallbacksPrivate::reset();
976+
FallbacksPrivateCommon::reset();
977977
job_ = pullInterface(dir_)->end(); // indicate fresh start
978+
job_has_solutions_ = false;
979+
}
980+
981+
void FallbacksPrivatePropagator::onNewSolution(const SolutionBase& s) {
982+
job_has_solutions_ = true;
983+
FallbacksPrivateCommon::onNewSolution(s);
978984
}
979985

980986
bool FallbacksPrivatePropagator::nextJob() {

0 commit comments

Comments
 (0)