Imported Upstream version 0.9.1
[platform/upstream/iotivity.git] / service / soft-sensor-manager / SSMCore / src / QueryProcessor / ContextQuery.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 "ContextQuery.h"
21
22 result_model::result_model()
23 {
24     modelID = 0;
25 }
26
27 CContextQuery::CContextQuery()
28 {
29 }
30
31 SSMRESULT CContextQuery::initialize(Token &input_root)
32 {
33     m_root = input_root;
34     return CreateGlobalInstance(OID_IPropagationEngine, (IBase **)&m_pPropagationEngine);
35 }
36
37 std::string CContextQuery::search_last_modelName(Token *temp)
38 {
39     while (true)
40     {
41         if (temp->child_token.size() > 0)
42         {
43             temp = &(temp->child_token.at(0));
44         }
45         else
46         {
47             return temp->name;
48         }
49     }
50 }
51
52 void CContextQuery::integrate_result(std::vector<result_model> *result, int modelId,
53                                      std::vector<int> *dataid, std::string modelName)
54 {
55     bool flag = false;
56
57     for (unsigned int i = 0 ; i < result->size() ; i++)
58     {
59         if (result->at(i).modelID == modelId)
60         {
61             flag = true;
62             for (unsigned int k = 0 ; k < result->at(i).dataId.size() ; k++)
63             {
64                 bool flag1 = false;
65                 for (unsigned int l = 0 ; l < dataid->size() ; l++)
66                 {
67                     if (result->at(i).dataId.at(k) == dataid->at(l))
68                     {
69                         flag1 = true;
70                         break;
71                     }
72                 }
73                 if (flag1 == false)
74                 {
75                     result->at(i).dataId.erase(result->at(i).dataId.begin() + k);
76                     k--;
77                 }
78             }
79         }
80     }
81
82     if (flag == false)
83     {
84         result_model temp;
85         temp.modelID = modelId;
86         temp.dataId = *dataid;
87         temp.modelName = modelName;
88         result->push_back(temp);
89     }
90 }
91
92
93
94 void CContextQuery::return_contextName(std::vector<std::string> *contextName)
95 {
96     int k = m_root.child_token.at(0).child_token.size();
97
98     for (int i = 0; i < k; i++)
99     {
100         Token *temp = &(m_root.child_token.at(0).child_token.at(i));
101         contextName->push_back(search_last_modelName(temp));
102     }
103
104 }
105
106 void CContextQuery::check_result_model()
107 {
108     if (m_root.child_token.at(0).child_token.size() <= 0)
109     {
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;
115
116         for (int i = 0 ; i < k; i++)
117         {
118             Token *temp = &(m_root.child_token.at(1).child_token.at(i));
119             std::string model_name;
120
121             if (temp->type == Context)
122             {
123                 while (true)
124                 {
125                     if (temp->child_token.size() > 0)
126                     {
127                         model_name += temp->name + ".";
128                         temp = &(temp->child_token.at(0));
129                     }
130                     else
131                     {
132                         model_name += temp->name;
133                         break;
134                     }
135                 }
136
137                 compare_string.push_back(model_name);
138                 if (min_cnt > model_name.size())
139                 {
140                     min_cnt = model_name.size();
141                 }
142             }
143
144         }
145
146         unsigned int cnt = 0;
147
148         while (true)
149         {
150             if ((unsigned int)cnt > min_cnt - 1)
151             {
152                 break;
153             }
154
155             char char_temp = 0;
156
157             for (unsigned int i = 0 ; i < compare_string.size(); i++)
158             {
159                 if ( i == 0)
160                 {
161                     char_temp = compare_string.at(i)[cnt];
162                     compare_flag = true;
163                 }
164                 else
165                 {
166                     if (char_temp != compare_string.at(i)[cnt])
167                     {
168                         compare_flag = false;
169                         break;
170                     }
171                 }
172             }
173
174             if (compare_flag == true)
175             {
176                 ModelName += char_temp;
177                 cnt++;
178             }
179             else if (compare_flag == false)
180             {
181                 break;
182             }
183
184         }
185
186
187         std::string result_modelname = "";
188
189         for (int i = (int)ModelName.size() - 1 ; i > -1 ; i--)
190         {
191             if (ModelName[i] != '.' || (unsigned int)i != ModelName.size() - 1)
192             {
193                 result_modelname.insert(0, 1, ModelName[i]);
194             }
195         }
196
197         if (result_modelname.size() > 0)
198         {
199             Token temp_token;
200             temp_token.name = result_modelname;
201             m_root.child_token.at(0).child_token.push_back(temp_token);
202         }
203     }
204 }
205
206 void CContextQuery::return_modelID(std::vector<int> *vector_int)
207 {
208     int k = m_root.child_token.at(0).child_token.size();
209
210     IContextModel *pContextModel = NULL;
211     int pModel = 0;
212
213     for (int i = 0; i < k; i++)
214     {
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)
218         {
219             pModel = pContextModel->getModelId();
220             vector_int->push_back(pModel);
221         }
222         SAFE_RELEASE(pContextModel);
223     }
224 }
225
226 void CContextQuery::make_QueryCondition(QueryCondition *result)
227 {
228
229     if (m_root.child_token.size() < 2)
230     {
231         ModelConditionVec modelcondition;
232         ModelCondition model_data;
233         int dataId = 0;
234
235         for (unsigned int i = 0 ; i < m_root.child_token.at(0).child_token.size(); i++)
236         {
237             Token *temp_token = &(m_root.child_token.at(0).child_token.at(i));
238             std::string temp_name;
239
240             while (true)
241             {
242                 if (temp_token->child_token.size() > 0)
243                 {
244                     temp_token = &(temp_token->child_token.at(0));
245                     continue;
246                 }
247                 else
248                 {
249                     dataId = temp_token->number;
250                     temp_name = temp_token->name;
251                 }
252
253                 modelcondition.clear();
254
255                 if (dataId == INDEX_ALL)
256                 {
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));
265                 }
266                 else if (dataId == INDEX_THIS)
267                 {
268
269                 }
270                 else
271                 {
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));
280
281                 }
282
283                 break;
284             }// end while
285         }//end for
286     }//end if
287     else
288     {
289         int k = m_root.child_token.at(1).child_token.size();
290         std::string model_name = "";
291
292         std::map<std::string, ModelConditionVec> modelConditionList;
293
294         for (int i = 0; i < k; i++)
295         {
296             Token *temp = &(m_root.child_token.at(1).child_token.at(i));
297
298             if (temp->type == Context)
299             {
300                 while (1)
301                 {
302                     if (temp->child_token.size() > 0)
303                     {
304                         //  model_name[i/3] = temp->name + ".";
305                         temp = &(temp->child_token.at(0));
306                     }
307                     else
308                     {
309                         model_name = temp->name;
310                         break;
311                     }
312                 }
313
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);
320             }
321         }
322
323         for (std::map<std::string, ModelConditionVec>::iterator itor = modelConditionList.begin();
324              itor != modelConditionList.end(); ++itor)
325         {
326             result->push_back(std::pair<std::string, ModelConditionVec>(itor->first,
327                               itor->second));
328         }
329     }
330 }