25 std::string _taskString;
27 void splitTasks(
const std::string&
string, std::vector<std::string>& tokens)
const;
28 SteeringPtr getSteering(
const std::string& nodeName);
31 IParser(), _aiFactory(aiFactory) {
32 _taskString = ai::Str::eraseAllSpaces(taskString);
37 TreeNodePtr getTreeNode(
const std::string& name =
"");
40 inline void TreeNodeParser::splitTasks(
const std::string&
string, std::vector<std::string>& tokens)
const {
41 bool inParameter =
false;
42 bool inChildren =
false;
44 for (std::string::const_iterator i =
string.begin(); i !=
string.end(); ++i) {
47 }
else if (*i ==
'}') {
49 }
else if (*i ==
'(') {
51 }
else if (*i ==
')') {
55 if (!inParameter && !inChildren) {
57 tokens.push_back(token);
64 tokens.push_back(token);
67 inline SteeringPtr TreeNodeParser::getSteering (
const std::string& nodeName) {
68 std::string steerType;
69 const std::string& parameters = getBetween(nodeName,
"{",
"}");
70 std::size_t n = nodeName.find(
"{");
71 if (n == std::string::npos)
72 n = nodeName.find(
"(");
73 if (n != std::string::npos) {
74 steerType = nodeName.substr(0, n);
79 const SteeringFactoryContext ctx(parameters);
83 inline TreeNodePtr TreeNodeParser::getTreeNode(
const std::string& name) {
86 std::string parameters;
87 std::size_t n = _taskString.find(
"(");
88 if (n == std::string::npos || _taskString.find(
"{") < n) {
89 parameters = getBetween(_taskString,
"{",
"}");
90 n = _taskString.find(
"{");
92 if (n != std::string::npos) {
93 nodeType = _taskString.substr(0, n);
95 nodeType = _taskString;
97 const std::string& subTrees = getBetween(_taskString,
"(",
")");
98 if (!subTrees.empty()) {
99 if (nodeType !=
"Steer") {
100 return TreeNodePtr();
102 std::vector<std::string> tokens;
103 splitTasks(subTrees, tokens);
104 movement::Steerings steerings;
105 for (
const std::string& nodeName : tokens) {
106 const SteeringPtr& steering = getSteering(nodeName);
108 return TreeNodePtr();
110 steerings.push_back(steering);
112 const SteerNodeFactoryContext steerFactoryCtx(name.empty() ? nodeType : name, parameters, True::get(), steerings);
116 const TreeNodeFactoryContext factoryCtx(name.empty() ? nodeType : name, parameters, True::get());
117 return _aiFactory.
createNode(nodeType, factoryCtx);
Transforms the string representation of a TreeNode with all its parameters into a TreeNode instance...
Definition: TreeNodeParser.h:22
virtual SteeringPtr createSteering(const std::string &type, const SteeringFactoryContext &ctx) const =0
Creates a new ISteering for the given type. The type must be registered in the AIRegistry for this to...
Definition: IAIFactory.h:39
virtual TreeNodePtr createNode(const std::string &type, const TreeNodeFactoryContext &ctx) const =0
Allocates a new TreeNode for the given type. The type must be registered in the AIRegistry for this t...
virtual TreeNodePtr createSteerNode(const std::string &type, const SteerNodeFactoryContext &ctx) const =0
Allocates a new TreeNode for the given type. The type must be registered in the AIRegistry for this t...