2 * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 //////////////////////////////////////////////////////////////////////////////////////////////////
18 /// \defgroup <<Group Tag>> <<Group Name>>
19 /// \ingroup tag_NSFramework
21 ///////////////////////////////////////////////////////////////////////////////////////////////////
23 ///////////////////////////////////////////////////////////////////////////////////////////////////
24 /// \ingroup tag_NSFramework
27 /// This file has class definition of the CFrameworkunifiedConditionConnector. This class is responsible for
28 /// implementing interfaces required to use condition connector in statemachine.
30 ///////////////////////////////////////////////////////////////////////////////////////////////////
32 ///////////////////////////////////////////////////////////////////////////////////////////////////
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>
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 * >();
53 ///////////////////////////////////////////////////////////////////////////////////////////
54 /// ~CFrameworkunifiedConditionConnector
56 ///////////////////////////////////////////////////////////////////////////////////////////
57 CFrameworkunifiedConditionConnector::~CFrameworkunifiedConditionConnector() {
58 if (m_pConditionList) {
59 TConditionIterator l_itConditionList = m_pConditionList->begin();
61 while (m_pConditionList->end() != l_itConditionList) {
62 delete *l_itConditionList;
65 // clear condition list
66 m_pConditionList->clear();
67 delete m_pConditionList;
71 ///////////////////////////////////////////////////////////////////////////////////////////
73 /// Parameterized constructor
74 ///////////////////////////////////////////////////////////////////////////////////////////
75 CFrameworkunifiedConditionConnector::CCondition::CCondition(CFrameworkunifiedGuard *f_pGuard, CFrameworkunifiedState *f_pTargetState)
76 : m_pGuard(f_pGuard), m_pTargetState(f_pTargetState) {
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;
88 CHKNULL(f_pSourceState);
89 CHKNULL(m_pConditionList);
90 TConditionIterator l_itConditionList
91 = m_pConditionList->begin();
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);
98 CFrameworkunifiedGuard *l_pGuard = l_pCondition->m_pGuard;
99 CFrameworkunifiedState *l_pTargetState = l_pCondition->m_pTargetState;
102 CHKNULL(l_pTargetState);
104 if (l_pGuard->FrameworkunifiedEvaluate()) {
105 m_pTargetState = l_pTargetState;
106 l_bAllowTransition = TRUE;
112 if (l_bAllowTransition) {
113 // Transition to target state
114 l_pActiveState = ExecuteTransition(f_pSourceState, f_pData);
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());
122 l_pActiveState = f_pSourceState;
124 } catch (std::exception &e) {
125 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Exception %s", e.what());
128 return l_pActiveState;
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;
138 CHKNULL(m_pConditionList);
140 CHKNULL(f_pTargetState);
142 CCondition *l_pCondition = new CCondition(f_pGuard, f_pTargetState);
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;
151 return l_eFrameworkunifiedStatus;