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

Model author: Simon Van Mierlo
Model name:   Bouncing_Balls_Python_Version
Model description:
Bouncing Balls - no UI code.
"""

from sccd.runtime.statecharts_core import *
from ui_classes import *

# 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_windows = 0
    
    def user_defined_destructor(self):
        pass
    
    
    # builds Statechart structure
    def build_statechart_structure(self):
        
        # state <root>
        self.states[""] = State(0, "", self)
        
        # state /main
        self.states["/main"] = ParallelState(1, "/main", self)
        
        # state /main/main_behaviour
        self.states["/main/main_behaviour"] = State(2, "/main/main_behaviour", self)
        
        # state /main/main_behaviour/initializing
        self.states["/main/main_behaviour/initializing"] = State(3, "/main/main_behaviour/initializing", self)
        self.states["/main/main_behaviour/initializing"].setEnter(self._main_main_behaviour_initializing_enter)
        
        # state /main/main_behaviour/running
        self.states["/main/main_behaviour/running"] = State(4, "/main/main_behaviour/running", self)
        
        # state /main/creating_behaviour
        self.states["/main/creating_behaviour"] = State(5, "/main/creating_behaviour", self)
        
        # state /main/creating_behaviour/waiting
        self.states["/main/creating_behaviour/waiting"] = State(6, "/main/creating_behaviour/waiting", self)
        
        # state /main/creating_behaviour/creating
        self.states["/main/creating_behaviour/creating"] = State(7, "/main/creating_behaviour/creating", self)
        
        # state /main/deleting_behaviour
        self.states["/main/deleting_behaviour"] = State(8, "/main/deleting_behaviour", self)
        
        # state /main/deleting_behaviour/waiting
        self.states["/main/deleting_behaviour/waiting"] = State(9, "/main/deleting_behaviour/waiting", self)
        
        # state /main/deleting_behaviour/deleting
        self.states["/main/deleting_behaviour/deleting"] = State(10, "/main/deleting_behaviour/deleting", self)
        
        # state /stopped
        self.states["/stopped"] = State(11, "/stopped", self)
        self.states["/stopped"].setEnter(self._stopped_enter)
        
        # add children
        self.states[""].addChild(self.states["/main"])
        self.states[""].addChild(self.states["/stopped"])
        self.states["/main"].addChild(self.states["/main/main_behaviour"])
        self.states["/main"].addChild(self.states["/main/creating_behaviour"])
        self.states["/main"].addChild(self.states["/main/deleting_behaviour"])
        self.states["/main/main_behaviour"].addChild(self.states["/main/main_behaviour/initializing"])
        self.states["/main/main_behaviour"].addChild(self.states["/main/main_behaviour/running"])
        self.states["/main/creating_behaviour"].addChild(self.states["/main/creating_behaviour/waiting"])
        self.states["/main/creating_behaviour"].addChild(self.states["/main/creating_behaviour/creating"])
        self.states["/main/deleting_behaviour"].addChild(self.states["/main/deleting_behaviour/waiting"])
        self.states["/main/deleting_behaviour"].addChild(self.states["/main/deleting_behaviour/deleting"])
        self.states[""].fixTree()
        self.states[""].default_state = self.states["/main"]
        self.states["/main/main_behaviour"].default_state = self.states["/main/main_behaviour/initializing"]
        self.states["/main/creating_behaviour"].default_state = self.states["/main/creating_behaviour/waiting"]
        self.states["/main/deleting_behaviour"].default_state = self.states["/main/deleting_behaviour/waiting"]
        
        # transition /main/main_behaviour/initializing
        _main_main_behaviour_initializing_0 = Transition(self, self.states["/main/main_behaviour/initializing"], [self.states["/main/main_behaviour/running"]])
        _main_main_behaviour_initializing_0.setTrigger(None)
        self.states["/main/main_behaviour/initializing"].addTransition(_main_main_behaviour_initializing_0)
        
        # transition /main/main_behaviour/running
        _main_main_behaviour_running_0 = Transition(self, self.states["/main/main_behaviour/running"], [self.states["/main/main_behaviour/running"]])
        _main_main_behaviour_running_0.setAction(self._main_main_behaviour_running_0_exec)
        _main_main_behaviour_running_0.setTrigger(Event("window_created", None))
        self.states["/main/main_behaviour/running"].addTransition(_main_main_behaviour_running_0)
        _main_main_behaviour_running_1 = Transition(self, self.states["/main/main_behaviour/running"], [self.states["/main/main_behaviour/running"]])
        _main_main_behaviour_running_1.setAction(self._main_main_behaviour_running_1_exec)
        _main_main_behaviour_running_1.setTrigger(Event("window_deleted", None))
        _main_main_behaviour_running_1.setGuard(self._main_main_behaviour_running_1_guard)
        self.states["/main/main_behaviour/running"].addTransition(_main_main_behaviour_running_1)
        _main_main_behaviour_running_2 = Transition(self, self.states["/main/main_behaviour/running"], [self.states["/main/main_behaviour/running"]])
        _main_main_behaviour_running_2.setAction(self._main_main_behaviour_running_2_exec)
        _main_main_behaviour_running_2.setTrigger(Event("window_deleted", None))
        _main_main_behaviour_running_2.setGuard(self._main_main_behaviour_running_2_guard)
        self.states["/main/main_behaviour/running"].addTransition(_main_main_behaviour_running_2)
        
        # transition /main/creating_behaviour/waiting
        _main_creating_behaviour_waiting_0 = Transition(self, self.states["/main/creating_behaviour/waiting"], [self.states["/main/creating_behaviour/creating"]])
        _main_creating_behaviour_waiting_0.setAction(self._main_creating_behaviour_waiting_0_exec)
        _main_creating_behaviour_waiting_0.setTrigger(Event("create_window", None))
        self.states["/main/creating_behaviour/waiting"].addTransition(_main_creating_behaviour_waiting_0)
        
        # transition /main/creating_behaviour/creating
        _main_creating_behaviour_creating_0 = Transition(self, self.states["/main/creating_behaviour/creating"], [self.states["/main/creating_behaviour/waiting"]])
        _main_creating_behaviour_creating_0.setAction(self._main_creating_behaviour_creating_0_exec)
        _main_creating_behaviour_creating_0.setTrigger(Event("instance_created", None))
        self.states["/main/creating_behaviour/creating"].addTransition(_main_creating_behaviour_creating_0)
        
        # transition /main/deleting_behaviour/waiting
        _main_deleting_behaviour_waiting_0 = Transition(self, self.states["/main/deleting_behaviour/waiting"], [self.states["/main/deleting_behaviour/deleting"]])
        _main_deleting_behaviour_waiting_0.setAction(self._main_deleting_behaviour_waiting_0_exec)
        _main_deleting_behaviour_waiting_0.setTrigger(Event("delete_window", None))
        self.states["/main/deleting_behaviour/waiting"].addTransition(_main_deleting_behaviour_waiting_0)
        
        # transition /main/deleting_behaviour/deleting
        _main_deleting_behaviour_deleting_0 = Transition(self, self.states["/main/deleting_behaviour/deleting"], [self.states["/main/deleting_behaviour/waiting"]])
        _main_deleting_behaviour_deleting_0.setAction(self._main_deleting_behaviour_deleting_0_exec)
        _main_deleting_behaviour_deleting_0.setTrigger(Event("instance_deleted", None))
        self.states["/main/deleting_behaviour/deleting"].addTransition(_main_deleting_behaviour_deleting_0)
        
        # transition /main
        _main_0 = Transition(self, self.states["/main"], [self.states["/stopped"]])
        _main_0.setTrigger(Event("stop", None))
        self.states["/main"].addTransition(_main_0)
    
    def _main_main_behaviour_initializing_enter(self):
        self.raiseInternalEvent(Event("create_window", None, []))
    
    def _stopped_enter(self):
        self.big_step.outputEvent(Event("stop_ui", self.getOutPortName("ui_out"), []))
    
    def _main_main_behaviour_running_0_exec(self, parameters):
        self.nr_of_windows += 1
    
    def _main_main_behaviour_running_1_exec(self, parameters):
        self.nr_of_windows -= 1
    
    def _main_main_behaviour_running_1_guard(self, parameters):
        return self.nr_of_windows > 1
    
    def _main_main_behaviour_running_2_exec(self, parameters):
        self.raiseInternalEvent(Event("stop", None, []))
    
    def _main_main_behaviour_running_2_guard(self, parameters):
        return self.nr_of_windows == 1
    
    def _main_creating_behaviour_waiting_0_exec(self, parameters):
        self.big_step.outputEventOM(Event("create_instance", None, [self, 'windows', 'Window']))
    
    def _main_creating_behaviour_creating_0_exec(self, parameters):
        association_name = parameters[0]
        self.big_step.outputEvent(Event("create_new_window", self.getOutPortName("ui_out"), [association_name, self.getSingleChild(association_name)]))
        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.raiseInternalEvent(Event("window_created", None, []))
    
    def _main_deleting_behaviour_waiting_0_exec(self, parameters):
        association_name = parameters[0]
        self.big_step.outputEvent(Event("delete_window", self.getOutPortName("ui_out"), [association_name]))
        self.big_step.outputEventOM(Event("delete_instance", None, [self, association_name]))
    
    def _main_deleting_behaviour_deleting_0_exec(self, parameters):
        self.raiseInternalEvent(Event("window_deleted", None, []))
    
    def initializeStatechart(self):
        # enter default state
        self.default_targets = self.states["/main"].getEffectiveTargetStates()
        RuntimeClassBase.initializeStatechart(self)

class Window(RuntimeClassBase):
    def __init__(self, controller):
        RuntimeClassBase.__init__(self, controller)
        
        self.inports["window_ui_in"] = controller.addInputPort("window_ui_in", self)
        self.outports["window_ui_out"] = controller.addOutputPort("window_ui_out", self)
        
        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
        Window.user_defined_constructor(self)
    
    def user_defined_constructor(self):
        pass
    
    def user_defined_destructor(self):
        pass
    
    
    # builds Statechart structure
    def build_statechart_structure(self):
        
        # state <root>
        self.states[""] = State(0, "", self)
        
        # state /main
        self.states["/main"] = ParallelState(1, "/main", self)
        
        # state /main/main_behaviour
        self.states["/main/main_behaviour"] = State(2, "/main/main_behaviour", self)
        
        # state /main/main_behaviour/initializing
        self.states["/main/main_behaviour/initializing"] = State(3, "/main/main_behaviour/initializing", self)
        
        # state /main/main_behaviour/waiting_for_ui
        self.states["/main/main_behaviour/waiting_for_ui"] = State(4, "/main/main_behaviour/waiting_for_ui", self)
        
        # state /main/main_behaviour/creating_button
        self.states["/main/main_behaviour/creating_button"] = State(5, "/main/main_behaviour/creating_button", self)
        
        # state /main/main_behaviour/running
        self.states["/main/main_behaviour/running"] = State(6, "/main/main_behaviour/running", self)
        
        # state /main/main_behaviour/creating_ball
        self.states["/main/main_behaviour/creating_ball"] = State(7, "/main/main_behaviour/creating_ball", self)
        
        # state /main/size_change_listener
        self.states["/main/size_change_listener"] = State(8, "/main/size_change_listener", self)
        
        # state /main/size_change_listener/listening
        self.states["/main/size_change_listener/listening"] = State(9, "/main/size_change_listener/listening", self)
        
        # state /main/delete_listener
        self.states["/main/delete_listener"] = State(10, "/main/delete_listener", self)
        
        # state /main/delete_listener/listening
        self.states["/main/delete_listener/listening"] = State(11, "/main/delete_listener/listening", self)
        
        # state /stopped
        self.states["/stopped"] = State(12, "/stopped", self)
        self.states["/stopped"].setEnter(self._stopped_enter)
        
        # add children
        self.states[""].addChild(self.states["/main"])
        self.states[""].addChild(self.states["/stopped"])
        self.states["/main"].addChild(self.states["/main/main_behaviour"])
        self.states["/main"].addChild(self.states["/main/size_change_listener"])
        self.states["/main"].addChild(self.states["/main/delete_listener"])
        self.states["/main/main_behaviour"].addChild(self.states["/main/main_behaviour/initializing"])
        self.states["/main/main_behaviour"].addChild(self.states["/main/main_behaviour/waiting_for_ui"])
        self.states["/main/main_behaviour"].addChild(self.states["/main/main_behaviour/creating_button"])
        self.states["/main/main_behaviour"].addChild(self.states["/main/main_behaviour/running"])
        self.states["/main/main_behaviour"].addChild(self.states["/main/main_behaviour/creating_ball"])
        self.states["/main/size_change_listener"].addChild(self.states["/main/size_change_listener/listening"])
        self.states["/main/delete_listener"].addChild(self.states["/main/delete_listener/listening"])
        self.states[""].fixTree()
        self.states[""].default_state = self.states["/main"]
        self.states["/main/main_behaviour"].default_state = self.states["/main/main_behaviour/initializing"]
        self.states["/main/size_change_listener"].default_state = self.states["/main/size_change_listener/listening"]
        self.states["/main/delete_listener"].default_state = self.states["/main/delete_listener/listening"]
        
        # transition /main/main_behaviour/initializing
        _main_main_behaviour_initializing_0 = Transition(self, self.states["/main/main_behaviour/initializing"], [self.states["/main/main_behaviour/waiting_for_ui"]])
        _main_main_behaviour_initializing_0.setAction(self._main_main_behaviour_initializing_0_exec)
        _main_main_behaviour_initializing_0.setTrigger(Event("set_association_name", None))
        self.states["/main/main_behaviour/initializing"].addTransition(_main_main_behaviour_initializing_0)
        
        # transition /main/main_behaviour/waiting_for_ui
        _main_main_behaviour_waiting_for_ui_0 = Transition(self, self.states["/main/main_behaviour/waiting_for_ui"], [self.states["/main/main_behaviour/creating_button"]])
        _main_main_behaviour_waiting_for_ui_0.setAction(self._main_main_behaviour_waiting_for_ui_0_exec)
        _main_main_behaviour_waiting_for_ui_0.setTrigger(Event("ui_initialized", self.getInPortName("window_ui_in")))
        self.states["/main/main_behaviour/waiting_for_ui"].addTransition(_main_main_behaviour_waiting_for_ui_0)
        
        # transition /main/main_behaviour/creating_button
        _main_main_behaviour_creating_button_0 = Transition(self, self.states["/main/main_behaviour/creating_button"], [self.states["/main/main_behaviour/running"]])
        _main_main_behaviour_creating_button_0.setAction(self._main_main_behaviour_creating_button_0_exec)
        _main_main_behaviour_creating_button_0.setTrigger(Event("instance_created", None))
        self.states["/main/main_behaviour/creating_button"].addTransition(_main_main_behaviour_creating_button_0)
        
        # transition /main/main_behaviour/running
        _main_main_behaviour_running_0 = Transition(self, self.states["/main/main_behaviour/running"], [self.states["/main/main_behaviour/running"]])
        _main_main_behaviour_running_0.setAction(self._main_main_behaviour_running_0_exec)
        _main_main_behaviour_running_0.setTrigger(Event("button_pressed", None))
        _main_main_behaviour_running_0.setGuard(self._main_main_behaviour_running_0_guard)
        self.states["/main/main_behaviour/running"].addTransition(_main_main_behaviour_running_0)
        _main_main_behaviour_running_1 = Transition(self, self.states["/main/main_behaviour/running"], [self.states["/main/main_behaviour/running"]])
        _main_main_behaviour_running_1.setAction(self._main_main_behaviour_running_1_exec)
        _main_main_behaviour_running_1.setTrigger(Event("close_window", self.getInPortName("window_ui_in")))
        self.states["/main/main_behaviour/running"].addTransition(_main_main_behaviour_running_1)
        _main_main_behaviour_running_2 = Transition(self, self.states["/main/main_behaviour/running"], [self.states["/main/main_behaviour/creating_ball"]])
        _main_main_behaviour_running_2.setAction(self._main_main_behaviour_running_2_exec)
        _main_main_behaviour_running_2.setTrigger(Event("create_ball", self.getInPortName("window_ui_in")))
        self.states["/main/main_behaviour/running"].addTransition(_main_main_behaviour_running_2)
        _main_main_behaviour_running_3 = Transition(self, self.states["/main/main_behaviour/running"], [self.states["/main/main_behaviour/running"]])
        _main_main_behaviour_running_3.setAction(self._main_main_behaviour_running_3_exec)
        _main_main_behaviour_running_3.setTrigger(Event("delete_ball", None))
        self.states["/main/main_behaviour/running"].addTransition(_main_main_behaviour_running_3)
        
        # transition /main/main_behaviour/creating_ball
        _main_main_behaviour_creating_ball_0 = Transition(self, self.states["/main/main_behaviour/creating_ball"], [self.states["/main/main_behaviour/running"]])
        _main_main_behaviour_creating_ball_0.setAction(self._main_main_behaviour_creating_ball_0_exec)
        _main_main_behaviour_creating_ball_0.setTrigger(Event("instance_created", None))
        self.states["/main/main_behaviour/creating_ball"].addTransition(_main_main_behaviour_creating_ball_0)
        
        # transition /main/size_change_listener/listening
        _main_size_change_listener_listening_0 = Transition(self, self.states["/main/size_change_listener/listening"], [self.states["/main/size_change_listener/listening"]])
        _main_size_change_listener_listening_0.setAction(self._main_size_change_listener_listening_0_exec)
        _main_size_change_listener_listening_0.setTrigger(Event("size_changed", self.getInPortName("window_ui_in")))
        self.states["/main/size_change_listener/listening"].addTransition(_main_size_change_listener_listening_0)
        
        # transition /main/delete_listener/listening
        _main_delete_listener_listening_0 = Transition(self, self.states["/main/delete_listener/listening"], [self.states["/main/delete_listener/listening"]])
        _main_delete_listener_listening_0.setAction(self._main_delete_listener_listening_0_exec)
        _main_delete_listener_listening_0.setTrigger(Event("delete", self.getInPortName("window_ui_in")))
        self.states["/main/delete_listener/listening"].addTransition(_main_delete_listener_listening_0)
        
        # transition /main
        _main_0 = Transition(self, self.states["/main"], [self.states["/stopped"]])
        _main_0.setAction(self._main_0_exec)
        _main_0.setTrigger(Event("stop", None))
        self.states["/main"].addTransition(_main_0)
    
    def _stopped_enter(self):
        self.big_step.outputEventOM(Event("narrow_cast", None, [self, 'parent', Event("delete_window", None, [self.association_name])]))
    
    def _main_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 _main_main_behaviour_initializing_0_exec(self, parameters):
        association_name = parameters[0]
        self.association_name = association_name
    
    def _main_main_behaviour_waiting_for_ui_0_exec(self, parameters):
        width = parameters[0]
        height = parameters[1]
        self.width = width
        self.height = height
        self.big_step.outputEventOM(Event("create_instance", None, [self, "buttons", "Button", 'create_window']))
    
    def _main_main_behaviour_creating_button_0_exec(self, parameters):
        association_name = parameters[0]
        self.big_step.outputEvent(Event("create_new_button", self.getOutPortName("window_ui_out"), [association_name, self.getSingleChild(association_name)]))
        self.big_step.outputEventOM(Event("start_instance", None, [self, association_name]))
    
    def _main_main_behaviour_running_0_exec(self, parameters):
        event_name = parameters[0]
        self.big_step.outputEventOM(Event("narrow_cast", None, [self, 'parent', Event("create_window", None, [])]))
    
    def _main_main_behaviour_running_0_guard(self, parameters):
        event_name = parameters[0]
        return event_name == 'create_window'
    
    def _main_main_behaviour_running_1_exec(self, parameters):
        self.raiseInternalEvent(Event("stop", None, []))
    
    def _main_main_behaviour_running_2_exec(self, parameters):
        x = parameters[0]
        y = parameters[1]
        self.big_step.outputEventOM(Event("create_instance", None, [self, "balls", "Ball", x, y, self.width, self.height]))
    
    def _main_main_behaviour_running_3_exec(self, parameters):
        association_name = parameters[0]
        self.big_step.outputEventOM(Event("delete_instance", None, [self, association_name]))
        self.big_step.outputEvent(Event("delete_ball", self.getOutPortName("window_ui_out"), [association_name]))
    
    def _main_main_behaviour_creating_ball_0_exec(self, parameters):
        association_name = parameters[0]
        self.big_step.outputEvent(Event("create_new_ball", self.getOutPortName("window_ui_out"), [association_name, self.getSingleChild(association_name)]))
        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 _main_size_change_listener_listening_0_exec(self, parameters):
        new_width = parameters[0]
        new_height = parameters[1]
        self.width = new_width
        self.height = new_height
        self.big_step.outputEventOM(Event("narrow_cast", None, [self, 'balls', Event("window_size_changed", None, [new_width, new_height])]))
    
    def _main_delete_listener_listening_0_exec(self, parameters):
        self.big_step.outputEventOM(Event("narrow_cast", None, [self, 'balls', Event("delete", None, [])]))
    
    def initializeStatechart(self):
        # enter default state
        self.default_targets = self.states["/main"].getEffectiveTargetStates()
        RuntimeClassBase.initializeStatechart(self)

class Button(RuntimeClassBase):
    def __init__(self, controller, event_name):
        RuntimeClassBase.__init__(self, controller)
        
        self.inports["button_ui_in"] = controller.addInputPort("button_ui_in", self)
        self.outports["button_ui_out"] = controller.addOutputPort("button_ui_out", self)
        
        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
        Button.user_defined_constructor(self, event_name)
    
    def user_defined_constructor(self, event_name):
        self.event_name = event_name
    
    def user_defined_destructor(self):
        pass
    
    
    # builds Statechart structure
    def build_statechart_structure(self):
        
        # state <root>
        self.states[""] = State(0, "", self)
        
        # state /waiting
        self.states["/waiting"] = State(1, "/waiting", self)
        
        # state /running
        self.states["/running"] = State(2, "/running", self)
        
        # add children
        self.states[""].addChild(self.states["/waiting"])
        self.states[""].addChild(self.states["/running"])
        self.states[""].fixTree()
        self.states[""].default_state = self.states["/waiting"]
        
        # transition /waiting
        _waiting_0 = Transition(self, self.states["/waiting"], [self.states["/running"]])
        _waiting_0.setAction(self._waiting_0_exec)
        _waiting_0.setTrigger(Event("ui_initialized", self.getInPortName("button_ui_in")))
        self.states["/waiting"].addTransition(_waiting_0)
        
        # transition /running
        _running_0 = Transition(self, self.states["/running"], [self.states["/running"]])
        _running_0.setAction(self._running_0_exec)
        _running_0.setTrigger(Event("clicked", self.getInPortName("button_ui_in")))
        self.states["/running"].addTransition(_running_0)
    
    def _waiting_0_exec(self, parameters):
        self.big_step.outputEvent(Event("set_text", self.getOutPortName("button_ui_out"), [self.event_name]))
    
    def _running_0_exec(self, parameters):
        self.big_step.outputEventOM(Event("narrow_cast", None, [self, 'parent', Event("button_pressed", None, [self.event_name])]))
    
    def initializeStatechart(self):
        # enter default state
        self.default_targets = self.states["/waiting"].getEffectiveTargetStates()
        RuntimeClassBase.initializeStatechart(self)

class Ball(RuntimeClassBase):
    def __init__(self, controller, x, y, window_width, window_height):
        RuntimeClassBase.__init__(self, controller)
        
        self.inports["ball_ui_in"] = controller.addInputPort("ball_ui_in", self)
        self.outports["ball_ui_out"] = controller.addOutputPort("ball_ui_out", self)
        
        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
        Ball.user_defined_constructor(self, x, y, window_width, window_height)
    
    def user_defined_constructor(self, x, y, window_width, window_height):
        self.x = x
        self.y = y
        self.window_width = window_width
        self.window_height = window_height
        self.r = 20.0;
        self.vel = {'x': random.uniform(-5.0, 5.0), 'y': random.uniform(-5.0, 5.0)};
        self.smooth = 0.4 # value between 0 and 1
    
    def user_defined_destructor(self):
        pass
    
    
    # builds Statechart structure
    def build_statechart_structure(self):
        
        # state <root>
        self.states[""] = State(0, "", self)
        
        # state /main
        self.states["/main"] = ParallelState(1, "/main", self)
        
        # state /main/main_behaviour
        self.states["/main/main_behaviour"] = State(2, "/main/main_behaviour", self)
        
        # state /main/main_behaviour/initializing
        self.states["/main/main_behaviour/initializing"] = State(3, "/main/main_behaviour/initializing", self)
        
        # state /main/main_behaviour/waiting_for_ui
        self.states["/main/main_behaviour/waiting_for_ui"] = State(4, "/main/main_behaviour/waiting_for_ui", self)
        
        # state /main/main_behaviour/bouncing
        self.states["/main/main_behaviour/bouncing"] = State(5, "/main/main_behaviour/bouncing", self)
        self.states["/main/main_behaviour/bouncing"].setEnter(self._main_main_behaviour_bouncing_enter)
        self.states["/main/main_behaviour/bouncing"].setExit(self._main_main_behaviour_bouncing_exit)
        
        # state /main/main_behaviour/dragging
        self.states["/main/main_behaviour/dragging"] = State(6, "/main/main_behaviour/dragging", self)
        
        # state /main/main_behaviour/selected
        self.states["/main/main_behaviour/selected"] = State(7, "/main/main_behaviour/selected", self)
        
        # state /main/size_change_listener
        self.states["/main/size_change_listener"] = State(8, "/main/size_change_listener", self)
        
        # state /main/size_change_listener/listening
        self.states["/main/size_change_listener/listening"] = State(9, "/main/size_change_listener/listening", self)
        
        # state /deleted
        self.states["/deleted"] = State(10, "/deleted", self)
        
        # add children
        self.states[""].addChild(self.states["/main"])
        self.states[""].addChild(self.states["/deleted"])
        self.states["/main"].addChild(self.states["/main/main_behaviour"])
        self.states["/main"].addChild(self.states["/main/size_change_listener"])
        self.states["/main/main_behaviour"].addChild(self.states["/main/main_behaviour/initializing"])
        self.states["/main/main_behaviour"].addChild(self.states["/main/main_behaviour/waiting_for_ui"])
        self.states["/main/main_behaviour"].addChild(self.states["/main/main_behaviour/bouncing"])
        self.states["/main/main_behaviour"].addChild(self.states["/main/main_behaviour/dragging"])
        self.states["/main/main_behaviour"].addChild(self.states["/main/main_behaviour/selected"])
        self.states["/main/size_change_listener"].addChild(self.states["/main/size_change_listener/listening"])
        self.states[""].fixTree()
        self.states[""].default_state = self.states["/main"]
        self.states["/main/main_behaviour"].default_state = self.states["/main/main_behaviour/initializing"]
        self.states["/main/size_change_listener"].default_state = self.states["/main/size_change_listener/listening"]
        
        # transition /main/main_behaviour/initializing
        _main_main_behaviour_initializing_0 = Transition(self, self.states["/main/main_behaviour/initializing"], [self.states["/main/main_behaviour/waiting_for_ui"]])
        _main_main_behaviour_initializing_0.setAction(self._main_main_behaviour_initializing_0_exec)
        _main_main_behaviour_initializing_0.setTrigger(Event("set_association_name", None))
        self.states["/main/main_behaviour/initializing"].addTransition(_main_main_behaviour_initializing_0)
        
        # transition /main/main_behaviour/waiting_for_ui
        _main_main_behaviour_waiting_for_ui_0 = Transition(self, self.states["/main/main_behaviour/waiting_for_ui"], [self.states["/main/main_behaviour/bouncing"]])
        _main_main_behaviour_waiting_for_ui_0.setAction(self._main_main_behaviour_waiting_for_ui_0_exec)
        _main_main_behaviour_waiting_for_ui_0.setTrigger(Event("ui_initialized", self.getInPortName("ball_ui_in")))
        self.states["/main/main_behaviour/waiting_for_ui"].addTransition(_main_main_behaviour_waiting_for_ui_0)
        
        # transition /main/main_behaviour/bouncing
        _main_main_behaviour_bouncing_0 = Transition(self, self.states["/main/main_behaviour/bouncing"], [self.states["/main/main_behaviour/bouncing"]])
        _main_main_behaviour_bouncing_0.setAction(self._main_main_behaviour_bouncing_0_exec)
        _main_main_behaviour_bouncing_0.setTrigger(Event("_0after"))
        self.states["/main/main_behaviour/bouncing"].addTransition(_main_main_behaviour_bouncing_0)
        _main_main_behaviour_bouncing_1 = Transition(self, self.states["/main/main_behaviour/bouncing"], [self.states["/main/main_behaviour/selected"]])
        _main_main_behaviour_bouncing_1.setAction(self._main_main_behaviour_bouncing_1_exec)
        _main_main_behaviour_bouncing_1.setTrigger(Event("select_ball", self.getInPortName("ball_ui_in")))
        self.states["/main/main_behaviour/bouncing"].addTransition(_main_main_behaviour_bouncing_1)
        
        # transition /main/main_behaviour/dragging
        _main_main_behaviour_dragging_0 = Transition(self, self.states["/main/main_behaviour/dragging"], [self.states["/main/main_behaviour/dragging"]])
        _main_main_behaviour_dragging_0.setAction(self._main_main_behaviour_dragging_0_exec)
        _main_main_behaviour_dragging_0.setTrigger(Event("motion", self.getInPortName("ball_ui_in")))
        self.states["/main/main_behaviour/dragging"].addTransition(_main_main_behaviour_dragging_0)
        _main_main_behaviour_dragging_1 = Transition(self, self.states["/main/main_behaviour/dragging"], [self.states["/main/main_behaviour/bouncing"]])
        _main_main_behaviour_dragging_1.setAction(self._main_main_behaviour_dragging_1_exec)
        _main_main_behaviour_dragging_1.setTrigger(Event("unselect_ball", self.getInPortName("ball_ui_in")))
        self.states["/main/main_behaviour/dragging"].addTransition(_main_main_behaviour_dragging_1)
        
        # transition /main/main_behaviour/selected
        _main_main_behaviour_selected_0 = Transition(self, self.states["/main/main_behaviour/selected"], [self.states["/main/main_behaviour/dragging"]])
        _main_main_behaviour_selected_0.setTrigger(Event("select_ball", self.getInPortName("ball_ui_in")))
        self.states["/main/main_behaviour/selected"].addTransition(_main_main_behaviour_selected_0)
        _main_main_behaviour_selected_1 = Transition(self, self.states["/main/main_behaviour/selected"], [self.states["/main/main_behaviour/selected"]])
        _main_main_behaviour_selected_1.setAction(self._main_main_behaviour_selected_1_exec)
        _main_main_behaviour_selected_1.setTrigger(Event("delete", None))
        self.states["/main/main_behaviour/selected"].addTransition(_main_main_behaviour_selected_1)
        
        # transition /main/size_change_listener/listening
        _main_size_change_listener_listening_0 = Transition(self, self.states["/main/size_change_listener/listening"], [self.states["/main/size_change_listener/listening"]])
        _main_size_change_listener_listening_0.setAction(self._main_size_change_listener_listening_0_exec)
        _main_size_change_listener_listening_0.setTrigger(Event("window_size_changed", None))
        self.states["/main/size_change_listener/listening"].addTransition(_main_size_change_listener_listening_0)
        
        # transition /main
        _main_0 = Transition(self, self.states["/main"], [self.states["/deleted"]])
        _main_0.setTrigger(Event("delete_local", None))
        self.states["/main"].addTransition(_main_0)
    
    def _main_main_behaviour_bouncing_enter(self):
        self.addTimer(0, (20 - self.getSimulatedTime() % 20) / 1000.0)
    
    def _main_main_behaviour_bouncing_exit(self):
        self.removeTimer(0)
    
    def _main_main_behaviour_initializing_0_exec(self, parameters):
        association_name = parameters[0]
        self.association_name = association_name
    
    def _main_main_behaviour_waiting_for_ui_0_exec(self, parameters):
        self.big_step.outputEvent(Event("set_initial_params", self.getOutPortName("ball_ui_out"), [self.x, self.y, self.r]))
    
    def _main_main_behaviour_bouncing_0_exec(self, parameters):
        if self.x <= 0 or self.x + (self.r * 2) >= self.window_width:
            self.vel['x'] = -self.vel['x']
        if self.y <= 0 or self.y + (self.r * 2) >= self.window_height:
            self.vel['y'] = -self.vel['y']
        self.x += self.vel['x']
        self.y += self.vel['y']
        self.big_step.outputEvent(Event("change_position", self.getOutPortName("ball_ui_out"), [self.x, self.y]))
    
    def _main_main_behaviour_bouncing_1_exec(self, parameters):
        self.big_step.outputEvent(Event("change_color", self.getOutPortName("ball_ui_out"), ['yellow']))
    
    def _main_main_behaviour_dragging_0_exec(self, parameters):
        dx = parameters[0]
        dy = parameters[1]
        self.x += dx
        self.y += dy
        if self.x - self.r <= 0:
            self.x = 1;
        elif self.x + self.r >= self.window_width:
            self.x = self.window_width - (2 * self.r) - 1
        if self.y - self.r <= 0:
            self.y = 1
        elif self.y + self.r >= self.window_height:
            self.y = self.window_height - (2 * self.r) - 1;
        self.vel = {
            'x': (1 - self.smooth) * dx + self.smooth * self.vel['x'],
            'y': (1 - self.smooth) * dy + self.smooth * self.vel['y']
        }
        self.big_step.outputEvent(Event("change_position", self.getOutPortName("ball_ui_out"), [self.x, self.y]))
    
    def _main_main_behaviour_dragging_1_exec(self, parameters):
        self.big_step.outputEvent(Event("change_color", self.getOutPortName("ball_ui_out"), ['red']))
    
    def _main_main_behaviour_selected_1_exec(self, parameters):
        self.big_step.outputEventOM(Event("narrow_cast", None, [self, 'parent', Event("delete_ball", None, [self.association_name])]))
        self.raiseInternalEvent(Event("delete_local", None, []))
    
    def _main_size_change_listener_listening_0_exec(self, parameters):
        new_width = parameters[0]
        new_height = parameters[1]
        self.window_width = new_width
        self.window_height = new_height
    
    def initializeStatechart(self):
        # enter default state
        self.default_targets = self.states["/main"].getEffectiveTargetStates()
        RuntimeClassBase.initializeStatechart(self)

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["windows"] = Association("Window", 0, -1)
        elif class_name == "Window":
            instance = Window(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])
            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], construct_params[3])
            instance.associations = {}
            instance.associations["parent"] = Association("Window", 1, 1)
        else:
            raise Exception("Cannot instantiate class " + class_name)
        return instance

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