iotivity 0.9.0
[platform/upstream/iotivity.git] / service / soft-sensor-manager / SSMCore / src / QueryProcessor / ConditionedModel.cpp
1 /******************************************************************
2 *
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
4 *
5 *
6 *
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
10 *
11 *      http://www.apache.org/licenses/LICENSE-2.0
12 *
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.
18 *
19 ******************************************************************/
20 #include "ConditionedModel.h"
21
22 SSMRESULT CConditionedModel::finalConstruct()
23 {
24     SSMRESULT res = SSM_E_FAIL;
25
26     m_triggerId = -1;
27
28     m_pConditionedModelEvent = NULL;
29
30     SSM_CLEANUP_ASSERT(CreateGlobalInstance(OID_IEvaluationEngine, (IBase **)&m_pEvaluationEngine));
31
32 CLEANUP:
33     return res;
34 }
35
36 void CConditionedModel::finalRelease()
37 {
38     m_pConditionedModelEvent = NULL;
39
40     if (m_triggerId > -1)
41     {
42         deactivateTrigger();
43     }
44
45     m_triggerId = -1;
46 }
47
48 SSMRESULT CConditionedModel::create(IN IContextModel *pBaseModel,
49                                     IN ModelConditionVec *pModelConditions)
50 {
51     m_watchCondition = *pModelConditions;
52     return pBaseModel->queryInterface(OID_IContextModel, (IBase **)&m_pBaseModel);
53 }
54
55 SSMRESULT CConditionedModel::getBaseContextModel(OUT IContextModel **ppBaseContextModel)
56 {
57     SSMRESULT res = SSM_E_FAIL;
58
59     SSM_CLEANUP_ASSERT(m_pBaseModel->queryInterface(OID_IContextModel, (IBase **)ppBaseContextModel));
60
61 CLEANUP:
62     return res;
63 }
64
65 SSMRESULT CConditionedModel::registerConditionedModelEvent(IN IConditionedModelEvent
66         *pConditionedModelEvent)
67 {
68     m_pConditionedModelEvent = pConditionedModelEvent;
69     return SSM_S_OK;
70 }
71
72 SSMRESULT CConditionedModel::onWatchModelData(IN int triggerId, IN int dataId)
73 {
74     SSMRESULT res = SSM_E_FAIL;
75
76     m_mtxConditionedData.lock();
77     m_triggeredDataIds.push_back(dataId);
78     m_mtxConditionedData.unlock();
79
80     if (m_pConditionedModelEvent)
81     {
82         res = m_pConditionedModelEvent->onConditionedModelTriggered(triggerId);
83     }
84
85     return res;
86 }
87
88 SSMRESULT CConditionedModel::activateTrigger(OUT int *pTriggerId)
89 {
90     SSMRESULT res = SSM_E_FAIL;
91
92     SSM_CLEANUP_ASSERT(m_pEvaluationEngine->watchModelData(m_pBaseModel->getModelId(),
93                        &m_watchCondition, this, &m_triggerId));
94
95     *pTriggerId = m_triggerId;
96
97 CLEANUP:
98     return res;
99 }
100
101 SSMRESULT CConditionedModel::deactivateTrigger()
102 {
103     SSMRESULT res = SSM_S_OK;
104
105     if (m_triggerId > -1)
106     {
107         SSM_CLEANUP_ASSERT(m_pEvaluationEngine->dropWatchModelData(m_triggerId));
108     }
109
110     m_triggerId = -1;
111
112 CLEANUP:
113     return res;
114 }
115
116 bool CConditionedModel::hasAffectedData()
117 {
118     bool ret = false;
119
120     m_mtxConditionedData.lock();
121
122     if (m_triggeredDataIds.size() > 0 || m_affectedDataIds.size() > 0)
123     {
124         ret = true;
125         goto CLEANUP;
126     }
127
128     if (m_pEvaluationEngine->getConditionedModelData(m_pBaseModel->getModelId(),
129             &m_watchCondition, &m_affectedDataIds) != SSM_S_OK)
130     {
131         ret = false;
132         goto CLEANUP;
133     }
134
135     if (m_affectedDataIds.size() > 0)
136     {
137         ret = true;
138         goto CLEANUP;
139     }
140
141 CLEANUP:
142     m_mtxConditionedData.unlock();
143     return ret;
144 }
145
146 SSMRESULT CConditionedModel::getAffectedData(OUT IntVec *pDataIds)
147 {
148     SSMRESULT res = SSM_E_FAIL;
149
150     m_mtxConditionedData.lock();
151
152     if (m_triggeredDataIds.size() > 0)
153     {
154         pDataIds->push_back(m_triggeredDataIds.front());
155         m_triggeredDataIds.pop_front();
156         SSM_CLEANUP_ASSERT(SSM_S_OK);
157     }
158     else
159     {
160         m_affectedDataIds.clear();
161         SSM_CLEANUP_ASSERT(m_pEvaluationEngine->getConditionedModelData(m_pBaseModel->getModelId(),
162                            &m_watchCondition, &m_affectedDataIds));
163         *pDataIds = m_affectedDataIds;
164     }
165
166 CLEANUP:
167     m_mtxConditionedData.unlock();
168     return res;
169 }
170
171 SSMRESULT CConditionedModel::getWatchCondition(OUT ModelConditionVec *pModelConditions)
172 {
173     *pModelConditions = m_watchCondition;
174     return SSM_S_OK;
175 }