-
Notifications
You must be signed in to change notification settings - Fork 291
/
Copy pathbehavior_strategy.cpp
60 lines (52 loc) · 1.88 KB
/
behavior_strategy.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include "behavior_strategy.h"
#include <unordered_map>
#include "behavior.h"
namespace behavior
{
sequential_t default_sequential;
fallback_t default_fallback;
sequential_until_done_t default_until_done;
std::unordered_map<std::string, const strategy_t *> strategy_map = {{
{ "sequential", &default_sequential },
{ "fallback", &default_fallback },
{ "sequential_until_done", &default_until_done }
}
};
} // namespace behavior
using namespace behavior;
// A standard behavior strategy, execute runnable children in order unless one fails.
behavior_return sequential_t::evaluate( const oracle_t *subject,
const std::vector<const node_t *> children ) const
{
for( const node_t *child : children ) {
behavior_return outcome = child->tick( subject );
if( outcome.result == running || outcome.result == failure ) {
return outcome;
}
}
return { success, nullptr };
}
// A standard behavior strategy, execute runnable children in order until one succeeds.
behavior_return fallback_t::evaluate( const oracle_t *subject,
const std::vector<const node_t *> children ) const
{
for( const node_t *child : children ) {
behavior_return outcome = child->tick( subject );
if( outcome.result == running || outcome.result == success ) {
return outcome;
}
}
return { failure, nullptr };
}
// A non-standard behavior strategy, execute runnable children in order unconditionally.
behavior_return sequential_until_done_t::evaluate( const oracle_t *subject,
const std::vector<const node_t *> children ) const
{
for( const node_t *child : children ) {
behavior_return outcome = child->tick( subject );
if( outcome.result == running ) {
return outcome;
}
}
return { success, nullptr };
}