00001 // Copyright CERN 2012 - Developed in collaboration with GSI 00002 00003 #include <fesa-core/Sorting/SortingExpression.h> 00004 #include <fesa-core/Sorting/FieldExpression.h> 00005 #include <fesa-core/Sorting/ComplementExpression.h> 00006 #include <fesa-core/Sorting/UnionExpression.h> 00007 #include <fesa-core/Sorting/IntersectExpression.h> 00008 #include <fesa-core/Sorting/HomogeneousDevCol.h> 00009 00010 #include <cmw-log/Logger.h> 00011 00012 #include <iostream> 00013 00014 namespace 00015 { 00016 CMW::Log::Logger& logger = CMW::Log::LoggerFactory::getLogger("FESA.FWK.fesa-core.Sorting.SortingExpression"); 00017 } 00018 00019 namespace fesa 00020 { 00021 00022 // use to transmit the device collection to sub-class which need it (FieldExpresion for instance) 00023 std::vector<AbstractDevice*>* SortingExpression::pDevCol_ = NULL; 00024 00025 // NOTE actually the function below shall remove all blanks 00026 // within the string, and be invoked only once, when starting 00027 // the parser. 00028 void SortingExpression::removeBlanks(std::string * pS) 00029 { 00030 00031 // remove front-blanks 00032 00033 std::string::size_type n; 00034 do 00035 { 00036 n = pS->find(' '); 00037 if (n != std::string::npos) 00038 pS->erase(n, 1); 00039 } 00040 while (n != std::string::npos); 00041 00042 // remove trail-blanks 00043 00044 do 00045 { 00046 n = pS->rfind(' '); 00047 if (n != std::string::npos) 00048 pS->erase(n, 1); 00049 } 00050 while (n != std::string::npos); 00051 00052 } 00053 00054 void SortingExpression::setDeviceCollection(std::vector<AbstractDevice *>* pDevCol) 00055 { 00056 pDevCol_ = pDevCol; 00057 } 00058 00059 // match and tryMatch together form a recursive algorithm 00060 00061 SortingExpression * SortingExpression::tryMatch(SortingContext sc) 00062 { 00063 if(logger.isLoggable(CMW::Log::Level::LL_TRACE)) 00064 { 00065 std::ostringstream msg; 00066 msg << "Tries to match " << sc.formula_; 00067 LOG_TRACE_IF(logger, msg.str()); 00068 } 00069 00070 SortingExpression * pE; 00071 00072 pE = new FieldExpression(); 00073 00074 if (pE->match(sc)) 00075 { 00076 return pE; 00077 } 00078 00079 delete pE; 00080 00081 pE = new ComplementExpression(); 00082 00083 if (pE->match(sc)) 00084 { 00085 return pE; 00086 } 00087 00088 delete pE; 00089 00090 pE = new UnionExpression(); 00091 00092 if (pE->match(sc)) 00093 { 00094 return pE; 00095 } 00096 00097 delete pE; 00098 00099 pE = new IntersectExpression(); 00100 00101 if (pE->match(sc)) 00102 { 00103 return pE; 00104 } 00105 00106 delete pE; 00107 00108 return 0; 00109 00110 } 00111 00112 } // fesa