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

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

from sccd.runtime.DEVS_statecharts_core import *
from sccd.runtime.libs import ui_v2 as ui
from sccd.runtime.libs.utils import utils
import random

CANVAS_WIDTH = 800
CANVAS_HEIGHT = 550

# package "Bouncing_Balls_Python_Version"

class MainAppInstance(RuntimeClassBase):
    def __init__(self, atomdevs):
        RuntimeClassBase.__init__(self, atomdevs)
        self.associations = {}
        self.associations["fields"] = Association("Field", 0, -1)
        
        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
        MainAppInstance.user_defined_constructor(self)
        port_name = Ports.addInputPort("<narrow_cast>", self)
        atomdevs.addInPort(port_name)
    
    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, "/running", self)
        
        # state /running/root
        self.states["/running/root"] = ParallelState(2, "/running/root", self)
        
        # state /running/root/main_behaviour
        self.states["/running/root/main_behaviour"] = State(3, "/running/root/main_behaviour", self)
        
        # state /running/root/main_behaviour/initializing
        self.states["/running/root/main_behaviour/initializing"] = State(4, "/running/root/main_behaviour/initializing", self)
        
        # state /running/root/main_behaviour/running
        self.states["/running/root/main_behaviour/running"] = State(5, "/running/root/main_behaviour/running", self)
        
        # state /running/root/cd_behaviour
        self.states["/running/root/cd_behaviour"] = State(6, "/running/root/cd_behaviour", self)
        
        # state /running/root/cd_behaviour/waiting
        self.states["/running/root/cd_behaviour/waiting"] = State(7, "/running/root/cd_behaviour/waiting", self)
        
        # state /running/root/cd_behaviour/creating
        self.states["/running/root/cd_behaviour/creating"] = State(8, "/running/root/cd_behaviour/creating", self)
        
        # state /running/root/cd_behaviour/check_nr_of_fields
        self.states["/running/root/cd_behaviour/check_nr_of_fields"] = State(9, "/running/root/cd_behaviour/check_nr_of_fields", 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/root/cd_behaviour/stopped
        self.states["/running/root/cd_behaviour/stopped"] = State(10, "/running/root/cd_behaviour/stopped", self)
        
        # state /running/stopped
        self.states["/running/stopped"] = State(11, "/running/stopped", 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["/running/root/cd_behaviour"].addChild(self.states["/running/root/cd_behaviour/stopped"])
        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)
        _running_root_main_behaviour_initializing_0.setTrigger(None)
        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.setTrigger(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.setTrigger(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.setTrigger(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.setTrigger(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/stopped"]])
        _running_root_cd_behaviour_check_nr_of_fields_0.setAction(self._running_root_cd_behaviour_check_nr_of_fields_0_exec)
        _running_root_cd_behaviour_check_nr_of_fields_0.setTrigger(Event("_0after"))
        _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/root/cd_behaviour/waiting"]])
        _running_root_cd_behaviour_check_nr_of_fields_1.setTrigger(None)
        _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)
        
        # transition /running/root
        _running_root_0 = Transition(self, self.states["/running/root"], [self.states["/running/stopped"]])
        _running_root_0.setAction(self._running_root_0_exec)
        _running_root_0.setTrigger(Event("stop", None))
        self.states["/running/root"].addTransition(_running_root_0)
    
    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_0_exec(self, parameters):
        self.big_step.outputEvent(Event("destroy_all", self.getOutPortName("ui"), []))
    
    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_exec(self, parameters):
        self.raiseInternalEvent(Event("stop", None, []))
    
    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_guard(self, parameters):
        return self.nr_of_fields != 0
    
    def initializeStatechart(self):
        # enter default state
        self.default_targets = self.states["/running"].getEffectiveTargetStates()
        RuntimeClassBase.initializeStatechart(self)

class MainApp(ObjectManagerBase):
    def __init__(self, name):
        ObjectManagerBase.__init__(self, name)
        self.input = self.addInPort("input")
        self.output = self.addOutPort("ui")
        self.outputs["fields"] = self.addOutPort("fields")
        self.instances[self.next_instance] = MainAppInstance(self)
        self.next_instance = self.next_instance + 1
    
    def constructObject(self, parameters):
        new_instance = MainAppInstance(self)
        return new_instance

class FieldInstance(RuntimeClassBase):
    def __init__(self, atomdevs):
        RuntimeClassBase.__init__(self, atomdevs)
        self.associations = {}
        self.associations["balls"] = Association("Ball", 0, -1)
        self.associations["buttons"] = Association("Button", 0, -1)
        self.associations["parent"] = Association("MainApp", 1, 1)
        
        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()
        
        # user defined attributes
        self.window_id = None
        self.canvas_id = None
        
        # call user defined constructor
        FieldInstance.user_defined_constructor(self)
        port_name = Ports.addInputPort("<narrow_cast>", self)
        atomdevs.addInPort(port_name)
        port_name = Ports.addInputPort("field_ui", self)
        atomdevs.addInPort(port_name)
        atomdevs.port_mappings[port_name] = atomdevs.next_instance
        self.inports["field_ui"] = port_name
    
    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 /root
        self.states["/root"] = State(1, "/root", self)
        
        # state /root/waiting
        self.states["/root/waiting"] = State(2, "/root/waiting", self)
        
        # state /root/creating_window
        self.states["/root/creating_window"] = State(3, "/root/creating_window", self)
        self.states["/root/creating_window"].setEnter(self._root_creating_window_enter)
        
        # state /root/creating_canvas
        self.states["/root/creating_canvas"] = State(4, "/root/creating_canvas", self)
        self.states["/root/creating_canvas"].setEnter(self._root_creating_canvas_enter)
        
        # state /root/creating_button
        self.states["/root/creating_button"] = State(5, "/root/creating_button", self)
        self.states["/root/creating_button"].setEnter(self._root_creating_button_enter)
        
        # state /root/running
        self.states["/root/running"] = ParallelState(6, "/root/running", self)
        
        # state /root/running/main_behaviour
        self.states["/root/running/main_behaviour"] = State(7, "/root/running/main_behaviour", self)
        
        # state /root/running/main_behaviour/running
        self.states["/root/running/main_behaviour/running"] = State(8, "/root/running/main_behaviour/running", self)
        
        # state /root/running/main_behaviour/creating_ball
        self.states["/root/running/main_behaviour/creating_ball"] = State(9, "/root/running/main_behaviour/creating_ball", self)
        
        # state /root/running/deleting_behaviour
        self.states["/root/running/deleting_behaviour"] = State(10, "/root/running/deleting_behaviour", self)
        
        # state /root/running/deleting_behaviour/running
        self.states["/root/running/deleting_behaviour/running"] = State(11, "/root/running/deleting_behaviour/running", self)
        
        # state /root/running/child_behaviour
        self.states["/root/running/child_behaviour"] = State(12, "/root/running/child_behaviour", self)
        
        # state /root/running/child_behaviour/listening
        self.states["/root/running/child_behaviour/listening"] = State(13, "/root/running/child_behaviour/listening", self)
        
        # state /root/running/deleting_balls_behaviour
        self.states["/root/running/deleting_balls_behaviour"] = State(14, "/root/running/deleting_balls_behaviour", self)
        
        # state /root/running/deleting_balls_behaviour/listening
        self.states["/root/running/deleting_balls_behaviour/listening"] = State(15, "/root/running/deleting_balls_behaviour/listening", self)
        
        # state /root/deleting
        self.states["/root/deleting"] = State(16, "/root/deleting", self)
        
        # state /root/deleted
        self.states["/root/deleted"] = State(17, "/root/deleted", self)
        
        # add children
        self.states[""].addChild(self.states["/root"])
        self.states["/root"].addChild(self.states["/root/waiting"])
        self.states["/root"].addChild(self.states["/root/creating_window"])
        self.states["/root"].addChild(self.states["/root/creating_canvas"])
        self.states["/root"].addChild(self.states["/root/creating_button"])
        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_ball"])
        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/creating_window"]])
        _root_waiting_0.setAction(self._root_waiting_0_exec)
        _root_waiting_0.setTrigger(Event("set_association_name", None))
        self.states["/root/waiting"].addTransition(_root_waiting_0)
        
        # transition /root/creating_window
        _root_creating_window_0 = Transition(self, self.states["/root/creating_window"], [self.states["/root/creating_canvas"]])
        _root_creating_window_0.setAction(self._root_creating_window_0_exec)
        _root_creating_window_0.setTrigger(Event("window_created", None))
        self.states["/root/creating_window"].addTransition(_root_creating_window_0)
        
        # transition /root/creating_canvas
        _root_creating_canvas_0 = Transition(self, self.states["/root/creating_canvas"], [self.states["/root/creating_button"]])
        _root_creating_canvas_0.setAction(self._root_creating_canvas_0_exec)
        _root_creating_canvas_0.setTrigger(Event("canvas_created", None))
        self.states["/root/creating_canvas"].addTransition(_root_creating_canvas_0)
        
        # transition /root/creating_button
        _root_creating_button_0 = Transition(self, self.states["/root/creating_button"], [self.states["/root/running"]])
        _root_creating_button_0.setAction(self._root_creating_button_0_exec)
        _root_creating_button_0.setTrigger(Event("instance_created", None))
        self.states["/root/creating_button"].addTransition(_root_creating_button_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_ball"]])
        _root_running_main_behaviour_running_0.setAction(self._root_running_main_behaviour_running_0_exec)
        _root_running_main_behaviour_running_0.setTrigger(Event("right_click", self.getInPortName("field_ui")))
        self.states["/root/running/main_behaviour/running"].addTransition(_root_running_main_behaviour_running_0)
        
        # transition /root/running/main_behaviour/creating_ball
        _root_running_main_behaviour_creating_ball_0 = Transition(self, self.states["/root/running/main_behaviour/creating_ball"], [self.states["/root/running/main_behaviour/running"]])
        _root_running_main_behaviour_creating_ball_0.setAction(self._root_running_main_behaviour_creating_ball_0_exec)
        _root_running_main_behaviour_creating_ball_0.setTrigger(Event("instance_created", None))
        self.states["/root/running/main_behaviour/creating_ball"].addTransition(_root_running_main_behaviour_creating_ball_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.setTrigger(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.setTrigger(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.setTrigger(Event("key_press", self.getInPortName("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)
        _root_deleting_0.setTrigger(None)
        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.setTrigger(Event("window_close", self.getInPortName("field_ui")))
        self.states["/root/running"].addTransition(_root_running_0)
    
    def _root_creating_window_enter(self):
        self.big_step.outputEvent(Event("create_window", self.getOutPortName("ui"), [800, 600, "BouncingBalls", self.inports['field_ui']]))
    
    def _root_creating_canvas_enter(self):
        self.big_step.outputEvent(Event("create_canvas", self.getOutPortName("ui"), [self.window_id, CANVAS_WIDTH, CANVAS_HEIGHT, {'background':'#eee'}, self.inports['field_ui']]))
    
    def _root_creating_button_enter(self):
        self.big_step.outputEventOM(Event("create_instance", None, [self, "buttons", "Button", self.window_id, 'create_new_field', 'Spawn New Window']))
    
    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_creating_window_0_exec(self, parameters):
        window_id = parameters[0]
        self.window_id = window_id
        self.big_step.outputEvent(Event("bind_event", self.getOutPortName("ui"), [window_id, ui.EVENTS.WINDOW_CLOSE, 'window_close', self.inports['field_ui']]))
        self.big_step.outputEvent(Event("bind_event", self.getOutPortName("ui"), [window_id, ui.EVENTS.KEY_PRESS, 'key_press', self.inports['field_ui']]))
    
    def _root_creating_canvas_0_exec(self, parameters):
        canvas_id = parameters[0]
        self.canvas_id = canvas_id
        self.big_step.outputEvent(Event("bind_event", self.getOutPortName("ui"), [canvas_id, ui.EVENTS.MOUSE_RIGHT_CLICK, 'right_click', self.inports['field_ui']]))
        self.big_step.outputEvent(Event("bind_event", self.getOutPortName("ui"), [canvas_id, ui.EVENTS.MOUSE_MOVE, 'mouse_move', self.inports['field_ui']]))
        self.big_step.outputEvent(Event("bind_event", self.getOutPortName("ui"), [canvas_id, ui.EVENTS.MOUSE_RELEASE, 'mouse_release', self.inports['field_ui']]))
    
    def _root_creating_button_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_id, x, y]))
    
    def _root_running_main_behaviour_creating_ball_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])]))
        self.big_step.outputEvent(Event("destroy_window", self.getOutPortName("ui"), [self.window_id]))
    
    def initializeStatechart(self):
        # enter default state
        self.default_targets = self.states["/root"].getEffectiveTargetStates()
        RuntimeClassBase.initializeStatechart(self)

class Field(ObjectManagerBase):
    def __init__(self, name):
        ObjectManagerBase.__init__(self, name)
        self.input = self.addInPort("input")
        self.output = self.addOutPort("ui")
        self.outputs["balls"] = self.addOutPort("balls")
        self.outputs["buttons"] = self.addOutPort("buttons")
        self.outputs["parent"] = self.addOutPort("parent")
        self.field_ui = self.addInPort("field_ui")
    
    def constructObject(self, parameters):
        new_instance = FieldInstance(self)
        return new_instance

class ButtonInstance(RuntimeClassBase):
    def __init__(self, atomdevs, window_id, event_name, button_text):
        RuntimeClassBase.__init__(self, atomdevs)
        self.associations = {}
        self.associations["parent"] = Association("Field", 1, 1)
        
        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()
        
        # user defined attributes
        self.window_id = None
        self.event_name = None
        self.button_id = None
        
        # call user defined constructor
        ButtonInstance.user_defined_constructor(self, window_id, event_name, button_text)
        port_name = Ports.addInputPort("<narrow_cast>", self)
        atomdevs.addInPort(port_name)
        port_name = Ports.addInputPort("button_ui", self)
        atomdevs.addInPort(port_name)
        atomdevs.port_mappings[port_name] = atomdevs.next_instance
        self.inports["button_ui"] = port_name
    
    def user_defined_constructor(self, window_id, event_name, button_text):
        self.window_id = window_id;
        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 /creating_button
        self.states["/creating_button"] = State(1, "/creating_button", self)
        self.states["/creating_button"].setEnter(self._creating_button_enter)
        
        # state /running
        self.states["/running"] = State(2, "/running", self)
        
        # add children
        self.states[""].addChild(self.states["/creating_button"])
        self.states[""].addChild(self.states["/running"])
        self.states[""].fixTree()
        self.states[""].default_state = self.states["/creating_button"]
        
        # transition /creating_button
        _creating_button_0 = Transition(self, self.states["/creating_button"], [self.states["/running"]])
        _creating_button_0.setAction(self._creating_button_0_exec)
        _creating_button_0.setTrigger(Event("button_created", None))
        self.states["/creating_button"].addTransition(_creating_button_0)
        
        # transition /running
        _running_0 = Transition(self, self.states["/running"], [self.states["/running"]])
        _running_0.setAction(self._running_0_exec)
        _running_0.setTrigger(Event("mouse_click", self.getInPortName("button_ui")))
        _running_0.setGuard(self._running_0_guard)
        self.states["/running"].addTransition(_running_0)
    
    def _creating_button_enter(self):
        self.big_step.outputEvent(Event("create_button", self.getOutPortName("ui"), [self.window_id, self.event_name, self.inports['button_ui']]))
    
    def _creating_button_0_exec(self, parameters):
        button_id = parameters[0]
        self.button_id = button_id
        self.big_step.outputEvent(Event("bind_event", self.getOutPortName("ui"), [button_id, ui.EVENTS.MOUSE_CLICK, "mouse_click", self.inports['button_ui']]))
    
    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
        self.default_targets = self.states["/creating_button"].getEffectiveTargetStates()
        RuntimeClassBase.initializeStatechart(self)

class Button(ObjectManagerBase):
    def __init__(self, name):
        ObjectManagerBase.__init__(self, name)
        self.input = self.addInPort("input")
        self.output = self.addOutPort("ui")
        self.outputs["parent"] = self.addOutPort("parent")
        self.button_ui = self.addInPort("button_ui")
    
    def constructObject(self, parameters):
        new_instance = ButtonInstance(self, parameters[2], parameters[3], parameters[4])
        return new_instance

class BallInstance(RuntimeClassBase):
    def __init__(self, atomdevs, canvas_id, x, y):
        RuntimeClassBase.__init__(self, atomdevs)
        self.associations = {}
        self.associations["parent"] = Association("Field", 1, 1)
        
        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()
        
        # user defined attributes
        self.canvas_id = None
        self.pos = None
        
        # call user defined constructor
        BallInstance.user_defined_constructor(self, canvas_id, x, y)
        port_name = Ports.addInputPort("<narrow_cast>", self)
        atomdevs.addInPort(port_name)
        port_name = Ports.addInputPort("ball_ui", self)
        atomdevs.addInPort(port_name)
        atomdevs.port_mappings[port_name] = atomdevs.next_instance
        self.inports["ball_ui"] = port_name
    
    def user_defined_constructor(self, canvas_id, x, y):
        self.canvas_id = canvas_id;
        self.r = 20.0;
        self.vel = {'x': random.uniform(-5.0, 5.0), 'y': random.uniform(-5.0, 5.0)};
        self.pos = {'x': x, 'y': y};
        self.smooth = 0.6; # 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_behaviour
        self.states["/main_behaviour"] = State(1, "/main_behaviour", self)
        
        # state /main_behaviour/initializing
        self.states["/main_behaviour/initializing"] = State(2, "/main_behaviour/initializing", self)
        
        # state /main_behaviour/creating_circle
        self.states["/main_behaviour/creating_circle"] = State(3, "/main_behaviour/creating_circle", self)
        self.states["/main_behaviour/creating_circle"].setEnter(self._main_behaviour_creating_circle_enter)
        
        # state /main_behaviour/bouncing
        self.states["/main_behaviour/bouncing"] = State(4, "/main_behaviour/bouncing", 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(5, "/main_behaviour/dragging", self)
        
        # state /main_behaviour/selected
        self.states["/main_behaviour/selected"] = State(6, "/main_behaviour/selected", self)
        
        # state /deleted
        self.states["/deleted"] = State(7, "/deleted", 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/creating_circle"])
        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/creating_circle"]])
        _main_behaviour_initializing_0.setAction(self._main_behaviour_initializing_0_exec)
        _main_behaviour_initializing_0.setTrigger(Event("set_association_name", None))
        self.states["/main_behaviour/initializing"].addTransition(_main_behaviour_initializing_0)
        
        # transition /main_behaviour/creating_circle
        _main_behaviour_creating_circle_0 = Transition(self, self.states["/main_behaviour/creating_circle"], [self.states["/main_behaviour/bouncing"]])
        _main_behaviour_creating_circle_0.setAction(self._main_behaviour_creating_circle_0_exec)
        _main_behaviour_creating_circle_0.setTrigger(Event("circle_created", None))
        self.states["/main_behaviour/creating_circle"].addTransition(_main_behaviour_creating_circle_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.setTrigger(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.setTrigger(Event("mouse_press", self.getInPortName("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.setTrigger(Event("mouse_move", self.getInPortName("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.setTrigger(Event("mouse_release", self.getInPortName("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.setTrigger(Event("mouse_press", self.getInPortName("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.setTrigger(Event("delete_self", None))
        self.states["/main_behaviour/selected"].addTransition(_main_behaviour_selected_1)
    
    def _main_behaviour_creating_circle_enter(self):
        self.big_step.outputEvent(Event("create_circle", self.getOutPortName("ui"), [self.canvas_id, self.pos['x'], self.pos['y'], self.r, {'fill':'#000'}, self.inports['ball_ui']]))
    
    def _main_behaviour_bouncing_enter(self):
        self.addTimer(0, 0.02)
    
    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_creating_circle_0_exec(self, parameters):
        canvas_id = parameters[0]
        circle_id = parameters[1]
        self.circle_id = circle_id
        self.big_step.outputEvent(Event("bind_canvas_event", self.getOutPortName("ui"), [self.canvas_id, circle_id, ui.EVENTS.MOUSE_PRESS, 'mouse_press', self.inports['ball_ui']]))
        self.big_step.outputEvent(Event("bind_canvas_event", self.getOutPortName("ui"), [self.canvas_id, circle_id, ui.EVENTS.MOUSE_MOVE, 'mouse_move', self.inports['ball_ui']]))
        self.big_step.outputEvent(Event("bind_canvas_event", self.getOutPortName("ui"), [self.canvas_id, circle_id, ui.EVENTS.MOUSE_RELEASE, 'mouse_release', self.inports['ball_ui']]))
    
    def _main_behaviour_bouncing_0_exec(self, parameters):
        # Invert velocity when colliding with canvas border:
        if self.pos['x']-self.r <= 0 or self.pos['x']+self.r >= CANVAS_WIDTH:
            self.vel['x'] = -self.vel['x'];
        if self.pos['y']-self.r <= 0 or self.pos['y']+self.r >= CANVAS_HEIGHT:
            self.vel['y'] = -self.vel['y'];
        self.big_step.outputEvent(Event("move_element", self.getOutPortName("ui"), [self.canvas_id, self.circle_id, self.vel['x'], self.vel['y']]))
        self.pos['x'] += self.vel['x']
        self.pos['y'] += self.vel['y']
    
    def _main_behaviour_bouncing_1_exec(self, parameters):
        x = parameters[0]
        y = parameters[1]
        button = parameters[2]
        self.big_step.outputEvent(Event("set_element_color", self.getOutPortName("ui"), [self.canvas_id, self.circle_id, '#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]
        # Always keep ball within canvas:
        x = min(max(0+self.r, x), CANVAS_WIDTH-self.r)
        y = min(max(0+self.r, y), CANVAS_HEIGHT-self.r)
        
        dx = x - self.pos['x']
        dy = y - self.pos['y']
        
        self.vel = {
            'x': (1-self.smooth)*dx + self.smooth*self.vel['x'],
            'y': (1-self.smooth)*dy + self.smooth*self.vel['y']
        }
        
        self.pos = {'x': x, 'y': y}
        self.big_step.outputEvent(Event("set_element_pos", self.getOutPortName("ui"), [self.canvas_id, self.circle_id, x-self.r, y-self.r]))
    
    def _main_behaviour_dragging_1_exec(self, parameters):
        x = parameters[0]
        y = parameters[1]
        self.big_step.outputEvent(Event("set_element_color", self.getOutPortName("ui"), [self.canvas_id, self.circle_id, '#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])]))
        self.big_step.outputEvent(Event("destroy_element", self.getOutPortName("ui"), [self.canvas_id, self.element_id]))
    
    def initializeStatechart(self):
        # enter default state
        self.default_targets = self.states["/main_behaviour"].getEffectiveTargetStates()
        RuntimeClassBase.initializeStatechart(self)

class Ball(ObjectManagerBase):
    def __init__(self, name):
        ObjectManagerBase.__init__(self, name)
        self.input = self.addInPort("input")
        self.output = self.addOutPort("ui")
        self.outputs["parent"] = self.addOutPort("parent")
        self.ball_ui = self.addInPort("ball_ui")
    
    def constructObject(self, parameters):
        new_instance = BallInstance(self, parameters[2], parameters[3], parameters[4])
        return new_instance

class ObjectManagerState:
    def __init__(self):
        self.to_send = [("MainApp", "MainApp", Event("start_instance", None, ["MainApp[0]"], 0))]

class ObjectManager(TheObjectManager):
    def __init__(self, name):
        TheObjectManager.__init__(self, name)
        self.State = ObjectManagerState()
        self.input = self.addInPort("input")
        self.output["MainApp"] = self.addOutPort()
        self.output["Field"] = self.addOutPort()
        self.output["Button"] = self.addOutPort()
        self.output["Ball"] = self.addOutPort()

class Controller(CoupledDEVS):
    def __init__(self, name):
        self.terminate = False

        CoupledDEVS.__init__(self, name)
        self.in_ui = self.addInPort("ui")
        Ports.addInputPort("ui")
        self.out_ui = self.addOutPort("ui")
        Ports.addOutputPort("ui")
        self.objectmanager = self.addSubModel(ObjectManager("ObjectManager"))
        self.atomic0 = self.addSubModel(MainApp("MainApp"))
        self.atomic1 = self.addSubModel(Field("Field"))
        self.atomic2 = self.addSubModel(Button("Button"))
        self.atomic3 = self.addSubModel(Ball("Ball"))
        self.connectPorts(self.atomic0.obj_manager_out, self.objectmanager.input)
        self.connectPorts(self.objectmanager.output["MainApp"], self.atomic0.obj_manager_in)
        self.connectPorts(self.atomic0.outputs["fields"], self.atomic1.input)
        self.connectPorts(self.atomic1.obj_manager_out, self.objectmanager.input)
        self.connectPorts(self.objectmanager.output["Field"], self.atomic1.obj_manager_in)
        self.connectPorts(self.atomic1.outputs["balls"], self.atomic3.input)
        self.connectPorts(self.atomic1.outputs["buttons"], self.atomic2.input)
        self.connectPorts(self.atomic1.outputs["parent"], self.atomic0.input)
        self.connectPorts(self.atomic2.obj_manager_out, self.objectmanager.input)
        self.connectPorts(self.objectmanager.output["Button"], self.atomic2.obj_manager_in)
        self.connectPorts(self.atomic2.outputs["parent"], self.atomic1.input)
        self.connectPorts(self.atomic3.obj_manager_out, self.objectmanager.input)
        self.connectPorts(self.objectmanager.output["Ball"], self.atomic3.obj_manager_in)
        self.connectPorts(self.atomic3.outputs["parent"], self.atomic1.input)
        self.connectPorts(self.atomic0.output, self.out_ui)
        self.connectPorts(self.atomic1.output, self.out_ui)
        self.connectPorts(self.atomic2.output, self.out_ui)
        self.connectPorts(self.atomic3.output, self.out_ui)