UnionExpression.cpp
Go to the documentation of this file.00001
00002
00003 #include <fesa-core/Sorting/UnionExpression.h>
00004
00005 #include <fesa-core/Sorting/HomogeneousDevCol.h>
00006
00007
00008 namespace fesa
00009 {
00010
00011 bool UnionExpression::match(SortingContext sc)
00012 {
00013 std::string unionExprCandidate = sc.formula_;
00014
00015 typedef enum
00016 {
00017 initState, parenthesesState, expr1State, unionState, expr2State, endState
00018 } automaton;
00019
00020 automaton state = initState, newState = initState;
00021
00022 std::string::size_type pos;
00023
00024 while (state != endState)
00025 {
00026
00027 switch (state)
00028 {
00029
00030 case initState:
00031 newState = parenthesesState;
00032 break;
00033
00034 case parenthesesState:
00035 pos = unionExprCandidate.find("(");
00036
00037 if (pos != 0)
00038 return false;
00039
00040 unionExprCandidate.erase(0, 1);
00041
00042 pos = unionExprCandidate.rfind(")");
00043
00044 if (pos != (unionExprCandidate.length() - 1))
00045 return false;
00046
00047 unionExprCandidate.erase(pos, 1);
00048
00049 newState = expr1State;
00050
00051 break;
00052
00053 case expr1State:
00054 {
00055 pos = unionExprCandidate.find("or(");
00056
00057 if ((pos == std::string::npos))
00058 return false;
00059
00060 std::string s1 = unionExprCandidate.substr(0, pos);
00061
00062 SortingContext aSc;
00063 aSc.formula_ = s1;
00064 aSc.className_ = sc.className_;
00065
00066 pExpr1_ = SortingExpression::tryMatch(aSc);
00067
00068 if (pExpr1_ == 0)
00069 {
00070 return false;
00071 }
00072
00073 unionExprCandidate.erase(0, pos + 2);
00074
00075 newState = unionState;
00076 break;
00077 }
00078
00079 case unionState:
00080 newState = expr2State;
00081 break;
00082
00083 case expr2State:
00084 {
00085 std::string s2 = unionExprCandidate;
00086
00087 SortingContext aSc;
00088 aSc.formula_ = s2;
00089 aSc.className_ = sc.className_;
00090
00091 pExpr2_ = SortingExpression::tryMatch(aSc);
00092
00093 if (pExpr2_ == 0)
00094 {
00095 return false;
00096 }
00097
00098 newState = endState;
00099 break;
00100
00101 }
00102
00103 case endState:
00104 break;
00105
00106 }
00107
00108 state = newState;
00109
00110 }
00111
00112 return true;
00113 }
00114
00115 std::set<HomogeneousDevCol*> UnionExpression::evaluate()
00116 {
00117
00118 std::set<HomogeneousDevCol*> resultDevCol;
00119
00120 std::set<HomogeneousDevCol*> set1 = pExpr1_->evaluate();
00121 std::set<HomogeneousDevCol*> set2 = pExpr2_->evaluate();
00122
00123 std::set<HomogeneousDevCol*>::const_iterator it1, it2;
00124
00125 for (it1 = set1.begin(); it1 != set1.end(); it1++)
00126 {
00127 for (it2 = set2.begin(); it2 != set2.end(); it2++)
00128 {
00129 HomogeneousDevCol* pResult = new HomogeneousDevCol();
00130 HomogeneousDevCol c1 = *(*it1);
00131 HomogeneousDevCol c2 = *(*it2);
00132 *pResult = c1 + c2;
00133 resultDevCol.insert(pResult);
00134 }
00135 }
00136
00137 return resultDevCol;
00138
00139 }
00140
00141 UnionExpression::UnionExpression():pExpr1_(0),pExpr2_(0){}
00142
00143 UnionExpression::~UnionExpression()
00144 {
00145 if (pExpr1_ != NULL)
00146 {
00147 delete pExpr1_;
00148 pExpr1_ = NULL;
00149 }
00150 if (pExpr2_ != NULL)
00151 {
00152 delete pExpr2_;
00153 pExpr2_ = NULL;
00154 }
00155 }
00156
00157 }