1 /******************************************************************
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 ******************************************************************/
20 #include "QueryEngine.h"
21 #include "DataReader.h"
23 SSMRESULT CQueryEngine::finalConstruct()
25 SSMRESULT res = SSM_E_FAIL;
29 m_pQueryEngineEvent = NULL;
31 SSM_CLEANUP_ASSERT(CreateInstance(OID_ITasker, (IBase **)&m_pTasker));
33 SSM_CLEANUP_ASSERT(CreateGlobalInstance(OID_IPropagationEngine, (IBase **)&m_pPropagationEngine));
39 void CQueryEngine::finalRelease()
41 m_pQueryEngineEvent = NULL;
45 for (std::map<int, IConditionedQuery *>::iterator itor = m_conditionedQueries.begin();
46 itor != m_conditionedQueries.end(); ++itor)
48 itor->second->deactivateTriggers();
49 SAFE_RELEASE(itor->second);
52 for (std::map<int, CContextQuery *>::iterator itor = m_contextQueries.begin();
53 itor != m_contextQueries.end(); ++itor)
55 //Token *root = itor->second->getRoot();
57 SAFE_DELETE(itor->second);
60 m_mtxQueries.unlock();
63 SSMRESULT CQueryEngine::processQueryResult(IN int userTriggerId,
64 IN std::vector<result_model> *result)
66 SSMRESULT res = SSM_E_FAIL;
67 ModelPropertyVec modelData;
68 std::vector<result_model> result_model_data_id;
70 std::vector<std::string> contextName;
71 IContextModel *temp_contextmodel = NULL;
72 IContextModel *temp_contextmodel2 = NULL;
74 intptr_t *pData = NULL;
76 CDataReader *pDataReader = NULL;
80 if (m_contextQueries.find(userTriggerId) == m_contextQueries.end())
82 SSM_CLEANUP_ASSERT(SSM_E_FAIL);
85 m_contextQueries[userTriggerId]->check_result_model();
86 m_contextQueries[userTriggerId]->return_modelID(&modelID);
89 m_contextQueries[userTriggerId]->return_contextName(&contextName);
91 for (unsigned int i = 0; i < modelID.size(); i++)
93 SSM_CLEANUP_ASSERT(m_pPropagationEngine->getContextModel(contextName.at(i), &temp_contextmodel2));
95 for (unsigned int j = 0 ; j < result->size() ; j++)
98 if (result->at(j).dataId.size() <= 0)
103 int modelid = modelID.at(i);
104 std::vector<int> dataid;
105 for (unsigned int k = 0 ; k < result->at(j).dataId.size() ; k++)
107 SSM_CLEANUP_ASSERT(m_pPropagationEngine->getContextModel(result->at(j).modelName,
108 &temp_contextmodel));
109 data = result->at(j).dataId.at(k);
111 if (modelID.at(i) < result->at(j).modelID )
113 SSM_CLEANUP_ASSERT(temp_contextmodel->getParentDataId(data, temp_contextmodel2, &data));
114 dataid.push_back(data);
116 else if (modelID.at(i) > result->at(j).modelID )
118 SSM_CLEANUP_ASSERT(temp_contextmodel->getChildDataId(data, temp_contextmodel2, &dataid));
122 dataid.push_back(data);
124 SAFE_RELEASE(temp_contextmodel);
127 m_contextQueries[userTriggerId]->integrate_result(&result_model_data_id, modelid, &dataid,
128 temp_contextmodel2->getModelName());
131 SAFE_RELEASE(temp_contextmodel2);
134 pDataReader = new CDataReader();
136 for (unsigned int i = 0; i < result_model_data_id.size(); i++)
138 std::vector<CModelData *> modelDataSet;
140 for (unsigned int j = 0; j < (result_model_data_id)[i].dataId.size(); j++)
142 CModelData *pModelData = new CModelData();
143 IContextModel *pCM = NULL;
144 ModelPropertyVec modelPropertyVec;
146 SSM_CLEANUP_ASSERT(m_pPropagationEngine->getContextModel((result_model_data_id)[i].modelName,
148 SSM_CLEANUP_ASSERT(pCM->getModelData((result_model_data_id)[i].dataId[j], &modelPropertyVec));
149 pModelData->setDataId((result_model_data_id)[i].dataId[j]);
150 for (ModelPropertyVec::iterator itor = modelPropertyVec.begin();
151 itor != modelPropertyVec.end(); ++itor)
153 pModelData->addModelData(itor->propertyName, itor->propertyValue);
156 modelDataSet.push_back(pModelData);
161 SSM_CLEANUP_ASSERT(pDataReader->addModelData((result_model_data_id)[i].modelName, &modelDataSet));
163 pData = new intptr_t [3];
164 pData[0] = EVENT_TYPE_OUTER;
165 pData[1] = userTriggerId;
166 pData[2] = reinterpret_cast<intptr_t>(pDataReader);
168 m_pTasker->addTask(this, (void *)pData);
173 m_mtxQueries.unlock();
174 SAFE_RELEASE(temp_contextmodel);
175 SAFE_RELEASE(temp_contextmodel2);
179 SSMRESULT CQueryEngine::validateQueryResult(IN IConditionedQueryResult *pConditionedQueryResult,
180 OUT std::vector<result_model> *resultData)
182 SSMRESULT res = SSM_E_FAIL;
183 IContextModel *pContextModel = NULL;
184 IConditionedModel *pConditionedModel = NULL;
186 for (unsigned int i = 0 ; i < pConditionedQueryResult->getConditionedModelCount() ; i++)
188 std::vector<int> temp_dataID;
189 result_model temp_result;
190 SSM_CLEANUP_ASSERT(pConditionedQueryResult->getConditionedContextModel(i, &pConditionedModel));
191 SSM_CLEANUP_ASSERT(pConditionedModel->getAffectedData(&temp_dataID));
193 if (temp_dataID.size() == 0)
198 SSM_CLEANUP_ASSERT(pConditionedModel->getBaseContextModel(&pContextModel));
199 temp_result.modelID = pContextModel->getModelId();
200 temp_result.dataId = temp_dataID;
201 temp_result.modelName = pContextModel->getModelName();
202 resultData->push_back(temp_result);
203 SAFE_RELEASE(pConditionedModel);
204 SAFE_RELEASE(pContextModel);
207 if (resultData->size() == pConditionedQueryResult->getConditionedModelCount())
217 SAFE_RELEASE(pConditionedModel);
218 SAFE_RELEASE(pContextModel);
222 SSMRESULT CQueryEngine::onConditionedQueryEvent(IN int userTriggerId,
223 IN IConditionedQueryResult *pConditionedQueryResult)
225 SSMRESULT res = SSM_E_FAIL;
226 std::vector<result_model> result;
228 SSM_CLEANUP_ASSERT(validateQueryResult(pConditionedQueryResult, &result));
229 SSM_CLEANUP_ASSERT(processQueryResult(userTriggerId, &result));
235 void CQueryEngine::onExecute(void *pArg)
237 intptr_t *pData = (intptr_t *)pArg;
241 case EVENT_TYPE_INNER:
242 processQueryResult(pData[1], (std::vector<result_model> *)pData[2]);
245 case EVENT_TYPE_OUTER:
246 if (m_pQueryEngineEvent != NULL)
248 m_pQueryEngineEvent->onQueryEngineEvent(pData[1], (IDataReader *)pData[2]);
257 void CQueryEngine::onTerminate(void *pArg)
259 intptr_t *pData = (intptr_t *)pArg;
260 std::vector<result_model> *pResult = NULL;
261 CDataReader *pDataReader = NULL;
265 case EVENT_TYPE_INNER:
266 pResult = (std::vector<result_model> *)(((int *)pArg)[2]);
267 SAFE_DELETE(pResult);
270 case EVENT_TYPE_OUTER:
271 pDataReader = (CDataReader *)(((int *)pArg)[2]);
272 SAFE_DELETE(pDataReader);
278 SAFE_ARRAY_DELETE(pData);
281 SSMRESULT CQueryEngine::executeContextQuery(IN std::string contextQuery, OUT int *cqid)
283 SSMRESULT res = SSM_E_FAIL;
284 IConditionedQuery *pConditionedQuery = NULL;
285 IConditionedQueryResult *pConditionedQueryResult = NULL;
286 QueryCondition queryConditions;
287 CContextQuery *clsContextQuery = NULL;
289 CCQLParser cqlParser;
290 IContextModel::ActivationType queryCommandType;
292 cqlParser.parse(contextQuery, &token);
294 if (!cqlParser.check_grammer(&token))
300 clsContextQuery = new CContextQuery();
301 SSM_CLEANUP_NULL_ASSERT(clsContextQuery);
302 SSM_CLEANUP_ASSERT(clsContextQuery->initialize(token));
303 clsContextQuery->make_QueryCondition(&queryConditions);
305 if (CCQLParser::tolower(token.child_token[0].name) == "subscribe")
307 queryCommandType = IContextModel::ACTIVATION_TYPE_SUBSCRIBE;
311 queryCommandType = IContextModel::ACTIVATION_TYPE_GET;
314 SSM_CLEANUP_ASSERT(m_pPropagationEngine->createConditionedQuery(queryCommandType,
315 &queryConditions, this, &pConditionedQuery));
318 pConditionedQuery->addRef();
319 m_conditionedQueries[m_cqid] = pConditionedQuery;
320 m_contextQueries[m_cqid] = clsContextQuery;
321 m_mtxQueries.unlock();
323 if (pConditionedQuery->hasAllConditionedModels() == true)
325 std::vector<result_model> *pResult = NULL;
327 SSM_CLEANUP_ASSERT(pConditionedQuery->getConditionedQueryResult(&pConditionedQueryResult));
328 pResult = new std::vector<result_model>();
329 if (validateQueryResult(pConditionedQueryResult, pResult) == SSM_S_OK)
331 //We have valid data, let's deliver to application.
332 intptr_t *pData = new intptr_t [3];
333 pData[0] = EVENT_TYPE_INNER;
335 pData[2] = reinterpret_cast<intptr_t>(pResult);
337 m_pTasker->addTask(this, (void *)pData);
341 SAFE_DELETE(pResult);
342 if (queryCommandType == IContextModel::ACTIVATION_TYPE_GET)
344 //There is no valid data. let's request new one
345 SSM_CLEANUP_ASSERT(pConditionedQuery->activateTriggers(m_cqid));
351 if (queryCommandType == IContextModel::ACTIVATION_TYPE_GET)
353 //There is no models such name
354 SSM_CLEANUP_ASSERT(SSM_E_FAIL);
358 //Every subscribe command must request new data to models
359 if (queryCommandType == IContextModel::ACTIVATION_TYPE_SUBSCRIBE)
361 SSM_CLEANUP_ASSERT(pConditionedQuery->activateTriggers(m_cqid));
367 SAFE_RELEASE(pConditionedQuery);
368 SAFE_RELEASE(pConditionedQueryResult);
372 //TODO: Registration with multiple instance support
373 SSMRESULT CQueryEngine::registerQueryEvent(IN IQueryEngineEvent *pQueryEngineEvent)
375 m_pQueryEngineEvent = pQueryEngineEvent;
379 SSMRESULT CQueryEngine::unregisterQueryEvent(IN IQueryEngineEvent *pQueryEngineEvent)
381 if (m_pQueryEngineEvent == pQueryEngineEvent)
383 m_pQueryEngineEvent = NULL;
390 SSMRESULT CQueryEngine::killContextQuery(IN int cqid)
392 SSMRESULT res = SSM_E_FAIL;
394 std::map<int, IConditionedQuery *>::iterator itorConditionedQueries;
395 std::map<int, CContextQuery *>::iterator itorContextQuries;
398 itorConditionedQueries = m_conditionedQueries.find(cqid);
399 itorContextQuries = m_contextQueries.find(cqid);
401 if (itorConditionedQueries != m_conditionedQueries.end())
403 SSM_CLEANUP_ASSERT(itorConditionedQueries->second->deactivateTriggers());
404 itorConditionedQueries->second->release();
405 m_conditionedQueries.erase(itorConditionedQueries);
408 if (itorContextQuries != m_contextQueries.end())
410 SAFE_DELETE(itorContextQuries->second);
411 m_contextQueries.erase(itorContextQuries);
415 m_mtxQueries.unlock();