diff --git a/org.yakindu.sct.examples.trafficlight.python/.project b/com.yakindu.sct.examples.pong.python/.project similarity index 92% rename from org.yakindu.sct.examples.trafficlight.python/.project rename to com.yakindu.sct.examples.pong.python/.project index 54f4a3ac..5fab0d43 100644 --- a/org.yakindu.sct.examples.trafficlight.python/.project +++ b/com.yakindu.sct.examples.pong.python/.project @@ -1,17 +1,17 @@ - org.yakindu.sct.examples.trafficlight.python + com.yakindu.sct.examples.pong.python - org.python.pydev.PyDevBuilder + org.eclipse.xtext.ui.shared.xtextBuilder - org.eclipse.xtext.ui.shared.xtextBuilder + org.python.pydev.PyDevBuilder @@ -22,8 +22,8 @@ - org.yakindu.sct.builder.SCTNature - org.eclipse.xtext.ui.shared.xtextNature org.python.pydev.pythonNature + org.eclipse.xtext.ui.shared.xtextNature + org.yakindu.sct.builder.SCTNature diff --git a/org.yakindu.sct.examples.trafficlight.python/.pydevproject b/com.yakindu.sct.examples.pong.python/.pydevproject similarity index 63% rename from org.yakindu.sct.examples.trafficlight.python/.pydevproject rename to com.yakindu.sct.examples.pong.python/.pydevproject index 40e9f40a..ad74947d 100644 --- a/org.yakindu.sct.examples.trafficlight.python/.pydevproject +++ b/com.yakindu.sct.examples.pong.python/.pydevproject @@ -1,5 +1,8 @@ + +/${PROJECT_DIR_NAME} + +python interpreter Default -python 2.7 diff --git a/com.yakindu.sct.examples.pong.python/code_gen.sgen b/com.yakindu.sct.examples.pong.python/code_gen.sgen new file mode 100644 index 00000000..1458223d --- /dev/null +++ b/com.yakindu.sct.examples.pong.python/code_gen.sgen @@ -0,0 +1,10 @@ +GeneratorModel for yakindu::python { + + statechart pong_npc { + + feature Outlet { + targetProject = "com.yakindu.sct.examples.pong.python" + targetFolder = "" + } + } +} \ No newline at end of file diff --git a/com.yakindu.sct.examples.pong.python/main.py b/com.yakindu.sct.examples.pong.python/main.py new file mode 100644 index 00000000..e8b4d2cc --- /dev/null +++ b/com.yakindu.sct.examples.pong.python/main.py @@ -0,0 +1,58 @@ +import pygame +from pong.pong_ui import PongUI +from pong.pong_game import PongGame +from pong_npc.pong_npc_statemachine import Pong_npc + +if __name__ == "__main__": + pong_game = PongGame() + ui = PongUI(pygame, pong_game) + pong_npc = Pong_npc() + pong_npc.init() + pong_npc.enter() + + def loop(): + pressed_up_w = False + pressed_down_w = False + while True: + for event in pygame.event.get(): + if event.type is pygame.QUIT: + return False + if event.type is pygame.KEYDOWN: + if event.key is pygame.K_w: + pong_game.move(pong_game.UP, pong_game.PLAYER_ONE) + pressed_up_w = True + if event.key is pygame.K_s: + pong_game.move(pong_game.DOWN, pong_game.PLAYER_ONE) + pressed_down_w = True + + keys = pygame.key.get_pressed() + if pressed_up_w: + pressed_up_w = False + else: + if keys[pygame.K_w]: + pong_game.move(pong_game.UP, pong_game.PLAYER_ONE) + + if pressed_down_w: + pressed_down_w = False + else: + if keys[pygame.K_s]: + pong_game.move(pong_game.DOWN, pong_game.PLAYER_ONE) + + pong_npc.sci_ball.direction = pong_game.ball_direction + pong_npc.sci_ball.position = pong_game.ball_pos[1] + PongUI.BALL_RADIUS/2 + pong_npc.sci_player.position = pong_game.player2_position + PongUI.PLAYER_HEIGHT / 2 + + print(pong_npc.sci_player.position) + + pong_npc.run_cycle() + + if(pong_npc.sci_interface.is_raised_up()): + pong_game.move(pong_game.UP, pong_game.PLAYER_TWO) + if(pong_npc.sci_interface.is_raised_down()): + pong_game.move(pong_game.DOWN, pong_game.PLAYER_TWO) + + pong_game.update_ball() + ui.update() + + loop() + pygame.quit() diff --git a/com.yakindu.sct.examples.pong.python/pong/__init__.py b/com.yakindu.sct.examples.pong.python/pong/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/com.yakindu.sct.examples.pong.python/pong/__pycache__/__init__.cpython-36.pyc b/com.yakindu.sct.examples.pong.python/pong/__pycache__/__init__.cpython-36.pyc new file mode 100644 index 00000000..e4c69287 Binary files /dev/null and b/com.yakindu.sct.examples.pong.python/pong/__pycache__/__init__.cpython-36.pyc differ diff --git a/com.yakindu.sct.examples.pong.python/pong/__pycache__/pong_game.cpython-36.pyc b/com.yakindu.sct.examples.pong.python/pong/__pycache__/pong_game.cpython-36.pyc new file mode 100644 index 00000000..b95de175 Binary files /dev/null and b/com.yakindu.sct.examples.pong.python/pong/__pycache__/pong_game.cpython-36.pyc differ diff --git a/com.yakindu.sct.examples.pong.python/pong/__pycache__/pong_ui.cpython-36.pyc b/com.yakindu.sct.examples.pong.python/pong/__pycache__/pong_ui.cpython-36.pyc new file mode 100644 index 00000000..825348b2 Binary files /dev/null and b/com.yakindu.sct.examples.pong.python/pong/__pycache__/pong_ui.cpython-36.pyc differ diff --git a/com.yakindu.sct.examples.pong.python/pong/pong_game.py b/com.yakindu.sct.examples.pong.python/pong/pong_game.py new file mode 100644 index 00000000..49b476b5 --- /dev/null +++ b/com.yakindu.sct.examples.pong.python/pong/pong_game.py @@ -0,0 +1,75 @@ +from pong.pong_ui import PongUI +from random import uniform +import math + + +class PongGame: + UP = 'up' + DOWN = 'down' + PLAYER_ONE = 1 + PLAYER_TWO = 2 + RIGHT = 'right' + LEFT = 'left' + + def __init__(self): + starting_position = PongUI.get_starting_position() + self.player1_position = starting_position + self.player2_position = starting_position + self.ball_pos = [int((PongUI.WINDOW_WIDTH / 2.)), int(PongUI.WINDOW_HEIGHT / 2)] + self.ball_direction = self.RIGHT + self.ball_angle = 0 + self.ball_speed = 20 + + def move(self, direction, player): + if player == self.PLAYER_ONE and not self.border_collision(self.player1_position, direction): + if direction == self.UP: + self.player1_position -= 10 + if direction == self.DOWN: + self.player1_position += 10 + + if player == self.PLAYER_TWO and not self.border_collision(self.player2_position, direction): + if direction == self.UP: + self.player2_position -= 10 + if direction == self.DOWN: + self.player2_position += 10 + + def border_collision(self, position, direction): + if position == 0 and direction == self.UP: + return True + if position == PongUI.WINDOW_HEIGHT - PongUI.PLAYER_HEIGHT and direction == self.DOWN: + return True + return False + + def check_for_collisions(self): + if self.ball_pos[1] - PongUI.BALL_RADIUS <= 0 or self.ball_pos[1] + PongUI.BALL_RADIUS >= PongUI.WINDOW_HEIGHT: + self.ball_angle *= -1 + if self.ball_direction == self.RIGHT: + if self.player2_position <= self.ball_pos[1] <= self.player2_position + PongUI.PLAYER_HEIGHT: + if PongUI.WINDOW_WIDTH - PongUI.PLAYER_OFFSET >= self.ball_pos[0] >= \ + PongUI.WINDOW_WIDTH - PongUI.PLAYER_OFFSET - PongUI.PLAYER_WIDTH: + self.ball_direction = self.LEFT + self.update_angle_after_hit() + + if self.ball_direction == self.LEFT: + if self.player1_position <= self.ball_pos[1] <= self.player1_position + PongUI.PLAYER_HEIGHT: + if PongUI.PLAYER_OFFSET <= self.ball_pos[0] <= PongUI.PLAYER_OFFSET + PongUI.PLAYER_WIDTH: + self.ball_direction = self.RIGHT + self.update_angle_after_hit() + + def update_angle_after_hit(self): + self.ball_angle = int(uniform(-30, 30)) + + def update_ball(self): + self.check_for_collisions() + angle_r = math.radians(self.ball_angle) + dx = int(math.cos(angle_r) * self.ball_speed) + dy = int(math.sin(angle_r) * self.ball_speed) + if self.ball_direction == self.RIGHT: + self.ball_pos[0] = self.ball_pos[0] + dx + self.ball_pos[1] = self.ball_pos[1] - dy + if self.ball_direction == self.LEFT: + self.ball_pos[0] = self.ball_pos[0] - dx + self.ball_pos[1] = self.ball_pos[1] + dy + + def restart(self): + self.__init__() diff --git a/com.yakindu.sct.examples.pong.python/pong/pong_ui.py b/com.yakindu.sct.examples.pong.python/pong/pong_ui.py new file mode 100644 index 00000000..291ac1d6 --- /dev/null +++ b/com.yakindu.sct.examples.pong.python/pong/pong_ui.py @@ -0,0 +1,44 @@ +class PongUI: + WHITE = (255, 255, 255) + BLACK = (0, 0, 0) + WINDOW_WIDTH = 1200 + WINDOW_HEIGHT = 700 + PLAYER_WIDTH = 20 + PLAYER_HEIGHT = 100 + PLAYER_OFFSET = 100 + BALL_RADIUS = 20 + SIZE = (WINDOW_WIDTH, WINDOW_HEIGHT) + + def __init__(self, pygame, pong_game): + self.pygame = pygame + self.window = pygame.display.set_mode(self.SIZE) + self.timer = pygame.time.Clock() + self.pong_game = pong_game + self.rect_player1 = None + self.rect_player2 = None + + def draw_player(self): + self.rect_player1 = (self.PLAYER_OFFSET - self.PLAYER_WIDTH/2, self.pong_game.player1_position, self.PLAYER_WIDTH, self.PLAYER_HEIGHT) + self.pygame.draw.rect(self.window, self.WHITE, self.rect_player1) + + self.rect_player2 = ( + self.WINDOW_WIDTH - self.PLAYER_OFFSET - self.PLAYER_WIDTH/2, self.pong_game.player2_position, self.PLAYER_WIDTH, + self.PLAYER_HEIGHT) + self.pygame.draw.rect(self.window, self.WHITE, self.rect_player2) + + def draw_ball(self): + self.pygame.draw.circle(self.window, self.WHITE, self.pong_game.ball_pos, self.BALL_RADIUS) + + @staticmethod + def get_starting_position(): + return PongUI.WINDOW_HEIGHT / 2 - PongUI.PLAYER_HEIGHT / 2 + + def update(self): + self.window.fill(self.BLACK) + self.draw_player() + self.draw_ball() + self.pygame.display.update() + self.timer.tick(30) + + def restart(self): + self.__init__() diff --git a/com.yakindu.sct.examples.pong.python/pong_npc.png b/com.yakindu.sct.examples.pong.python/pong_npc.png new file mode 100644 index 00000000..237845bb Binary files /dev/null and b/com.yakindu.sct.examples.pong.python/pong_npc.png differ diff --git a/com.yakindu.sct.examples.pong.python/pong_npc.sct b/com.yakindu.sct.examples.pong.python/pong_npc.sct new file mode 100644 index 00000000..03fecb06 --- /dev/null +++ b/com.yakindu.sct.examples.pong.python/pong_npc.sct @@ -0,0 +1,401 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/com.yakindu.sct.examples.pong.python/pong_npc/__init__.py b/com.yakindu.sct.examples.pong.python/pong_npc/__init__.py new file mode 100644 index 00000000..25f977a9 --- /dev/null +++ b/com.yakindu.sct.examples.pong.python/pong_npc/__init__.py @@ -0,0 +1,6 @@ + +""" + +Empty file that initializes the package it is contained in. + +""" diff --git a/com.yakindu.sct.examples.pong.python/pong_npc/__pycache__/__init__.cpython-36.pyc b/com.yakindu.sct.examples.pong.python/pong_npc/__pycache__/__init__.cpython-36.pyc new file mode 100644 index 00000000..62f9ac16 Binary files /dev/null and b/com.yakindu.sct.examples.pong.python/pong_npc/__pycache__/__init__.cpython-36.pyc differ diff --git a/com.yakindu.sct.examples.pong.python/pong_npc/__pycache__/pong_npc_statemachine.cpython-36.pyc b/com.yakindu.sct.examples.pong.python/pong_npc/__pycache__/pong_npc_statemachine.cpython-36.pyc new file mode 100644 index 00000000..d72b5fb2 Binary files /dev/null and b/com.yakindu.sct.examples.pong.python/pong_npc/__pycache__/pong_npc_statemachine.cpython-36.pyc differ diff --git a/com.yakindu.sct.examples.pong.python/pong_npc/__pycache__/pong_npc_statemachine_interfaces.cpython-36.pyc b/com.yakindu.sct.examples.pong.python/pong_npc/__pycache__/pong_npc_statemachine_interfaces.cpython-36.pyc new file mode 100644 index 00000000..79319e6a Binary files /dev/null and b/com.yakindu.sct.examples.pong.python/pong_npc/__pycache__/pong_npc_statemachine_interfaces.cpython-36.pyc differ diff --git a/com.yakindu.sct.examples.pong.python/pong_npc/pong_npc_statemachine.py b/com.yakindu.sct.examples.pong.python/pong_npc/pong_npc_statemachine.py new file mode 100644 index 00000000..03114065 --- /dev/null +++ b/com.yakindu.sct.examples.pong.python/pong_npc/pong_npc_statemachine.py @@ -0,0 +1,352 @@ +# Implementation of statechart pong_npc_statemachine. + +import warnings +# implemented interfaces: +from .pong_npc_statemachine_interfaces import SCI_Internal +from .pong_npc_statemachine_interfaces import SCI_ball, SCI_player, SCI_Interface +# to store states: +from enum import Enum + +class Pong_npc: + + """ + + Implementation of the state machine 'Pong_npc'. + + """ + + def __init__(self): + """ Declares all necessary variables including list of states, histories etc. + """ + self.sci_internal = SCI_Internal() + self.sci_ball = SCI_ball() + self.sci_player = SCI_player() + self.sci_interface = SCI_Interface() + self.initialized = False + self.state_vector = [None] * 1 + self.next_state_index = None + # internal scope variables: + self.RIGHT = "right" + self.LEFT = "left" + self.MIDDLE = 350 + # enumeration of all states: + self.State = Enum('State', '\ + main_region_ball_moves_left,\ + main_region_ball_moves_left_r1_go_to_the_middle,\ + main_region_ball_moves_right,\ + main_region_ball_moves_right_r1_wait,\ + main_region_ball_moves_right_r1_go_to_the_ball,\ + main_region_ball_moves_right_r1_go_to_the_ball_r1_down,\ + main_region_ball_moves_right_r1_go_to_the_ball_r1_up,\ + null_state') + + def init(self): + """ Initializes the state machine by checking the timer, + initializing states and clearing events. + """ + self.initialized = True + for state_index in range(1): + self.state_vector[state_index] = self.State.null_state + self._clear_events() + self._clear_out_events() + self.sci_ball.position = 0.0 + self.sci_ball.direction = "" + self.sci_player.position = 0.0 + + def enter(self): + if self.initialized is not True: + raise ValueError( + 'The state machine needs to be initialized first by calling the init() function.') + self.enter_sequence_main_region_default() + + def exit(self): + """ Exit the the state machine. + """ + self.exit_sequence_main_region() + + def is_active(self): + """ @see IStatemachine#is_active() + """ + return (self.state_vector[0] is not self.State.null_state) + + def is_final(self): + """Always returns 'false' since this state machine can never become final. + @see IStatemachine#is_final() + """ + return False + + def _clear_events(self): + """ Resets incoming events (time events included). + """ + + def _clear_out_events(self): + """ Resets outgoing events. + """ + self.sci_interface.clear_out_events() + + def is_state_active(self, state): + """ Returns True if the given state is currently active otherwise false. + """ + s = state.name + if s == 'main_region_ball_moves_left_r1_go_to_the_middle': + return self.state_vector[0] == self.State.main_region_ball_moves_left_r1_go_to_the_middle + elif s == 'main_region_ball_moves_left': + return (self.state_vector[0].value >= self.State.main_region_ball_moves_left.value)\ + and (self.state_vector[0].value <= self.State.main_region_ball_moves_left_r1_go_to_the_middle.value) + elif s == 'main_region_ball_moves_right': + return (self.state_vector[0].value >= self.State.main_region_ball_moves_right.value)\ + and (self.state_vector[0].value <= self.State.main_region_ball_moves_right_r1_go_to_the_ball_r1_up.value) + elif s == 'main_region_ball_moves_right_r1_wait': + return self.state_vector[0] == self.State.main_region_ball_moves_right_r1_wait + elif s == 'main_region_ball_moves_right_r1_go_to_the_ball': + return (self.state_vector[0].value >= self.State.main_region_ball_moves_right_r1_go_to_the_ball.value)\ + and (self.state_vector[0].value <= self.State.main_region_ball_moves_right_r1_go_to_the_ball_r1_up.value) + elif s == 'main_region_ball_moves_right_r1_go_to_the_ball_r1_down': + return self.state_vector[0] == self.State.main_region_ball_moves_right_r1_go_to_the_ball_r1_down + elif s == 'main_region_ball_moves_right_r1_go_to_the_ball_r1_up': + return self.state_vector[0] == self.State.main_region_ball_moves_right_r1_go_to_the_ball_r1_up + else: + return False + + def enter_sequence_main_region_ball_moves_left_default(self): + self.enter_sequence_main_region_ball_moves_left_r1_default() + + def enter_sequence_main_region_ball_moves_left_r1_go_to_the_middle_default(self): + self.next_state_index = 0 + self.state_vector[0] = self.State.main_region_ball_moves_left_r1_go_to_the_middle + self.state_vector_changed = True + + def enter_sequence_main_region_ball_moves_right_default(self): + self.enter_sequence_main_region_ball_moves_right_r1_default() + + def enter_sequence_main_region_ball_moves_right_r1_wait_default(self): + self.next_state_index = 0 + self.state_vector[0] = self.State.main_region_ball_moves_right_r1_wait + self.state_vector_changed = True + + def enter_sequence_main_region_ball_moves_right_r1_go_to_the_ball_default(self): + self.enter_sequence_main_region_ball_moves_right_r1_go_to_the_ball_r1_default() + + def enter_sequence_main_region_ball_moves_right_r1_go_to_the_ball_r1_down_default(self): + self.next_state_index = 0 + self.state_vector[0] = self.State.main_region_ball_moves_right_r1_go_to_the_ball_r1_down + self.state_vector_changed = True + + def enter_sequence_main_region_ball_moves_right_r1_go_to_the_ball_r1_up_default(self): + self.next_state_index = 0 + self.state_vector[0] = self.State.main_region_ball_moves_right_r1_go_to_the_ball_r1_up + self.state_vector_changed = True + + def enter_sequence_main_region_default(self): + self.react_main_region__entry_default() + + def enter_sequence_main_region_ball_moves_left_r1_default(self): + self.react_main_region_ball_moves_left_r1__entry_default() + + def enter_sequence_main_region_ball_moves_right_r1_default(self): + self.react_main_region_ball_moves_right_r1__entry_default() + + def enter_sequence_main_region_ball_moves_right_r1_go_to_the_ball_r1_default(self): + self.react_main_region_ball_moves_right_r1_go_to_the_ball_r1__entry_default() + + def exit_sequence_main_region_ball_moves_left(self): + self.exit_sequence_main_region_ball_moves_left_r1() + + def exit_sequence_main_region_ball_moves_left_r1_go_to_the_middle(self): + self.next_state_index = 0 + self.state_vector[0] = self.State.null_state + + def exit_sequence_main_region_ball_moves_right(self): + self.exit_sequence_main_region_ball_moves_right_r1() + + def exit_sequence_main_region_ball_moves_right_r1_wait(self): + self.next_state_index = 0 + self.state_vector[0] = self.State.null_state + + def exit_sequence_main_region_ball_moves_right_r1_go_to_the_ball(self): + self.exit_sequence_main_region_ball_moves_right_r1_go_to_the_ball_r1() + + def exit_sequence_main_region_ball_moves_right_r1_go_to_the_ball_r1_down(self): + self.next_state_index = 0 + self.state_vector[0] = self.State.null_state + + def exit_sequence_main_region_ball_moves_right_r1_go_to_the_ball_r1_up(self): + self.next_state_index = 0 + self.state_vector[0] = self.State.null_state + + def exit_sequence_main_region(self): + state = self.state_vector[0].name + if state == 'main_region_ball_moves_left_r1_go_to_the_middle': + self.exit_sequence_main_region_ball_moves_left_r1_go_to_the_middle() + elif state == 'main_region_ball_moves_right_r1_wait': + self.exit_sequence_main_region_ball_moves_right_r1_wait() + elif state == 'main_region_ball_moves_right_r1_go_to_the_ball_r1_down': + self.exit_sequence_main_region_ball_moves_right_r1_go_to_the_ball_r1_down() + elif state == 'main_region_ball_moves_right_r1_go_to_the_ball_r1_up': + self.exit_sequence_main_region_ball_moves_right_r1_go_to_the_ball_r1_up() + + def exit_sequence_main_region_ball_moves_left_r1(self): + state = self.state_vector[0].name + if state == 'main_region_ball_moves_left_r1_go_to_the_middle': + self.exit_sequence_main_region_ball_moves_left_r1_go_to_the_middle() + + def exit_sequence_main_region_ball_moves_right_r1(self): + state = self.state_vector[0].name + if state == 'main_region_ball_moves_right_r1_wait': + self.exit_sequence_main_region_ball_moves_right_r1_wait() + elif state == 'main_region_ball_moves_right_r1_go_to_the_ball_r1_down': + self.exit_sequence_main_region_ball_moves_right_r1_go_to_the_ball_r1_down() + elif state == 'main_region_ball_moves_right_r1_go_to_the_ball_r1_up': + self.exit_sequence_main_region_ball_moves_right_r1_go_to_the_ball_r1_up() + + def exit_sequence_main_region_ball_moves_right_r1_go_to_the_ball_r1(self): + state = self.state_vector[0].name + if state == 'main_region_ball_moves_right_r1_go_to_the_ball_r1_down': + self.exit_sequence_main_region_ball_moves_right_r1_go_to_the_ball_r1_down() + elif state == 'main_region_ball_moves_right_r1_go_to_the_ball_r1_up': + self.exit_sequence_main_region_ball_moves_right_r1_go_to_the_ball_r1_up() + + def react_main_region__entry_default(self): + self.enter_sequence_main_region_ball_moves_left_default() + + def react_main_region_ball_moves_left_r1__entry_default(self): + self.enter_sequence_main_region_ball_moves_left_r1_go_to_the_middle_default() + + def react_main_region_ball_moves_right_r1__entry_default(self): + self.enter_sequence_main_region_ball_moves_right_r1_wait_default() + + def react_main_region_ball_moves_right_r1_go_to_the_ball_r1__entry_default(self): + self.enter_sequence_main_region_ball_moves_right_r1_go_to_the_ball_r1_up_default() + + def react(self): + return False + + + def main_region_ball_moves_left_react(self, try_transition): + did_transition = try_transition + + if try_transition: + if (self.react()) == False: + if ((self.RIGHT is None) if (self.sci_ball.direction is None) else (self.sci_ball.direction == self.RIGHT)): + self.exit_sequence_main_region_ball_moves_left() + self.enter_sequence_main_region_ball_moves_right_default() + else: + did_transition = False + + return did_transition + + + def main_region_ball_moves_left_r1_go_to_the_middle_react(self, try_transition): + did_transition = try_transition + + if try_transition: + if (self.main_region_ball_moves_left_react(try_transition)) == False: + if self.sci_player.position < self.MIDDLE: + self.exit_sequence_main_region_ball_moves_left_r1_go_to_the_middle() + self.sci_interface.raise_down() + self.enter_sequence_main_region_ball_moves_left_r1_go_to_the_middle_default() + elif self.sci_player.position > self.MIDDLE: + self.exit_sequence_main_region_ball_moves_left_r1_go_to_the_middle() + self.sci_interface.raise_up() + self.enter_sequence_main_region_ball_moves_left_r1_go_to_the_middle_default() + else: + did_transition = False + + return did_transition + + + def main_region_ball_moves_right_react(self, try_transition): + did_transition = try_transition + + if try_transition: + if (self.react()) == False: + if ((self.LEFT is None) if (self.sci_ball.direction is None) else (self.sci_ball.direction == self.LEFT)): + self.exit_sequence_main_region_ball_moves_right() + self.enter_sequence_main_region_ball_moves_left_default() + else: + did_transition = False + + return did_transition + + + def main_region_ball_moves_right_r1_wait_react(self, try_transition): + did_transition = try_transition + + if try_transition: + if (self.main_region_ball_moves_right_react(try_transition)) == False: + if self.sci_player.position != self.sci_ball.position: + self.exit_sequence_main_region_ball_moves_right_r1_wait() + self.enter_sequence_main_region_ball_moves_right_r1_go_to_the_ball_default() + else: + did_transition = False + + return did_transition + + + def main_region_ball_moves_right_r1_go_to_the_ball_react(self, try_transition): + did_transition = try_transition + + if try_transition: + if (self.main_region_ball_moves_right_react(try_transition)) == False: + if self.sci_player.position == self.sci_ball.position: + self.exit_sequence_main_region_ball_moves_right_r1_go_to_the_ball() + self.enter_sequence_main_region_ball_moves_right_r1_wait_default() + else: + did_transition = False + + return did_transition + + + def main_region_ball_moves_right_r1_go_to_the_ball_r1_down_react(self, try_transition): + did_transition = try_transition + + if try_transition: + if (self.main_region_ball_moves_right_r1_go_to_the_ball_react(try_transition)) == False: + if self.sci_player.position > self.sci_ball.position: + self.exit_sequence_main_region_ball_moves_right_r1_go_to_the_ball_r1_down() + self.enter_sequence_main_region_ball_moves_right_r1_go_to_the_ball_r1_up_default() + else: + did_transition = False + + if (did_transition) == False: + self.sci_interface.raise_down() + + return did_transition + + + def main_region_ball_moves_right_r1_go_to_the_ball_r1_up_react(self, try_transition): + did_transition = try_transition + + if try_transition: + if (self.main_region_ball_moves_right_r1_go_to_the_ball_react(try_transition)) == False: + if self.sci_player.position < self.sci_ball.position: + self.exit_sequence_main_region_ball_moves_right_r1_go_to_the_ball_r1_up() + self.enter_sequence_main_region_ball_moves_right_r1_go_to_the_ball_r1_down_default() + else: + did_transition = False + + if (did_transition) == False: + self.sci_interface.raise_up() + + return did_transition + + + def run_cycle(self): + """ Starts a cycle in the state machine. + """ + if self.initialized is not True: + raise ValueError( + 'The state machine needs to be initialized first by calling the init() function.') + self._clear_out_events() + self.next_state_index = 0 + while self.next_state_index < len(self.state_vector): + if self.state_vector[self.next_state_index].name == 'main_region_ball_moves_left_r1_go_to_the_middle' : + self.main_region_ball_moves_left_r1_go_to_the_middle_react(True) + elif self.state_vector[self.next_state_index].name == 'main_region_ball_moves_right_r1_wait' : + self.main_region_ball_moves_right_r1_wait_react(True) + elif self.state_vector[self.next_state_index].name == 'main_region_ball_moves_right_r1_go_to_the_ball_r1_down' : + self.main_region_ball_moves_right_r1_go_to_the_ball_r1_down_react(True) + elif self.state_vector[self.next_state_index].name == 'main_region_ball_moves_right_r1_go_to_the_ball_r1_up' : + self.main_region_ball_moves_right_r1_go_to_the_ball_r1_up_react(True) + self.next_state_index += 1 + self._clear_events() diff --git a/com.yakindu.sct.examples.pong.python/pong_npc/pong_npc_statemachine_interfaces.py b/com.yakindu.sct.examples.pong.python/pong_npc/pong_npc_statemachine_interfaces.py new file mode 100644 index 00000000..424669f8 --- /dev/null +++ b/com.yakindu.sct.examples.pong.python/pong_npc/pong_npc_statemachine_interfaces.py @@ -0,0 +1,68 @@ +"""Interfaces defined in the state chart model. + +The interfaces defined in the state chart model are represented +as separate classes. + +""" + +class SCI_Internal: + + """ Implementation of the Internal interface. + """ + + def __init__(self): + # init done by state machine class itself. + pass + + +class SCI_ball: + + """Implementation of scope sci_ball. + """ + + def __init__(self): + self.position = None + self.direction = None + + + + +class SCI_player: + + """Implementation of scope sci_player. + """ + + def __init__(self): + self.position = None + + + + +class SCI_Interface: + + """Implementation of scope sci_interface. + """ + + def __init__(self): + self.up = None + self.down = None + + + + def is_raised_up(self): + return self.up + + def raise_up(self): + self.up = True + + def is_raised_down(self): + return self.down + + def raise_down(self): + self.down = True + + def clear_out_events(self): + self.up = False + self.down = False + + diff --git a/com.yakindu.sct.examples.pong.python/pong_npc/timer/__init__.py b/com.yakindu.sct.examples.pong.python/pong_npc/timer/__init__.py new file mode 100644 index 00000000..25f977a9 --- /dev/null +++ b/com.yakindu.sct.examples.pong.python/pong_npc/timer/__init__.py @@ -0,0 +1,6 @@ + +""" + +Empty file that initializes the package it is contained in. + +""" diff --git a/org.yakindu.sct.examples.trafficlight.python.raspberry/.gitignore b/com.yakindu.sct.examples.trafficlight.python.raspberry/.gitignore similarity index 100% rename from org.yakindu.sct.examples.trafficlight.python.raspberry/.gitignore rename to com.yakindu.sct.examples.trafficlight.python.raspberry/.gitignore diff --git a/org.yakindu.sct.examples.trafficlight.python.raspberry/.project b/com.yakindu.sct.examples.trafficlight.python.raspberry/.project similarity index 91% rename from org.yakindu.sct.examples.trafficlight.python.raspberry/.project rename to com.yakindu.sct.examples.trafficlight.python.raspberry/.project index 9d916f93..040bd9ff 100644 --- a/org.yakindu.sct.examples.trafficlight.python.raspberry/.project +++ b/com.yakindu.sct.examples.trafficlight.python.raspberry/.project @@ -1,6 +1,6 @@ - org.yakindu.sct.examples.trafficlight.python.raspberry + com.yakindu.sct.examples.trafficlight.python.raspberry diff --git a/org.yakindu.sct.examples.trafficlight.python.raspberry/.pydevproject b/com.yakindu.sct.examples.trafficlight.python.raspberry/.pydevproject similarity index 100% rename from org.yakindu.sct.examples.trafficlight.python.raspberry/.pydevproject rename to com.yakindu.sct.examples.trafficlight.python.raspberry/.pydevproject diff --git a/org.yakindu.sct.examples.trafficlight.python.raspberry/images/TrafficLightCtrl.png b/com.yakindu.sct.examples.trafficlight.python.raspberry/images/TrafficLightCtrl.png similarity index 100% rename from org.yakindu.sct.examples.trafficlight.python.raspberry/images/TrafficLightCtrl.png rename to com.yakindu.sct.examples.trafficlight.python.raspberry/images/TrafficLightCtrl.png diff --git a/org.yakindu.sct.examples.trafficlight.python.raspberry/images/rpi_circuit.svg b/com.yakindu.sct.examples.trafficlight.python.raspberry/images/rpi_circuit.svg similarity index 100% rename from org.yakindu.sct.examples.trafficlight.python.raspberry/images/rpi_circuit.svg rename to com.yakindu.sct.examples.trafficlight.python.raspberry/images/rpi_circuit.svg diff --git a/org.yakindu.sct.examples.trafficlight.python.raspberry/images/traffic_light_python_rasp.jpg b/com.yakindu.sct.examples.trafficlight.python.raspberry/images/traffic_light_python_rasp.jpg similarity index 100% rename from org.yakindu.sct.examples.trafficlight.python.raspberry/images/traffic_light_python_rasp.jpg rename to com.yakindu.sct.examples.trafficlight.python.raspberry/images/traffic_light_python_rasp.jpg diff --git a/org.yakindu.sct.examples.trafficlight.python.raspberry/index.html b/com.yakindu.sct.examples.trafficlight.python.raspberry/index.html similarity index 100% rename from org.yakindu.sct.examples.trafficlight.python.raspberry/index.html rename to com.yakindu.sct.examples.trafficlight.python.raspberry/index.html diff --git a/org.yakindu.sct.examples.trafficlight.python.raspberry/metadata.json b/com.yakindu.sct.examples.trafficlight.python.raspberry/metadata.json similarity index 93% rename from org.yakindu.sct.examples.trafficlight.python.raspberry/metadata.json rename to com.yakindu.sct.examples.trafficlight.python.raspberry/metadata.json index 2854a5c8..778ef1f6 100644 --- a/org.yakindu.sct.examples.trafficlight.python.raspberry/metadata.json +++ b/com.yakindu.sct.examples.trafficlight.python.raspberry/metadata.json @@ -6,8 +6,7 @@ "author": "Norbert Lang", "organization": "itemis AG", "license": "Creative Commons Attribution 4.0 International (CC BY 4.0)", - "category": ["labs", - "open source", + "category": ["professional", "embedded" ], "dependencies": [ diff --git a/org.yakindu.sct.examples.trafficlight.python.raspberry/model/TrafficLightCtrl.sct b/com.yakindu.sct.examples.trafficlight.python.raspberry/model/TrafficLightCtrl.sct similarity index 100% rename from org.yakindu.sct.examples.trafficlight.python.raspberry/model/TrafficLightCtrl.sct rename to com.yakindu.sct.examples.trafficlight.python.raspberry/model/TrafficLightCtrl.sct diff --git a/org.yakindu.sct.examples.trafficlight.python.raspberry/model/TrafficLightCtrl.sgen b/com.yakindu.sct.examples.trafficlight.python.raspberry/model/TrafficLightCtrl.sgen similarity index 79% rename from org.yakindu.sct.examples.trafficlight.python.raspberry/model/TrafficLightCtrl.sgen rename to com.yakindu.sct.examples.trafficlight.python.raspberry/model/TrafficLightCtrl.sgen index 0c97be6b..dca65263 100644 --- a/org.yakindu.sct.examples.trafficlight.python.raspberry/model/TrafficLightCtrl.sgen +++ b/com.yakindu.sct.examples.trafficlight.python.raspberry/model/TrafficLightCtrl.sgen @@ -3,7 +3,7 @@ GeneratorModel for yakindu::python { statechart TrafficLightCtrl { feature Outlet { - targetProject = "org.yakindu.sct.examples.trafficlight.python.raspberry" + targetProject = "com.yakindu.sct.examples.trafficlight.python.raspberry" targetFolder = "src-gen" } @@ -15,4 +15,4 @@ GeneratorModel for yakindu::python { DefaultTimer = true } } -} \ No newline at end of file +} diff --git a/org.yakindu.sct.examples.trafficlight.python.raspberry/src/main.py b/com.yakindu.sct.examples.trafficlight.python.raspberry/src/main.py similarity index 100% rename from org.yakindu.sct.examples.trafficlight.python.raspberry/src/main.py rename to com.yakindu.sct.examples.trafficlight.python.raspberry/src/main.py diff --git a/org.yakindu.sct.examples.trafficlight.python.raspberry/src/trafficscene.py b/com.yakindu.sct.examples.trafficlight.python.raspberry/src/trafficscene.py similarity index 100% rename from org.yakindu.sct.examples.trafficlight.python.raspberry/src/trafficscene.py rename to com.yakindu.sct.examples.trafficlight.python.raspberry/src/trafficscene.py diff --git a/org.yakindu.sct.examples.trafficlight.python.raspberry/src/trafficscene2.py b/com.yakindu.sct.examples.trafficlight.python.raspberry/src/trafficscene2.py similarity index 100% rename from org.yakindu.sct.examples.trafficlight.python.raspberry/src/trafficscene2.py rename to com.yakindu.sct.examples.trafficlight.python.raspberry/src/trafficscene2.py diff --git a/org.yakindu.sct.examples.trafficlight.python/.gitignore b/com.yakindu.sct.examples.trafficlight.python/.gitignore similarity index 100% rename from org.yakindu.sct.examples.trafficlight.python/.gitignore rename to com.yakindu.sct.examples.trafficlight.python/.gitignore diff --git a/com.yakindu.sct.examples.trafficlight.python/.project b/com.yakindu.sct.examples.trafficlight.python/.project new file mode 100644 index 00000000..d777e890 --- /dev/null +++ b/com.yakindu.sct.examples.trafficlight.python/.project @@ -0,0 +1,29 @@ + + + com.yakindu.sct.examples.trafficlight.python + + + + + + org.python.pydev.PyDevBuilder + + + + + org.eclipse.xtext.ui.shared.xtextBuilder + + + + + org.yakindu.sct.builder.SCTBuilder + + + + + + org.yakindu.sct.builder.SCTNature + org.eclipse.xtext.ui.shared.xtextNature + org.python.pydev.pythonNature + + diff --git a/com.yakindu.sct.examples.trafficlight.python/.pydevproject b/com.yakindu.sct.examples.trafficlight.python/.pydevproject new file mode 100644 index 00000000..7e2ecafa --- /dev/null +++ b/com.yakindu.sct.examples.trafficlight.python/.pydevproject @@ -0,0 +1,9 @@ + + +Default +python interpreter + +/${PROJECT_DIR_NAME}/src +/${PROJECT_DIR_NAME}/src-gen + + diff --git a/org.yakindu.sct.examples.trafficlight.python/images/TrafficLightCtrl.png b/com.yakindu.sct.examples.trafficlight.python/images/TrafficLightCtrl.png similarity index 100% rename from org.yakindu.sct.examples.trafficlight.python/images/TrafficLightCtrl.png rename to com.yakindu.sct.examples.trafficlight.python/images/TrafficLightCtrl.png diff --git a/org.yakindu.sct.examples.trafficlight.python/images/trafficLightGui.png b/com.yakindu.sct.examples.trafficlight.python/images/trafficLightGui.png similarity index 100% rename from org.yakindu.sct.examples.trafficlight.python/images/trafficLightGui.png rename to com.yakindu.sct.examples.trafficlight.python/images/trafficLightGui.png diff --git a/org.yakindu.sct.examples.trafficlight.python/images/traffic_light_python.jpg b/com.yakindu.sct.examples.trafficlight.python/images/traffic_light_python.jpg similarity index 100% rename from org.yakindu.sct.examples.trafficlight.python/images/traffic_light_python.jpg rename to com.yakindu.sct.examples.trafficlight.python/images/traffic_light_python.jpg diff --git a/org.yakindu.sct.examples.trafficlight.python/index.html b/com.yakindu.sct.examples.trafficlight.python/index.html similarity index 100% rename from org.yakindu.sct.examples.trafficlight.python/index.html rename to com.yakindu.sct.examples.trafficlight.python/index.html diff --git a/org.yakindu.sct.examples.trafficlight.python/metadata.json b/com.yakindu.sct.examples.trafficlight.python/metadata.json similarity index 94% rename from org.yakindu.sct.examples.trafficlight.python/metadata.json rename to com.yakindu.sct.examples.trafficlight.python/metadata.json index 0e89a372..39a5729e 100644 --- a/org.yakindu.sct.examples.trafficlight.python/metadata.json +++ b/com.yakindu.sct.examples.trafficlight.python/metadata.json @@ -6,7 +6,7 @@ "author": "Norbert Lang", "organization": "itemis AG", "license": "Creative Commons Attribution 4.0 International (CC BY 4.0)", - "category": ["open source", + "category": ["professional", "advanced" ], "dependencies": [ diff --git a/org.yakindu.sct.examples.trafficlight.python/model/TrafficLightCtrl.sct b/com.yakindu.sct.examples.trafficlight.python/model/TrafficLightCtrl.sct similarity index 100% rename from org.yakindu.sct.examples.trafficlight.python/model/TrafficLightCtrl.sct rename to com.yakindu.sct.examples.trafficlight.python/model/TrafficLightCtrl.sct diff --git a/org.yakindu.sct.examples.trafficlight.python/model/TrafficLightCtrl.sgen b/com.yakindu.sct.examples.trafficlight.python/model/TrafficLightCtrl.sgen similarity index 60% rename from org.yakindu.sct.examples.trafficlight.python/model/TrafficLightCtrl.sgen rename to com.yakindu.sct.examples.trafficlight.python/model/TrafficLightCtrl.sgen index b9a74383..0bb0c0f8 100644 --- a/org.yakindu.sct.examples.trafficlight.python/model/TrafficLightCtrl.sgen +++ b/com.yakindu.sct.examples.trafficlight.python/model/TrafficLightCtrl.sgen @@ -3,16 +3,12 @@ GeneratorModel for yakindu::python { statechart TrafficLightCtrl { feature Outlet { - targetProject = "org.yakindu.sct.examples.trafficlight.python" + targetProject = "com.yakindu.sct.examples.trafficlight.python" targetFolder = "src-gen" } feature GeneralFeatures { DefaultTimer = true } - - feature Naming { - basePackage = "traffic.light" - } } } \ No newline at end of file diff --git a/org.yakindu.sct.examples.trafficlight.python/src/main.py b/com.yakindu.sct.examples.trafficlight.python/src/main.py similarity index 94% rename from org.yakindu.sct.examples.trafficlight.python/src/main.py rename to com.yakindu.sct.examples.trafficlight.python/src/main.py index b5e6a5a7..7955ed57 100644 --- a/org.yakindu.sct.examples.trafficlight.python/src/main.py +++ b/com.yakindu.sct.examples.trafficlight.python/src/main.py @@ -15,9 +15,6 @@ file, activate `General Feature -> DefaultRuntime` in your SGen file. """ # statemachine -import sys, os -path_to_statemachine = '../src-gen/traffic/light/TrafficLightCtrl' -sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), path_to_statemachine))) from trafficlightctrl.trafficlightctrl_statemachine import TrafficLightCtrl from trafficlightctrl.timer.sct_timer import Timer diff --git a/org.yakindu.sct.examples.trafficlight.python/src/traffic_scene.py b/com.yakindu.sct.examples.trafficlight.python/src/traffic_scene.py similarity index 100% rename from org.yakindu.sct.examples.trafficlight.python/src/traffic_scene.py rename to com.yakindu.sct.examples.trafficlight.python/src/traffic_scene.py