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 "ContextQuery.h"
22 result_model::result_model()
27 CContextQuery::CContextQuery()
31 SSMRESULT CContextQuery::initialize(Token &input_root)
34 return CreateGlobalInstance(OID_IPropagationEngine, (IBase **)&m_pPropagationEngine);
37 std::string CContextQuery::search_last_modelName(Token *temp)
41 if (temp->child_token.size() > 0)
43 temp = &(temp->child_token.at(0));
52 void CContextQuery::integrate_result(std::vector<result_model> *result, int modelId,
53 std::vector<int> *dataid, std::string modelName)
57 for (unsigned int i = 0 ; i < result->size() ; i++)
59 if (result->at(i).modelID == modelId)
62 for (unsigned int k = 0 ; k < result->at(i).dataId.size() ; k++)
65 for (unsigned int l = 0 ; l < dataid->size() ; l++)
67 if (result->at(i).dataId.at(k) == dataid->at(l))
75 result->at(i).dataId.erase(result->at(i).dataId.begin() + k);
85 temp.modelID = modelId;
86 temp.dataId = *dataid;
87 temp.modelName = modelName;
88 result->push_back(temp);
94 void CContextQuery::return_contextName(std::vector<std::string> *contextName)
96 int k = m_root.child_token.at(0).child_token.size();
98 for (int i = 0; i < k; i++)
100 Token *temp = &(m_root.child_token.at(0).child_token.at(i));
101 contextName->push_back(search_last_modelName(temp));
106 void CContextQuery::check_result_model()
108 if (m_root.child_token.at(0).child_token.size() <= 0)
110 int k = m_root.child_token.at(1).child_token.size();
111 std::vector<std::string> compare_string;
112 bool compare_flag = true;
113 std::string ModelName = "";
114 unsigned int min_cnt = MIN_NUM;
116 for (int i = 0 ; i < k; i++)
118 Token *temp = &(m_root.child_token.at(1).child_token.at(i));
119 std::string model_name;
121 if (temp->type == Context)
125 if (temp->child_token.size() > 0)
127 model_name += temp->name + ".";
128 temp = &(temp->child_token.at(0));
132 model_name += temp->name;
137 compare_string.push_back(model_name);
138 if (min_cnt > model_name.size())
140 min_cnt = model_name.size();
146 unsigned int cnt = 0;
150 if ((unsigned int)cnt > min_cnt - 1)
157 for (unsigned int i = 0 ; i < compare_string.size(); i++)
161 char_temp = compare_string.at(i)[cnt];
166 if (char_temp != compare_string.at(i)[cnt])
168 compare_flag = false;
174 if (compare_flag == true)
176 ModelName += char_temp;
179 else if (compare_flag == false)
187 std::string result_modelname = "";
189 for (int i = (int)ModelName.size() - 1 ; i > -1 ; i--)
191 if (ModelName[i] != '.' || (unsigned int)i != ModelName.size() - 1)
193 result_modelname.insert(0, 1, ModelName[i]);
197 if (result_modelname.size() > 0)
200 temp_token.name = result_modelname;
201 m_root.child_token.at(0).child_token.push_back(temp_token);
206 void CContextQuery::return_modelID(std::vector<int> *vector_int)
208 int k = m_root.child_token.at(0).child_token.size();
210 IContextModel *pContextModel = NULL;
213 for (int i = 0; i < k; i++)
215 Token *temp = &(m_root.child_token.at(0).child_token.at(i));
216 if (m_pPropagationEngine->getContextModel(search_last_modelName(temp),
217 &pContextModel) == SSM_S_OK)
219 pModel = pContextModel->getModelId();
220 vector_int->push_back(pModel);
222 SAFE_RELEASE(pContextModel);
226 void CContextQuery::make_QueryCondition(QueryCondition *result)
229 if (m_root.child_token.size() < 2)
231 ModelConditionVec modelcondition;
232 ModelCondition model_data;
235 for (unsigned int i = 0 ; i < m_root.child_token.at(0).child_token.size(); i++)
237 Token *temp_token = &(m_root.child_token.at(0).child_token.at(i));
238 std::string temp_name;
242 if (temp_token->child_token.size() > 0)
244 temp_token = &(temp_token->child_token.at(0));
249 dataId = temp_token->number;
250 temp_name = temp_token->name;
253 modelcondition.clear();
255 if (dataId == INDEX_ALL)
257 ///m_pPropagationEngine->GetContextModel(temp_name,&contextmodel);
258 //contextmodel->GetModelSchema(modelpropertyVec);
259 model_data.modelProperty.propertyName = "dataId";
260 model_data.predicate = ModelCondition::PREDICATE_NEQ;
261 model_data.modelProperty.propertyType = ModelProperty::TYPE_INTEGER;
262 model_data.modelProperty.propertyValue = "0";
263 modelcondition.push_back(model_data);
264 result->push_back(std::pair<std::string, ModelConditionVec>(temp_name, modelcondition));
266 else if (dataId == INDEX_THIS)
272 std::stringstream sstream;
273 model_data.modelProperty.propertyName = "dataId";
274 model_data.predicate = ModelCondition::PREDICATE_EQ;
275 model_data.modelProperty.propertyType = ModelProperty::TYPE_INTEGER;
276 sstream << dataId << std::ends;
277 model_data.modelProperty.propertyValue = sstream.str();
278 modelcondition.push_back(model_data);
279 result->push_back(std::pair<std::string, ModelConditionVec>(temp_name, modelcondition));
289 int k = m_root.child_token.at(1).child_token.size();
290 std::string model_name = "";
292 std::map<std::string, ModelConditionVec> modelConditionList;
294 for (int i = 0; i < k; i++)
296 Token *temp = &(m_root.child_token.at(1).child_token.at(i));
298 if (temp->type == Context)
302 if (temp->child_token.size() > 0)
304 // model_name[i/3] = temp->name + ".";
305 temp = &(temp->child_token.at(0));
309 model_name = temp->name;
314 ModelCondition model_data;
315 model_data.predicate = m_root.child_token.at(1).child_token.at(i + 1).condition;
316 model_data.modelProperty.propertyName = temp->model_property.propertyName;
317 model_data.modelProperty.propertyType = temp->model_property.propertyType;
318 model_data.modelProperty.propertyValue = temp->model_property.propertyValue;
319 modelConditionList[model_name].push_back(model_data);
323 for (std::map<std::string, ModelConditionVec>::iterator itor = modelConditionList.begin();
324 itor != modelConditionList.end(); ++itor)
326 result->push_back(std::pair<std::string, ModelConditionVec>(itor->first,