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 "PropagationEngine.h"
21 #include "ContextModel.h"
23 SSMRESULT CPropagationEngine::finalConstruct()
25 SSMRESULT res = SSM_E_FAIL;
27 SSM_CLEANUP_ASSERT(CreateGlobalInstance(OID_ITasker, (IBase **)&m_pTasker));
29 SSM_CLEANUP_ASSERT(CreateGlobalInstance(OID_IContextDataReader, (IBase **)&m_pContextDataReader));
31 SSM_CLEANUP_ASSERT(m_pContextDataReader->registerContextModelAccessor(this));
33 SSM_CLEANUP_ASSERT(CreateGlobalInstance(OID_IEvaluationEngine, (IBase **)&m_pEvaluationEngine));
35 SSM_CLEANUP_ASSERT(CreateGlobalInstance(OID_IContextRepository, (IBase **)&m_pContextRepository));
37 SSM_CLEANUP_ASSERT(m_pContextRepository->registerResourceFinderEvent(this));
39 SSM_CLEANUP_ASSERT(CreateGlobalInstance(OID_ISensingEngine, (IBase **)&m_pSensingEngine));
41 SSM_CLEANUP_ASSERT(initializeEngine());
47 void CPropagationEngine::finalRelease()
51 for (std::map<std::string, IContextModel *>::iterator itor = m_installedContextModel.begin();
52 itor != m_installedContextModel.end(); ++itor)
54 ((CContextModel *)itor->second)->registerContextModelEvent(NULL);
55 SAFE_RELEASE(itor->second);
58 for (VirtualModelMap::iterator itor = m_lookUpContextModel.begin();
59 itor != m_lookUpContextModel.end(); ++itor)
61 for (std::vector< std::pair<ModelConditionVec, IConditionedQuery *> >::iterator inneritor =
62 itor->second.begin(); inneritor != itor->second.end(); ++inneritor)
64 SAFE_RELEASE(inneritor->second);
69 SSMRESULT CPropagationEngine::findContextModel(std::string modelName,
70 IContextModel **ppContextModel)
72 SSMRESULT res = SSM_S_FALSE;
73 std::map<std::string, IContextModel *>::iterator idxContextModel;
75 m_mtxContextModelList.lock();
76 idxContextModel = m_installedContextModel.find(modelName);
78 if (idxContextModel != m_installedContextModel.end())
80 SSM_CLEANUP_ASSERT(idxContextModel->second->queryInterface(OID_IContextModel,
81 (IBase **)ppContextModel));
85 m_mtxContextModelList.unlock();
89 SSMRESULT CPropagationEngine::registerCondition(IContextModel *pContextModel,
90 ModelConditionVec *pConditions, IConditionedQuery *pConditionedQuery)
92 SSMRESULT res = SSM_E_FAIL;
93 IConditionedModel *pConditionedModel = NULL;
95 SSM_CLEANUP_ASSERT(pContextModel->createConditionedModel(pConditions, &pConditionedModel));
96 SSM_CLEANUP_ASSERT(pConditionedQuery->registerConditionedModel(pConditionedModel));
101 SAFE_RELEASE(pConditionedModel);
105 SSMRESULT CPropagationEngine::registerVirtualCondition(std::string virtualContextModelName,
106 ModelConditionVec *pConditions, IConditionedQuery *pConditionedQuery)
108 pConditionedQuery->addRef();
109 m_mtxLookUpList.lock();
110 m_lookUpContextModel[virtualContextModelName].push_back(std::make_pair(*pConditions,
112 m_mtxLookUpList.unlock();
116 SSMRESULT CPropagationEngine::propagateNewModel(std::string newModelName,
117 IContextModel *pContextModel)
119 SSMRESULT res = SSM_E_FAIL;
120 VirtualModelMap::iterator itorVModels;
122 m_mtxLookUpList.lock();
123 itorVModels = m_lookUpContextModel.find(newModelName);
125 if (itorVModels != m_lookUpContextModel.end())
127 for (std::vector< std::pair<ModelConditionVec, IConditionedQuery *> >::iterator itor =
128 itorVModels->second.begin(); itor != itorVModels->second.end(); ++itor)
130 SSM_CLEANUP_ASSERT(registerCondition(pContextModel, &itor->first, itor->second));
132 SAFE_RELEASE(itor->second);
135 m_lookUpContextModel.erase(itorVModels);
141 m_mtxLookUpList.unlock();
145 SSMRESULT CPropagationEngine::addResourceFromGetList()
147 SSMRESULT res = SSM_E_FAIL;
148 std::vector<ISSMResource *> contextModelList;
149 std::stringstream sstream;
151 m_pSensingEngine->getList(&contextModelList);
152 for (std::vector<ISSMResource *>::iterator itor = contextModelList.begin();
153 itor != contextModelList.end(); ++itor)
155 SSM_CLEANUP_ASSERT(installContextModelFromISSMResource(*itor));
164 //Install new resource and update Device table
165 SSMRESULT CPropagationEngine::installContextModelFromISSMResource(ISSMResource *pSSMResource)
167 SSMRESULT res = SSM_E_FAIL;
169 ModelProperty modelProperty;
170 ModelPropertyVec modelProperties;
171 CObject<CContextModel> *pContextModel;
173 std::string lifeTime;
175 switch ((res = findContextModel(pSSMResource->type.c_str(), (IContextModel **)&pContextModel)))
181 //no models using that name, create new one
183 for (std::vector<std::map<std::string, std::string> >::iterator itor =
184 pSSMResource->outputProperty.begin(); itor != pSSMResource->outputProperty.end();
187 if ((*itor)["name"] == "lifetime")
189 lifeTime = (*itor)["value"];
193 modelProperty.propertyName = (*itor)["name"];
194 if ((*itor)["type"] == "string")
196 modelProperty.propertyType = ModelProperty::TYPE_TEXT;
198 else if ((*itor)["type"] == "int")
200 modelProperty.propertyType = ModelProperty::TYPE_INTEGER;
202 else if ((*itor)["type"] == "double")
204 modelProperty.propertyType = ModelProperty::TYPE_REAL;
206 else if ((*itor)["type"] == "float")
208 modelProperty.propertyType = ModelProperty::TYPE_REAL;
212 modelProperty.propertyType = ModelProperty::TYPE_TEXT;
214 modelProperties.push_back(modelProperty);
217 //Install new resource
218 SSM_CLEANUP_ASSERT(installContextModel(m_pDeviceModel, IContextModel::CONSTRUCTION_TYPE_EXTERNAL,
219 pSSMResource->type.c_str(), &modelProperties, (IContextModel **)&pContextModel));
221 if (lifeTime.length() > 0)
223 pContextModel->setLifeTime(lifeTime);
226 modelProperties.clear();
233 //Update Device data to DB
234 SSM_CLEANUP_ASSERT(updateDeviceInfo(pSSMResource, &deviceId));
236 //TODO: Must be modified for restructuring
237 pContextModel->addSSMResourceAndDeviceDataId(pSSMResource->type, deviceId, pSSMResource);
242 SAFE_RELEASE(pContextModel);
246 SSMRESULT CPropagationEngine::updateDeviceInfo(ISSMResource *pSSMResource, int *deviceId)
248 SSMRESULT res = SSM_E_FAIL;
250 ModelProperty modelProperty;
251 ModelPropertyVec modelProperties;
252 std::map<std::string, int>::iterator itorSearchedDeviceId;
255 modelProperty.propertyName = "fname";
256 modelProperty.propertyType = ModelProperty::TYPE_TEXT;
257 modelProperty.propertyValue = pSSMResource->friendlyName;
258 modelProperties.push_back(modelProperty);
260 modelProperty.propertyName = "ip";
261 modelProperty.propertyType = ModelProperty::TYPE_TEXT;
262 modelProperty.propertyValue = pSSMResource->ip;
263 modelProperties.push_back(modelProperty);
265 modelProperty.propertyName = "version";
266 modelProperty.propertyType = ModelProperty::TYPE_TEXT;
267 modelProperty.propertyValue = "1.0";
268 modelProperties.push_back(modelProperty);
270 itorSearchedDeviceId = m_searchedSensorDataId.find(pSSMResource->ip);
272 if (itorSearchedDeviceId == m_searchedSensorDataId.end())
274 SSM_CLEANUP_ASSERT(m_pDeviceModel->addModelData(1, &modelProperties, &deviceDataId));
275 m_searchedSensorDataId[pSSMResource->ip] = deviceDataId;
279 deviceDataId = m_searchedSensorDataId[pSSMResource->ip];
282 if (deviceId != NULL)
283 *deviceId = deviceDataId;
291 SSMRESULT CPropagationEngine::installCurrentSensors()
293 SSMRESULT res = SSM_E_FAIL;
294 std::vector<ISSMResource *> contextModelList;
296 m_pSensingEngine->getList(&contextModelList);
298 for (std::vector<ISSMResource *>::iterator itor = contextModelList.begin();
299 itor != contextModelList.end(); ++itor)
301 SSM_CLEANUP_ASSERT(installContextModelFromISSMResource(*itor));
310 SSMRESULT CPropagationEngine::installContextModel(IContextModel *pParentModel,
311 IContextModel::ConstructionType constructionType, const char *modelName,
312 ModelPropertyVec *pModelDescs, IContextModel **ppContextModel)
314 SSMRESULT res = SSM_E_FAIL;
315 CObject<CContextModel> *pNewModel;
317 SSM_CLEANUP_ASSERT(CreateInstance(OID_IContextModel, (IBase **)&pNewModel));
318 SSM_CLEANUP_ASSERT(pNewModel->create(constructionType, pParentModel, modelName, pModelDescs));
319 SSM_CLEANUP_ASSERT(pNewModel->registerContextModelEvent(this));
322 m_mtxContextModelList.lock();
323 m_installedContextModel[modelName] = pNewModel;
324 m_mtxContextModelList.unlock();
326 SSM_CLEANUP_ASSERT(propagateNewModel(modelName, pNewModel));
330 SSM_CLEANUP_ASSERT(pNewModel->queryInterface(OID_IContextModel, (IBase **)ppContextModel));
334 SAFE_RELEASE(pNewModel);
338 SSMRESULT CPropagationEngine::cleanUpRemoteDeviceInfo()
340 SSMRESULT res = SSM_E_FAIL;
341 ModelConditionVec deviceCondition(1);
342 IConditionedModel *pRemoteDeviceInformation = NULL;
343 IntVec deviceDataIds;
345 deviceCondition[0].modelProperty.propertyName = "dataId";
346 deviceCondition[0].modelProperty.propertyValue = "1";
347 deviceCondition[0].modelProperty.propertyType = ModelProperty::TYPE_NUMERIC;
348 deviceCondition[0].predicate = ModelCondition::PREDICATE_GT;
349 SSM_CLEANUP_ASSERT(m_pDeviceModel->createConditionedModel(&deviceCondition,
350 &pRemoteDeviceInformation));
351 SSM_CLEANUP_ASSERT(pRemoteDeviceInformation->getAffectedData(&deviceDataIds));
353 for (unsigned int i = 0; i < deviceDataIds.size(); i++)
355 SSM_CLEANUP_ASSERT(m_pDeviceModel->deleteModelData(deviceDataIds[i]));
359 SAFE_RELEASE(pRemoteDeviceInformation);
363 void CPropagationEngine::onExecute(void *pArg)
365 SSMRESULT res = SSM_E_FAIL;
367 intptr_t *pMessage = (intptr_t *)pArg;
369 RESOURCE_EVENT_TYPE eventType = (RESOURCE_EVENT_TYPE)pMessage[0];
370 ISSMResource *pResource = (ISSMResource *)pMessage[1];
374 case SSM_EVENT_NORMAL:
375 SSM_CLEANUP_ASSERT(addResourceFromGetList());
378 case SSM_EVENT_ADDED:
379 SSM_CLEANUP_ASSERT(installContextModelFromISSMResource(pResource));
396 void CPropagationEngine::onTerminate(void *pArg)
398 intptr_t *pMessage = (intptr_t *)pArg;
403 int CPropagationEngine::onResourceEvent(RESOURCE_EVENT_TYPE eventType,
404 ISSMResource *pSSMResource, std::string info)
406 intptr_t *pMessage = new intptr_t [2];
408 pMessage[0] = eventType;
409 pMessage[1] = reinterpret_cast<intptr_t>(pSSMResource);
410 return (int)m_pTasker->addTask(this, (void *)pMessage);
413 SSMRESULT CPropagationEngine::initializeEngine()
415 SSMRESULT res = SSM_E_FAIL;
416 CObject<CContextModel> *pRootModel = NULL;
417 ModelPropertyVec rootModelProperties(1);
419 ModelPropertyVec deviceModelProperties(3);
421 ISSMResource ssmResource;
423 SSM_CLEANUP_ASSERT(m_pEvaluationEngine->initializeEngine());
425 //Create root model (Root)
426 rootModelProperties[0].propertyName = "name";
427 rootModelProperties[0].propertyType = ModelProperty::TYPE_TEXT;
429 SSM_CLEANUP_ASSERT(CreateInstance(OID_IContextModel, (IBase **)&pRootModel));
431 SSM_CLEANUP_ASSERT(pRootModel->create(IContextModel::CONSTRUCTION_TYPE_INTERNAL, NULL,
432 "root", &rootModelProperties));
433 SSM_CLEANUP_ASSERT(pRootModel->queryInterface(OID_IContextModel, (IBase **)&m_pRootModel));
435 //Create basic model (Device)
436 deviceModelProperties[0].propertyName = "fname";
437 deviceModelProperties[0].propertyType = ModelProperty::TYPE_TEXT;
438 deviceModelProperties[1].propertyName = "ip";
439 deviceModelProperties[1].propertyType = ModelProperty::TYPE_TEXT;
440 deviceModelProperties[2].propertyName = "version";
441 deviceModelProperties[2].propertyType = ModelProperty::TYPE_TEXT;
443 SSM_CLEANUP_ASSERT(installContextModel(m_pRootModel, IContextModel::CONSTRUCTION_TYPE_INTERNAL,
445 &deviceModelProperties, &m_pDeviceModel));
447 ssmResource.ip = "coap://127.0.0.1/";
448 ssmResource.friendlyName = "MyDevice";
450 SSM_CLEANUP_ASSERT(updateDeviceInfo(&ssmResource, NULL));
452 SSM_CLEANUP_ASSERT(installCurrentSensors());
455 SAFE_RELEASE(pRootModel);
459 void CPropagationEngine::terminateEngine()
461 SSMRESULT res = SSM_E_FAIL;
463 SSM_CLEANUP_ASSERT(cleanUpRemoteDeviceInfo());
469 SSMRESULT CPropagationEngine::getContextModel(std:: string modelName,
470 IContextModel **ppContextModel)
472 SSMRESULT res = SSM_E_FAIL;
474 res = findContextModel(modelName, ppContextModel);
476 return res == SSM_S_OK ? SSM_S_OK : SSM_E_FAIL;
479 SSMRESULT CPropagationEngine::createConditionedQuery(IContextModel::ActivationType
480 activationType, QueryCondition *pQueryCondition, IConditionedQueryEvent *pEvent,
481 IConditionedQuery **ppConditionedQuery)
483 SSMRESULT res = SSM_E_FAIL;
484 CObject<CConditionedQuery> *pConditionedQuery;
485 IContextModel *pContextModel = NULL;
487 SSM_CLEANUP_ASSERT(CreateInstance(OID_IConditionedQuery, (IBase **)&pConditionedQuery));
489 SSM_CLEANUP_ASSERT(pConditionedQuery->create(activationType, pQueryCondition->size()));
490 for (QueryCondition::iterator itor = pQueryCondition->begin();
491 itor != pQueryCondition->end(); ++itor)
493 switch (findContextModel(itor->first, &pContextModel))
496 SSM_CLEANUP_ASSERT(registerCondition(pContextModel, &itor->second, pConditionedQuery));
500 SSM_CLEANUP_ASSERT(registerVirtualCondition(itor->first, &itor->second, pConditionedQuery));
507 SAFE_RELEASE(pContextModel);
510 SSM_CLEANUP_ASSERT(pConditionedQuery->registerQueryConditionEvent(pEvent));
512 SSM_CLEANUP_ASSERT(pConditionedQuery->queryInterface(OID_IConditionedQuery,
513 (IBase **)ppConditionedQuery));
516 SAFE_RELEASE(pConditionedQuery);
517 SAFE_RELEASE(pContextModel);
521 SSMRESULT CPropagationEngine::onModelStatusChange(IContextModel::Status newStatus,
522 ISSMResource *pSSMResource, IContextModel *pModel)
526 case IContextModel::STATUS_ACTIVATE:
528 m_pSensingEngine->registerContext(SSM_REPEAT, pSSMResource, (CContextModel *)pModel);
531 case IContextModel::STATUS_DEACTIVATE:
532 m_pSensingEngine->unregisterContext(SSM_REPEAT, pSSMResource, (CContextModel *)pModel);
533 //pModel->CleanUpModelData();
537 case IContextModel::STATUS_START_READ_VALUE:
538 //Model must be released from OnEvent callType
540 m_pSensingEngine->registerContext(SSM_ONCE, pSSMResource, (CContextModel *)pModel);
543 case IContextModel::STATUS_STOP_READ_VALUE:
554 SSMRESULT CPropagationEngine::onQueryContextModel(std::string modelName,
555 IContextModel **ppContextModel)
557 std::map<std::string, IContextModel *>::iterator itorContextModel;
559 itorContextModel = m_installedContextModel.find(modelName);
561 if (itorContextModel == m_installedContextModel.end())
566 return itorContextModel->second->queryInterface(OID_IContextModel, (IBase **)ppContextModel);