"""
Generated by Statechart compiler by Glenn De Jonghe, Joeri Exelmans, Simon Van Mierlo, and Yentl Van Tendeloo (for the inspiration)

Date:   Wed Aug 03 15:29:29 2016

Model author: Simon Van Mierlo+Raphael Mannadiar
Model name:   Bouncing_Balls_Python_Version
Model description:
Tkinter frame with bouncing balls in it.
"""

from python_runtime.statecharts_core import *
from python_runtime.libs.ui import ui
from python_runtime.libs.utils import utils
import random

# package "Bouncing_Balls_Python_Version"

class MainApp(RuntimeClassBase):
    def __init__(self, controller):
        RuntimeClassBase.__init__(self, controller)
        
        self.semantics.big_step_maximality = StatechartSemantics.TakeMany
        self.semantics.internal_event_lifeline = StatechartSemantics.Queue
        self.semantics.input_event_lifeline = StatechartSemantics.FirstComboStep
        self.semantics.priority = StatechartSemantics.SourceParent
        self.semantics.concurrency = StatechartSemantics.Single
        
        # build Statechart structure
        self.build_statechart_structure()
        
        # call user defined constructor
        MainApp.user_defined_constructor(self)
    
    def user_defined_constructor(self):
        self.nr_of_fields = 0
    
    def user_defined_destructor(self):
        pass
    
    
    # builds Statechart structure
    def build_statechart_structure(self):
        
        # state <root>
        self.states[""] = State(0, self)
        
        # state /running
        self.states["/running"] = State(1, self)
        
        # state /running/root
        self.states["/running/root"] = ParallelState(2, self)
        
        # state /running/root/main_behaviour
        self.states["/running/root/main_behaviour"] = State(3, self)
        
        # state /running/root/main_behaviour/initializing
        self.states["/running/root/main_behaviour/initializing"] = State(4, self)
        
        # state /running/root/main_behaviour/running
        self.states["/running/root/main_behaviour/running"] = State(5, self)
        
        # state /running/root/cd_behaviour
        self.states["/running/root/cd_behaviour"] = State(6, self)
        
        # state /running/root/cd_behaviour/waiting
        self.states["/running/root/cd_behaviour/waiting"] = State(7, self)
        
        # state /running/root/cd_behaviour/creating
        self.states["/running/root/cd_behaviour/creating"] = State(8, self)
        
        # state /running/root/cd_behaviour/check_nr_of_fields
        self.states["/running/root/cd_behaviour/check_nr_of_fields"] = State(9, self)
        self.states["/running/root/cd_behaviour/check_nr_of_fields"].setEnter(self._running_root_cd_behaviour_check_nr_of_fields_enter)
        self.states["/running/root/cd_behaviour/check_nr_of_fields"].setExit(self._running_root_cd_behaviour_check_nr_of_fields_exit)
        
        # state /running/stopped
        self.states["/running/stopped"] = State(10, self)
        
        # add children
        self.states[""].addChild(self.states["/running"])
        self.states["/running"].addChild(self.states["/running/root"])
        self.states["/running"].addChild(self.states["/running/stopped"])
        self.states["/running/root"].addChild(self.states["/running/root/main_behaviour"])
        self.states["/running/root"].addChild(self.states["/running/root/cd_behaviour"])
        self.states["/running/root/main_behaviour"].addChild(self.states["/running/root/main_behaviour/initializing"])
        self.states["/running/root/main_behaviour"].addChild(self.states["/running/root/main_behaviour/running"])
        self.states["/running/root/cd_behaviour"].addChild(self.states["/running/root/cd_behaviour/waiting"])
        self.states["/running/root/cd_behaviour"].addChild(self.states["/running/root/cd_behaviour/creating"])
        self.states["/running/root/cd_behaviour"].addChild(self.states["/running/root/cd_behaviour/check_nr_of_fields"])
        self.states[""].fixTree()
        self.states[""].default_state = self.states["/running"]
        self.states["/running"].default_state = self.states["/running/root"]
        self.states["/running/root/main_behaviour"].default_state = self.states["/running/root/main_behaviour/initializing"]
        self.states["/running/root/cd_behaviour"].default_state = self.states["/running/root/cd_behaviour/waiting"]
        
        # transition /running/root/main_behaviour/initializing
        _running_root_main_behaviour_initializing_0 = Transition(self, self.states["/running/root/main_behaviour/initializing"], [self.states["/running/root/main_behaviour/running"]])
        _running_root_main_behaviour_initializing_0.setAction(self._running_root_main_behaviour_initializing_0_exec)
        self.states["/running/root/main_behaviour/initializing"].addTransition(_running_root_main_behaviour_initializing_0)
        
        # transition /running/root/main_behaviour/running
        _running_root_main_behaviour_running_0 = Transition(self, self.states["/running/root/main_behaviour/running"], [self.states["/running/root/main_behaviour/running"]])
        _running_root_main_behaviour_running_0.setAction(self._running_root_main_behaviour_running_0_exec)
        _running_root_main_behaviour_running_0.trigger = Event("button_pressed", None)
        _running_root_main_behaviour_running_0.setGuard(self._running_root_main_behaviour_running_0_guard)
        self.states["/running/root/main_behaviour/running"].addTransition(_running_root_main_behaviour_running_0)
        
        # transition /running/root/cd_behaviour/waiting
        _running_root_cd_behaviour_waiting_0 = Transition(self, self.states["/running/root/cd_behaviour/waiting"], [self.states["/running/root/cd_behaviour/creating"]])
        _running_root_cd_behaviour_waiting_0.setAction(self._running_root_cd_behaviour_waiting_0_exec)
        _running_root_cd_behaviour_waiting_0.trigger = Event("create_field", None)
        self.states["/running/root/cd_behaviour/waiting"].addTransition(_running_root_cd_behaviour_waiting_0)
        _running_root_cd_behaviour_waiting_1 = Transition(self, self.states["/running/root/cd_behaviour/waiting"], [self.states["/running/root/cd_behaviour/check_nr_of_fields"]])
        _running_root_cd_behaviour_waiting_1.setAction(self._running_root_cd_behaviour_waiting_1_exec)
        _running_root_cd_behaviour_waiting_1.trigger = Event("delete_field", None)
        self.states["/running/root/cd_behaviour/waiting"].addTransition(_running_root_cd_behaviour_waiting_1)
        
        # transition /running/root/cd_behaviour/creating
        _running_root_cd_behaviour_creating_0 = Transition(self, self.states["/running/root/cd_behaviour/creating"], [self.states["/running/root/cd_behaviour/waiting"]])
        _running_root_cd_behaviour_creating_0.setAction(self._running_root_cd_behaviour_creating_0_exec)
        _running_root_cd_behaviour_creating_0.trigger = Event("instance_created", None)
        self.states["/running/root/cd_behaviour/creating"].addTransition(_running_root_cd_behaviour_creating_0)
        
        # transition /running/root/cd_behaviour/check_nr_of_fields
        _running_root_cd_behaviour_check_nr_of_fields_0 = Transition(self, self.states["/running/root/cd_behaviour/check_nr_of_fields"], [self.states["/running/root/cd_behaviour/waiting"]])
        _running_root_cd_behaviour_check_nr_of_fields_0.setGuard(self._running_root_cd_behaviour_check_nr_of_fields_0_guard)
        self.states["/running/root/cd_behaviour/check_nr_of_fields"].addTransition(_running_root_cd_behaviour_check_nr_of_fields_0)
        _running_root_cd_behaviour_check_nr_of_fields_1 = Transition(self, self.states["/running/root/cd_behaviour/check_nr_of_fields"], [self.states["/running/stopped"]])
        _running_root_cd_behaviour_check_nr_of_fields_1.setAction(self._running_root_cd_behaviour_check_nr_of_fields_1_exec)
        _running_root_cd_behaviour_check_nr_of_fields_1.trigger = Event("_0after")
        _running_root_cd_behaviour_check_nr_of_fields_1.setGuard(self._running_root_cd_behaviour_check_nr_of_fields_1_guard)
        self.states["/running/root/cd_behaviour/check_nr_of_fields"].addTransition(_running_root_cd_behaviour_check_nr_of_fields_1)
    
    def _running_root_cd_behaviour_check_nr_of_fields_enter(self):
        self.addTimer(0, 0.05)
    
    def _running_root_cd_behaviour_check_nr_of_fields_exit(self):
        self.removeTimer(0)
    
    def _running_root_main_behaviour_initializing_0_exec(self, parameters):
        self.raiseInternalEvent(Event("create_field", None, []))
    
    def _running_root_main_behaviour_running_0_exec(self, parameters):
        event_name = parameters[0]
        self.raiseInternalEvent(Event("create_field", None, []))
    
    def _running_root_main_behaviour_running_0_guard(self, parameters):
        event_name = parameters[0]
        return event_name == "create_new_field"
    
    def _running_root_cd_behaviour_waiting_0_exec(self, parameters):
        self.big_step.outputEventOM(Event("create_instance", None, [self, "fields"]))
    
    def _running_root_cd_behaviour_waiting_1_exec(self, parameters):
        association_name = parameters[0]
        self.big_step.outputEventOM(Event("delete_instance", None, [self, association_name]))
        self.nr_of_fields -= 1
    
    def _running_root_cd_behaviour_creating_0_exec(self, parameters):
        association_name = parameters[0]
        self.big_step.outputEventOM(Event("start_instance", None, [self, association_name]))
        self.big_step.outputEventOM(Event("narrow_cast", None, [self, association_name, Event("set_association_name", None, [association_name])]))
        self.nr_of_fields += 1
    
    def _running_root_cd_behaviour_check_nr_of_fields_0_guard(self, parameters):
        return self.nr_of_fields != 0
    
    def _running_root_cd_behaviour_check_nr_of_fields_1_exec(self, parameters):
        ui.close_window(ui.window)
    
    def _running_root_cd_behaviour_check_nr_of_fields_1_guard(self, parameters):
        return self.nr_of_fields == 0
    
    def initializeStatechart(self):
        # enter default state
        states = self.states["/running"].getEffectiveTargetStates()
        self.updateConfiguration(states)
        for state in states:
            if state.enter:
                state.enter()

class Field(RuntimeClassBase):
    def __init__(self, controller):
        RuntimeClassBase.__init__(self, controller)
        
        self.semantics.big_step_maximality = StatechartSemantics.TakeMany
        self.semantics.internal_event_lifeline = StatechartSemantics.Queue
        self.semantics.input_event_lifeline = StatechartSemantics.FirstComboStep
        self.semantics.priority = StatechartSemantics.SourceParent
        self.semantics.concurrency = StatechartSemantics.Single
        
        # build Statechart structure
        self.build_statechart_structure()
        self.inports["field_ui"] = controller.addInputPort("field_ui", self)
        
        # user defined attributes
        self.canvas = None
        self.field_window = None
        
        # call user defined constructor
        Field.user_defined_constructor(self)
    
    def user_defined_constructor(self):
        self.field_window = ui.new_window(400,450);
        self.canvas = ui.append_canvas(self.field_window,400,400,{'background':'#eee'});
        ui.bind_event(self.field_window, ui.EVENTS.WINDOW_CLOSE, self.controller, 'window_close', self.inports['field_ui']);
        ui.bind_event(self.field_window, ui.EVENTS.KEY_PRESS, self.controller, 'key_press', self.inports['field_ui']);
        ui.bind_event(self.canvas.element, ui.EVENTS.MOUSE_RIGHT_CLICK,    self.controller, 'right_click', self.inports['field_ui']);
        ui.bind_event(self.canvas.element, ui.EVENTS.MOUSE_MOVE, self.controller, 'mouse_move', self.inports['field_ui']);
        ui.bind_event(self.canvas.element, ui.EVENTS.MOUSE_RELEASE, self.controller, 'mouse_release', self.inports['field_ui']);
    
    def user_defined_destructor(self):
        ui.close_window(self.field_window);
    
    
    # builds Statechart structure
    def build_statechart_structure(self):
        
        # state <root>
        self.states[""] = State(0, self)
        
        # state /root
        self.states["/root"] = State(1, self)
        
        # state /root/waiting
        self.states["/root/waiting"] = State(2, self)
        
        # state /root/initializing
        self.states["/root/initializing"] = State(3, self)
        
        # state /root/creating
        self.states["/root/creating"] = State(4, self)
        
        # state /root/packing
        self.states["/root/packing"] = State(5, self)
        
        # state /root/running
        self.states["/root/running"] = ParallelState(6, self)
        
        # state /root/running/main_behaviour
        self.states["/root/running/main_behaviour"] = State(7, self)
        
        # state /root/running/main_behaviour/running
        self.states["/root/running/main_behaviour/running"] = State(8, self)
        
        # state /root/running/main_behaviour/creating
        self.states["/root/running/main_behaviour/creating"] = State(9, self)
        
        # state /root/running/deleting_behaviour
        self.states["/root/running/deleting_behaviour"] = State(10, self)
        
        # state /root/running/deleting_behaviour/running
        self.states["/root/running/deleting_behaviour/running"] = State(11, self)
        
        # state /root/running/child_behaviour
        self.states["/root/running/child_behaviour"] = State(12, self)
        
        # state /root/running/child_behaviour/listening
        self.states["/root/running/child_behaviour/listening"] = State(13, self)
        
        # state /root/running/deleting_balls_behaviour
        self.states["/root/running/deleting_balls_behaviour"] = State(14, self)
        
        # state /root/running/deleting_balls_behaviour/listening
        self.states["/root/running/deleting_balls_behaviour/listening"] = State(15, self)
        
        # state /root/deleting
        self.states["/root/deleting"] = State(16, self)
        
        # state /root/deleted
        self.states["/root/deleted"] = State(17, self)
        
        # add children
        self.states[""].addChild(self.states["/root"])
        self.states["/root"].addChild(self.states["/root/waiting"])
        self.states["/root"].addChild(self.states["/root/initializing"])
        self.states["/root"].addChild(self.states["/root/creating"])
        self.states["/root"].addChild(self.states["/root/packing"])
        self.states["/root"].addChild(self.states["/root/running"])
        self.states["/root"].addChild(self.states["/root/deleting"])
        self.states["/root"].addChild(self.states["/root/deleted"])
        self.states["/root/running"].addChild(self.states["/root/running/main_behaviour"])
        self.states["/root/running"].addChild(self.states["/root/running/deleting_behaviour"])
        self.states["/root/running"].addChild(self.states["/root/running/child_behaviour"])
        self.states["/root/running"].addChild(self.states["/root/running/deleting_balls_behaviour"])
        self.states["/root/running/main_behaviour"].addChild(self.states["/root/running/main_behaviour/running"])
        self.states["/root/running/main_behaviour"].addChild(self.states["/root/running/main_behaviour/creating"])
        self.states["/root/running/deleting_behaviour"].addChild(self.states["/root/running/deleting_behaviour/running"])
        self.states["/root/running/child_behaviour"].addChild(self.states["/root/running/child_behaviour/listening"])
        self.states["/root/running/deleting_balls_behaviour"].addChild(self.states["/root/running/deleting_balls_behaviour/listening"])
        self.states[""].fixTree()
        self.states[""].default_state = self.states["/root"]
        self.states["/root"].default_state = self.states["/root/waiting"]
        self.states["/root/running/main_behaviour"].default_state = self.states["/root/running/main_behaviour/running"]
        self.states["/root/running/deleting_behaviour"].default_state = self.states["/root/running/deleting_behaviour/running"]
        self.states["/root/running/child_behaviour"].default_state = self.states["/root/running/child_behaviour/listening"]
        self.states["/root/running/deleting_balls_behaviour"].default_state = self.states["/root/running/deleting_balls_behaviour/listening"]
        
        # transition /root/waiting
        _root_waiting_0 = Transition(self, self.states["/root/waiting"], [self.states["/root/initializing"]])
        _root_waiting_0.setAction(self._root_waiting_0_exec)
        _root_waiting_0.trigger = Event("set_association_name", None)
        self.states["/root/waiting"].addTransition(_root_waiting_0)
        
        # transition /root/initializing
        _root_initializing_0 = Transition(self, self.states["/root/initializing"], [self.states["/root/creating"]])
        _root_initializing_0.setAction(self._root_initializing_0_exec)
        self.states["/root/initializing"].addTransition(_root_initializing_0)
        
        # transition /root/creating
        _root_creating_0 = Transition(self, self.states["/root/creating"], [self.states["/root/packing"]])
        _root_creating_0.setAction(self._root_creating_0_exec)
        _root_creating_0.trigger = Event("instance_created", None)
        self.states["/root/creating"].addTransition(_root_creating_0)
        
        # transition /root/packing
        _root_packing_0 = Transition(self, self.states["/root/packing"], [self.states["/root/running"]])
        _root_packing_0.trigger = Event("button_created", None)
        self.states["/root/packing"].addTransition(_root_packing_0)
        
        # transition /root/running/main_behaviour/running
        _root_running_main_behaviour_running_0 = Transition(self, self.states["/root/running/main_behaviour/running"], [self.states["/root/running/main_behaviour/creating"]])
        _root_running_main_behaviour_running_0.setAction(self._root_running_main_behaviour_running_0_exec)
        _root_running_main_behaviour_running_0.trigger = Event("right_click", "field_ui")
        self.states["/root/running/main_behaviour/running"].addTransition(_root_running_main_behaviour_running_0)
        
        # transition /root/running/main_behaviour/creating
        _root_running_main_behaviour_creating_0 = Transition(self, self.states["/root/running/main_behaviour/creating"], [self.states["/root/running/main_behaviour/running"]])
        _root_running_main_behaviour_creating_0.setAction(self._root_running_main_behaviour_creating_0_exec)
        _root_running_main_behaviour_creating_0.trigger = Event("instance_created", None)
        self.states["/root/running/main_behaviour/creating"].addTransition(_root_running_main_behaviour_creating_0)
        
        # transition /root/running/deleting_behaviour/running
        _root_running_deleting_behaviour_running_0 = Transition(self, self.states["/root/running/deleting_behaviour/running"], [self.states["/root/running/deleting_behaviour/running"]])
        _root_running_deleting_behaviour_running_0.setAction(self._root_running_deleting_behaviour_running_0_exec)
        _root_running_deleting_behaviour_running_0.trigger = Event("delete_ball", None)
        self.states["/root/running/deleting_behaviour/running"].addTransition(_root_running_deleting_behaviour_running_0)
        
        # transition /root/running/child_behaviour/listening
        _root_running_child_behaviour_listening_0 = Transition(self, self.states["/root/running/child_behaviour/listening"], [self.states["/root/running/child_behaviour/listening"]])
        _root_running_child_behaviour_listening_0.setAction(self._root_running_child_behaviour_listening_0_exec)
        _root_running_child_behaviour_listening_0.trigger = Event("button_pressed", None)
        self.states["/root/running/child_behaviour/listening"].addTransition(_root_running_child_behaviour_listening_0)
        
        # transition /root/running/deleting_balls_behaviour/listening
        _root_running_deleting_balls_behaviour_listening_0 = Transition(self, self.states["/root/running/deleting_balls_behaviour/listening"], [self.states["/root/running/deleting_balls_behaviour/listening"]])
        _root_running_deleting_balls_behaviour_listening_0.setAction(self._root_running_deleting_balls_behaviour_listening_0_exec)
        _root_running_deleting_balls_behaviour_listening_0.trigger = Event("key_press", "field_ui")
        _root_running_deleting_balls_behaviour_listening_0.setGuard(self._root_running_deleting_balls_behaviour_listening_0_guard)
        self.states["/root/running/deleting_balls_behaviour/listening"].addTransition(_root_running_deleting_balls_behaviour_listening_0)
        
        # transition /root/deleting
        _root_deleting_0 = Transition(self, self.states["/root/deleting"], [self.states["/root/deleted"]])
        _root_deleting_0.setAction(self._root_deleting_0_exec)
        self.states["/root/deleting"].addTransition(_root_deleting_0)
        
        # transition /root/running
        _root_running_0 = Transition(self, self.states["/root/running"], [self.states["/root/deleting"]])
        _root_running_0.setAction(self._root_running_0_exec)
        _root_running_0.trigger = Event("window_close", "field_ui")
        self.states["/root/running"].addTransition(_root_running_0)
    
    def _root_running_0_exec(self, parameters):
        self.big_step.outputEventOM(Event("delete_instance", None, [self, "buttons"]))
        self.big_step.outputEventOM(Event("delete_instance", None, [self, "balls"]))
    
    def _root_waiting_0_exec(self, parameters):
        association_name = parameters[0]
        self.association_name = association_name
    
    def _root_initializing_0_exec(self, parameters):
        self.big_step.outputEventOM(Event("create_instance", None, [self, "buttons", "Button", self, 'create_new_field', 'Spawn New Window']))
    
    def _root_creating_0_exec(self, parameters):
        association_name = parameters[0]
        self.big_step.outputEventOM(Event("start_instance", None, [self, association_name]))
    
    def _root_running_main_behaviour_running_0_exec(self, parameters):
        x = parameters[0]
        y = parameters[1]
        button = parameters[2]
        self.big_step.outputEventOM(Event("create_instance", None, [self, "balls", "Ball", self.canvas, x, y]))
    
    def _root_running_main_behaviour_creating_0_exec(self, parameters):
        association_name = parameters[0]
        self.big_step.outputEventOM(Event("start_instance", None, [self, association_name]))
        self.big_step.outputEventOM(Event("narrow_cast", None, [self, association_name, Event("set_association_name", None, [association_name])]))
    
    def _root_running_deleting_behaviour_running_0_exec(self, parameters):
        association_name = parameters[0]
        self.big_step.outputEventOM(Event("delete_instance", None, [self, association_name]))
    
    def _root_running_child_behaviour_listening_0_exec(self, parameters):
        event_name = parameters[0]
        self.big_step.outputEventOM(Event("narrow_cast", None, [self, 'parent', Event("button_pressed", None, [event_name])]))
    
    def _root_running_deleting_balls_behaviour_listening_0_exec(self, parameters):
        key = parameters[0]
        self.big_step.outputEventOM(Event("narrow_cast", None, [self, 'balls', Event("delete_self", None, [])]))
    
    def _root_running_deleting_balls_behaviour_listening_0_guard(self, parameters):
        key = parameters[0]
        return key == ui.KEYCODES.DELETE
    
    def _root_deleting_0_exec(self, parameters):
        self.big_step.outputEventOM(Event("narrow_cast", None, [self, 'parent', Event("delete_field", None, [self.association_name])]))
    
    def initializeStatechart(self):
        # enter default state
        states = self.states["/root"].getEffectiveTargetStates()
        self.updateConfiguration(states)
        for state in states:
            if state.enter:
                state.enter()

class Button(RuntimeClassBase):
    def __init__(self, controller, parent, event_name, button_text):
        RuntimeClassBase.__init__(self, controller)
        
        self.semantics.big_step_maximality = StatechartSemantics.TakeMany
        self.semantics.internal_event_lifeline = StatechartSemantics.Queue
        self.semantics.input_event_lifeline = StatechartSemantics.FirstComboStep
        self.semantics.priority = StatechartSemantics.SourceParent
        self.semantics.concurrency = StatechartSemantics.Single
        
        # build Statechart structure
        self.build_statechart_structure()
        self.inports["button_ui"] = controller.addInputPort("button_ui", self)
        
        # call user defined constructor
        Button.user_defined_constructor(self, parent, event_name, button_text)
    
    def user_defined_constructor(self, parent, event_name, button_text):
        self.event_name = event_name;
        button = ui.append_button(parent.field_window, event_name);
        ui.bind_event(button.element, ui.EVENTS.MOUSE_CLICK, self.controller, 'mouse_click', self.inports['button_ui']);
    
    def user_defined_destructor(self):
        pass
    
    
    # builds Statechart structure
    def build_statechart_structure(self):
        
        # state <root>
        self.states[""] = State(0, self)
        
        # state /initializing
        self.states["/initializing"] = State(1, self)
        
        # state /running
        self.states["/running"] = State(2, self)
        
        # add children
        self.states[""].addChild(self.states["/initializing"])
        self.states[""].addChild(self.states["/running"])
        self.states[""].fixTree()
        self.states[""].default_state = self.states["/initializing"]
        
        # transition /initializing
        _initializing_0 = Transition(self, self.states["/initializing"], [self.states["/running"]])
        _initializing_0.setAction(self._initializing_0_exec)
        self.states["/initializing"].addTransition(_initializing_0)
        
        # transition /running
        _running_0 = Transition(self, self.states["/running"], [self.states["/running"]])
        _running_0.setAction(self._running_0_exec)
        _running_0.trigger = Event("mouse_click", "button_ui")
        _running_0.setGuard(self._running_0_guard)
        self.states["/running"].addTransition(_running_0)
    
    def _initializing_0_exec(self, parameters):
        self.big_step.outputEventOM(Event("narrow_cast", None, [self, 'parent', Event("button_created", None, [])]))
    
    def _running_0_exec(self, parameters):
        x = parameters[0]
        y = parameters[1]
        button = parameters[2]
        self.big_step.outputEventOM(Event("narrow_cast", None, [self, 'parent', Event("button_pressed", None, [self.event_name])]))
    
    def _running_0_guard(self, parameters):
        x = parameters[0]
        y = parameters[1]
        button = parameters[2]
        return button == ui.MOUSE_BUTTONS.LEFT
    
    def initializeStatechart(self):
        # enter default state
        states = self.states["/initializing"].getEffectiveTargetStates()
        self.updateConfiguration(states)
        for state in states:
            if state.enter:
                state.enter()

class Ball(RuntimeClassBase):
    def __init__(self, controller, canvas, x, y):
        RuntimeClassBase.__init__(self, controller)
        
        self.semantics.big_step_maximality = StatechartSemantics.TakeMany
        self.semantics.internal_event_lifeline = StatechartSemantics.Queue
        self.semantics.input_event_lifeline = StatechartSemantics.FirstComboStep
        self.semantics.priority = StatechartSemantics.SourceParent
        self.semantics.concurrency = StatechartSemantics.Single
        
        # build Statechart structure
        self.build_statechart_structure()
        self.inports["ball_ui"] = controller.addInputPort("ball_ui", self)
        
        # user defined attributes
        self.canvas = None
        
        # call user defined constructor
        Ball.user_defined_constructor(self, canvas, x, y)
    
    def user_defined_constructor(self, canvas, x, y):
        self.canvas = canvas;
        self.r = 20.0;
        self.vel = {'x': random.uniform(-5.0, 5.0), 'y': random.uniform(-5.0, 5.0)};
        self.mouse_pos = {};
        self.smooth = 0.4; # value between 0 and 1
        
        circle = self.canvas.add_circle(x, y, self.r, {'fill':'#000'});
        ui.bind_event(circle, ui.EVENTS.MOUSE_PRESS, self.controller, 'mouse_press', self.inports["ball_ui"]);
        ui.bind_event(circle, ui.EVENTS.MOUSE_MOVE, self.controller, 'mouse_move', self.inports['ball_ui']);
        ui.bind_event(circle, ui.EVENTS.MOUSE_RELEASE, self.controller, 'mouse_release', self.inports['ball_ui']);
        self.element = circle;
    
    def user_defined_destructor(self):
        self.canvas.remove_element(self.element);
    
    
    # builds Statechart structure
    def build_statechart_structure(self):
        
        # state <root>
        self.states[""] = State(0, self)
        
        # state /main_behaviour
        self.states["/main_behaviour"] = State(1, self)
        
        # state /main_behaviour/initializing
        self.states["/main_behaviour/initializing"] = State(2, self)
        
        # state /main_behaviour/bouncing
        self.states["/main_behaviour/bouncing"] = State(3, self)
        self.states["/main_behaviour/bouncing"].setEnter(self._main_behaviour_bouncing_enter)
        self.states["/main_behaviour/bouncing"].setExit(self._main_behaviour_bouncing_exit)
        
        # state /main_behaviour/dragging
        self.states["/main_behaviour/dragging"] = State(4, self)
        
        # state /main_behaviour/selected
        self.states["/main_behaviour/selected"] = State(5, self)
        
        # state /deleted
        self.states["/deleted"] = State(6, self)
        
        # add children
        self.states[""].addChild(self.states["/main_behaviour"])
        self.states[""].addChild(self.states["/deleted"])
        self.states["/main_behaviour"].addChild(self.states["/main_behaviour/initializing"])
        self.states["/main_behaviour"].addChild(self.states["/main_behaviour/bouncing"])
        self.states["/main_behaviour"].addChild(self.states["/main_behaviour/dragging"])
        self.states["/main_behaviour"].addChild(self.states["/main_behaviour/selected"])
        self.states[""].fixTree()
        self.states[""].default_state = self.states["/main_behaviour"]
        self.states["/main_behaviour"].default_state = self.states["/main_behaviour/initializing"]
        
        # transition /main_behaviour/initializing
        _main_behaviour_initializing_0 = Transition(self, self.states["/main_behaviour/initializing"], [self.states["/main_behaviour/bouncing"]])
        _main_behaviour_initializing_0.setAction(self._main_behaviour_initializing_0_exec)
        _main_behaviour_initializing_0.trigger = Event("set_association_name", None)
        self.states["/main_behaviour/initializing"].addTransition(_main_behaviour_initializing_0)
        
        # transition /main_behaviour/bouncing
        _main_behaviour_bouncing_0 = Transition(self, self.states["/main_behaviour/bouncing"], [self.states["/main_behaviour/bouncing"]])
        _main_behaviour_bouncing_0.setAction(self._main_behaviour_bouncing_0_exec)
        _main_behaviour_bouncing_0.trigger = Event("_0after")
        self.states["/main_behaviour/bouncing"].addTransition(_main_behaviour_bouncing_0)
        _main_behaviour_bouncing_1 = Transition(self, self.states["/main_behaviour/bouncing"], [self.states["/main_behaviour/selected"]])
        _main_behaviour_bouncing_1.setAction(self._main_behaviour_bouncing_1_exec)
        _main_behaviour_bouncing_1.trigger = Event("mouse_press", "ball_ui")
        _main_behaviour_bouncing_1.setGuard(self._main_behaviour_bouncing_1_guard)
        self.states["/main_behaviour/bouncing"].addTransition(_main_behaviour_bouncing_1)
        
        # transition /main_behaviour/dragging
        _main_behaviour_dragging_0 = Transition(self, self.states["/main_behaviour/dragging"], [self.states["/main_behaviour/dragging"]])
        _main_behaviour_dragging_0.setAction(self._main_behaviour_dragging_0_exec)
        _main_behaviour_dragging_0.trigger = Event("mouse_move", "ball_ui")
        self.states["/main_behaviour/dragging"].addTransition(_main_behaviour_dragging_0)
        _main_behaviour_dragging_1 = Transition(self, self.states["/main_behaviour/dragging"], [self.states["/main_behaviour/bouncing"]])
        _main_behaviour_dragging_1.setAction(self._main_behaviour_dragging_1_exec)
        _main_behaviour_dragging_1.trigger = Event("mouse_release", "ball_ui")
        self.states["/main_behaviour/dragging"].addTransition(_main_behaviour_dragging_1)
        
        # transition /main_behaviour/selected
        _main_behaviour_selected_0 = Transition(self, self.states["/main_behaviour/selected"], [self.states["/main_behaviour/dragging"]])
        _main_behaviour_selected_0.setAction(self._main_behaviour_selected_0_exec)
        _main_behaviour_selected_0.trigger = Event("mouse_press", "ball_ui")
        _main_behaviour_selected_0.setGuard(self._main_behaviour_selected_0_guard)
        self.states["/main_behaviour/selected"].addTransition(_main_behaviour_selected_0)
        _main_behaviour_selected_1 = Transition(self, self.states["/main_behaviour/selected"], [self.states["/deleted"]])
        _main_behaviour_selected_1.setAction(self._main_behaviour_selected_1_exec)
        _main_behaviour_selected_1.trigger = Event("delete_self", None)
        self.states["/main_behaviour/selected"].addTransition(_main_behaviour_selected_1)
    
    def _main_behaviour_bouncing_enter(self):
        self.addTimer(0, 0.05)
    
    def _main_behaviour_bouncing_exit(self):
        self.removeTimer(0)
    
    def _main_behaviour_initializing_0_exec(self, parameters):
        association_name = parameters[0]
        self.association_name = association_name
    
    def _main_behaviour_bouncing_0_exec(self, parameters):
        pos = self.element.get_position();    
        if pos.x-self.r <= 0 or pos.x+self.r >= self.canvas.width :
            self.vel['x'] = -self.vel['x'];
        if pos.y-self.r <= 0 or pos.y+self.r >= self.canvas.height :
            self.vel['y'] = -self.vel['y'];
        self.element.move(self.vel['x'], self.vel['y']);
    
    def _main_behaviour_bouncing_1_exec(self, parameters):
        x = parameters[0]
        y = parameters[1]
        button = parameters[2]
        self.element.set_color("#ff0");
    
    def _main_behaviour_bouncing_1_guard(self, parameters):
        x = parameters[0]
        y = parameters[1]
        button = parameters[2]
        return button == ui.MOUSE_BUTTONS.LEFT
    
    def _main_behaviour_dragging_0_exec(self, parameters):
        x = parameters[0]
        y = parameters[1]
        button = parameters[2]
        dx = x - self.mouse_pos['x'];
        dy = y - self.mouse_pos['y'];
        
        self.element.move(dx, dy);
        
        # keep ball within boundaries
        pos = self.element.get_position();
        if pos.x-self.r <= 0 :
            pos.x = self.r + 1;
        elif pos.x+self.r >= self.canvas.width :
            pos.x = self.canvas.width-self.r-1;
        if pos.y-self.r <= 0 :
            pos.y = self.r + 1;
        elif pos.y+self.r >= self.canvas.height :
            pos.y = self.canvas.height-self.r-1;
        self.element.set_position(pos.x, pos.y);
        self.mouse_pos = {'x':x, 'y':y};
        self.vel = {
            'x': (1-self.smooth)*dx + self.smooth*self.vel['x'],
            'y': (1-self.smooth)*dy + self.smooth*self.vel['y']
        };
    
    def _main_behaviour_dragging_1_exec(self, parameters):
        x = parameters[0]
        y = parameters[1]
        self.element.set_color("#f00");
    
    def _main_behaviour_selected_0_exec(self, parameters):
        x = parameters[0]
        y = parameters[1]
        button = parameters[2]
        self.mouse_pos = {'x':x, 'y':y};
    
    def _main_behaviour_selected_0_guard(self, parameters):
        x = parameters[0]
        y = parameters[1]
        button = parameters[2]
        return button == ui.MOUSE_BUTTONS.LEFT
    
    def _main_behaviour_selected_1_exec(self, parameters):
        self.big_step.outputEventOM(Event("narrow_cast", None, [self, 'parent', Event("delete_ball", None, [self.association_name])]))
    
    def initializeStatechart(self):
        # enter default state
        states = self.states["/main_behaviour"].getEffectiveTargetStates()
        self.updateConfiguration(states)
        for state in states:
            if state.enter:
                state.enter()

class ObjectManager(ObjectManagerBase):
    def __init__(self, controller):
        ObjectManagerBase.__init__(self, controller)
    
    def instantiate(self, class_name, construct_params):
        if class_name == "MainApp":
            instance = MainApp(self.controller)
            instance.associations = {}
            instance.associations["fields"] = Association("Field", 0, -1)
        elif class_name == "Field":
            instance = Field(self.controller)
            instance.associations = {}
            instance.associations["balls"] = Association("Ball", 0, -1)
            instance.associations["buttons"] = Association("Button", 0, -1)
            instance.associations["parent"] = Association("MainApp", 1, 1)
        elif class_name == "Button":
            instance = Button(self.controller, construct_params[0], construct_params[1], construct_params[2])
            instance.associations = {}
            instance.associations["parent"] = Association("Field", 1, 1)
        elif class_name == "Ball":
            instance = Ball(self.controller, construct_params[0], construct_params[1], construct_params[2])
            instance.associations = {}
            instance.associations["parent"] = Association("Field", 1, 1)
        return instance

class Controller(EventLoopControllerBase):
    def __init__(self, event_loop_callbacks, finished_callback = None):
        if finished_callback == None: finished_callback = None
        EventLoopControllerBase.__init__(self, ObjectManager(self), event_loop_callbacks, finished_callback)
        self.addInputPort("ui")
        self.object_manager.createInstance("MainApp", [])