[SSM] Fix context query parser issues
authorjk13 <jihyeok13.kim@samsung.com>
Tue, 6 Jan 2015 00:33:35 +0000 (09:33 +0900)
committerSudarshan Prasad <sudarshan.prasad@intel.com>
Thu, 22 Jan 2015 08:28:46 +0000 (08:28 +0000)
This patch resolves issues that related to context query

which occures infinite loop and crash

Change-Id: I5acd9c57ee82a7ba1d3a5a416680a23676acfabb
Signed-off-by: jk13 <jihyeok13.kim@samsung.com>
(cherry picked from commit ba9725edbd9dccf87e8e57184e440168122b2cac)
Reviewed-on: https://gerrit.iotivity.org/gerrit/205
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Sudarshan Prasad <sudarshan.prasad@intel.com>
14 files changed:
service/soft-sensor-manager/SDK/cpp/build/linux/Makefile
service/soft-sensor-manager/SSMCore/build/linux/Makefile
service/soft-sensor-manager/SSMCore/src/Common/PlatformLayer.h
service/soft-sensor-manager/SSMCore/src/QueryProcessor/CQLParser.cpp
service/soft-sensor-manager/SSMCore/src/QueryProcessor/CQLParser.h
service/soft-sensor-manager/SSMCore/src/QueryProcessor/ConditionedQuery.cpp
service/soft-sensor-manager/SSMCore/src/QueryProcessor/ContextQuery.cpp
service/soft-sensor-manager/SSMCore/src/QueryProcessor/EvaluationEngine.cpp
service/soft-sensor-manager/SSMCore/src/QueryProcessor/QueryEngine.cpp
service/soft-sensor-manager/SSMCore/src/SensorProcessor/ContextExecutor.cpp
service/soft-sensor-manager/SSMCore/src/SensorProcessor/ContextRepository.cpp
service/soft-sensor-manager/SSMCore/src/SensorProcessor/ContextRepository.h
service/soft-sensor-manager/SSMCore/src/SensorProcessor/SensingEngine.h
service/soft-sensor-manager/SampleApp/linux/SSMTesterApp/build/Makefile

index 918471b..495c640 100644 (file)
@@ -12,7 +12,7 @@ GAR=ar
 TARGET=${SSM_LIB}
 
 CXX=g++
-CXX_FLAGS=-std=c++0x -Wall -pthread -DLINUX
+CXX_FLAGS=-std=c++0x -Wall -pthread -DLINUX -DNDEBUG
 CXX_INC=-I../../ -I${INC_PATH}/ -I${FD_SSMCORE}/include -I${FD_SSMCORE}/src -I${BOOST}
 CXX_LIB= 
 
index 01d6453..8cf72ba 100644 (file)
@@ -12,7 +12,7 @@ EXCLUDE_LIST=SSMCore_JNI.cpp
 
 # C++ type Compile Flag define.
 CXX=g++
-CXX_FLAGS=-std=c++0x -Wall -pthread -DLINUX -ldl
+CXX_FLAGS=-std=c++0x -Wall -pthread -DLINUX -ldl -DNDEBUG
 CXX_INC=-I${INC_PATH}/ -I${SRC_PATH}/ -I${IOT_BASE}/include/ -I${IOT_LOG_DIR}/include/ -I${IOT_BASE}/csdk/stack/include -I${IOT_BASE}/csdk/ocsocket/include -I${IOT_BASE}/csdk/ocrandom/include -I${IOT_BASE}/csdk/logger/include -I${BOOST}
 CXX_LIB=-L""
 
@@ -24,7 +24,7 @@ CXX_OBJLIST=${CXX_USESRCS:.cpp=.o}
 
 # C type Compile Flag define.
 GCC=gcc
-GCC_FLAGS=-Wall -pthread -DLINUX -ldl
+GCC_FLAGS=-Wall -pthread -DLINUX -ldl -DNDEBUG
 GCC_INC=-I../../ -I${INC_PATH}/ -I${IOT_BASE}/include/ -I${IOT_BASE}/csdk/stack/include -I${IOT_BASE}/csdk/ocsocket/include -I${IOT_BASE}/csdk/ocrandom/include -I${IOT_BASE}/csdk/logger/include -I${BOOST} -I${SRC_PATH} -I${SRC_PATH}/Common -I${SRC_PATH}/QueryProcessor -I${SRC_PATH}/SensorProcessor -I${SRC_PATH}/SSMInterface
 
 GCC_SRCPATH=${wildcard ${SRC_PATH}/**/*.c}
index 5e458e0..4ed125c 100644 (file)
@@ -65,12 +65,12 @@ void ReportMessage(const char *tag, const char *msg);
 #elif defined(TIZEN)
 
 #define REPORT_MESSAGE(tag, msg) printf("[%s] %s\n", tag, msg)
-#define PRINT_LOG(strError) printf("[SSM] %s:%d %s\n", __FUNCTION__, __LINE__, strError)
+#define PRINT_LOG(strError) printf("[SSM] %s:%d %s\n", __PRETTY_FUNCTION__, __LINE__, strError)
 
 #else //Default linux
 
 #define REPORT_MESSAGE(tag, msg) printf("[%s] %s\n", tag, msg)
-#define PRINT_LOG(strError) printf("[SSM] %s:%d %s\n", __FUNCTION__, __LINE__, strError)
+#define PRINT_LOG(strError) printf("[SSM] %s:%d %s\n", __PRETTY_FUNCTION__, __LINE__, strError)
 
 #endif
 
index 7ba46e7..ae2896f 100644 (file)
@@ -51,11 +51,12 @@ std::vector<std::string> CCQLParser::tokenize(IN const std::string &input)
     return tokens_temp;
 }
 
-void CCQLParser::parse(IN std::string input, OUT Token *root)
+bool CCQLParser::parse(IN std::string input, OUT Token *root)
 {
     std::vector<std::string> tokens = tokenize(input);
     bool flag;//get,sub,if
     bool value_flag = false;
+    bool isCondition = false;
 
     for (unsigned int i = 0 ; i < tokens.size() ; i++)
     {
@@ -82,6 +83,7 @@ void CCQLParser::parse(IN std::string input, OUT Token *root)
                 i++;
                 //temp1 = temp;
                 flag = false;
+                isCondition = true;
             }
             else
             {
@@ -95,6 +97,7 @@ void CCQLParser::parse(IN std::string input, OUT Token *root)
                 if (tokens.at(i) == "if")
                 {
                     flag = false;
+                    isCondition = true;
                 }
                 else
                 {
@@ -102,6 +105,7 @@ void CCQLParser::parse(IN std::string input, OUT Token *root)
                 }
             }
 
+            SORT lastType = Command;
             while (1)
             {
                 //int count = 0;
@@ -110,6 +114,7 @@ void CCQLParser::parse(IN std::string input, OUT Token *root)
                 if (value_flag == true)
                 {
                     value_flag = false;
+                    lastType = Context;
                     i++;
                 }
 
@@ -164,8 +169,13 @@ void CCQLParser::parse(IN std::string input, OUT Token *root)
                 else
                 {
                     temp1.type = Context;
-                }
 
+                    //if current context token is reserved keyword, that return error
+                    if (tolower(tokens.at(i)) == "get" || tolower(tokens.at(i)) == "subscribe")
+                    {
+                        return false;
+                    }
+                }
 
                 if (flag == true)
                 {
@@ -175,20 +185,42 @@ void CCQLParser::parse(IN std::string input, OUT Token *root)
                         temp1.type = And_or;
                         flag = false;
                     }
-                    split(tokens.at(i), &temp1, flag );//false -> Property
-                    temp.child_token.push_back(temp1);
+
+                    if (split(tokens.at(i), &temp1, flag ) != true) //false -> Property
+                    {
+                        return false;
+                    }
                 }
                 else
                 {
-                    split(tokens.at(i), &temp1, flag , tokens.at(i + 1), tokens.at(i + 2)); //false -> Property
+                    //token count not matched, return and let grammer checker detects
+                    if (tokens.size() < i + 3)
+                    {
+                        return false;
+                    }
+
+                    if (split(tokens.at(i), &temp1, flag , tokens.at(i + 1),
+                              tokens.at(i + 2)) != true) //false -> Property
+                    {
+                        return false;
+                    }
                     flag = true;
-                    temp.child_token.push_back(temp1);
                 }
 
+                if (isCondition && lastType == temp1.type)
+                {
+                    return false;
+                }
+
+                lastType = temp1.type;
+
+                temp.child_token.push_back(temp1);
             }
             root->child_token.push_back(temp);
         }
     }
+
+    return true;
 }
 
 std::string CCQLParser::tolower(IN std::string str)
@@ -218,6 +250,9 @@ std::vector<std::string> CCQLParser::getTokens(IN const std::string &str,
         pos = str.find_first_of(delimiters, lastPos);
     }
 
+    if (tokens.size() == 0)
+        tokens.push_back("\"\"");
+
     return tokens;
 }
 
@@ -248,7 +283,7 @@ void CCQLParser::check_index(IN std::string input, OUT Token *token)
     }
 }
 
-void CCQLParser::split(IN std::string input, IN Token *root, bool flag, IN std::string arg1,
+bool CCQLParser::split(IN std::string input, IN Token *root, bool flag, IN std::string arg1,
                        IN std::string arg2)
 {
     std::vector<std::string> tokens = getTokens(input, ".");
@@ -280,7 +315,7 @@ void CCQLParser::split(IN std::string input, IN Token *root, bool flag, IN std::
                     //root->child_token.push_back(temp1);
                 }
 
-                else if (check_number(arg2))
+                else
                 {
                     //temp1->number = atof(arg2.c_str());
 
@@ -293,6 +328,11 @@ void CCQLParser::split(IN std::string input, IN Token *root, bool flag, IN std::
                     {
                         property.propertyType = ModelProperty::TYPE_REAL;
                     }
+                    else
+                    {
+                        //unknown type raise error
+                        return false;
+                    }
                     property.propertyValue = arg2;
 
                     temp_token->model_property = property;
@@ -307,44 +347,44 @@ void CCQLParser::split(IN std::string input, IN Token *root, bool flag, IN std::
                 temp_token->child_token.push_back(temp1);
                 temp_token = &(temp_token->child_token.back());
             }
-
         }
     }
+
+    return true;
 }
 
 int CCQLParser::check_number(IN std::string &str)
 {
-    //int flag = 0; // 0 text /1 integer /2 real
+    int flag = 0; // 0 text /1 integer /2 real
+    int dotCount = 0;
 
-    int flag = 0;
-    for (unsigned int i = 0 ; i < str.size(); i++)
+    std::string::const_iterator it = str.begin();
+    while (it != str.end())
     {
-        if (str[i] == '.')
-        {
-            flag++;
-        }
-        else if (isdigit(str[i]))
+        if (*it == '.')
         {
-
+            dotCount++;
         }
-        else
+        else if (isdigit(*it) == false)
         {
-            return TYPETEXT;
+            //this is text
+            dotCount = 2;
+            break;
         }
-    }
 
-    if (flag == 1)
-    {
-        return TYPEREAL;
+        ++it;
     }
-    else if (flag > 1)
+
+    if (dotCount == 0)
     {
-        return TYPETEXT;
+        flag = 1;
     }
-    else
+    else if (dotCount == 1)
     {
-        return TYPEINTEGER;
+        flag = 2;
     }
+
+    return flag;
 }
 
 std::string CCQLParser::check_Predicate(IN std::string input)
index 609cec7..aa55257 100644 (file)
@@ -73,12 +73,12 @@ class CCQLParser
         *
         * @param [in] std::string input - Entered ContetxQuery
         * @param [out] Token* root - parsed result
-        * @return none
+        * @return bool
         * @warning
         * @exception
         * @see
         */
-        void parse(IN std::string input, OUT Token *root);
+        bool parse(IN std::string input, OUT Token *root);
 
         /**
         * @fn tolower
@@ -153,12 +153,12 @@ class CCQLParser
         * @param [in] bool flag - flag to distinguish token
         * @param [in] std::string arg1 - next token, default = ""
         * @param [in] std::string arg2 - next two token, default = ""
-        * @return none
+        * @return bool
         * @warning
         * @exception
         * @see
         */
-        void split(IN std::string input, IN Token *root, IN bool flag, IN std::string arg1 = "",
+        bool split(IN std::string input, IN Token *root, IN bool flag, IN std::string arg1 = "",
                    IN std::string arg2 = "");
 
         /**
index f7de567..85bbd6a 100644 (file)
@@ -142,6 +142,7 @@ SSMRESULT CConditionedQuery::registerConditionedModel(IN IConditionedModel *pCon
 
     if (hasAllConditionedModels() == true && m_reservedForActivate)
     {
+        m_reservedForActivate = false;
         SSM_CLEANUP_ASSERT(activateTriggers(m_userTriggerId));
     }
 
@@ -212,6 +213,11 @@ SSMRESULT CConditionedQuery::deactivateTriggers()
         SAFE_RELEASE(pBaseContextModel);
     }
 
+    if (m_reservedForActivate == true)
+    {
+        res = SSM_S_OK;
+    }
+
 CLEANUP:
     SAFE_RELEASE(pBaseContextModel);
     return res;
index dbf42c8..0fa9a79 100644 (file)
@@ -143,9 +143,10 @@ void CContextQuery::check_result_model()
 
         }
 
+        unsigned int cnt = 0;
+
         while (true)
         {
-            unsigned int cnt = 0;
             if ((unsigned int)cnt > min_cnt - 1)
             {
                 break;
@@ -206,18 +207,19 @@ void CContextQuery::return_modelID(OUT std::vector<int> *vector_int)
 {
     int k = m_root.child_token.at(0).child_token.size();
 
+    IContextModel *pContextModel = NULL;
+    int pModel = 0;
+
     for (int i = 0; i < k; i++)
     {
-
         Token *temp = &(m_root.child_token.at(0).child_token.at(i));
-
-        int pModel = 0;
-        IContextModel *contextmodel;
-
-        m_pPropagationEngine->getContextModel(search_last_modelName(temp), &contextmodel);
-        pModel = contextmodel->getModelId();
-        vector_int->push_back(pModel);
-        SAFE_RELEASE(contextmodel);
+        if (m_pPropagationEngine->getContextModel(search_last_modelName(temp),
+                &pContextModel) == SSM_S_OK)
+        {
+            pModel = pContextModel->getModelId();
+            vector_int->push_back(pModel);
+        }
+        SAFE_RELEASE(pContextModel);
     }
 }
 
index 1e102c2..b2d1c6f 100644 (file)
@@ -291,8 +291,7 @@ SSMRESULT CEvaluationEngine::createModel(IN int parentModelId, IN const char *ne
                 break;
 
             default:
-                res = SSM_E_FAIL;
-                goto CLEANUP;
+                SSM_CLEANUP_ASSERT(SSM_E_FAIL);
         }
     }
 
@@ -366,8 +365,7 @@ SSMRESULT CEvaluationEngine::addModelData(IN int modelId, IN int parentModelId,
                 break;
 
             default:
-                res = SSM_E_FAIL;
-                goto CLEANUP;
+                SSM_CLEANUP_ASSERT(SSM_E_FAIL);
         }
 
         i++;
@@ -451,8 +449,7 @@ SSMRESULT CEvaluationEngine::updateModelData(IN int modelId, IN int dataId,
                 break;
 
             default:
-                res = SSM_E_FAIL;
-                goto CLEANUP;
+                SSM_CLEANUP_ASSERT(SSM_E_FAIL);
         }
 
         i++;
@@ -795,8 +792,7 @@ SSMRESULT CEvaluationEngine::getConditionedModelData(IN int modelId,
                 break;
 
             default:
-                res = SSM_E_FAIL;
-                goto CLEANUP;
+                SSM_CLEANUP_ASSERT(SSM_E_FAIL);
         }
 
         switch (itor->modelProperty.propertyType)
@@ -812,8 +808,7 @@ SSMRESULT CEvaluationEngine::getConditionedModelData(IN int modelId,
                 break;
 
             default:
-                res = SSM_E_FAIL;
-                goto CLEANUP;
+                SSM_CLEANUP_ASSERT(SSM_E_FAIL);
         }
 
         if (itor < pModelConditions->end() - 1)
@@ -878,8 +873,7 @@ SSMRESULT CEvaluationEngine::watchModelData(IN int modelId, IN ModelConditionVec
                 break;
 
             default:
-                res = SSM_E_FAIL;
-                goto CLEANUP;
+                SSM_CLEANUP_ASSERT(SSM_E_FAIL);
         }
 
         switch (itor->modelProperty.propertyType)
@@ -895,8 +889,7 @@ SSMRESULT CEvaluationEngine::watchModelData(IN int modelId, IN ModelConditionVec
                 break;
 
             default:
-                res = SSM_E_FAIL;
-                goto CLEANUP;
+                SSM_CLEANUP_ASSERT(SSM_E_FAIL);
         }
 
         if (itor < pModelConditions->end() - 1)
index 100de71..59e130b 100644 (file)
@@ -83,10 +83,8 @@ SSMRESULT CQueryEngine::processQueryResult(IN int userTriggerId,
     }
 
     m_contextQueries[userTriggerId]->check_result_model();
-    m_contextQueries[userTriggerId]->return_modelID(&modelID);
-
-
     m_contextQueries[userTriggerId]->return_contextName(&contextName);
+    m_contextQueries[userTriggerId]->return_modelID(&modelID);
 
     for (unsigned int i = 0; i < modelID.size(); i++)
     {
@@ -289,12 +287,14 @@ SSMRESULT CQueryEngine::executeContextQuery(IN std::string contextQuery, OUT int
     CCQLParser              cqlParser;
     IContextModel::ActivationType   queryCommandType;
 
-    cqlParser.parse(contextQuery, &token);
+    if (!cqlParser.parse(contextQuery, &token))
+    {
+        SSM_CLEANUP_ASSERT(SSM_E_FAIL);
+    }
 
     if (!cqlParser.check_grammer(&token))
     {
-        res = SSM_E_FAIL;
-        goto CLEANUP;
+        SSM_CLEANUP_ASSERT(SSM_E_FAIL);
     }
 
     clsContextQuery = new CContextQuery();
index 668421f..368ab84 100644 (file)
@@ -31,8 +31,6 @@ SSMRESULT CContextExecutor::finalConstruct()
 
     SSM_CLEANUP_ASSERT(m_pContextRepository->registerResourceFinderEvent(this));
 
-    SSM_CLEANUP_ASSERT(m_pContextRepository->startResourceFinder());
-
 CLEANUP:
     return res;
 }
@@ -294,9 +292,8 @@ int CContextExecutor::onEvent(std::string type, TypeofEvent callType,
             runLogic(inputData, softSensorName);
         }
     }
-    else //This data is primitive
+    else if (m_registeredResources.find(type) != m_registeredResources.end()) //This data is primitive
     {
-        //TODO: Temporally added for primitive data testing
         addOutput(ctxData);
     }
 
@@ -308,6 +305,10 @@ void  CContextExecutor::unregisterContext(TypeofEvent callType, ISSMResource *pS
 {
     std::vector<ISSMResource *> baseList;
 
+    //////////
+    ///TODO: Need to clean up m_mapResourceLookup list
+    //////////
+
     //This is primitive sensor
     if (pSSMResource->inputList.size() == 0)
     {
@@ -396,4 +397,4 @@ void CContextExecutor::runLogic(std::vector<ContextData> inputData, std::string
         m_ctxEventList[softSensor]->onCtxEvent(SPF_START, inputData);
     }
     m_mtxLibraryIO.unlock();
-}
\ No newline at end of file
+}
index d4b133d..767f6c8 100644 (file)
@@ -77,7 +77,7 @@ SSMRESULT CContextRepository::initRepository(IN std::string name, IN std::string
 
     if (pathSoftSensors.length() == 0)
     {
-        SSM_CLEANUP_ASSERT(GetCurrentPath(&m_pathSoftSensors));
+        SSM_CLEANUP_ASSERT(getCurrentPath(&m_pathSoftSensors));
         m_pathSoftSensors.append("/");
     }
     else
@@ -87,7 +87,7 @@ SSMRESULT CContextRepository::initRepository(IN std::string name, IN std::string
 
     if (pathDescription.length() == 0)
     {
-        SSM_CLEANUP_ASSERT(GetCurrentPath(&m_pathSoftSensorsDescription));
+        SSM_CLEANUP_ASSERT(getCurrentPath(&m_pathSoftSensorsDescription));
         m_pathSoftSensorsDescription.append("/");
         m_pathSoftSensorsDescription.append(DEFAULT_PATH_SOFT_SENSORS);
     }
@@ -384,7 +384,7 @@ CLEANUP:
     return res;
 }
 
-SSMRESULT CContextRepository::GetCurrentPath(std::string *path)
+SSMRESULT CContextRepository::getCurrentPath(std::string *path)
 {
     char        buffer[2048];
     SSMRESULT   res = SSM_E_FAIL;
index d3e13e3..d822679 100644 (file)
@@ -147,6 +147,6 @@ class CContextRepository :
         SSMRESULT loadXMLFromFile(IN std::string descriptionFilePath,
                                   IN std::vector<DictionaryData> *dataList);
         SSMRESULT loadXMLFromString(IN char *xmlData, IN std::vector<DictionaryData> *dataList);
-        SSMRESULT GetCurrentPath(OUT std::string *path);
+        SSMRESULT getCurrentPath(OUT std::string *path);
 };
 #endif
\ No newline at end of file
index 7f6827a..f2c42a7 100644 (file)
@@ -68,6 +68,13 @@ class CSensingEngine :
                 *ppObject = pBase;
                 return SSM_S_OK;
             }
+            else if (IsEqualOID(objectID, OID_IContextRepository))
+            {
+                IBase *pBase = this;
+                pBase->addRef();
+                *ppObject = m_pContextRepository;
+                return SSM_S_OK;
+            }
 
             return SSM_E_NOINTERFACE;
         }
index abf7cf7..aa3b797 100644 (file)
@@ -10,7 +10,7 @@ RST_NAME=release
 TARGET=SSMTesterApp
 
 CXX=g++
-CXX_FLAGS=-std=c++0x -Wall -DLINUX
+CXX_FLAGS=-std=c++0x -Wall -DLINUX -DNDEBUG
 CXX_INC=-I${SRC_PATH}/ -I${INC_PATH}/ -I${OUTPUTS_DIR} -I${BOOST}