MCPcopy Create free account
hub / github.com/BehaviorTree/BehaviorTree.CPP / checkPreConditions

Method checkPreConditions

src/tree_node.cpp:204–259  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

202}
203
204Expected<NodeStatus> TreeNode::checkPreConditions()
205{
206 Ast::Environment env = { config().blackboard, config().enums };
207
208 // Check pre-conditions in order: FAILURE_IF, SUCCESS_IF, SKIP_IF, WHILE_TRUE.
209 // IMPORTANT: _failureIf, _successIf, and _skipIf are evaluated ONLY when the
210 // node is IDLE or SKIPPED. They are NOT re-evaluated while the node is RUNNING.
211 for(size_t index = 0; index < size_t(PreCond::COUNT_); index++)
212 {
213 const auto& parse_executor = _p->pre_parsed[index];
214 if(!parse_executor)
215 {
216 continue;
217 }
218
219 const auto preID = static_cast<PreCond>(index);
220
221 // _failureIf, _successIf, _skipIf: only checked when IDLE or SKIPPED
222 // _while: checked here AND also when RUNNING (see below)
223 if(_p->status == NodeStatus::IDLE || _p->status == NodeStatus::SKIPPED)
224 {
225 // what to do if the condition is true
226 if(parse_executor(env).cast<bool>())
227 {
228 if(preID == PreCond::FAILURE_IF)
229 {
230 return NodeStatus::FAILURE;
231 }
232 if(preID == PreCond::SUCCESS_IF)
233 {
234 return NodeStatus::SUCCESS;
235 }
236 if(preID == PreCond::SKIP_IF)
237 {
238 return NodeStatus::SKIPPED;
239 }
240 }
241 // if the conditions is false
242 else if(preID == PreCond::WHILE_TRUE)
243 {
244 return NodeStatus::SKIPPED;
245 }
246 }
247 else if(_p->status == NodeStatus::RUNNING && preID == PreCond::WHILE_TRUE)
248 {
249 // _while is the ONLY precondition checked while RUNNING.
250 // If the condition becomes false, halt the node and return SKIPPED.
251 if(!parse_executor(env).cast<bool>())
252 {
253 haltNode();
254 return NodeStatus::SKIPPED;
255 }
256 }
257 }
258 return nonstd::make_unexpected(""); // no precondition
259}
260
261void TreeNode::checkPostConditions(NodeStatus status)

Callers

nothing calls this directly

Calls 1

make_unexpectedFunction · 0.85

Tested by

no test coverage detected