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 "ContextModel.h"
21 #include "ConditionedModel.h"
23 SSMRESULT CContextModel::finalConstruct()
25 SSMRESULT res = SSM_E_FAIL;
26 ModelProperty defaultModelProperty;
28 SSM_CLEANUP_ASSERT(CreateGlobalInstance(OID_ITasker, (IBase **)&m_pTasker));
30 SSM_CLEANUP_ASSERT(CreateGlobalInstance(OID_IEvaluationEngine, (IBase **)&m_pEvaluationEngine));
34 m_pContextModelEvent = NULL;
36 m_activationCount = 0;
38 m_constructionType = CONSTRUCTION_TYPE_INTERNAL;
40 m_secLifeTime = "2147483647";
42 defaultModelProperty.propertyName = "dataId";
43 defaultModelProperty.propertyType = ModelProperty::TYPE_INTEGER;
44 m_modelProperties.push_back(defaultModelProperty);
46 defaultModelProperty.propertyName = "lastTime";
47 defaultModelProperty.propertyType = ModelProperty::TYPE_INTEGER;
48 m_modelProperties.push_back(defaultModelProperty);
50 defaultModelProperty.propertyName = "lifetime";
51 defaultModelProperty.propertyType = ModelProperty::TYPE_INTEGER;
52 m_modelProperties.push_back(defaultModelProperty);
54 defaultModelProperty.propertyName = "available";
55 defaultModelProperty.propertyType = ModelProperty::TYPE_TEXT;
56 m_modelProperties.push_back(defaultModelProperty);
62 void CContextModel::finalRelease()
64 SSMRESULT res = SSM_E_FAIL;
65 std::map<int, IntVec>::iterator itorDeviceId = m_mapDeviceDataIds.begin();
66 m_pContextModelEvent = NULL;
68 for (unsigned int i = 0; i < m_mapDeviceDataIds.size(); i++)
71 if (itorDeviceId->first > 1)
73 for (unsigned int j = 0; j < itorDeviceId->second.size(); j++)
75 SSM_CLEANUP_ASSERT(deleteModelData(itorDeviceId->second[j]));
82 //for(std::vector<IConditionedModel*>::iterator itor = m_ConditionedModels.begin();
83 // itor != m_ConditionedModels.end(); ++itor)
85 // (*itor)->Release();
92 void CContextModel::registerSSMResource(ActivationType activationType, int targetDeviceDataId,
93 ISSMResource *pSSMResource)
95 intptr_t *pData = NULL;
97 m_mtxActivationCount.lock();
98 switch (activationType)
100 case ACTIVATION_TYPE_SUBSCRIBE:
101 if (m_mapSubscribedDevice.find(targetDeviceDataId) == m_mapSubscribedDevice.end())
103 pData = new intptr_t [2];
104 pData[0] = STATUS_ACTIVATE;
105 pData[1] = reinterpret_cast<intptr_t>(pSSMResource);
106 m_pTasker->addTask(this, (void *)pData);
107 m_mapSubscribedDevice[targetDeviceDataId] = 1;
111 m_mapSubscribedDevice[targetDeviceDataId]++;
115 case ACTIVATION_TYPE_GET:
116 if (m_mapSubscribedDevice.find(targetDeviceDataId) == m_mapSubscribedDevice.end())
118 if (m_mapGetDevice.find(targetDeviceDataId) == m_mapGetDevice.end())
120 pData = new intptr_t[2];
121 pData[0] = STATUS_START_READ_VALUE;
122 pData[1] = reinterpret_cast<intptr_t>(pSSMResource);
123 m_pTasker->addTask(this, (void *)pData);
124 m_mapGetDevice[targetDeviceDataId] = 1;
128 m_mapGetDevice[targetDeviceDataId]++;
136 m_mtxActivationCount.unlock();
139 void CContextModel::unregisterSSMResource(ActivationType activationType,
140 int targetDeviceDataId, ISSMResource *pSSMResource)
142 intptr_t *pData = NULL;
144 m_mtxActivationCount.lock();
145 switch (activationType)
147 case ACTIVATION_TYPE_SUBSCRIBE:
148 if (m_mapSubscribedDevice.find(targetDeviceDataId) != m_mapSubscribedDevice.end())
150 if (--m_mapSubscribedDevice[targetDeviceDataId] == 0)
152 pData = new intptr_t [2];
153 pData[0] = STATUS_DEACTIVATE;
154 pData[1] = reinterpret_cast<intptr_t>(pSSMResource);
155 m_pTasker->addTask(this, (void *)pData);
156 m_mapSubscribedDevice.erase(targetDeviceDataId);
161 case ACTIVATION_TYPE_GET:
162 if (m_mapGetDevice.find(targetDeviceDataId) != m_mapGetDevice.end())
164 if (--m_mapGetDevice[targetDeviceDataId] == 0)
166 pData = new intptr_t [2];
167 pData[0] = STATUS_STOP_READ_VALUE;
168 pData[1] = reinterpret_cast<intptr_t>(pSSMResource);
169 m_pTasker->addTask(this, (void *)pData);
177 m_mtxActivationCount.unlock();
180 SSMRESULT CContextModel::create(ConstructionType constructionType,
181 IContextModel *pParentModel, std::string modelName, ModelPropertyVec *pModelProperties)
183 SSMRESULT res = SSM_E_FAIL;
188 SSM_CLEANUP_ASSERT(pParentModel->queryInterface(OID_IContextModel, (IBase **)&m_pParentModel));
189 modelId = pParentModel->getModelId();
192 m_constructionType = constructionType;
193 m_modelName = modelName;
195 SSM_CLEANUP_ASSERT(m_pEvaluationEngine->createModel(modelId, modelName.c_str(), pModelProperties,
198 for (ModelPropertyVec::iterator itor = pModelProperties->begin();
199 itor != pModelProperties->end(); ++itor)
201 m_modelProperties.push_back(*itor);
208 SSMRESULT CContextModel::registerContextModelEvent(IContextModelEvent *pContextModelEvent)
210 m_pContextModelEvent = pContextModelEvent;
214 void CContextModel::onExecute(void *pArg)
216 intptr_t *pData = (intptr_t *)pArg;
218 if (m_pContextModelEvent)
220 m_pContextModelEvent->onModelStatusChange((Status)pData[0], (ISSMResource *)pData[1], this);
224 void CContextModel::onTerminate(void *pArg)
226 intptr_t *pData = (intptr_t *)pArg;
227 SAFE_ARRAY_DELETE(pData);
230 //TODO: called when new data arrived
231 int CContextModel::onEvent(std::string deviceID, TypeofEvent callType,
232 std::vector<ContextData> ctxData)
234 SSMRESULT res = SSM_E_FAIL;
236 ModelProperty modelProperty;
237 ModelPropertyVec modelProperties;
238 IntVec *deviceDataIds = NULL;
239 std::map<int, IntVec>::iterator itorDeviceDataIds;
242 if (m_modelName != ctxData[0].rootName)
244 SSM_CLEANUP_ASSERT(SSM_E_POINTER);
247 if (m_mapSSM.find(deviceID) == m_mapSSM.end())
249 SSM_CLEANUP_ASSERT(SSM_E_POINTER);
252 itorDeviceDataIds = m_mapDeviceDataIds.find(m_mapSSM[deviceID].first);
254 if (itorDeviceDataIds != m_mapDeviceDataIds.end())
256 deviceDataIds = &(itorDeviceDataIds->second);
259 for (std::vector<ContextData>::iterator itorContextData =
260 ctxData.begin(); itorContextData != ctxData.end(); ++itorContextData)
262 for (std::vector<std::map<std::string, std::string> >::iterator itor =
263 (*itorContextData).outputProperty.begin(); itor != (*itorContextData).outputProperty.end();
266 if (itor->find("error") != itor->end())
268 modelProperty.propertyName = "available";
269 modelProperty.propertyValue = "false";
270 modelProperty.propertyType = ModelProperty::TYPE_TEXT;
271 modelProperties.push_back(modelProperty);
275 modelProperty.propertyName = (*itor)["name"];
276 modelProperty.propertyValue = (*itor)["value"];
277 if ((*itor)["type"] == "string")
279 modelProperty.propertyType = ModelProperty::TYPE_TEXT;
281 else if ((*itor)["type"] == "int")
283 modelProperty.propertyType = ModelProperty::TYPE_INTEGER;
285 else if ((*itor)["type"] == "double")
287 modelProperty.propertyType = ModelProperty::TYPE_REAL;
289 else if ((*itor)["type"] == "float")
291 modelProperty.propertyType = ModelProperty::TYPE_REAL;
295 modelProperty.propertyType = ModelProperty::TYPE_TEXT;
297 modelProperties.push_back(modelProperty);
300 //Add lifetime to field
301 modelProperty.propertyName = "lifetime";
302 modelProperty.propertyValue = m_secLifeTime;
303 modelProperty.propertyType = ModelProperty::TYPE_INTEGER;
305 modelProperties.push_back(modelProperty);
307 if (deviceDataIds == NULL)
309 SSM_CLEANUP_ASSERT(addModelData(m_mapSSM[deviceID].first, &modelProperties, &dataId));
310 m_mapDeviceDataIds[m_mapSSM[deviceID].first].push_back(dataId);
314 SSM_CLEANUP_ASSERT(updateModelData(m_mapDeviceDataIds[m_mapSSM[deviceID].first][idxUpdate++],
318 modelProperties.clear();
325 int CContextModel::getModelId()
330 std::string CContextModel::getModelName()
335 IContextModel::ConstructionType CContextModel::getConstructionType()
337 return m_constructionType;
340 SSMRESULT CContextModel::getParentDataId(int dataId, IContextModel *pParentModel,
343 return m_pEvaluationEngine->getParentDataId(m_modelId, dataId, pParentModel->getModelId(),
347 SSMRESULT CContextModel::getChildDataId(int dataId, IContextModel *pChildModel,
348 IntVec *pChildDataIds)
350 return m_pEvaluationEngine->getChildDataId(m_modelId, dataId, pChildModel->getModelId(),
354 SSMRESULT CContextModel::activate(ActivationType activationType, int targetDeviceDataId)
356 if (targetDeviceDataId < 0 && activationType == ACTIVATION_TYPE_SUBSCRIBE)
358 m_mtxActivationCount.lock();
360 m_mtxActivationCount.unlock();
363 for (std::map<std::string, std::pair<int, ISSMResource> >::iterator itor = m_mapSSM.begin();
364 itor != m_mapSSM.end(); ++itor)
366 if (targetDeviceDataId < 0)
368 registerSSMResource(activationType, itor->second.first, &itor->second.second);
370 else if (itor->second.first == targetDeviceDataId)
372 registerSSMResource(activationType, itor->second.first, &itor->second.second);
380 SSMRESULT CContextModel::deactivate(ActivationType activationType, int targetDeviceDataId)
382 if (targetDeviceDataId < 0 && activationType == ACTIVATION_TYPE_SUBSCRIBE)
384 m_mtxActivationCount.lock();
386 m_mtxActivationCount.unlock();
389 for (std::map<std::string, std::pair<int, ISSMResource> >::iterator itor = m_mapSSM.begin();
390 itor != m_mapSSM.end(); ++itor)
392 if (targetDeviceDataId < 0)
394 unregisterSSMResource(activationType, itor->second.first, &itor->second.second);
396 else if (itor->second.first == targetDeviceDataId)
398 unregisterSSMResource(activationType, itor->second.first, &itor->second.second);
406 SSMRESULT CContextModel::GetModelSchema(ModelPropertyVec *pModelProperties)
408 return m_pEvaluationEngine->GetModelSchema(m_ModelId, pModelProperties);
411 SSMRESULT CContextModel::addModelData(int parentDataId, ModelPropertyVec *pData,
414 return m_pEvaluationEngine->addModelData(m_modelId, m_pParentModel->getModelId(), parentDataId,
418 SSMRESULT CContextModel::updateModelData(int dataId, ModelPropertyVec *pData)
420 return m_pEvaluationEngine->updateModelData(m_modelId, dataId, pData);
423 SSMRESULT CContextModel::deleteModelData(int dataId)
425 return m_pEvaluationEngine->deleteModelData(m_modelId, dataId);
428 SSMRESULT CContextModel::getModelData(int dataId, ModelPropertyVec *pData)
430 SSMRESULT res = SSM_E_FAIL;
433 SSM_CLEANUP_ASSERT(m_pEvaluationEngine->getModelData(m_modelId, dataId, pData));
434 for (ModelPropertyVec::iterator itor = pData->begin(); itor != pData->end(); ++itor)
436 itor->propertyName = m_modelProperties[i++].propertyName;
443 SSMRESULT CContextModel::getModelDataSet(int startIndex, int count,
444 std::vector<ModelPropertyVec> *pDataSet, int *pLastIndex)
446 SSMRESULT res = SSM_E_FAIL;
449 SSM_CLEANUP_ASSERT(m_pEvaluationEngine->getModelDataSet(m_modelId, startIndex, count, pDataSet,
452 for (std::vector<ModelPropertyVec>::iterator itorModelProperty = pDataSet->begin();
453 itorModelProperty != pDataSet->end(); ++itorModelProperty)
456 for (ModelPropertyVec::iterator itor = itorModelProperty->begin(); itor != itorModelProperty->end();
459 itor->propertyName = m_modelProperties[i++].propertyName;
467 SSMRESULT CContextModel::createConditionedModel(ModelConditionVec *pModelConditionVec,
468 IConditionedModel **ppConditionedModel)
470 SSMRESULT res = SSM_E_FAIL;
471 CObject<CConditionedModel> *pConditionedModel;
473 SSM_CLEANUP_ASSERT(CreateInstance(OID_IConditionedModel, (IBase **)&pConditionedModel));
474 SSM_CLEANUP_ASSERT(pConditionedModel->create(this, pModelConditionVec));
475 SSM_CLEANUP_ASSERT(pConditionedModel->queryInterface(OID_IConditionedModel,
476 (IBase **)ppConditionedModel));
478 //(*ppConditionedModel)->AddRef();
479 //m_ConditionedModels.push_back(*ppConditionedModel);
482 SAFE_RELEASE(pConditionedModel);
486 SSMRESULT CContextModel::CleanUpModelData()
488 SSMRESULT res = SSM_E_FAIL;
490 for(std::map<int,int>::iterator itor = m_mapDataId.begin();
491 itor != m_mapDataId.end(); ++itor)
493 CHK_SSMRESULT(DeleteModelData(itor->second));
503 //Called new install or re install
504 void CContextModel::addSSMResourceAndDeviceDataId(std::string deviceId, int deviceDataId,
505 ISSMResource *pSSMResource)
507 if (deviceDataId == 1)
510 m_pParentModel->getChildDataId(deviceDataId, this, &dataId);
511 if (dataId.size() > 0)
513 m_mapDeviceDataIds[deviceDataId] = dataId;
517 if (m_mapSSM.find(deviceId) != m_mapSSM.end())
519 m_mtxActivationCount.lock();
520 if (m_activationCount > 0)
522 unregisterSSMResource(ACTIVATION_TYPE_SUBSCRIBE, deviceDataId, &m_mapSSM[deviceId].second);
524 m_mtxActivationCount.unlock();
528 m_mapSSM[deviceId] = std::pair<int, ISSMResource>(deviceDataId, *pSSMResource);
531 m_mtxActivationCount.lock();
532 if (m_activationCount > 0)
534 registerSSMResource(ACTIVATION_TYPE_SUBSCRIBE, deviceDataId, &m_mapSSM[deviceId].second);
536 m_mtxActivationCount.unlock();
539 void CContextModel::setLifeTime(std::string seconds)
541 if (atoi(seconds.c_str()) > 0)
543 m_secLifeTime = seconds;