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 "SSMInterface/SoftSensorManager.h"
21 #include "QueryProcessor/QueryEngine.h"
22 #include "QueryProcessor/ContextModel.h"
23 #include "QueryProcessor/ConditionedModel.h"
24 #include "QueryProcessor/EvaluationEngine.h"
25 #include "QueryProcessor/PropagationEngine.h"
26 #include "SensorProcessor/SensingEngine.h"
27 #include "SensorProcessor/ContextExecutor.h"
28 #include "SensorProcessor/ContextDataReader.h"
29 #include "SensorProcessor/ResourceFinder.h"
30 #include "rapidxml/rapidxml.hpp"
32 inline bool operator<( const OID &lhs, const OID &rhs )
34 int ret = memcmp( &lhs, &rhs, sizeof(OID));
35 return (ret < 0 ? true : false );
38 SSMRESULT CSoftSensorManager::finalConstruct()
43 void CSoftSensorManager::finalRelease()
47 SSMRESULT CSoftSensorManager::initializeCore(std::string xmlDescription)
49 SSMRESULT res = SSM_E_FAIL;
50 rapidxml::xml_document<> xmlDoc;
53 rapidxml::xml_node<> *root = NULL;
54 rapidxml::xml_node<> *itemSSMCore = NULL;
55 rapidxml::xml_node<> *itemDevice = NULL;
59 std::string pathSoftSensors;
60 std::string pathDescription;
62 std::string copyDescription = xmlDescription.c_str();
66 xmlDoc.parse<0>((char *)copyDescription.c_str());
68 root = xmlDoc.first_node();
72 throw rapidxml::parse_error("No Root Element", 0);
75 strKey = root->name();
77 if (strKey != "SSMCore")
79 throw rapidxml::parse_error("Invalid root tag name", 0);
82 for (itemSSMCore = root->first_node(); itemSSMCore; itemSSMCore = itemSSMCore->next_sibling())
84 strKey = itemSSMCore->name();
86 if (strKey == "Device")
88 for (itemDevice = itemSSMCore->first_node(); itemDevice; itemDevice = itemDevice->next_sibling())
90 strKey = itemDevice->name();
94 name = itemDevice->value();
96 else if (strKey == "Type")
98 type = itemDevice->value();
106 else if (strKey == "Config")
108 for (itemDevice = itemSSMCore->first_node(); itemDevice; itemDevice = itemDevice->next_sibling())
110 strKey = itemDevice->name();
112 if (strKey == "SoftSensorRepository")
114 pathSoftSensors = itemDevice->value();
116 else if (strKey == "SoftSensorDescription")
118 pathDescription = itemDevice->value();
132 catch (rapidxml::parse_error &e)
134 SSM_CLEANUP_ASSERT(SSM_E_INVALIDXML);
137 SSM_CLEANUP_ASSERT(CreateGlobalInstance(OID_ISensingEngine, (IBase **)&m_pSensingEngine));
138 SSM_CLEANUP_ASSERT(m_pSensingEngine->queryInterface(OID_IContextRepository,
139 (IBase **)&m_pContextRepository));
140 SSM_CLEANUP_ASSERT(m_pContextRepository->initRepository(name, type, pathSoftSensors,
143 SSM_CLEANUP_ASSERT(CreateGlobalInstance(OID_IPropagationEngine, (IBase **)&m_pPropagationEngine));
148 terminateCore(false);
154 SSMRESULT CSoftSensorManager::startCore()
156 SSMRESULT res = SSM_E_FAIL;
158 SSM_CLEANUP_ASSERT(m_pContextRepository->startResourceFinder());
164 SSMRESULT CSoftSensorManager::stopCore()
166 SSMRESULT res = SSM_E_FAIL;
168 SSM_CLEANUP_ASSERT(m_pContextRepository->stopResourceFinder());
174 SSMRESULT CSoftSensorManager::terminateCore(bool factoryResetFlag)
179 SSMRESULT CSoftSensorManager::createQueryEngine(IQueryEngine **ppQueryEngine)
181 SSMRESULT res = SSM_E_FAIL;
182 IQueryEngineInternal *pQueryEngineInternal = NULL;
183 SSM_CLEANUP_ASSERT(CreateInstance(OID_IQueryEngineInternal, (IBase **)&pQueryEngineInternal));
184 *ppQueryEngine = (IQueryEngine *)pQueryEngineInternal;
190 unsigned long CSoftSensorManager::releaseQueryEngine(IQueryEngine *pQueryEngine)
192 IQueryEngineInternal *pQueryEngineInternal = NULL;
193 pQueryEngineInternal = (IQueryEngineInternal *)(CQueryEngine *)pQueryEngine;
195 return pQueryEngineInternal->release();
198 SSMRESULT CSoftSensorManager::getInstalledModelList(std::vector<ISSMResource *> *pList)
200 m_pSensingEngine->getList(pList);
205 CSimpleMutex *g_mtxGlobalInstance = NULL;
206 std::map<OID, IBase *> *g_globalInstance = NULL;
207 IThreadPool *g_pThreadPool = NULL;
209 SSMRESULT CreateGlobalInstance(const OID &objectID, IBase **ppvObject)
211 SSMRESULT res = SSM_E_NOINTERFACE;
213 if (ppvObject == NULL)
215 return SSM_E_POINTER;
220 g_mtxGlobalInstance->lock();
223 if (IsEqualOID(OID_ITasker, objectID))
225 if (g_globalInstance->find(OID_ITasker) == g_globalInstance->end())
227 SSM_CLEANUP_ASSERT(CreateInstance(OID_ITasker, ppvObject));
230 else if (IsEqualOID(OID_IThreadPool, objectID))
232 if (g_globalInstance->find(OID_IThreadPool) == g_globalInstance->end())
234 SSM_CLEANUP_ASSERT(CreateInstance(OID_IThreadPool, ppvObject));
237 else if (IsEqualOID(OID_IEvaluationEngine, objectID))
239 if (g_globalInstance->find(OID_IEvaluationEngine) == g_globalInstance->end())
241 SSM_CLEANUP_ASSERT(CreateInstance(OID_IEvaluationEngine, ppvObject));
244 else if (IsEqualOID(OID_IPropagationEngine, objectID))
246 if (g_globalInstance->find(OID_IPropagationEngine) == g_globalInstance->end())
248 SSM_CLEANUP_ASSERT(CreateInstance(OID_IPropagationEngine, ppvObject));
251 else if (IsEqualOID(OID_IContextRepository, objectID))
253 if (g_globalInstance->find(OID_IContextRepository) == g_globalInstance->end())
255 SSM_CLEANUP_ASSERT(CreateInstance(OID_IContextRepository, ppvObject));
258 else if (IsEqualOID(OID_IContextDataReader, objectID))
260 if (g_globalInstance->find(OID_IContextDataReader) == g_globalInstance->end())
262 SSM_CLEANUP_ASSERT(CreateInstance(OID_IContextDataReader, ppvObject));
265 else if (IsEqualOID(OID_ISensingEngine, objectID))
267 if (g_globalInstance->find(OID_ISensingEngine) == g_globalInstance->end())
269 SSM_CLEANUP_ASSERT(CreateInstance(OID_ISensingEngine, ppvObject));
274 res = SSM_E_NOINTERFACE;
280 (*g_globalInstance)[objectID] = *ppvObject;
284 (*g_globalInstance)[objectID]->addRef();
285 *ppvObject = (*g_globalInstance)[objectID];
294 g_mtxGlobalInstance->unlock();
298 SSMRESULT CreateInstance(const OID &objectID, IBase **ppObject)
300 SSMRESULT res = SSM_E_NOINTERFACE;
302 if (ppObject == NULL)
304 return SSM_E_POINTER;
309 if (IsEqualOID(OID_ITasker, objectID))
311 SSM_CLEANUP_ASSERT(CreateNewObject<CTasker>(objectID, ppObject));
313 else if (IsEqualOID(OID_IWorkerThread, objectID))
315 SSM_CLEANUP_ASSERT(CreateNewObject<CWorkerThread>(objectID, ppObject));
317 else if (IsEqualOID(OID_IThreadPool, objectID))
319 SSM_CLEANUP_ASSERT(CreateNewObject<CThreadPool>(objectID, ppObject));
321 else if (IsEqualOID(OID_IEvaluationEngine, objectID))
323 SSM_CLEANUP_ASSERT(CreateNewObject<CEvaluationEngine>(objectID, ppObject));
325 else if (IsEqualOID(OID_IPropagationEngine, objectID))
327 SSM_CLEANUP_ASSERT(CreateNewObject<CPropagationEngine>(objectID, ppObject));
329 else if (IsEqualOID(OID_IContextRepository, objectID))
331 SSM_CLEANUP_ASSERT(CreateNewObject<CContextRepository>(objectID, ppObject));
333 else if (IsEqualOID(OID_ISensingEngine, objectID))
335 SSM_CLEANUP_ASSERT(CreateNewObject<CSensingEngine>(objectID, ppObject));
337 else if (IsEqualOID(OID_IContextExecutor, objectID))
339 SSM_CLEANUP_ASSERT(CreateNewObject<CContextExecutor>(objectID, ppObject));
341 else if (IsEqualOID(OID_IContextModel, objectID))
343 SSM_CLEANUP_ASSERT(CreateNewObject<CContextModel>(objectID, ppObject));
345 else if (IsEqualOID(OID_IConditionedModel, objectID))
347 SSM_CLEANUP_ASSERT(CreateNewObject<CConditionedModel>(objectID, ppObject));
349 else if (IsEqualOID(OID_IConditionedQueryResult, objectID))
351 SSM_CLEANUP_ASSERT(CreateNewObject<CConditionedQueryResult>(objectID, ppObject));
353 else if (IsEqualOID(OID_IConditionedQuery, objectID))
355 SSM_CLEANUP_ASSERT(CreateNewObject<CConditionedQuery>(objectID, ppObject));
357 else if (IsEqualOID(OID_IResourceFinder, objectID))
359 SSM_CLEANUP_ASSERT(CreateNewObject<CResourceFinder>(objectID, ppObject));
361 else if (IsEqualOID(OID_IQueryEngineInternal, objectID))
363 SSM_CLEANUP_ASSERT(CreateNewObject<CQueryEngine>(objectID, ppObject));
365 else if (IsEqualOID(OID_ISoftSensorManager, objectID))
367 SSM_CLEANUP_ASSERT(CreateNewObject<CSoftSensorManager>(objectID, ppObject));
369 else if (IsEqualOID(OID_IContextDataReader, objectID))
371 SSM_CLEANUP_ASSERT(CreateNewObject<CContextDataReader>(objectID, ppObject));
378 SSMRESULT CreateGlobalInstanceRepo()
380 SSMRESULT res = SSM_E_FAIL;
382 if (g_mtxGlobalInstance != NULL)
383 SSM_CLEANUP_ASSERT(SSM_E_OUTOFMEMORY);
385 if (g_globalInstance != NULL)
386 SSM_CLEANUP_ASSERT(SSM_E_OUTOFMEMORY);
388 g_mtxGlobalInstance = new CSimpleMutex();
389 SSM_CLEANUP_NULL_ASSERT(g_mtxGlobalInstance);
390 g_globalInstance = new std::map<OID, IBase *>();
391 SSM_CLEANUP_NULL_ASSERT(g_globalInstance);
393 SSM_CLEANUP_ASSERT(CreateGlobalInstance(OID_IThreadPool, (IBase **)&g_pThreadPool));
399 SSMRESULT DestroyGlobalInstanceRepo()
401 SSMRESULT res = SSM_E_FAIL;
403 SSM_CLEANUP_ASSERT(g_pThreadPool->destroyThreadPool());
405 SAFE_RELEASE(g_pThreadPool);
406 SAFE_DELETE(g_mtxGlobalInstance);
407 SAFE_DELETE(g_globalInstance);