Imported Upstream version 0.9.1
[platform/upstream/iotivity.git] / service / soft-sensor-manager / SSMCore / src / QueryProcessor / PropagationEngine.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 _PropagationEngine_H_
21 #define _PropagationEngine_H_
22
23 #include "SSMInterface/SSMCore.h"
24 #include "Common/PlatformLayer.h"
25 #include "Common/InternalInterface.h"
26 #include "ConditionedQuery.h"
27
28 typedef std::map< std::string, std::vector< std::pair<ModelConditionVec, IConditionedQuery *> > >
29 VirtualModelMap;
30
31 /**
32 * @class    CPropagationEngine
33 * @brief    CPropagationEngine Interface
34 *            This class represents propagation engine that manages ContextModel's automation
35 *
36 * @see
37 */
38 class CPropagationEngine :
39     public CObjectRoot<CObjectMultiThreadModel>
40     , public IPropagationEngine
41     , public IContextModelAccessor
42     , public IThreadClient
43     , public IContextModelEvent
44     , public IResourceEvent
45 {
46     private:
47         CObjectPtr<ITasker>                         m_pTasker;
48         CObjectPtr<IEvaluationEngine>               m_pEvaluationEngine;
49         CObjectPtr<IContextModel>                   m_pRootModel;
50
51         CObjectPtr<IContextDataReader>              m_pContextDataReader;
52         CObjectPtr<IContextRepository>              m_pContextRepository;
53
54         std::map<std::string, IContextModel *>       m_installedContextModel;
55         VirtualModelMap                             m_lookUpContextModel;
56         CSimpleMutex                                m_mtxContextModelList;
57         CSimpleMutex                                m_mtxLookUpList;
58
59         CObjectPtr<ISensingEngine>                  m_pSensingEngine;
60
61         CObjectPtr<IContextModel>                   m_pDeviceModel;
62
63         std::map<std::string, int>                  m_searchedSensorDataId;
64
65         SSMRESULT findContextModel(std::string modelName, IContextModel  **ppContextModel);
66
67         SSMRESULT registerCondition(IContextModel *pContextModel, ModelConditionVec *pConditions,
68                                     IConditionedQuery *pConditionedQuery);
69
70         SSMRESULT registerVirtualCondition(std::string virtualContextModelName,
71                                            ModelConditionVec *pConditions, IConditionedQuery *pConditionedQuery);
72
73         SSMRESULT propagateNewModel(std::string newModelName, IContextModel *pContextModel);
74
75         SSMRESULT addResourceFromGetList();
76
77         //TODO: Install Context model from SSMResource
78         SSMRESULT installCurrentSensors();
79
80         SSMRESULT installContextModelFromISSMResource(ISSMResource *pSSMResource);
81
82         SSMRESULT installContextModel(IContextModel *pParentModel,
83                                       IContextModel::ConstructionType constructionType,
84                                       const char *modelName, ModelPropertyVec *pModelDescs, IContextModel **ppContextModel);
85
86         SSMRESULT recoverEngineFromDatabase();
87
88         SSMRESULT cleanUpRemoteDeviceInfo();
89
90         SSMRESULT initializeEngine();
91
92         void terminateEngine();
93
94         SSMRESULT updateDeviceInfo(ISSMResource *pSSMResource, int *deviceId);
95
96     public:
97         SSMRESULT finalConstruct();
98
99         void finalRelease();
100
101         void onExecute(void *pArg);
102
103         void onTerminate(void *pArg);
104
105         /**
106         * @fn     onResourceEvent
107         * @brief Called when new device has discovered
108         *
109         * @param [in] RESOURCE_EVENT_TYPE eventType - Enumerator that represents Device's status
110         * @param [in] ISSMResource *pSSMResouce - NULL
111         * @param [in] std::string info - NULL
112         *
113         * @return int
114         * @warning
115         * @exception
116         * @see
117         */
118         int onResourceEvent(RESOURCE_EVENT_TYPE eventType, ISSMResource *pSSMResource,
119                             std::string info);
120
121         SSMRESULT queryInterface(const OID &objectID, IBase **ppObject)
122         {
123             if (ppObject == NULL)
124                 return SSM_E_POINTER;
125
126             if (IsEqualOID(objectID, OID_IPropagationEngine))
127             {
128                 IBase *pBase = (IPropagationEngine *)this;
129                 pBase->addRef();
130                 *ppObject = pBase;
131                 return SSM_S_OK;
132             }
133
134             return SSM_E_NOINTERFACE;
135         }
136
137         /**
138         * @fn     getContextModel
139         * @brief Retrieve ContextModel interface specified name
140         *
141         * @param [in] std::string modelName - ContextModel's name that looking for
142         * @param [out] IContextModel **ppContextModel - Interface pointer for retrieving
143         *
144         * @return SSMRESULT
145         * @warning
146         * @exception
147         * @see
148         */
149         SSMRESULT getContextModel(std::string modelName, IContextModel **ppContextModel);
150
151         /**
152         * @fn     createConditionedQuery
153         * @brief Create Conditioned Query using conditions
154         *
155         * @param [in] IContextModel::ActivationType activationType - ActivationType for ContextModel's working strategy
156         * @param [in] QueryCondition *pQueryCondition - Conditions for data retrieving
157         * @param [in] IConditionedQueryEvent *pEvent - Event interface for Conditioned Result listening
158         * @param [out] IConditionedQuery **ppConditionedQuery - Interface for retrieving Conditioned Query
159         *
160         * @return SSMRESULT
161         * @warning
162         * @exception
163         * @see
164         */
165         SSMRESULT createConditionedQuery(IContextModel::ActivationType activationType,
166                                          QueryCondition *pQueryCondition,
167                                          IConditionedQueryEvent *pEvent, IConditionedQuery **ppConditionedQuery);
168
169         /**
170         * @fn     onModelStatusChange
171         * @brief Called when ContextModel wants to activate or deactivate
172         *
173         * @param [in] IContextModel::Status newStatus - Context Model's new status
174         * @param [in] ISSMResource *pSSMResource - Instance of actual resource
175         * @param [in] IContextModel *pModel - Affected Context Model
176         *
177         * @return SSMRESULT
178         * @warning
179         * @exception
180         * @see
181         */
182         SSMRESULT onModelStatusChange(IContextModel::Status newStatus, ISSMResource *pSSMResource,
183                                       IContextModel *pModel);
184
185         /**
186         * @fn     onQueryContextModel
187         * @brief Called when ContextModelAccessor wants to query installed ContextModel
188         *
189         * @param [in] std::string modelName - ContextModel name to find out
190         * @param [out] IContextModel **ppContextModel - Interface of quried ContextModel
191         *
192         * @return SSMRESULT
193         * @warning
194         * @exception
195         * @see
196         */
197         SSMRESULT onQueryContextModel(std::string modelName, IContextModel **ppContextModel);
198 };
199 #endif