Skip to content

Commit 52ed55c

Browse files
authored
Added Post Actions with first test (#9)
* Added Post Actions with first test
1 parent 7b0e409 commit 52ed55c

File tree

4 files changed

+105
-2
lines changed

4 files changed

+105
-2
lines changed

sample/sample4.xml

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?xml version="1.0"?>
2+
<x:States xmlns:x="pystatemachine:sm">
3+
<State>
4+
<Name>Enter</Name>
5+
<Event>
6+
<Name>ToExit</Name>
7+
<ToState>Exit</ToState>
8+
<PreConditions>
9+
<Condition>
10+
<Expression>testBaseStateMachine.everFalse</Expression>
11+
<Result>False</Result>
12+
</Condition>
13+
<Condition>
14+
<Expression>testBaseStateMachine.test</Expression>
15+
<Result>2</Result>
16+
</Condition>
17+
</PreConditions>
18+
<PreActions>
19+
<Action>
20+
<Expression>testBaseStateMachine.testPrint</Expression>
21+
</Action>
22+
</PreActions>
23+
</Event>
24+
</State>
25+
<State>
26+
<Name>Exit</Name>
27+
<Event>
28+
<Name>ToNull</Name>
29+
<ToState>Null</ToState>
30+
<PreConditions>
31+
<Condition>
32+
<Expression>testBaseStateMachine.everTrue</Expression>
33+
<Result>True</Result>
34+
</Condition>
35+
</PreConditions>
36+
<PostActions>
37+
<Action>
38+
<Expression>testBaseStateMachine.testPrint</Expression>
39+
</Action>
40+
</PostActions>
41+
</Event>
42+
</State>
43+
<State>
44+
<Name>Null</Name>
45+
<Event>
46+
<Name>ToNull</Name>
47+
<ToState>Null</ToState>
48+
<PostActions>
49+
<Action>
50+
<Expression>testBaseStateMachine.testPrint</Expression>
51+
</Action>
52+
</PostActions>
53+
</Event>
54+
</State>
55+
<Initial_State>Enter</Initial_State>
56+
</x:States>

src/ReadStateMachine.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,18 @@ def ReadStateMachineFile(xml_file : str):
8181

8282
elif event_child.tag == "PostActions":
8383
# State->Event->PostActions Element
84+
post_action_elements = []
8485
for postactions_child in event_child:
8586
#print(postactions_child.text)
8687
# State->Event->PostActions->Action Element
87-
None
88+
if postactions_child.tag == "Action":
89+
expression = ""
90+
for action_child in postactions_child:
91+
if action_child.tag == "Expression":
92+
expression = action_child.text
93+
post_action_elements.append(Action(expression))
94+
post_actions = Actions(pre_action_elements)
95+
8896
events[event_name] = Event(event_name,to_state,pre_conditions,post_conditions,pre_actions,post_actions)
8997
#print(event.to_string())
9098
elif state_child.tag == "Name":

src/StateMachine.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,19 @@ def InjectEvent(self, event : str):
7979
## Preactions
8080
all_pre_actions_executed = self.__ExecActions(handled_event.pre_actions)
8181
if(all_pre_actions_executed):
82+
## Save Old State
8283
## Transition
8384
logging.debug("Transition %s ------> %s", self.current_state, handled_event.to_state)
8485
self.current_state = handled_event.to_state
8586
## Postactions
86-
## Postconditions
87+
all_post_actions_executed = self.__ExecActions(handled_event.post_actions)
88+
if(all_post_actions_executed):
89+
None
90+
## Postconditions
91+
else:
92+
logging.error("Not all PostActions Executed")
93+
else:
94+
logging.error("Not all PretActions Executed")
8795
else:
8896
logging.error("Not all PreConditions satisfied")
8997
else:

tests/testBaseStateMachine.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
def everFalse():
1010
return False
1111

12+
def everTrue():
13+
return True
14+
1215
def testPrint():
1316
print("Test")
1417

@@ -92,6 +95,34 @@ def test3(self):
9295
sm.InjectEvent("ToNull")
9396
self.assertEqual(sm.get_current_state(),"Exit", "Should be Exit")
9497

98+
def test4(self):
99+
sm = StateMachine("../sample/sample4.xml")
100+
101+
sm.LoadStateMachine()
102+
103+
sm.addVariableToContext("testBaseStateMachine", "everFalse")
104+
sm.addVariableToContext("testBaseStateMachine", "everTrue")
105+
sm.addVariableToContext("testBaseStateMachine", "test")
106+
sm.addVariableToContext("testBaseStateMachine", "testPrint")
107+
108+
self.assertEqual(sm.get_current_state(),"Enter", "Should be Enter")
109+
# Precondition Verified / Execute PreAction
110+
sm.InjectEvent("ToExit")
111+
self.assertEqual(sm.get_current_state(),"Exit", "Should be Exit")
112+
# Not Valid Event
113+
sm.InjectEvent("ToExit")
114+
self.assertEqual(sm.get_current_state(),"Exit", "Should be Exit")
115+
116+
# Precondition Verified /Execute Post Action
117+
sm.InjectEvent("ToNull")
118+
self.assertEqual(sm.get_current_state(),"Null", "Should be Null")
119+
# Not Valid Event
120+
sm.InjectEvent("ToExit")
121+
self.assertEqual(sm.get_current_state(),"Null", "Should be Null")
122+
# Precondition Verified /Execute Post Action
123+
sm.InjectEvent("ToNull")
124+
self.assertEqual(sm.get_current_state(),"Null", "Should be Null")
125+
95126

96127

97128
if __name__ == '__main__':

0 commit comments

Comments
 (0)