IntersectExpression.cpp

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

Generated on 18 Jan 2013 for Fesa by  doxygen 1.6.1