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 "ConditionedQuery.h"
22 SSMRESULT CConditionedQuery::finalConstruct()
24 SSMRESULT res = SSM_E_FAIL;
26 SSM_CLEANUP_ASSERT(CreateGlobalInstance(OID_ITasker, (IBase **)&m_pTasker));
27 SSM_CLEANUP_ASSERT(CreateInstance(OID_IConditionedQueryResult,
28 (IBase **)&m_pConditionedQueryResult));
30 m_conditionedQueryEvent = NULL;
32 m_untrackedConditionsCount = 0;
34 m_reservedForActivate = false;
38 m_targetDeviceDataId = -1;
44 void CConditionedQuery::finalRelease()
46 m_conditionedQueryEvent = NULL;
48 SAFE_RELEASE(m_pConditionedQueryResult);
50 for (std::vector<IConditionedModel *>::iterator itor = m_conditionedModels.begin();
51 itor != m_conditionedModels.end(); ++itor)
53 (*itor)->registerConditionedModelEvent(NULL);
54 SAFE_RELEASE((*itor));
58 SSMRESULT CConditionedQuery::create(IContextModel::ActivationType activationType,
59 int totalConditionsCount)
61 m_activationType = activationType;
62 m_untrackedConditionsCount = totalConditionsCount;
66 SSMRESULT CConditionedQuery::registerQueryConditionEvent(IConditionedQueryEvent
67 *pConditionedQueryEvent)
69 m_conditionedQueryEvent = pConditionedQueryEvent;
73 SSMRESULT CConditionedQuery::onConditionedModelTriggered(int triggerId)
75 SSMRESULT res = SSM_E_FAIL;
76 int evaluatedConditions = m_conditionedModels.size();
78 for (unsigned int i = 0; i < m_conditionedModels.size(); i++)
80 if (m_conditionedModels[i]->hasAffectedData())
82 evaluatedConditions--;
86 if (evaluatedConditions == 0)
88 m_pConditionedQueryResult->addRef();
89 SSM_CLEANUP_ASSERT(m_pTasker->addTask(this, (IConditionedQueryResult *)m_pConditionedQueryResult));
98 void CConditionedQuery::onExecute(void *pArg)
100 if (m_conditionedQueryEvent)
102 m_conditionedQueryEvent->onConditionedQueryEvent(m_userTriggerId, (IConditionedQueryResult *)pArg);
106 void CConditionedQuery::onTerminate(void *pArg)
108 IConditionedQueryResult *pResult = (IConditionedQueryResult *)pArg;
109 SAFE_RELEASE(pResult);
112 SSMRESULT CConditionedQuery::registerConditionedModel(IConditionedModel *pConditionedModel)
114 SSMRESULT res = SSM_E_FAIL;
115 IContextModel *pContextModel = NULL;
117 SSM_CLEANUP_ASSERT(pConditionedModel->registerConditionedModelEvent(this));
118 SSM_CLEANUP_ASSERT(m_pConditionedQueryResult->addConditionedModel(pConditionedModel));
119 pConditionedModel->addRef();
120 m_conditionedModels.push_back(pConditionedModel);
122 m_untrackedConditionsCount--;
124 SSM_CLEANUP_ASSERT(pConditionedModel->getBaseContextModel(&pContextModel));
126 //Some special cases, we use device::dataId property to efficient request
127 if (pContextModel->getModelName() == "Device")
129 ModelConditionVec modelConditionVec;
130 SSM_CLEANUP_ASSERT(pConditionedModel->getWatchCondition(&modelConditionVec));
131 for (ModelConditionVec::iterator itor = modelConditionVec.begin();
132 itor != modelConditionVec.end(); ++itor)
134 if (itor->modelProperty.propertyName == "dataId" &&
135 itor->predicate == ModelCondition::PREDICATE_EQ)
137 m_targetDeviceDataId = atoi(itor->modelProperty.propertyValue.c_str());
143 if (hasAllConditionedModels() == true && m_reservedForActivate)
145 m_reservedForActivate = false;
146 SSM_CLEANUP_ASSERT(activateTriggers(m_userTriggerId));
150 SAFE_RELEASE(pContextModel);
154 SSMRESULT CConditionedQuery::activateTriggers(int userTriggerId)
156 SSMRESULT res = SSM_E_FAIL;
158 IContextModel *pBaseContextModel = NULL;
160 m_userTriggerId = userTriggerId;
162 if (hasAllConditionedModels() == false)
164 m_reservedForActivate = true;
168 for (std::vector<IConditionedModel *>::iterator itor = m_conditionedModels.begin();
169 itor != m_conditionedModels.end(); ++itor)
171 SSM_CLEANUP_ASSERT((*itor)->activateTrigger(&triggerId));
172 SSM_CLEANUP_ASSERT((*itor)->getBaseContextModel(&pBaseContextModel));
174 switch (pBaseContextModel->getConstructionType())
176 case IContextModel::CONSTRUCTION_TYPE_EXTERNAL:
177 case IContextModel::CONSTRUCTION_TYPE_REMOTE:
178 SSM_CLEANUP_ASSERT(pBaseContextModel->activate(m_activationType, m_targetDeviceDataId));
184 SAFE_RELEASE(pBaseContextModel);
188 SAFE_RELEASE(pBaseContextModel);
192 SSMRESULT CConditionedQuery::deactivateTriggers()
194 SSMRESULT res = SSM_E_FAIL;
195 IContextModel *pBaseContextModel = NULL;
197 for (std::vector<IConditionedModel *>::iterator itor = m_conditionedModels.begin();
198 itor != m_conditionedModels.end(); ++itor)
200 SSM_CLEANUP_ASSERT((*itor)->deactivateTrigger());
201 SSM_CLEANUP_ASSERT((*itor)->getBaseContextModel(&pBaseContextModel));
203 switch (pBaseContextModel->getConstructionType())
205 case IContextModel::CONSTRUCTION_TYPE_EXTERNAL:
206 case IContextModel::CONSTRUCTION_TYPE_REMOTE:
207 SSM_CLEANUP_ASSERT(pBaseContextModel->deactivate(m_activationType, m_targetDeviceDataId));
213 SAFE_RELEASE(pBaseContextModel);
216 if (m_reservedForActivate == true)
222 SAFE_RELEASE(pBaseContextModel);
226 SSMRESULT CConditionedQuery::getConditionedQueryResult(IConditionedQueryResult
227 **ppConditionedQueryResult)
229 SSMRESULT res = SSM_E_FAIL;
231 SSM_CLEANUP_ASSERT(m_pConditionedQueryResult->queryInterface(OID_IConditionedQueryResult,
232 (IBase **)ppConditionedQueryResult));
238 bool CConditionedQuery::hasAllConditionedModels()
240 return m_untrackedConditionsCount == 0 ? true : false;