00001
00002
00003
00004
00005
00006
00007
00008 #ifndef BTCONSTRUCTOR_H_
00009 #define BTCONSTRUCTOR_H_
00010
00011 #include <deque>
00012 #include <map>
00013 #include <sstream>
00014 #include <vector>
00015 #include <iostream>
00016 #include "Container.h"
00017
00018 namespace bt_constructor{
00019 using namespace std;
00020
00021 #define DEF_STACK_MAP(TYPE, MNAME, NAME)\
00022 std::map<std::string, TYPE> MNAME;\
00023 std::deque<TYPE> stack_##NAME;\
00024 void create_##NAME(){\
00025 TYPE t; t.lib=lib;\
00026 stack_##NAME.push_back(t);\
00027 }\
00028 void drop_##NAME(){\
00029 stack_##NAME.pop_back();\
00030 }\
00031 void add_##NAME(){\
00032 MNAME[NAME().name] = NAME();\
00033 drop_##NAME();\
00034 }\
00035 TYPE& NAME(){ return stack_##NAME.back(); }
00036
00037 #define DEF_STACK_VECTOR(TYPE, VNAME, NAME)\
00038 std::vector<TYPE> VNAME;\
00039 std::deque<TYPE> stack_##NAME;\
00040 void create_##NAME(){\
00041 TYPE t; t.lib=lib;\
00042 stack_##NAME.push_back(t);\
00043 }\
00044 void drop_##NAME(){\
00045 stack_##NAME.pop_back();\
00046 }\
00047 void add_##NAME(){\
00048 VNAME.push_back( NAME() );\
00049 drop_##NAME();\
00050 }\
00051 TYPE& NAME(){ return stack_##NAME.back(); }
00052
00053 class BTConstructor;
00054 class Element{
00055 public:
00056 mutable BTConstructor const* lib;
00057 mutable string tab;
00058 };
00059
00060 class Node;
00061 std::deque<Node>& stack_node(Node* _this);
00062
00063 class Node:public Element{
00064 protected:
00065 Node(string t):type(t),line(0),pos(0){}
00066 public:
00067 mutable string id;
00068 string type;
00069 string name;
00070 string decorator_name;
00071 string task_result;
00072 int line, pos;
00073 string file;
00074 Node():line(0),pos(0){}
00075
00076 std::vector<Node> nodes;
00077
00078 void create_node(){
00079 Node t; t.lib=lib;
00080 stack_node(this).push_back(t);
00081 }
00082 void drop_node(){
00083 stack_node(this).pop_back();
00084 }
00085 void add_node(){
00086 Node t = node();
00087 drop_node();
00088 if(stack_node(this).empty()){
00089 t.id = getId();
00090 nodes.push_back( t );
00091 }else{
00092 Node &n = node();
00093 t.id = n.getId();
00094 n.nodes.push_back( t );
00095 }
00096 }
00097 Node& node(){
00098 if(stack_node(this).empty()){
00099 return *this;
00100 }else{
00101 return stack_node(this).back();
00102 }
00103 }
00104
00105 void create_call(){
00106 Node t; t.lib=lib;
00107 t.id = getId();
00108 nodes.push_back(t);
00109 }
00110 void add_call(){
00111 }
00112 Node& call(){
00113 return nodes.back();
00114 }
00115
00116 std::string getId()const{ return id+"/"+name; }
00117 };
00118
00119
00120 class Tree:public Node{
00121 public:
00122 Tree():Node("tree"){}
00123 };
00124
00125 class BTConstructor:public Element, public Container{
00126 public:
00127 BTConstructor(std::stringstream& errors, std::string filename):errors(errors), filename(filename){ lib=this; }
00128 DEF_STACK_MAP(Tree, trees, tree)
00129 mutable std::deque<Node> stack_node;
00130 Container* fsms;
00131 std::stringstream& errors;
00132 std::string filename;
00133
00134 bool contains_tree(string name)const{
00135 return contains(name);
00136 }
00137 std::string copy_tree(std::string name)const{
00138 return copy(name);
00139 }
00140
00141 bool contains_fsm(string name)const{
00142 return fsms->contains(name);
00143 }
00144 std::string copy_fsm(std::string name)const{
00145 return fsms->copy(name);
00146 }
00147
00148 bool contains(std::string name)const{ return trees.find(name)!=trees.end(); }
00149 std::string copy(std::string name)const;
00150
00151
00152 void saveXml_fsm(std::ostream& out, std::string tab, std::string name, std::string id)const{
00153 fsms->saveXml(out, tab, name, id);
00154 }
00155 void saveXml_tree(std::ostream& out, std::string tab, std::string name, std::string id)const{
00156 saveXml(out, tab, name, id);
00157 }
00158
00159 void saveXml(std::ostream& out, std::string tab, std::string name, std::string id)const;
00160
00161
00162 void saveDot_fsm(std::ostream& out, std::string tab, std::string name, std::string id)const{
00163 fsms->saveDot(out, tab, name, id);
00164 }
00165 void saveDot_tree(std::ostream& out, std::string tab, std::string name, std::string id)const{
00166 saveDot(out, tab, name, id);
00167 }
00168
00169 void saveDot(std::ostream& out, std::string tab, std::string name, std::string id)const;
00170
00171
00172 void map_ids_tree(std::string name, std::string id)const{map_ids(name,id);}
00173 void map_ids_fsm(std::string name, std::string id)const{fsms->map_ids(name,id);}
00174 void map_ids(std::string name, std::string id)const;
00175
00176 std::string search_simple_node(std::string name)const;
00177
00178 };
00179
00180 inline std::deque<Node>& stack_node(Node * _this){
00181 return _this->lib->stack_node;
00182 }
00183
00184 #define FOREACH_VEC(TYPE, VEC) for(std::vector<TYPE>::const_iterator i=o.VEC.begin();i!=o.VEC.end();i++)
00185 #define FOREACH_MAP(TYPE, VEC) for(std::map<std::string, TYPE>::const_iterator i=o.VEC.begin();i!=o.VEC.end();i++)
00186
00187 bool print_call_node(std::ostream& out, const Node& o);
00188 std::ostream& operator<<(std::ostream& out, const Node& o);
00189 std::ostream& operator<<(std::ostream& out, const Tree& o);
00190 std::ostream& operator<<(std::ostream& out, const BTConstructor& o);
00191
00192
00193
00194 bool saveXml_call_node(std::ostream& out, const Node& o);
00195 std::ostream& saveXml(std::ostream& out, const Node& o);
00196 std::ostream& saveXml(std::ostream& out, const Tree& o);
00197 std::ostream& saveXml(std::ostream& out, const BTConstructor& o);
00198 void saveXml(std::string prefix, const BTConstructor& o);
00199
00200
00201
00202
00203 bool saveDot_call_node(std::ostream& out, const Node& o);
00204 std::ostream& saveDot(std::ostream& out, const Node& o);
00205 std::ostream& saveDot(std::ostream& out, const Tree& o);
00206 std::ostream& saveDot(std::ostream& out, const BTConstructor& o);
00207 void saveDot(std::string prefix, const BTConstructor& o);
00208
00209 void map_ids(const Node& o);
00210 void map_ids(const Tree& o);
00211 void map_ids(const BTConstructor& o);
00212
00213 }
00214
00215
00216 #undef FOREACH_MAP
00217 #undef FOREACH_VEC
00218 #undef DEF_STACK_MAP
00219 #undef DEF_STACK_VECTOR_
00220
00221 #endif