Skip to content

Commit

Permalink
Work on correctly generating state transitions
Browse files Browse the repository at this point in the history
  • Loading branch information
dziegel committed Aug 21, 2024
1 parent cfe6bc1 commit 9e5ea6a
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 33 deletions.
18 changes: 15 additions & 3 deletions generator/src/generator/InstanceHandlerTransition.egl
Original file line number Diff line number Diff line change
@@ -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%]);
[%
}

Expand All @@ -16,4 +27,5 @@
transitionRootTemplate.populate("statemachine", statemachine);
transitionRootTemplate.populate("transition", transition);
%]
[%=closing_bracket%]
[%=transitionRootTemplate.process()%]
25 changes: 17 additions & 8 deletions generator/src/generator/Transformations.egl
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -144,27 +144,24 @@ 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();
}
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();
Expand All @@ -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)) {
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion test/generated/IXmiTestImpl.hxx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Statemachine XmiTest implementation interface
// Generated: 21.08.24, 08:12
// Generated: 21.08.24, 21:04

#pragma once

Expand Down
2 changes: 1 addition & 1 deletion test/generated/XmiTestDeclaration.hxx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Statemachine XmiTest declaration
// Generated: 21.08.24, 08:12
// Generated: 21.08.24, 21:03

#pragma once

Expand Down
43 changes: 23 additions & 20 deletions test/generated/XmiTestInstance.hxx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Statemachine XmiTest instance
// Generated: 21.08.24, 08:12
// Generated: 21.08.24, 21:04

#pragma once

Expand Down Expand Up @@ -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:
Expand All @@ -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::Transition::ActionType>({&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::Transition::ActionType>({&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:
Expand All @@ -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:
Expand All @@ -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::Transition::ActionType>({&XmiTest::Impl::ChoiceAction1, &XmiTest::Impl::ChoiceAction2});
return XmiTest::TransitionTo(XmiTest::kState_1State_4State_5, kActions);
}
}
{
static const auto kActions = std::to_array<XmiTest::Transition::ActionType>({&XmiTest::Impl::ChoiceAction1, &XmiTest::Impl::ChoiceAction3});
return XmiTest::TransitionTo(XmiTest::kState_1StateWithSameName, kActions);
}
impl->ChoiceAction3(event);
return XmiTest::TransitionTo(XmiTest::kState_1StateWithSameName);
}
break;

Expand All @@ -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:
Expand Down

0 comments on commit 9e5ea6a

Please sign in to comment.