6f273190dffcf2b76ebc4e7070fe6d328009dae3
[staging/basesystem.git] / video_in_hal / nsframework / framework_unified / client / NS_FrameworkCore / src / statemachine / frameworkunified_sm_conditionconnector.cpp
1 /*
2  * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 //////////////////////////////////////////////////////////////////////////////////////////////////
18 /// \defgroup <<Group Tag>> <<Group Name>>
19 /// \ingroup  tag_NSFramework
20 /// .
21 ///////////////////////////////////////////////////////////////////////////////////////////////////
22
23 ///////////////////////////////////////////////////////////////////////////////////////////////////
24 /// \ingroup  tag_NSFramework
25 /// \brief
26 ///
27 ///   This file has class definition of the CFrameworkunifiedConditionConnector. This class is responsible for
28 ///   implementing interfaces required to use condition connector in statemachine.
29 ///
30 ///////////////////////////////////////////////////////////////////////////////////////////////////
31
32 ///////////////////////////////////////////////////////////////////////////////////////////////////
33 // Include Files
34 ///////////////////////////////////////////////////////////////////////////////////////////////////
35 #include <native_service/frameworkunified_sm_conditionconnector.h>
36 #include <native_service/frameworkunified_sm_state.h>
37 #include <native_service/frameworkunified_sm_framework_types.h>
38 #include <native_service/frameworkunified_sm_guard.h>
39
40 #include <vector>
41 #include <string>
42
43 ///////////////////////////////////////////////////////////////////////////////////////////
44 /// CFrameworkunifiedConditionConnector
45 /// Parameterized constructor
46 ///////////////////////////////////////////////////////////////////////////////////////////
47 CFrameworkunifiedConditionConnector::CFrameworkunifiedConditionConnector(std::string f_strName): CFrameworkunifiedExternalTransition(NULL),
48   m_strName(f_strName) {
49   // Create the condition list
50   m_pConditionList = new std::vector<CCondition * >();
51 }
52
53 ///////////////////////////////////////////////////////////////////////////////////////////
54 /// ~CFrameworkunifiedConditionConnector
55 /// destructor
56 ///////////////////////////////////////////////////////////////////////////////////////////
57 CFrameworkunifiedConditionConnector::~CFrameworkunifiedConditionConnector() {
58   if (m_pConditionList) {
59     TConditionIterator l_itConditionList =  m_pConditionList->begin();
60
61     while (m_pConditionList->end() != l_itConditionList) {
62       delete *l_itConditionList;
63       l_itConditionList++;
64     }
65     // clear condition list
66     m_pConditionList->clear();
67     delete m_pConditionList;
68   }
69 }
70
71 ///////////////////////////////////////////////////////////////////////////////////////////
72 /// CCondition
73 /// Parameterized constructor
74 ///////////////////////////////////////////////////////////////////////////////////////////
75 CFrameworkunifiedConditionConnector::CCondition::CCondition(CFrameworkunifiedGuard *f_pGuard, CFrameworkunifiedState *f_pTargetState)
76   : m_pGuard(f_pGuard), m_pTargetState(f_pTargetState) {
77 }
78
79 ///////////////////////////////////////////////////////////////////////////////////////////
80 /// FrameworkunifiedReaction
81 /// This API evaluates the guards added in the condition list. If the guard is evaluated as
82 /// true then statemachine transitions to target state associated with guard.
83 //////////////////////////////////////////////////////////////////////////////////////////////
84 CFrameworkunifiedState *CFrameworkunifiedConditionConnector::FrameworkunifiedReaction(CFrameworkunifiedState *f_pSourceState, CEventDataPtr f_pData) {
85   CFrameworkunifiedState *l_pActiveState = NULL;
86   BOOL l_bAllowTransition = FALSE;
87   try {
88     CHKNULL(f_pSourceState);
89     CHKNULL(m_pConditionList);
90     TConditionIterator l_itConditionList
91       =  m_pConditionList->begin();
92
93     // iterate condition list and set the target state
94     while (m_pConditionList->end() != l_itConditionList) {
95       CCondition *l_pCondition = *l_itConditionList;
96       CHKNULL(l_pCondition);
97
98       CFrameworkunifiedGuard *l_pGuard = l_pCondition->m_pGuard;
99       CFrameworkunifiedState *l_pTargetState = l_pCondition->m_pTargetState;
100
101       CHKNULL(l_pGuard);
102       CHKNULL(l_pTargetState);
103
104       if (l_pGuard->FrameworkunifiedEvaluate()) {
105         m_pTargetState = l_pTargetState;
106         l_bAllowTransition = TRUE;
107         break;
108       }
109       l_itConditionList++;
110     }
111
112     if (l_bAllowTransition) {
113       // Transition to target state
114       l_pActiveState = ExecuteTransition(f_pSourceState, f_pData);
115
116     } else {
117       // No state changed
118       FRAMEWORKUNIFIEDLOG(ZONE_NS_SM_USR_INFO, __FUNCTION__,
119              "Guard condition for External Transition from state %s to condition connector %s failed",
120              f_pSourceState->m_strStateName.c_str(), m_strName.c_str());
121
122       l_pActiveState = f_pSourceState;
123     }
124   } catch (std::exception &e) {
125     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Exception %s", e.what());
126     return NULL;
127   }
128   return l_pActiveState;
129 }
130
131 ///////////////////////////////////////////////////////////////////////////////////////////
132 /// FrameworkunifiedAddCondition
133 /// Adds condition to condition list
134 //////////////////////////////////////////////////////////////////////////////////////////////
135 EFrameworkunifiedStatus CFrameworkunifiedConditionConnector::FrameworkunifiedAddCondition(CFrameworkunifiedGuard *f_pGuard, CFrameworkunifiedState *f_pTargetState) {
136   EFrameworkunifiedStatus l_eFrameworkunifiedStatus = eFrameworkunifiedStatusOK;
137   try {
138     CHKNULL(m_pConditionList);
139     CHKNULL(f_pGuard);
140     CHKNULL(f_pTargetState);
141
142     CCondition *l_pCondition = new CCondition(f_pGuard, f_pTargetState);
143
144     m_pConditionList->push_back(l_pCondition);
145   } catch (std::exception &e) {
146     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Exception %s", e.what());
147     l_eFrameworkunifiedStatus = eFrameworkunifiedStatusNullPointer;
148   }
149
150
151   return l_eFrameworkunifiedStatus;
152 }