00001 #include "rulecontext.h"
00002 #include "config.h"
00003 #include <sstream>
00004
00005 bool RuleContext::executeOnPHP(List* phpIn, List* phpOut, List* tplOut){
00006 RuleList::const_iterator rulesIt = rules.begin();
00007 RuleList::const_iterator rulesEnd = rules.end();
00008 if (verbose>2) cout << "Doing: " << (phpIn->front()) << endl;
00009 if (verbose>2) phpIn->front()->print(cout) << endl;
00010 while (rulesIt!=rulesEnd){
00011 if (verbose>2) cout << "Trying:" << rulesIt->second << endl;
00012 Node::Assignment assign;
00013 if (rulesIt->second->execute(this,phpIn,phpOut,tplOut,assign)) {
00014 if (verbose>2) cout << " Success" << endl;
00015 return true;
00016 }
00017 if (verbose>2) cout << " Failed" << endl;
00018 rulesIt++;
00019 };
00020 return false;
00021
00022
00023
00024
00025
00026
00027 };
00028
00029 void RuleContext::executeOnPHPList(List* phpIn, List *phpOut, List *tplOut){
00030 while (phpIn->getLength()){
00031 if (!executeOnPHP(phpIn,phpOut, tplOut))
00032 throw "ERROR: RuleContext::executeOnPHPList - no rule provided for : " + phpIn->front()->toString();
00033 }
00034 delete phpIn;
00035 };
00036
00037 RuleContext::RuleContext(RuleList &irules, NameSpace &inameSpace) : rules(irules), nameSpace(inameSpace) {};
00038
00039 RuleContext::~RuleContext(){
00040 list<RuleList::iterator>::iterator it = myRules.begin();
00041 list<RuleList::iterator>::iterator eit = myRules.end();
00042 while(it!=eit){
00043 delete (*it)->second;
00044 rules.erase(*it);
00045 it++;
00046 };
00047 };
00048
00049
00050 void RuleContext::insert(int priority, Rule *rule){
00051 myRules.push_back(rules.insert(PriorityRulePair(priority,rule)));
00052 };
00053
00054 void RuleContext::insert(PriorityRulePair *rulePair){
00055 myRules.push_back(rules.insert(rulePair));
00056 };
00057
00058 void RuleContext::execute(Node* phpIn,List *phpOut, List *tplOut){
00059 enterContext(phpOut,tplOut);
00060 if (verbose>2) cout << "IN EXECUTE" << endl;
00061 if (phpIn->getName() == CStatementList)
00062 executeOnPHPList(dynamic_cast<StatementList*>(phpIn),phpOut,tplOut);
00063 else
00064 executeOnPHPList(new StatementList(phpIn),phpOut,tplOut);
00065 if (verbose>2) cout << "OUT EXECUTE" << endl;
00066
00067 leaveContext(phpOut,tplOut);
00068 };
00069
00070 void RuleContext::assignVariableNames(List *phpList, List *tplList){
00071 Node::VariableList vlist;
00072 tplList->getVariables(vlist);
00073 phpList->getVariables(vlist);
00074 Node::Assignment phpAssign;
00075 Node::Assignment tplAssign;
00076
00077 generateVariableNames(vlist,phpAssign,tplAssign);
00078
00079 phpList->substitute(phpAssign);
00080 tplList->substitute(tplAssign);
00081
00082 processSmartyAssignInPhpList(phpList);
00083 };
00084
00085 void RuleContext::processSmartyAssignInPhpList(List* phpList){
00086 List::iterator it = phpList->begin();
00087 List::iterator eit = phpList->end();
00088
00089 while (it!=eit){
00090 if ((*it)->getName() == CSmartyAssign)
00091 processSmartyAssign(dynamic_cast<SmartyAssign*>(*it));
00092 it++;
00093 };
00094
00095 };
00096
00097 void RuleContext::enterContext(List *phpOut, List *tplOut) {};
00098 void RuleContext::leaveContext(List *phpOut, List *tplOut) {};
00099
00100