00001
00002
00003
00004
00005
00006
00007
00008 #include "BTConstructor.h"
00009 #include <fstream>
00010 #include "ParserExceptions.h"
00011
00012 namespace bt_constructor{
00013 using namespace std;
00014
00015
00016 #define FOREACH_VEC(TYPE, VEC) for(std::vector<TYPE>::const_iterator i=o.VEC.begin();i!=o.VEC.end();i++)
00017 #define FOREACH_MAP(TYPE, VEC) for(std::map<std::string, TYPE>::const_iterator i=o.VEC.begin();i!=o.VEC.end();i++)
00018
00019 void bt_NODE_bgn(std::ostream& out, std::string tab, std::string node, std::string name, string id){
00020 string _name = " name=\""+ name +"\"";
00021 string _id = " id=\""+ id +"\"";
00022 out<<tab<<"<"<<node<<"" <<_name <<_id <<">";
00023 }
00024
00025 void bt_NODE_bgn(std::ostream& out, std::string tab, std::string node, std::string name, string id, string attr, string attr_value){
00026 string _name = " name=\""+ name +"\"";
00027 string _id = " id=\""+ id +"\"";
00028 string _attr = " " + attr + "=\""+ attr_value +"\"";
00029 out<<tab<<"<"<<node<<"" <<_name <<_id << _attr <<">";
00030 }
00031
00032 void bt_tree_bgn(std::ostream& out, std::string tab, std::string name, string id){
00033 bt_NODE_bgn(out, tab, "plan", name , id);
00034 }
00035 void bt_tree_end(std::ostream& out, std::string tab){
00036 out<<tab<<"</plan>";
00037 }
00038 void bt_par_bgn(std::ostream& out, std::string tab, std::string name, string id){
00039 bt_NODE_bgn(out, tab, "par", name , id);
00040 }
00041 void bt_par_end(std::ostream& out, std::string tab){
00042 out<<tab<<"</par>";
00043 }
00044 void bt_seq_bgn(std::ostream& out, std::string tab, std::string name, string id){
00045 bt_NODE_bgn(out, tab, "seq", name , id);
00046 }
00047 void bt_seq_end(std::ostream& out, std::string tab){
00048 out<<tab<<"</seq>";
00049 }
00050 void bt_sel_bgn(std::ostream& out, std::string tab, std::string name, string id){
00051 bt_NODE_bgn(out, tab, "sel", name , id);
00052 }
00053 void bt_sel_end(std::ostream& out, std::string tab){
00054 out<<tab<<"</sel>";
00055 }
00056 void bt_task_bgn(std::ostream& out, std::string tab, std::string name, string id){
00057 bt_NODE_bgn(out, tab, "task", name , id);
00058 }
00059 void bt_task_end(std::ostream& out, std::string tab){
00060 out<<tab<<"</task>";
00061 }
00062 void bt_dec_bgn(std::ostream& out, std::string tab, std::string name, string id, string dec_type){
00063 bt_NODE_bgn(out, tab, "dec", name , id, "type", dec_type);
00064 }
00065 void bt_dec_end(std::ostream& out, std::string tab){
00066 out<<tab<<"</dec>";
00067 }
00068 void bt_call(std::ostream& out, std::string tab, std::string name, std::string type, string id){
00069 if(type=="rtask"){
00070 string _name = " name=\"TASK["+ name +"]\"";
00071 string _id = " id=\""+ id +"\"";
00072 out<<tab<<"<task"<< _name << _id <<" />";
00073 }
00074 }
00075 void bt_task_result(std::ostream& out, std::string tab, std::string name, string id, string task_result){
00076 bt_NODE_bgn(out, tab, "task_result", name , id, "result", task_result);
00077 }
00078 void bt_task_result_after(std::ostream& out, std::string tab, std::string name, string id, string task_result){
00079 bt_NODE_bgn(out, tab, "task_result_after", name , id, "result", task_result);
00080 }
00081 bool saveXml_call_node(std::ostream& out, const Node& o){
00082 string resolved = "";
00083 if(o.type == "bt"){
00084 bool isResolved = o.lib->contains_tree(o.name);
00085 if(not isResolved){
00086 o.lib->errors<<"in "<<o.file<<":"<<o.line<<":"<<o.pos<<endl;
00087 o.lib->errors<<" BT("<<o.name<<") definition does not found." <<endl;
00088 out<<o.tab<<"<error>"<<endl;
00089 out<<o.tab<<" "<<"in "<<o.file<<":"<<o.line<<":"<<o.pos<<endl;
00090 out<<o.tab<<" "<<" BT("<<o.name<<") definition does not found." <<endl;
00091 out<<o.tab<<"</error>";
00092 }else{
00093 bt_task_bgn(out, o.tab, o.name, o.getId()); out<<endl;
00094 o.lib->saveXml_tree(out, o.tab+" ", o.name, o.getId()); out<<endl;
00095 bt_task_end(out, o.tab);
00096 }
00097 return true;
00098 }
00099 if(o.type == "fsm"){
00100 bool isResolved = o.lib->contains_fsm(o.name);
00101 if(not isResolved){
00102 o.lib->errors<<"in "<<o.file<<":"<<o.line<<":"<<o.pos<<endl;
00103 o.lib->errors<<" FSM("<<o.name<<") definition does not found." <<endl;
00104 out<<o.tab<<"<error>"<<endl;
00105 out<<o.tab<<" "<<"in "<<o.file<<":"<<o.line<<":"<<o.pos<<endl;
00106 out<<o.tab<<" "<<" FSM("<<o.name<<") definition does not found." <<endl;
00107 out<<o.tab<<"</error>";
00108 }else{
00109 string tab = o.tab+" ";
00110 bt_task_bgn(out, o.tab, o.name, o.getId()); out<<endl;
00111 out<<tab<<"<scxml>"<<endl;
00112 o.lib->saveXml_fsm(out, tab+" ", o.name, o.getId()); out<<endl;
00113 out<<tab<<"</scxml>"<<endl;
00114 bt_task_end(out, o.tab);
00115 }
00116 return true;
00117 }
00118 if(o.type == "rtask"){
00119 bt_call(out, o.tab, o.name, o.type, o.getId());
00120 return true;
00121 }
00122 return false;
00123 }
00124
00125 std::ostream& saveXml(std::ostream& out, const Node& o){
00126 if(saveXml_call_node(out, o)) return out;
00127 if(o.type == "par") bt_par_bgn(out, o.tab, o.name, o.getId());
00128 if(o.type == "seq") bt_seq_bgn(out, o.tab, o.name, o.getId());
00129 if(o.type == "sel") bt_sel_bgn(out, o.tab, o.name, o.getId());
00130
00134 if(o.type == "dec") bt_dec_bgn(out, o.tab, o.name, o.getId(), o.decorator_name);
00135
00136 if(o.type == "task_result") bt_task_result(out, o.tab, o.name, o.getId(), o.task_result);
00137 if(o.type == "task_result_after") bt_task_result_after(out, o.tab, o.name, o.getId(), o.task_result);
00138
00139 if(o.type == "task")bt_task_bgn(out, o.tab, o.name, o.getId());
00140 FOREACH_VEC(Node, nodes){
00141 out<<endl;
00142 i->lib = o.lib;
00143 i->tab = o.tab+" ";
00144 i->id = o.getId();
00145 saveXml(out, *i);
00146 }
00147 out<<endl;
00148 if(o.type == "par") bt_par_end(out, o.tab);
00149 if(o.type == "seq") bt_seq_end(out, o.tab);
00150 if(o.type == "sel") bt_sel_end(out, o.tab);
00151 if(o.type == "dec") bt_dec_end(out, o.tab);
00152 if(o.type == "task")bt_task_end(out, o.tab);
00153 return out;
00154 }
00155
00156
00157 std::ostream& saveXml(std::ostream& out, const Tree& o){
00158 bt_tree_bgn(out, o.tab, o.name, o.getId());
00159 FOREACH_VEC(Node, nodes){
00160 out<<endl;
00161 i->lib = o.lib;
00162 i->tab = o.tab+" ";
00163 i->id = o.getId();
00164 saveXml(out, *i);
00165 }
00166 out<<endl;
00167 bt_tree_end(out, o.tab);
00168 return out;
00169 }
00170
00171 std::ostream& saveXml(std::ostream& out, const BTConstructor& o){
00172 FOREACH_MAP(Tree, trees){
00173 i->second.lib = &o;
00174 i->second.tab = "";
00175 saveXml(out, i->second)<<endl;
00176 }
00177 return out;
00178 }
00179
00180
00181 void saveXml(std::string path_prefix, const BTConstructor& o){
00182 FOREACH_MAP(Tree, trees){
00183 std::stringstream filename; filename << path_prefix << i->second.name <<".btxml";
00184 std::ofstream out(filename.str().c_str());
00185 if(out.is_open()){
00186 out<<"<?xml version=\"1.0\"?>"; out<<endl;
00187 out<<"<!-- from file: "<<o.filename<<" -->"; out<<endl;
00188 i->second.lib = &o;
00189 i->second.tab = "";
00190 saveXml(out, i->second)<<endl;
00191 out.close();
00192 }else{
00193 throw PEFileNotCreated(filename.str());
00194 }
00195 }
00196 }
00197
00198
00199
00200
00201 bool print_call_node(std::ostream& out, const Node& o){
00202 string resolved = "";
00203 if(o.type == "bt"){
00204 bool isResolved = o.lib->contains_tree(o.name);
00205 resolved = isResolved?"resolved":"not found";
00206 if(not isResolved){
00207 o.lib->errors<<"in "<<o.file<<":"<<o.line<<":"<<o.pos<<endl;
00208 o.lib->errors<<" BT("<<o.name<<") definition does not found." <<endl;
00209 }
00210 out<<o.tab<<"call "<<o.type<<" : "<<o.name << " : " << resolved;
00211 return true;
00212 }
00213 if(o.type == "fsm"){
00214 bool isResolved = o.lib->contains_fsm(o.name);
00215 resolved = isResolved?"resolved":"not found";
00216 if(not isResolved){
00217 o.lib->errors<<"in "<<o.file<<":"<<o.line<<":"<<o.pos<<endl;
00218 o.lib->errors<<" FSM("<<o.name<<") definition does not found." <<endl;
00219 }
00220 out<<o.tab<<"call "<<o.type<<" : "<<o.name << " : " << resolved;
00221 return true;
00222 }
00223 if(o.type == "rtask"){
00224 out<<o.tab<<"call "<<"task"<<" : "<<o.name;
00225 return true;
00226 }
00227 return false;
00228 }
00229
00230 std::ostream& operator<<(std::ostream& out, const Node& o){
00231 if(print_call_node(out, o)) return out;
00232 out<<o.tab<<"Node{"<<endl;
00233 out<<o.tab<<" name="<<o.name<<endl;
00234 out<<o.tab<<" type="<<o.type<<endl;
00235 out<<o.tab<<" Nodes:"<<endl;
00236 FOREACH_VEC(Node, nodes){
00237 i->lib = o.lib;
00238 i->tab = o.tab+" ";
00239 out<<(*i)<<endl;
00240 }
00241 out<<o.tab<<"}";
00242 return out;
00243 }
00244
00245
00246 std::ostream& operator<<(std::ostream& out, const Tree& o){
00247 out<<"BT{"<<endl;
00248 out<<" name="<<o.name<<endl;
00249 out<<" Nodes:"<<endl;
00250 FOREACH_VEC(Node, nodes){
00251 i->lib = o.lib;
00252 i->tab = " ";
00253 out<<(*i)<<endl;
00254 }
00255 out<<"}";
00256 return out;
00257 }
00258
00259 std::ostream& operator<<(std::ostream& out, const BTConstructor& o){
00260 FOREACH_MAP(Tree, trees){
00261 i->second.lib = &o;
00262 out<<i->second<<endl;
00263 }
00264 return out;
00265 }
00266
00267
00268 }
00269
00270