Skip to content

Commit 1f8dae8

Browse files
committed
Merge #156: Rework interface resolution
2 parents b4fb8bb + c4d0ab0 commit 1f8dae8

File tree

14 files changed

+702
-657
lines changed

14 files changed

+702
-657
lines changed

core/include/moveit/task_constructor/container.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,6 @@ class SerialContainer : public ContainerBase
9191
PRIVATE_CLASS(SerialContainer)
9292
SerialContainer(const std::string& name = "serial container");
9393

94-
void init(const moveit::core::RobotModelConstPtr& robot_model) override;
95-
9694
bool canCompute() const override;
9795
void compute() override;
9896

@@ -130,8 +128,6 @@ class ParallelContainerBase : public ContainerBase
130128
PRIVATE_CLASS(ParallelContainerBase)
131129
ParallelContainerBase(const std::string& name = "parallel container");
132130

133-
void init(const moveit::core::RobotModelConstPtr& robot_model) override;
134-
135131
protected:
136132
ParallelContainerBase(ParallelContainerBasePrivate* impl);
137133

core/include/moveit/task_constructor/container_p.h

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ class ContainerBasePrivate : public StagePrivate
105105

106106
void validateConnectivity() const override;
107107

108+
// Containers derive their required interface from their children
109+
// UNKNOWN until resolveInterface was called
110+
InterfaceFlags requiredInterface() const override { return required_interface_; }
111+
108112
// forward these methods to the public interface for containers
109113
bool canCompute() const override;
110114
void compute() override;
@@ -115,21 +119,17 @@ class ContainerBasePrivate : public StagePrivate
115119
protected:
116120
ContainerBasePrivate(ContainerBase* me, const std::string& name);
117121

118-
// Set child's push interfaces: allow pushing if child requires it or
119-
// if the interface is unknown: in this case greedily assume a push interface.
120-
// If, during pruneInterface() later, we notice that it's not needed, prune there.
121-
inline void setChildsPushBackwardInterface(Stage& child) {
122-
InterfaceFlags required = child.pimpl()->requiredInterface();
123-
bool allowed = (required & WRITES_PREV_END) || (required == UNKNOWN);
124-
child.pimpl()->setPrevEnds(allowed ? pending_backward_ : InterfacePtr());
122+
// Set child's push interfaces: allow pushing if child requires it.
123+
inline void setChildsPushBackwardInterface(StagePrivate* child) {
124+
InterfaceFlags required = child->requiredInterface();
125+
bool allowed = (required & WRITES_PREV_END);
126+
child->setPrevEnds(allowed ? pending_backward_ : InterfacePtr());
125127
}
126-
inline void setChildsPushForwardInterface(Stage& child) {
127-
InterfaceFlags required = child.pimpl()->requiredInterface();
128-
bool allowed = (required & WRITES_NEXT_START) || (required == UNKNOWN);
129-
child.pimpl()->setNextStarts(allowed ? pending_forward_ : InterfacePtr());
128+
inline void setChildsPushForwardInterface(StagePrivate* child) {
129+
InterfaceFlags required = child->requiredInterface();
130+
bool allowed = (required & WRITES_NEXT_START);
131+
child->setNextStarts(allowed ? pending_forward_ : InterfacePtr());
130132
}
131-
// report error about mismatching interface (start or end as determined by mask)
132-
void mismatchingInterface(InitStageException& errors, const StagePrivate& child, const InterfaceFlags mask) const;
133133

134134
/// copy external_state to a child's interface and remember the link in internal_to map
135135
void copyState(Interface::iterator external, const InterfacePtr& target, bool updated);
@@ -139,6 +139,9 @@ class ContainerBasePrivate : public StagePrivate
139139
auto& internalToExternalMap() { return internal_to_external_; }
140140
const auto& internalToExternalMap() const { return internal_to_external_; }
141141

142+
// set in resolveInterface()
143+
InterfaceFlags required_interface_;
144+
142145
private:
143146
container_type children_;
144147

@@ -166,28 +169,20 @@ class SerialContainerPrivate : public ContainerBasePrivate
166169
public:
167170
SerialContainerPrivate(SerialContainer* me, const std::string& name);
168171

169-
// containers derive their required interface from their children
170-
InterfaceFlags requiredInterface() const override;
171-
172172
// called by parent asking for pruning of this' interface
173-
void pruneInterface(InterfaceFlags accepted) override;
173+
void resolveInterface(InterfaceFlags expected) override;
174174
// validate connectivity of chain
175175
void validateConnectivity() const override;
176176

177-
private:
178-
// connect cur stage to its predecessor and successor
179-
bool connect(container_type::const_iterator cur);
177+
void reset();
180178

181-
// called by init() to prune interfaces for children in range [first, last)
182-
void pruneInterfaces(container_type::const_iterator first, container_type::const_iterator end);
183-
// prune interface for children in range [first, last) to given direction
184-
void pruneInterfaces(container_type::const_iterator first, container_type::const_iterator end,
185-
InterfaceFlags accepted);
186-
// store first/last child's interface as required for this container
187-
void storeRequiredInterface(container_type::const_iterator first, container_type::const_iterator end);
179+
protected:
180+
// connect two neighbors
181+
void connect(StagePrivate& stage1, StagePrivate& stage2);
188182

189-
private:
190-
InterfaceFlags required_interface_;
183+
// validate that child's interface matches mine (considering start or end only as determined by mask)
184+
template <unsigned int mask>
185+
void validateInterface(const StagePrivate& child, InterfaceFlags required) const;
191186
};
192187
PIMPL_FUNCTIONS(SerialContainer)
193188

@@ -219,14 +214,14 @@ class ParallelContainerBasePrivate : public ContainerBasePrivate
219214
public:
220215
ParallelContainerBasePrivate(ParallelContainerBase* me, const std::string& name);
221216

222-
// containers derive their required interface from their children
223-
InterfaceFlags requiredInterface() const override;
224-
225217
// called by parent asking for pruning of this' interface
226-
void pruneInterface(InterfaceFlags accepted) override;
218+
void resolveInterface(InterfaceFlags expected) override;
227219

228220
void validateConnectivity() const override;
229221

222+
protected:
223+
void validateInterfaces(const StagePrivate& child, InterfaceFlags& external, bool first = false) const;
224+
230225
private:
231226
/// callback for new externally received states
232227
void onNewExternalState(Interface::Direction dir, Interface::iterator external, bool updated);
@@ -256,7 +251,7 @@ class MergerPrivate : public ParallelContainerBasePrivate
256251
typedef std::function<void(SubTrajectory&&)> Spawner;
257252
MergerPrivate(Merger* me, const std::string& name);
258253

259-
InterfaceFlags requiredInterface() const override;
254+
void resolveInterface(InterfaceFlags expected) override;
260255

261256
void onNewPropagateSolution(const SolutionBase& s);
262257
void onNewGeneratorSolution(const SolutionBase& s);

core/include/moveit/task_constructor/stage.h

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,30 @@ enum InterfaceFlag
8383
PROPAGATE_BACKWARDS = READS_END | WRITES_PREV_END,
8484
GENERATE = WRITES_PREV_END | WRITES_NEXT_START,
8585
};
86+
8687
typedef Flags<InterfaceFlag> InterfaceFlags;
8788

89+
/** invert interface such that
90+
* - new end can connect to old start
91+
* - new start can connect to old end
92+
*/
93+
constexpr InterfaceFlags invert(InterfaceFlags f) {
94+
InterfaceFlags inv;
95+
if (f & READS_START)
96+
inv |= WRITES_NEXT_START;
97+
if (f & WRITES_PREV_END)
98+
inv |= READS_END;
99+
if (f & READS_END)
100+
inv |= WRITES_PREV_END;
101+
if (f & WRITES_NEXT_START)
102+
inv |= READS_START;
103+
return inv;
104+
};
105+
88106
// some useful constants
89107
constexpr InterfaceFlags UNKNOWN;
90108
constexpr InterfaceFlags START_IF_MASK({ READS_START, WRITES_PREV_END });
91109
constexpr InterfaceFlags END_IF_MASK({ READS_END, WRITES_NEXT_START });
92-
constexpr InterfaceFlags PROPAGATE_BOTHWAYS({ PROPAGATE_FORWARDS, PROPAGATE_BACKWARDS });
93110

94111
MOVEIT_CLASS_FORWARD(Interface)
95112
MOVEIT_CLASS_FORWARD(Stage)
@@ -253,12 +270,9 @@ class PropagatingEitherWay : public ComputeBase
253270
AUTO = 0x00, // auto-derive direction from context
254271
FORWARD = 0x01, // propagate forward only
255272
BACKWARD = 0x02, // propagate backward only
256-
BOTHWAY = FORWARD | BACKWARD, // propagate both ways
257273
};
258274
void restrictDirection(Direction dir);
259275

260-
void init(const moveit::core::RobotModelConstPtr& robot_model) override;
261-
262276
virtual void computeForward(const InterfaceState& from) = 0;
263277
virtual void computeBackward(const InterfaceState& to) = 0;
264278

@@ -368,6 +382,8 @@ class Connecting : public ComputeBase
368382
}
369383
};
370384

371-
const char* flowSymbol(moveit::task_constructor::InterfaceFlags f);
385+
/** Return (horizontal) flow symbol for start or end interface (specified by mask) */
386+
template <unsigned int mask>
387+
const char* flowSymbol(InterfaceFlags f);
372388
}
373389
}

core/include/moveit/task_constructor/stage_p.h

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,11 @@ class StagePrivate
7171
/// actually configured interface of this stage (only valid after init())
7272
InterfaceFlags interfaceFlags() const;
7373

74-
/** Interface required by this stage
75-
*
76-
* If the interface is unknown (because it is auto-detected from context), return 0 */
74+
/// Retrieve interface required by this stage, UNKNOWN if auto-detected from context
7775
virtual InterfaceFlags requiredInterface() const = 0;
7876

79-
/** Prune interface to comply with the given propagation direction
80-
*
81-
* PropagatingEitherWay uses this in restrictDirection() */
82-
virtual void pruneInterface(InterfaceFlags accepted) {}
77+
/// Resolve interface/propagation direction to comply with the given external interface
78+
virtual void resolveInterface(InterfaceFlags /* expected */) {}
8379

8480
/// validate connectivity of children (after init() was done)
8581
virtual void validateConnectivity() const;
@@ -163,6 +159,7 @@ class StagePrivate
163159
std::string name_;
164160
PropertyMap properties_;
165161

162+
// pull interfaces, created by the stage as required
166163
InterfacePtr starts_;
167164
InterfacePtr ends_;
168165

@@ -182,6 +179,8 @@ class StagePrivate
182179
ContainerBase* parent_; // owning parent
183180
container_type::iterator it_; // iterator into parent's children_ list referring to this
184181

182+
// push interfaces, assigned by the parent container
183+
// linking to previous/next sibling's pull interfaces
185184
InterfaceWeakPtr prev_ends_; // interface to be used for sendBackward()
186185
InterfaceWeakPtr next_starts_; // interface to be used for sendForward()
187186

@@ -208,19 +207,17 @@ class PropagatingEitherWayPrivate : public ComputeBasePrivate
208207
friend class PropagatingEitherWay;
209208

210209
public:
211-
PropagatingEitherWay::Direction required_interface_dirs_;
210+
PropagatingEitherWay::Direction configured_dir_;
211+
InterfaceFlags required_interface_;
212212

213-
inline PropagatingEitherWayPrivate(PropagatingEitherWay* me,
214-
PropagatingEitherWay::Direction required_interface_dirs_,
213+
inline PropagatingEitherWayPrivate(PropagatingEitherWay* me, PropagatingEitherWay::Direction configured_dir_,
215214
const std::string& name);
216215

217216
InterfaceFlags requiredInterface() const override;
218217
// initialize pull interfaces for given propagation directions
219218
void initInterface(PropagatingEitherWay::Direction dir);
220-
// prune interface to the given propagation direction
221-
void pruneInterface(InterfaceFlags accepted) override;
222-
// validate that we can propagate in one direction at least
223-
void validateConnectivity() const override;
219+
// resolve interface to the given propagation direction
220+
void resolveInterface(InterfaceFlags expected) override;
224221

225222
bool canCompute() const override;
226223
void compute() override;

0 commit comments

Comments
 (0)