SimpleAI
 All Classes Namespaces Files Functions Variables Typedefs Macros Groups Pages
TreeNodeParser.h
Go to the documentation of this file.
1 
4 #pragma once
5 
6 #include "tree/TreeNode.h"
7 #include "common/IParser.h"
8 #include "Steer.h"
9 
10 namespace ai {
11 
12 class IAIFactory;
13 
22 class TreeNodeParser: public IParser {
23 private:
24  const IAIFactory& _aiFactory;
25  std::string _taskString;
26 
27  void splitTasks(const std::string& string, std::vector<std::string>& tokens) const;
28  SteeringPtr getSteering(const std::string& nodeName);
29 public:
30  TreeNodeParser(const IAIFactory& aiFactory, const std::string& taskString) :
31  IParser(), _aiFactory(aiFactory) {
32  _taskString = ai::Str::eraseAllSpaces(taskString);
33  }
34 
35  virtual ~TreeNodeParser() {}
36 
37  TreeNodePtr getTreeNode(const std::string& name = "");
38 };
39 
40 inline void TreeNodeParser::splitTasks(const std::string& string, std::vector<std::string>& tokens) const {
41  bool inParameter = false;
42  bool inChildren = false;
43  std::string token;
44  for (std::string::const_iterator i = string.begin(); i != string.end(); ++i) {
45  if (*i == '{') {
46  inParameter = true;
47  } else if (*i == '}') {
48  inParameter = false;
49  } else if (*i == '(') {
50  inChildren = true;
51  } else if (*i == ')') {
52  inChildren = false;
53  }
54 
55  if (!inParameter && !inChildren) {
56  if (*i == ',') {
57  tokens.push_back(token);
58  token.clear();
59  continue;
60  }
61  }
62  token.push_back(*i);
63  }
64  tokens.push_back(token);
65 }
66 
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);
75  } else {
76  steerType = nodeName;
77  }
78 
79  const SteeringFactoryContext ctx(parameters);
80  return _aiFactory.createSteering(steerType, ctx);
81 }
82 
83 inline TreeNodePtr TreeNodeParser::getTreeNode(const std::string& name) {
84  resetError();
85  std::string nodeType;
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("{");
91  }
92  if (n != std::string::npos) {
93  nodeType = _taskString.substr(0, n);
94  } else {
95  nodeType = _taskString;
96  }
97  const std::string& subTrees = getBetween(_taskString, "(", ")");
98  if (!subTrees.empty()) {
99  if (nodeType != "Steer") {
100  return TreeNodePtr();
101  }
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);
107  if (!steering) {
108  return TreeNodePtr();
109  }
110  steerings.push_back(steering);
111  }
112  const SteerNodeFactoryContext steerFactoryCtx(name.empty() ? nodeType : name, parameters, True::get(), steerings);
113  return _aiFactory.createSteerNode(nodeType, steerFactoryCtx);
114  }
115 
116  const TreeNodeFactoryContext factoryCtx(name.empty() ? nodeType : name, parameters, True::get());
117  return _aiFactory.createNode(nodeType, factoryCtx);
118 }
119 
120 }
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: IParser.h:14
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...