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(IN IContextModel::ActivationType activationType,
59 IN int totalConditionsCount)
61 m_activationType = activationType;
62 m_untrackedConditionsCount = totalConditionsCount;
66 SSMRESULT CConditionedQuery::registerQueryConditionEvent(IN IConditionedQueryEvent
67 *pConditionedQueryEvent)
69 m_conditionedQueryEvent = pConditionedQueryEvent;
73 SSMRESULT CConditionedQuery::onConditionedModelTriggered(IN 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(IN void *pArg)
100 if (m_conditionedQueryEvent)
102 m_conditionedQueryEvent->onConditionedQueryEvent(m_userTriggerId, (IConditionedQueryResult *)pArg);
106 void CConditionedQuery::onTerminate(IN void *pArg)
108 IConditionedQueryResult *pResult = (IConditionedQueryResult *)pArg;
109 SAFE_RELEASE(pResult);
112 SSMRESULT CConditionedQuery::registerConditionedModel(IN 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 SSM_CLEANUP_ASSERT(activateTriggers(m_userTriggerId));
149 SAFE_RELEASE(pContextModel);
153 SSMRESULT CConditionedQuery::activateTriggers(IN int userTriggerId)
155 SSMRESULT res = SSM_E_FAIL;
157 IContextModel *pBaseContextModel = NULL;
159 m_userTriggerId = userTriggerId;
161 if (hasAllConditionedModels() == false)
163 m_reservedForActivate = true;
167 for (std::vector<IConditionedModel *>::iterator itor = m_conditionedModels.begin();
168 itor != m_conditionedModels.end(); ++itor)
170 SSM_CLEANUP_ASSERT((*itor)->activateTrigger(&triggerId));
171 SSM_CLEANUP_ASSERT((*itor)->getBaseContextModel(&pBaseContextModel));
173 switch (pBaseContextModel->getConstructionType())
175 case IContextModel::CONSTRUCTION_TYPE_EXTERNAL:
176 case IContextModel::CONSTRUCTION_TYPE_REMOTE:
177 SSM_CLEANUP_ASSERT(pBaseContextModel->activate(m_activationType, m_targetDeviceDataId));
183 SAFE_RELEASE(pBaseContextModel);
187 SAFE_RELEASE(pBaseContextModel);
191 SSMRESULT CConditionedQuery::deactivateTriggers()
193 SSMRESULT res = SSM_E_FAIL;
194 IContextModel *pBaseContextModel = NULL;
196 for (std::vector<IConditionedModel *>::iterator itor = m_conditionedModels.begin();
197 itor != m_conditionedModels.end(); ++itor)
199 SSM_CLEANUP_ASSERT((*itor)->deactivateTrigger());
200 SSM_CLEANUP_ASSERT((*itor)->getBaseContextModel(&pBaseContextModel));
202 switch (pBaseContextModel->getConstructionType())
204 case IContextModel::CONSTRUCTION_TYPE_EXTERNAL:
205 case IContextModel::CONSTRUCTION_TYPE_REMOTE:
206 SSM_CLEANUP_ASSERT(pBaseContextModel->deactivate(m_activationType, m_targetDeviceDataId));
212 SAFE_RELEASE(pBaseContextModel);
216 SAFE_RELEASE(pBaseContextModel);
220 SSMRESULT CConditionedQuery::getConditionedQueryResult(OUT IConditionedQueryResult
221 **ppConditionedQueryResult)
223 SSMRESULT res = SSM_E_FAIL;
225 SSM_CLEANUP_ASSERT(m_pConditionedQueryResult->queryInterface(OID_IConditionedQueryResult,
226 (IBase **)ppConditionedQueryResult));
232 bool CConditionedQuery::hasAllConditionedModels()
234 return m_untrackedConditionsCount == 0 ? true : false;