Merge "add missing packages requires" into tizen
[profile/ivi/ico-uxf-homescreen.git] / lib / misc / state-machine / CicoEventInfo.cpp
1 /*
2  * Copyright (c) 2013 TOYOTA MOTOR CORPORATION.
3  *
4  * Contact: 
5  *
6  * Licensed under the Apache License, Version 2.0 (the License){}
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an AS IS BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License. 
17  */
18 #include <iostream>
19 #include <string>
20 #include <vector>
21 #include <algorithm>
22 #include <sstream>
23 #include "CicoEvent.h"
24 #include "CicoEventInfo.h"
25 #include "CicoStateCore.h"
26
27
28 /**
29  * event infomation class
30  * @author m.kamoshida
31  * @version 0.1
32  */
33
34 /**
35  * @brief CicoEventInfo::CicoEventInfo
36  * @param ev base event data
37  */
38 CicoEventInfo::CicoEventInfo(unsigned short ev):
39     CicoEvent(ev)
40 {
41     m_JGCE =  Ejnj;                     //
42     m_transition = (CicoStateCore*)0;   // Initialize the pointer value
43 }
44
45 /**
46  * @brief CicoEventInfo::CicoEventInfo
47  * @param ev event number
48  * @param gcstr operator kind
49  * @param value Comparison value
50  */
51 CicoEventInfo::CicoEventInfo(unsigned short ev, const std::string& gcstr,
52                              int value):
53     CicoEvent(ev, value)
54 {
55     m_JGCE = chgGCType(gcstr);          // string change to enum
56     m_transition = (CicoStateCore*)0;   // Initialize the pointer value
57 }
58
59 /**
60  * @brief CicoEventInfo::CicoEventInfo
61  * @param ev event number
62  * @param gcstr operator kind
63  * @param value Comparison value
64  */
65 CicoEventInfo::CicoEventInfo(unsigned short ev, const std::string& gcstr,
66                              double value):
67     CicoEvent(ev, value)
68 {
69     m_JGCE = chgGCType(gcstr);          // string change to enum
70     m_transition = (CicoStateCore*)0;   // Initialize the pointer value
71 }
72
73 /**
74  * @brief CicoEventInfo::CicoEventInfo
75  * @param ev event number
76  * @param gcstr operator kind
77  * @param value Comparison value
78  */
79 CicoEventInfo::CicoEventInfo(unsigned short ev, const std::string& gcstr,
80                              const std::string& value):
81     CicoEvent(ev, value)
82 {
83     m_JGCE = chgGCType(gcstr);          // string change to enum
84     m_transition = (CicoStateCore*)0;   // Initialize the pointer value
85 }
86
87 /**
88  * @brief CicoEventInfo::CicoEventInfo
89  * @param name event name
90  * @param ev event number
91  */
92 CicoEventInfo::CicoEventInfo(const std::string& name, unsigned short ev):
93     CicoEvent(ev), m_name(name)
94 {
95     m_JGCE =  Ejnj;
96     m_transition = (CicoStateCore*)0;   // Initialize the pointer value
97 }
98
99 /**
100  * @brief CicoEventInfo::CicoEventInfo
101  * @param name event name
102  * @param ev event number
103  * @param gcstr operator kind
104  * @param value Comparison value
105  */
106 CicoEventInfo::CicoEventInfo(const std::string& name, unsigned short ev,
107                              const std::string& gcstr, int value):
108     CicoEvent(ev, value), m_name(name)
109 {
110     m_JGCE = chgGCType(gcstr);          // string change to enum
111     m_transition = (CicoStateCore*)0;   // Initialize the pointer value
112 }
113
114 /**
115  * @brief CicoEventInfo::CicoEventInfo
116  * @param name event name
117  * @param ev event number
118  * @param gcstr operator kind
119  * @param value Comparison value
120  */
121 CicoEventInfo::CicoEventInfo(const std::string& name, unsigned short ev,
122                              const std::string& gcstr, double value):
123     CicoEvent(ev, value), m_name(name)
124 {
125     m_JGCE = chgGCType(gcstr);          // string change to enum
126     m_transition = (CicoStateCore*)0;   // Initialize the pointer value
127 }
128
129 /**
130  * @brief CicoEventInfo::CicoEventInfo
131  * @param name event name
132  * @param ev event number
133  * @param gcstr operator kind
134  * @param value Comparison value
135  */
136 CicoEventInfo::CicoEventInfo(const std::string& name, unsigned short ev,
137                              const std::string& gcstr,
138                              const std::string& value):
139     CicoEvent(ev, value), m_name(name)
140 {
141     m_JGCE = chgGCType(gcstr);          // string change to enum
142     m_transition = (CicoStateCore*)0;   // Initialize the pointer value
143 }
144
145 /**
146  * @brief CicoEventInfo::CicoEventInfo
147  * @param ei copy source
148  */
149 CicoEventInfo::CicoEventInfo(const CicoEventInfo& ei):
150     CicoEvent()
151 {
152     m_JGCE = Ejnj;
153     m_transition = (CicoStateCore*)0;   // Initialize the pointer value
154     *this = ei;
155 }
156
157 /**
158  * @brief CicoEventInfo::~CicoEventInfo
159  */
160 CicoEventInfo::~CicoEventInfo()
161 {
162     if (false == m_vEvt.empty()) {
163         m_vEvt.clear();
164     }
165     if (false == m_vJGCE.empty()) {
166         m_vJGCE.clear();
167     }
168     if (false == m_vAgc.empty()) {
169         m_vAgc.clear();
170     }
171 //    m_transition = (CicoStateCore*)0;
172 }
173
174 /**
175  * @brief CicoEventInfo::addAndGurdCondition
176  * @param gcstr operator kind
177  * @param value Comparison value
178  * @return true:added success false:added fail
179  */
180 bool CicoEventInfo::addAndGurdCondition(const std::string& gcstr, int value)
181 {
182     CicoEvent e(m_ev, value);
183     E_JGCE j = chgGCType(gcstr);        // string change to enum
184     if (Ejnj == j) {
185         return false;
186     }
187     addGurdCondition(e, j, EAGC_AND);
188     return true;
189 }
190
191 /**
192  * @brief CicoEventInfo::addAndGurdCondition
193  * @param gcstr operator kind
194  * @param value Comparison value
195  * @return true:added success false:added fail
196  */
197 bool CicoEventInfo::addAndGurdCondition(const std::string& gcstr, double value)
198 {
199     CicoEvent e(m_ev, value);
200     E_JGCE j = chgGCType(gcstr);        // string change to enum
201     if (Ejnj == j) {
202         return false;
203     }
204     addGurdCondition(e, j, EAGC_AND);
205     return true;
206 }
207
208 /**
209  * @brief CicoEventInfo::addAndGurdCondition
210  * @param gcstr operator kind
211  * @param value Comparison value
212  * @return true:added success false:added fail
213  */
214 bool CicoEventInfo::addAndGurdCondition(const std::string& gcstr,
215                                         const std::string& value)
216 {
217     CicoEvent e(m_ev, value);
218     E_JGCE j = chgGCType(gcstr);        // string change to enum
219     if (Ejnj == j) {
220         return false;
221     }
222     addGurdCondition(e, j, EAGC_AND);
223     return true;
224 }
225
226 /**
227  * @brief CicoEventInfo::addOrGurdCondition
228  * @param gcstr operator kind
229  * @param value Comparison value
230  * @return true:added success false:added fail
231  */
232 bool CicoEventInfo::addOrGurdCondition(const std::string& gcstr, int value)
233 {
234     CicoEvent e(m_ev, value);
235     E_JGCE j = chgGCType(gcstr);        // string change to enum
236     if (Ejnj == j) {
237         return false;
238     }
239     addGurdCondition(e, j, EAGC_OR);
240     return true;
241 }
242
243
244 /**
245  * @brief CicoEventInfo::addOrGurdCondition
246  * @param gcstr operator kind
247  * @param value Comparison value
248  * @return true:added success false:added fail
249  */
250 bool CicoEventInfo::addOrGurdCondition(const std::string& gcstr,
251                                        double value)
252 {
253     CicoEvent e(m_ev, value);
254     E_JGCE j = chgGCType(gcstr);        // string change to enum
255     if (Ejnj == j) {
256         return false;
257     }
258     addGurdCondition(e, j, EAGC_OR);
259     return true;
260 }
261
262 /**
263  * @brief CicoEventInfo::addOrGurdCondition
264  * @param gcstr operator kind
265  * @param value Comparison value
266  * @return true:added success false:added fail
267  */
268 bool CicoEventInfo::addOrGurdCondition(const std::string& gcstr,
269                                        const std::string& value)
270 {
271     CicoEvent e(m_ev, value);
272     E_JGCE j = chgGCType(gcstr);        // string change to enum
273     if (Ejnj == j) {
274         return false;
275     }
276     addGurdCondition(e, j, EAGC_OR);
277     return true;
278 }
279
280 /**
281  * @brief CicoEventInfo::Judge
282  * @param ev event data
283  * @param sm compare trigger state-object
284  * @return true:event match false:event Mismatch
285  */
286 bool CicoEventInfo::Judge(const CicoEvent& ev, const CicoStateCore* sm) const
287 {
288     if (m_ev != ev.getEV()) {           //
289         return false;                   // event number mismatch
290     }
291     // The event number matches
292     if (Ejnj == m_JGCE) {               // Without guard condition
293         return true;                    // event match !
294     }
295     // check guard condition
296     bool bR = checkGurdCondition(m_JGCE, *((CicoEvent*)this), ev, sm); // return value
297
298     int sz = m_vEvt.size();
299     for (int i=0; i<sz; i++) {
300         bool bR2 = checkGurdCondition(m_vJGCE[i], m_vEvt[i], ev, sm);
301         E_AGC agc = m_vAgc[i];
302         if (EAGC_AND == agc) {
303             bR &= bR2;
304         }
305         else if (EAGC_OR == agc) {
306             bR |= bR2;
307         }
308         else {
309             return false;
310         }
311     }
312     return bR;
313 }
314
315 /**
316  * @brief CicoEventInfo::checkGurdCondition
317  * @param j operator
318  * @param base event data
319  * @param rec event data
320  * @param sm compair trigger state-object
321  * @return treu:match false:Mismatch
322  */
323 bool CicoEventInfo::checkGurdCondition(E_JGCE j, const CicoEvent& base,
324                                        const CicoEvent& rec,
325                                        const CicoStateCore* sm) const
326 {
327     bool bR = false;                    // return value init.
328     if ((Ein == j)||(Enot == j)) {      // Guard condition of active state object
329         const CicoStateCore* stt = (CicoStateCore*)0;
330         if (EEvtGCInt == base.getVTG()) {
331             stt = getStateF(sm, base.getGCVi());
332         }
333         else if (EEvtGCStr == base.getVTG()) {
334             stt = getStateF(sm, base.getGCVs());
335         }
336         if ((CicoStateCore*)0 == stt) {
337             return false;
338         }
339         bR = checkActive(j, *stt);
340     }
341     else {                              // Remaining comparisons
342         bR = checkCompare(j, base, rec);
343     }
344     return bR;
345 }
346
347 /**
348  * @brief CicoEventInfo::checkActive
349  * @param j in or not operator
350  * @param stt check target
351  * @return true:event match false:event Mismatch
352  */
353 bool CicoEventInfo::checkActive(E_JGCE j, const CicoStateCore& stt) const
354 {
355     if (Ein == j) {
356         if (true == stt.isActive()) {
357             return true;
358         }
359     }
360     else if (Enot == j) {
361         if (false == stt.isActive()) {
362             return true;
363         }
364     }
365     return false;
366 }
367
368 /**
369  * @brief CicoEventInfo::checkCompare
370  * @param j operator
371  * @param base Comparison Event
372  * @param rec Comparison Event
373  * @return true:match false:Mismatch
374  */
375 bool CicoEventInfo::checkCompare(E_JGCE j, const CicoEvent& base,
376                                  const CicoEvent& rec) const
377 {
378     bool bR = false;
379     switch (j) {
380     case Ejeq:                          // equal
381         if (base == rec) {
382             bR = true;
383         }
384         break;
385     case Ejne:                          // Not equal
386         if (base != rec) {
387             bR = true;
388         }
389         break;
390     case Ejgt:                          // greater than
391         if (base < rec) {
392             bR = true;
393         }
394         break;
395     case Ejge:                          // greater than or equal
396         if (base <= rec) {
397             bR = true;
398         }
399         break;
400     case Ejlt:                          // less than
401         if (base > rec) {
402             bR = true;
403         }
404         break;
405     case Ejle:                          // less than or equal
406         if (base >= rec) {
407             bR = true;
408         }
409         break;
410     default:                            // ERROR
411         break;
412     }
413     return bR;
414 }
415
416 /**
417  * @brief CicoEventInfo::addGurdCondition
418  * @param e Comparison Event
419  * @param j operator infomation(==/!=/>/>=/</<=/in/not)
420  * @param a and/or
421  */
422 void CicoEventInfo::addGurdCondition(CicoEvent& e, E_JGCE j, E_AGC a)
423 {
424     if (Ejnj == m_JGCE) {
425         E_TOV tov = e.getVTG();
426         if (EEvtGCInt == tov) {
427             setGCV(e.getGCVi());
428             m_JGCE = j;
429         }
430         else if (EEvtGCDouble == tov) {
431             setGCV(e.getGCVd());
432             m_JGCE = j;
433         }
434         else if (EEvtGCStr == tov) {
435             setGCV(e.getGCVs());
436             m_JGCE = j;
437         }
438     }
439     else {
440        m_vEvt.push_back(e);
441        m_vJGCE.push_back(j);
442        m_vAgc.push_back(a);
443     }
444 }
445
446 /**
447  * @brief CicoEventInfo::chgGCType
448  * @param gcstr string operator
449  * @return enum operator
450  */
451 CicoEventInfo::E_JGCE CicoEventInfo::chgGCType(const std::string& gcstr) const
452 {
453     if (gcstr == DEFJGCE_EQ) {          // "="
454         return Ejeq;                    // equal
455     }
456     else if (gcstr == DEFJGCE_NE) {     // "!="
457         return Ejne;                    // Not equal
458     }
459     else if (gcstr == DEFJGCE_GT) {     // "<"
460         return Ejgt;                    // greater than
461     }
462     else if (gcstr == DEFJGCE_GE) {     // "<="
463         return Ejge;                    // greater than or equal
464     }
465     else if (gcstr == DEFJGCE_GE2) {    // "=<"
466         return Ejge;                    // greater than or equal
467     }
468     else if (gcstr == DEFJGCE_LT) {     // ">"
469         return Ejlt;                    // less than
470     }
471     else if (gcstr == DEFJGCE_LE) {     // ">="
472         return Ejle;                    // less than or equal
473     }
474     else if (gcstr == DEFJGCE_LE2) {    // "=>"
475         return Ejle;                    // less than or equal
476     }
477     else {                              //
478         std::string s(gcstr);           // copy string
479         transform(s.begin(), s.end(), s.begin(), tolower); // converted to lower case
480         if (s == DEFJGCE_IN) {          // "in"
481             return Ein;                 // in active state object
482         }
483         else if (s == DEFJGCE_NOT) {    // "not"
484             return Enot;                // not active state object
485         }
486         else {
487             /*
488             * _D_ERROR
489             */
490         }
491     }
492     return Ejnj;                        // Not Judgment
493 }
494
495 /**
496  * @brief CicoEventInfo::operator =
497  * @param s copy source
498  */
499 void CicoEventInfo::operator=(const CicoEventInfo& s)
500 {
501     // CicoEvent Block
502     setEV(s.getEV());
503     E_TOV tov = s.getVTG();
504     switch (tov) {
505     case EEvtGCInt:
506         setGCV(s.getGCVi());
507         break;
508     case EEvtGCDouble:
509         setGCV(s.getGCVd());
510         break;
511     case EEvtGCStr:
512         setGCV(s.getGCVs());
513         break;
514     default:
515         break;
516     }
517
518     setAdVal(s.getAdVal());
519
520     // CicoEventInfo Block
521     m_name = s.m_name;                  // copy event name
522     m_JGCE = s.m_JGCE;                  // copy Judgment guard condition expression
523     int sz = s.m_vEvt.size();           // get vector member size
524     for (int i = 0; i < sz; i++) {      // vector menber size loop
525         m_vEvt.push_back(s.m_vEvt[i]);
526         m_vJGCE.push_back(s.m_vJGCE[i]);
527         m_vAgc.push_back(s.m_vAgc[i]);
528     }
529     m_transition = s.m_transition;      //
530 }
531
532 /**
533  * @brief getLogPartF
534  * @param e log target
535  * @param l store log string
536  */
537 void getLogPartF(const CicoEventInfo& e, std::string& l)
538 {
539     const char* opstr[] = {
540         "??",                       //       Ejnj Not Judgment
541         DEFJGCE_EQ,                     // "="   Ejeq equal
542         DEFJGCE_NE,                     // "!="  Ejne Not equal
543         DEFJGCE_LT,                     // "<"   Ejlt less than
544         DEFJGCE_LE,                     // "<="  Ejle less than or equal
545         DEFJGCE_GT,                     // ">"   Ejgt greater than
546         DEFJGCE_GE,                     // ">="  Ejge greater than or equal
547         DEFJGCE_IN,                     // "in"  Ein  in active state object
548         DEFJGCE_NOT,                    // "not" Enot not active state object
549     };
550     std::stringstream stm;
551     stm << "EI:{";
552     if (!((e.getName()).empty())) {
553         stm << "\"" << e.getName() << "\"";
554     }
555     stm << ":" << e.getEV() << ":";
556     CicoEventInfo::E_JGCE jgce = e.getGCType();
557     if (CicoEventInfo::Ejnj != jgce) {
558         stm << opstr[jgce];
559     }
560     bool bInNot = false;
561     if ((CicoEventInfo::Ein == jgce)||
562         (CicoEventInfo::Enot == jgce)) {
563         bInNot = true;
564         stm << "(";
565     }
566     switch (jgce) {
567     case CicoEvent::EEvtGCInt:
568         stm << e.getGCVi();
569         break;
570     case CicoEvent::EEvtGCDouble:
571         stm << e.getGCVd();
572         break;
573     case CicoEvent::EEvtGCStr:
574         stm << "\"" << e.getGCVs() << "\"";
575         break;
576     default:                            // CicoEvent::EEvtGCUnset
577         break;
578     }
579     if (true == bInNot) {
580         stm << ")";
581     }
582 //    if (0 != e->m_vAgc.size()) {
583 //        stm << ":{...}";
584 //    }
585     stm << "}";
586     l = stm.str();
587 }
588
589 /**
590  * @brief operator ==
591  * @param a Comparison target A
592  * @param b Comparison target B
593  * @return
594  */
595 bool operator==(const CicoEventInfo& a, const CicoEventInfo& b)
596 {
597     if (a.getEV() == b.getEV()) {       // event No. match
598         bool anm = a.m_name.empty();
599         bool bnm = b.m_name.empty();
600         if ((true == anm) && (true == bnm)) { //
601             return true;
602         }
603         else if ((false == anm) && (false == bnm)) {
604             if (a.m_name == b.m_name) {
605                 return true;
606             }
607         }
608     }
609     return false;
610 }