241 lines
6.5 KiB
Python
241 lines
6.5 KiB
Python
"""Implementation of statechart a.
|
|
Generated by itemis CREATE code generator.
|
|
"""
|
|
|
|
import queue
|
|
import sys, os
|
|
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '../lib')))
|
|
from yakindu.rx import Observable
|
|
|
|
class A:
|
|
"""Implementation of the state machine A.
|
|
"""
|
|
|
|
class State:
|
|
""" State Enum
|
|
"""
|
|
(
|
|
main_region_state_a,
|
|
null_state
|
|
) = range(2)
|
|
|
|
|
|
def __init__(self):
|
|
""" Declares all necessary variables including list of states, histories etc.
|
|
"""
|
|
|
|
self.x = None
|
|
self.x_observable = Observable()
|
|
self.y = None
|
|
self.y_observable = Observable()
|
|
|
|
self.in_event_queue = queue.Queue()
|
|
# enumeration of all states:
|
|
self.__State = A.State
|
|
self.__state_conf_vector_changed = None
|
|
self.__state_vector = [None] * 1
|
|
for __state_index in range(1):
|
|
self.__state_vector[__state_index] = self.State.null_state
|
|
|
|
# for timed statechart:
|
|
self.timer_service = None
|
|
self.__time_events = [None] * 2
|
|
|
|
# initializations:
|
|
self.__is_executing = False
|
|
|
|
def is_active(self):
|
|
"""Checks if the state machine is active.
|
|
"""
|
|
return self.__state_vector[0] is not self.__State.null_state
|
|
|
|
def is_final(self):
|
|
"""Checks if the statemachine is final.
|
|
Always returns 'false' since this state machine can never become final.
|
|
"""
|
|
return False
|
|
|
|
def is_state_active(self, state):
|
|
"""Checks if the state is currently active.
|
|
"""
|
|
s = state
|
|
if s == self.__State.main_region_state_a:
|
|
return self.__state_vector[0] == self.__State.main_region_state_a
|
|
return False
|
|
|
|
def time_elapsed(self, event_id):
|
|
"""Add time events to in event queue
|
|
"""
|
|
if event_id in range(2):
|
|
self.in_event_queue.put(lambda: self.raise_time_event(event_id))
|
|
self.run_cycle()
|
|
|
|
def raise_time_event(self, event_id):
|
|
"""Raise timed events using the event_id.
|
|
"""
|
|
self.__time_events[event_id] = True
|
|
|
|
def __execute_queued_event(self, func):
|
|
func()
|
|
|
|
def __get_next_event(self):
|
|
if not self.in_event_queue.empty():
|
|
return self.in_event_queue.get()
|
|
return None
|
|
|
|
def __entry_action_main_region_state_a(self):
|
|
"""Entry action for state 'StateA'..
|
|
"""
|
|
#Entry action for state 'StateA'.
|
|
self.timer_service.set_timer(self, 0, (1 * 1000), False)
|
|
self.timer_service.set_timer(self, 1, (2 * 1000), False)
|
|
|
|
def __exit_action_main_region_state_a(self):
|
|
"""Exit action for state 'StateA'..
|
|
"""
|
|
#Exit action for state 'StateA'.
|
|
self.timer_service.unset_timer(self, 0)
|
|
self.timer_service.unset_timer(self, 1)
|
|
|
|
def __enter_sequence_main_region_state_a_default(self):
|
|
"""'default' enter sequence for state StateA.
|
|
"""
|
|
#'default' enter sequence for state StateA
|
|
self.__entry_action_main_region_state_a()
|
|
self.__state_vector[0] = self.State.main_region_state_a
|
|
self.__state_conf_vector_changed = True
|
|
|
|
def __enter_sequence_main_region_default(self):
|
|
"""'default' enter sequence for region main region.
|
|
"""
|
|
#'default' enter sequence for region main region
|
|
self.__react_main_region__entry_default()
|
|
|
|
def __exit_sequence_main_region_state_a(self):
|
|
"""Default exit sequence for state StateA.
|
|
"""
|
|
#Default exit sequence for state StateA
|
|
self.__state_vector[0] = self.State.null_state
|
|
self.__exit_action_main_region_state_a()
|
|
|
|
def __exit_sequence_main_region(self):
|
|
"""Default exit sequence for region main region.
|
|
"""
|
|
#Default exit sequence for region main region
|
|
state = self.__state_vector[0]
|
|
if state == self.State.main_region_state_a:
|
|
self.__exit_sequence_main_region_state_a()
|
|
|
|
def __react_main_region__entry_default(self):
|
|
"""Default react sequence for initial entry .
|
|
"""
|
|
#Default react sequence for initial entry
|
|
self.__enter_sequence_main_region_state_a_default()
|
|
|
|
def __react(self, transitioned_before):
|
|
"""Implementation of __react function.
|
|
"""
|
|
#State machine reactions.
|
|
return transitioned_before
|
|
|
|
|
|
def __main_region_state_a_react(self, transitioned_before):
|
|
"""Implementation of __main_region_state_a_react function.
|
|
"""
|
|
#The reactions of state StateA.
|
|
transitioned_after = transitioned_before
|
|
if transitioned_after < 0:
|
|
if self.__time_events[0]:
|
|
self.__exit_sequence_main_region_state_a()
|
|
self.x_observable.next()
|
|
self.__time_events[0] = False
|
|
self.__enter_sequence_main_region_state_a_default()
|
|
self.__react(0)
|
|
transitioned_after = 0
|
|
elif self.__time_events[1]:
|
|
self.__exit_sequence_main_region_state_a()
|
|
self.y_observable.next()
|
|
self.__time_events[1] = False
|
|
self.__enter_sequence_main_region_state_a_default()
|
|
self.__react(0)
|
|
transitioned_after = 0
|
|
#If no transition was taken
|
|
if transitioned_after == transitioned_before:
|
|
#then execute local reactions.
|
|
transitioned_after = self.__react(transitioned_before)
|
|
return transitioned_after
|
|
|
|
|
|
def __clear_in_events(self):
|
|
"""Implementation of __clear_in_events function.
|
|
"""
|
|
self.__time_events[0] = False
|
|
self.__time_events[1] = False
|
|
|
|
|
|
def __micro_step(self):
|
|
"""Implementation of __micro_step function.
|
|
"""
|
|
state = self.__state_vector[0]
|
|
if state == self.State.main_region_state_a:
|
|
self.__main_region_state_a_react(-1)
|
|
|
|
|
|
def run_cycle(self):
|
|
"""Implementation of run_cycle function.
|
|
"""
|
|
#Performs a 'run to completion' step.
|
|
if self.timer_service is None:
|
|
raise ValueError('Timer service must be set.')
|
|
|
|
if self.__is_executing:
|
|
return
|
|
self.__is_executing = True
|
|
next_event = self.__get_next_event()
|
|
if next_event is not None:
|
|
self.__execute_queued_event(next_event)
|
|
condition_0 = True
|
|
while condition_0:
|
|
self.__micro_step()
|
|
self.__clear_in_events()
|
|
condition_0 = False
|
|
next_event = self.__get_next_event()
|
|
if next_event is not None:
|
|
self.__execute_queued_event(next_event)
|
|
condition_0 = True
|
|
self.__is_executing = False
|
|
|
|
|
|
def enter(self):
|
|
"""Implementation of enter function.
|
|
"""
|
|
#Activates the state machine.
|
|
if self.timer_service is None:
|
|
raise ValueError('Timer service must be set.')
|
|
|
|
if self.__is_executing:
|
|
return
|
|
self.__is_executing = True
|
|
#Default enter sequence for statechart A
|
|
self.__enter_sequence_main_region_default()
|
|
self.__is_executing = False
|
|
|
|
|
|
def exit(self):
|
|
"""Implementation of exit function.
|
|
"""
|
|
#Deactivates the state machine.
|
|
if self.__is_executing:
|
|
return
|
|
self.__is_executing = True
|
|
#Default exit sequence for statechart A
|
|
self.__exit_sequence_main_region()
|
|
self.__state_vector[0] = self.State.null_state
|
|
self.__is_executing = False
|
|
|
|
|
|
def trigger_without_event(self):
|
|
"""Implementation of triggerWithoutEvent function.
|
|
"""
|
|
self.run_cycle()
|
|
|