759f44aa2512ef483ec6f183abea84d83863b499
[platform/upstream/iotivity.git] / service / soft-sensor-manager / SSMCore / src / QueryProcessor / EvaluationEngine.h
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 #ifndef _EvaluationEngine_H_
21 #define _EvaluationEngine_H_
22
23 #include "SSMInterface/SSMCore.h"
24 #include "Common/PlatformLayer.h"
25 #include "Common/sqlite3.h"
26 #include "Common/InternalInterface.h"
27
28 /**
29 * @class    CEvaluationEngine
30 * @brief    CEvaluationEngine Interface
31 *            This class represents Evaluation Engine
32 *
33 * @see
34 */
35 class CEvaluationEngine :
36     public CObjectRoot<CObjectMultiThreadModel>
37     , public IEvaluationEngine
38     , public IThreadClient
39 {
40     private:
41         sqlite3             *m_pSQLite3;
42         int                 m_iTriggerId;
43         CSimpleMutex        m_mtxTriggerId;
44         std::map<int, IEvaluationEngineEvent *>  m_mapTriggers;
45         CSimpleMutex        m_mtxDataRelation;
46         CObjectPtr<ITasker> m_pTasker;
47
48         SSMRESULT executeSQL_NoReturn(std::string strSQL);
49
50         SSMRESULT executeSQL_IntReturn(std::string strSQL, int *pResult);
51
52         static void onSQLTrigger(sqlite3_context *context, int argc, sqlite3_value **argv);
53
54         SSMRESULT initializeEngine();
55
56         void terminateEngine();
57
58     public:
59         SSMRESULT finalConstruct();
60
61         void finalRelease();
62
63         void onExecute(void *pArg);
64
65         void onTerminate(void *pArg);
66
67         /**
68         * @fn     onWatcherTriggered
69         * @brief Called when newly added data meets the conditions
70         *
71         * @param [in] int triggerId - Trigger id, which condition is triggered
72         * @param [in] int dataId - Data id, which data meets the conditions
73         *
74         * @return SSMRESULT
75         * @warning
76         * @exception
77         * @see
78         */
79         SSMRESULT onWatcherTriggered(int triggerId, int dataId);
80
81         SSMRESULT queryInterface(const OID &objectID, IBase **ppObject)
82         {
83             if (ppObject == NULL)
84                 return SSM_E_POINTER;
85
86             if (IsEqualOID(objectID, OID_IEvaluationEngine))
87             {
88                 IBase *pBase = (IEvaluationEngine *)this;
89                 pBase->addRef();
90                 *ppObject = pBase;
91                 return SSM_S_OK;
92             }
93
94             return SSM_E_NOINTERFACE;
95         }
96
97         /**
98         * @fn     createModel
99         * @brief Create new context model
100         *
101         * @param [in] int parentModelId - Parent Context Model id that related to Model hierarchy.
102         * @param [in] const char *newModelName - Represent model's name
103         * @param [in] ModelPropertyVec *pModelProperties - Represent model's properties that exposed to CQL
104         * @param [out] int *pModelId - Created model's id
105         *
106         * @return SSMRESULT
107         * @warning
108         * @exception
109         * @see
110         */
111         SSMRESULT createModel(int parentModelId, const char *newModelName,
112                               ModelPropertyVec *pModelDescs, int *pModelId);
113
114         /**
115         * @fn     addModelData
116         * @brief Add model data to Context Model
117         *
118         * @param [in] int modelId - model id that created CreateModel's execution.
119         * @param [in] int parentModelId - parent model id that created CreateModel's execution.
120         * @param [in] int parentDataId - Specify current model's parent Context Model's dataId
121         * @param [in] ModelPropertyVec *pData - Packed data for adding
122         * @param [out] int *pDataId - Returned dataId that newly added
123         *
124         * @return SSMRESULT
125         * @warning
126         * @exception
127         * @see
128         */
129         SSMRESULT addModelData(int modelId, int parentModelId, int parentDataId,
130                                ModelPropertyVec *pModelValues, int *pDataId);
131
132         /**
133         * @fn     updateModelData
134         * @brief Update model data to Context Model
135         *
136         * @param [in] int modelId - model id that created CreateModel's execution.
137         * @param [in] int dataId - DataId that provided AddModelData's execution
138         * @param [in] ModelPropertyVec *pData - Packed data for updating
139         *
140         * @return SSMRESULT
141         * @warning
142         * @exception
143         * @see
144         */
145         SSMRESULT updateModelData(int modelId, int dataId, ModelPropertyVec *pModelValues);
146
147         //SSMRESULT DeleteModel(int modelId);
148
149         /**
150         * @fn     deleteModelData
151         * @brief Delete model data using given index
152         *
153         * @param [in] int modelId - model id that created CreateModel's execution.
154         * @param [in] int dataId - DataId that provided AddModelData's execution
155         *
156         * @return SSMRESULT
157         * @warning
158         * @exception
159         * @see
160         */
161         SSMRESULT deleteModelData(int modelId, int dataId);
162
163         /**
164         * @fn     getModelData
165         * @brief Get model data
166         *
167         * @param [in] int modelId - model id that created CreateModel's execution.
168         * @param [in] int dataId - DataId that provided AddModelData's execution
169         * @param [out] ModelPropertyVec *pData - Packed data
170         *
171         * @return SSMRESULT
172         * @warning
173         * @exception
174         * @see
175         */
176         SSMRESULT getModelData(int modelId, int dataId, ModelPropertyVec *pModelProperties);
177
178         /**
179         * @fn     getModelDataSet
180         * @brief Get model data set
181         *
182         * @param [in] int modelId - model id that created CreateModel's execution.
183         * @param [in] int startIndex - Starting index of model data
184         * @param [in] int count - Number of data to retrieve
185         * @param [out] vector<ModelPropertyVec> *pDataSet - Packed data
186         * @param [out] int *pLastIndex - Index of last data
187         *
188         * @return SSMRESULT
189         * @warning
190         * @exception
191         * @see
192         */
193         SSMRESULT getModelDataSet(int modelId, int startIndex, int count,
194                                   std::vector<ModelPropertyVec> *pDataSet, int *pLastIndex);
195
196         //SSMRESULT GetModelSchema(int modelId, ModelPropertyVec *pModelProperties);
197
198         /**
199         * @fn     getConditionedModelData
200         * @brief Get affected data from given conditions
201         *
202         * @param [in] int modelId - model id that created CreateModel's execution.
203         * @param [in] ModelConditionVec *pModelConditions - Conditions for data retrieving
204         * @param [out] IntVec   *pDataIds - Searched dataId sets.
205         *
206         * @return SSMRESULT
207         * @warning
208         * @exception
209         * @see
210         */
211         SSMRESULT getConditionedModelData(int modelId, ModelConditionVec *pModelConditions,
212                                           IntVec *pDataIds);
213
214         /**
215         * @fn     watchModelData
216         * @brief Watching data from given conditions
217         *
218         * @param [in] int modelId - model id that created CreateModel's execution.
219         * @param [in] ModelConditionVec *pModelConditions - Conditions for data triggering
220         * @param [in] IEvaluationEngineEvent *pEvaluationEngineEvent - Event interface where triggered event listening
221         * @param [out] int *pTriggerId - Identifier which trigger is activated
222         *
223         * @return SSMRESULT
224         * @warning
225         * @exception
226         * @see
227         */
228         SSMRESULT watchModelData(int modelId, ModelConditionVec *pModelConditions,
229                                  IEvaluationEngineEvent *pEvaluationEngineEvent, int *pTriggerId);
230
231         /**
232         * @fn     dropWatchModelData
233         * @brief Stop watching data
234         *
235         * @param [in] int triggerId - Identifier which trigger to drop
236         *
237         * @return SSMRESULT
238         * @warning
239         * @exception
240         * @see
241         */
242         SSMRESULT dropWatchModelData(int triggerId);
243
244         /**
245         * @fn     getParentDataId
246         * @brief Get parent dataId related to model hierarchy
247         *
248         * @param [in] int modelId - model id that created CreateModel's execution.
249         * @param [in] int dataId - DataId that current model's
250         * @param [in] int parentModelId - Designated parent Context Model's id
251         * @param [out] int *pParentDataId - Designated parent Context Model's data's Id
252         *
253         * @return SSMRESULT
254         * @warning
255         * @exception
256         * @see
257         */
258         SSMRESULT getParentDataId(int modelId, int dataId, int parentModelId,
259                                   int *pParentDataId);
260
261         /**
262         * @fn     getChildDataId
263         * @brief Get child dataId related to model hierarchy
264         *
265         * @param [in] int modelId - model id that created CreateModel's execution.
266         * @param [in] int dataId - DataId that current model's
267         * @param [in] int childModelId - Designated child Context Model's id
268         * @param [out] IntVec *pChildDataIds - Designated child Context Model's data's Id
269         *
270         * @return SSMRESULT
271         * @warning
272         * @exception
273         * @see
274         */
275         SSMRESULT getChildDataId(int modelId, int dataId, int childModelId,
276                                  IntVec *pChildDataIds);
277
278         //SSMRESULT GetPathToRoot(int currentModelId, IntVec *pPath);
279
280         //SSMRESULT GetModelId(const char *modelName, int *pModelId);
281
282         //SSMRESULT GetModelHierarchy(int rootModelId, StringVec *pPath);
283
284         //SSMRESULT GetDataHierarchy(int rootModelId, int dataId, StringVec *pPath);
285 };
286
287 #endif