From 9e5ea6ab1ed9a1287fec132f50c9812fa2509e0a Mon Sep 17 00:00:00 2001 From: Dirk Ziegelmeier Date: Wed, 21 Aug 2024 21:05:31 +0200 Subject: [PATCH] Work on correctly generating state transitions --- .../generator/InstanceHandlerTransition.egl | 18 ++++++-- generator/src/generator/Transformations.egl | 25 +++++++---- test/generated/IXmiTestImpl.hxx | 2 +- test/generated/XmiTestDeclaration.hxx | 2 +- test/generated/XmiTestInstance.hxx | 43 ++++++++++--------- 5 files changed, 57 insertions(+), 33 deletions(-) diff --git a/generator/src/generator/InstanceHandlerTransition.egl b/generator/src/generator/InstanceHandlerTransition.egl index bf56c08..b243841 100644 --- a/generator/src/generator/InstanceHandlerTransition.egl +++ b/generator/src/generator/InstanceHandlerTransition.egl @@ -1,13 +1,24 @@ - [%=transition.actions_list%] +[% + var actions = ""; + var closing_bracket = ""; + if (transition.actions_list != null) { + actions = ", kActions"; + closing_bracket = "}"; +%] + { + static const auto kActions = std::to_array<[%=statemachine.name%]::[%=statemachine.transition_action_type%]>({[%=transition.actions_list%]}); +[% + } +%] [% if (transition.internal) { %] - return [%=statemachine.name%]::NoTransition(); + return [%=statemachine.name%]::TransitionTo([%=statemachine.name%]::kNone[%=actions%]); [% } if (transition.target_variable_name != null) { %] - return [%=statemachine.name%]::TransitionTo([%=statemachine.name%]::[%=transition.target_variable_name%]); + return [%=statemachine.name%]::TransitionTo([%=statemachine.name%]::[%=transition.target_variable_name%][%=actions%]); [% } @@ -16,4 +27,5 @@ transitionRootTemplate.populate("statemachine", statemachine); transitionRootTemplate.populate("transition", transition); %] +[%=closing_bracket%] [%=transitionRootTemplate.process()%] \ No newline at end of file diff --git a/generator/src/generator/Transformations.egl b/generator/src/generator/Transformations.egl index a880fb1..fe1ec45 100644 --- a/generator/src/generator/Transformations.egl +++ b/generator/src/generator/Transformations.egl @@ -128,7 +128,7 @@ operation Tuple stateTransform(statemachine : Tuple, region : Tuple, parent_stat transitions.trans_unconditional = new List(); for (transition in self.transitions_for_trigger.get(trigger)) { - transition.transitionTransform(statemachine, self, trigger); + transition.transitionTransform(statemachine, self, trigger, new List()); if (not transition.guards.isEmpty()) { transitions.trans_conditional.add(transition); @@ -144,17 +144,17 @@ operation Tuple stateTransform(statemachine : Tuple, region : Tuple, parent_stat } } -operation Tuple transitionTransform(statemachine : Tuple, state : Tuple, trigger: String) { +operation Tuple transitionTransform(statemachine : Tuple, state : Tuple, trigger: String, all_actions: List) { self.trans_conditional = new List(); self.trans_unconditional = new List(); - self.actions_list = ""; self.guards_list = ""; self.target_variable_name = null; + self.actions_list = null; // Collect all actions var actions = new List(); for (action in self.actions) { - actions.add("impl->" + action + "(event)"); + all_actions.add("&" + statemachine.name + "::" + statemachine.impl_type + "::" + action); if (not statemachine.all_actions.includes(action)) { if (action == "") { (sm.name + ": Empty function behavior in transition starting from state " + self.start).println(); @@ -162,9 +162,6 @@ operation Tuple transitionTransform(statemachine : Tuple, state : Tuple, trigger statemachine.all_actions.add(action); } } - if (not actions.isEmpty()) { - self.actions_list = actions.concat("; ") + ";"; - } // Collect all guards and create guards_list var guards = new List(); @@ -175,6 +172,12 @@ operation Tuple transitionTransform(statemachine : Tuple, state : Tuple, trigger } } self.guards_list = guards.concat(" && "); + + if (self.internal) { + if (not all_actions.isEmpty()) { + self.actions_list = all_actions.concat(", "); + } + } // Transition has a target state if(not self.internal and (self.target_state_id != null)) { @@ -183,11 +186,17 @@ operation Tuple transitionTransform(statemachine : Tuple, state : Tuple, trigger ("Unable to find state id " + self.target_state_id).println(); } self.target_variable_name = target.variable_name; + + if (not all_actions.isEmpty()) { + self.actions_list = all_actions.concat(", "); + } } // Transform subtransitions (choice/junction) for (transition in self.all_transitions) { - transition.transitionTransform(statemachine, state, trigger); + var new_all_actions = new List(); + new_all_actions.addAll(all_actions); + transition.transitionTransform(statemachine, state, trigger, new_all_actions); if (not transition.guards.isEmpty()) { self.trans_conditional.add(transition); diff --git a/test/generated/IXmiTestImpl.hxx b/test/generated/IXmiTestImpl.hxx index 41109d9..c41c93d 100644 --- a/test/generated/IXmiTestImpl.hxx +++ b/test/generated/IXmiTestImpl.hxx @@ -1,5 +1,5 @@ // Statemachine XmiTest implementation interface -// Generated: 21.08.24, 08:12 +// Generated: 21.08.24, 21:04 #pragma once diff --git a/test/generated/XmiTestDeclaration.hxx b/test/generated/XmiTestDeclaration.hxx index 61aab43..2b3ac52 100644 --- a/test/generated/XmiTestDeclaration.hxx +++ b/test/generated/XmiTestDeclaration.hxx @@ -1,5 +1,5 @@ // Statemachine XmiTest declaration -// Generated: 21.08.24, 08:12 +// Generated: 21.08.24, 21:03 #pragma once diff --git a/test/generated/XmiTestInstance.hxx b/test/generated/XmiTestInstance.hxx index dccf8c6..c13f2fc 100644 --- a/test/generated/XmiTestInstance.hxx +++ b/test/generated/XmiTestInstance.hxx @@ -1,5 +1,5 @@ // Statemachine XmiTest instance -// Generated: 21.08.24, 08:12 +// Generated: 21.08.24, 21:04 #pragma once @@ -36,8 +36,7 @@ static XmiTest::Transition State_1StateWithSameNameHandler(XmiTest::ImplPtr impl switch(XmiTest_GET_INSTANCE_EVENT_ID(event)) { case XmiTest_GET_STATIC_EVENT_ID(Transition_7): - - return XmiTest::TransitionTo(XmiTest::kState_1State_4); + return XmiTest::TransitionTo(XmiTest::kState_1State_4); break; default: @@ -55,21 +54,24 @@ static XmiTest::Transition State_1State_2Handler(XmiTest::ImplPtr impl, XmiTest: switch(XmiTest_GET_INSTANCE_EVENT_ID(event)) { case XmiTest_GET_STATIC_EVENT_ID(Internal): - impl->State2InternalAction(event); - return XmiTest::NoTransition(); + { + static const auto kActions = std::to_array({&XmiTest::Impl::State2InternalAction}); + return XmiTest::TransitionTo(XmiTest::kNone, kActions); + } break; case XmiTest_GET_STATIC_EVENT_ID(Transition_3): if (impl->State2Transition3Guard(event)) { - impl->Transition3Action1(event); - return XmiTest::TransitionTo(XmiTest::kState_1StateWithSameName); + { + static const auto kActions = std::to_array({&XmiTest::Impl::Transition3Action1}); + return XmiTest::TransitionTo(XmiTest::kState_1StateWithSameName, kActions); + } } break; case XmiTest_GET_STATIC_EVENT_ID(Transition_6): - - return XmiTest::TransitionTo(XmiTest::kState_1State_4); + return XmiTest::TransitionTo(XmiTest::kState_1State_4); break; default: @@ -87,8 +89,7 @@ static XmiTest::Transition State_1State_4Handler(XmiTest::ImplPtr impl, XmiTest: switch(XmiTest_GET_INSTANCE_EVENT_ID(event)) { case XmiTest_GET_STATIC_EVENT_ID(Transition_8): - - return XmiTest::TransitionTo(XmiTest::kState_1State_2); + return XmiTest::TransitionTo(XmiTest::kState_1State_2); break; default: @@ -106,21 +107,24 @@ static XmiTest::Transition State_1State_4StateWithSameNameHandler(XmiTest::ImplP switch(XmiTest_GET_INSTANCE_EVENT_ID(event)) { case XmiTest_GET_STATIC_EVENT_ID(Transition_4): - - return XmiTest::TransitionTo(XmiTest::kState_1State_4State_5); + return XmiTest::TransitionTo(XmiTest::kState_1State_4State_5); break; case XmiTest_GET_STATIC_EVENT_ID(Transition_7): if (impl->ChoiceGuard1(event)) { - impl->ChoiceAction1(event); + if (impl->ChoiceGuard2(event)) { - impl->ChoiceAction2(event); - return XmiTest::TransitionTo(XmiTest::kState_1State_4State_5); + { + static const auto kActions = std::to_array({&XmiTest::Impl::ChoiceAction1, &XmiTest::Impl::ChoiceAction2}); + return XmiTest::TransitionTo(XmiTest::kState_1State_4State_5, kActions); + } + } + { + static const auto kActions = std::to_array({&XmiTest::Impl::ChoiceAction1, &XmiTest::Impl::ChoiceAction3}); + return XmiTest::TransitionTo(XmiTest::kState_1StateWithSameName, kActions); } - impl->ChoiceAction3(event); - return XmiTest::TransitionTo(XmiTest::kState_1StateWithSameName); } break; @@ -139,8 +143,7 @@ static XmiTest::Transition State_1State_4State_5Handler(XmiTest::ImplPtr impl, X switch(XmiTest_GET_INSTANCE_EVENT_ID(event)) { case XmiTest_GET_STATIC_EVENT_ID(Transition_3): - - return XmiTest::TransitionTo(XmiTest::kState_1State_4StateWithSameName); + return XmiTest::TransitionTo(XmiTest::kState_1State_4StateWithSameName); break; default: