"""
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:   NetLogo debugging-enhanced simulator
Model description:
A NetLogo simulator (enhanced with debugging support).
"""

from sccd.runtime.statecharts_core import *
import pyNetLogo
import pprint, time

class Breakpoint(object):
    def __init__(self, breakpoint_id, function, enabled, disable_on_trigger):
        self.id = breakpoint_id
        self.function = function
        self.enabled = enabled
        self.disable_on_trigger = disable_on_trigger

# package "NetLogo debugging-enhanced simulator"

class NetLogoSimulator(RuntimeClassBase):
    def __init__(self, controller, model):
        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
        NetLogoSimulator.user_defined_constructor(self, model)
    
    def user_defined_constructor(self, model):
        self.link = pyNetLogo.NetLogoLink(gui=True,
                     netlogo_home="C:\\Program Files\\NetLogo 6.0.3",
                     netlogo_version="6")
        self.link.load_model(model)
        self.attr_names = self.link.report('last last reflection:breeds')
        self.link.command("setup")
        self.ignored_attributes = set(["WHO", "HEADING", "SHAPE", "LABEL", "LABEL-COLOR", "BREED", "HIDDEN?", "PEN-SIZE", "PEN-MODE"])
        self.breakpoints = []
        self.realtime_scale = 1.0
        self.simulation_time = 0
        self.time_next = 1
        self.turtles = {}
        
        # Values to be set during simulation
        self.realtime_starttime = None
    
    def user_defined_destructor(self):
        pass
    
    
    # user defined method
    def turtles_created(self, new_turtles):
        return set(new_turtles.keys()) - set(self.turtles.keys())
    
    
    # user defined method
    def turtles_deleted(self, new_turtles):
        return set(self.turtles.keys()) - set(new_turtles.keys())
    
    
    # user defined method
    def get_all_turtles(self):
        try:
            all_turtle_attributes = self.link.report("reflection:turtles")
        except:
            all_turtle_attributes = {}
        turtles = {}
        for turtle_attributes in all_turtle_attributes:
            the_turtle = dict(zip(self.attr_names, turtle_attributes.getResultAsObject()))
            turtles[int(float(the_turtle["WHO"]))] = the_turtle
        turtles = {k: {ak: av for ak, av in turtles[k].iteritems() if ak not in self.ignored_attributes} for k in turtles.keys()}
        return turtles
    
    
    # user defined method
    def breakpoint_triggers(self):
        breakpoint_id = -1
        for bp in self.breakpoints:
            if not bp.enabled:
                continue
            # Include the function in the scope
            exec(bp.function)
            # And execute it, note that the breakpoint thus has to start with "def breakpoint("
            if breakpoint(self.link.report("ticks"), self.turtles):
                # Triggered!
                breakpoint_id = bp.id
            else:
                # Not triggered, so continue
                continue
        return breakpoint_id
    
    
    # user defined method
    def process_breakpoints(self):
        breakpoint_id = self.breakpoint_triggers()
        if (breakpoint_id != -1):
            for breakpoint in self.breakpoints:
                if breakpoint.id == breakpoint_id:
                    if breakpoint.disable_on_trigger:
                        breakpoint.enabled = False
        return breakpoint_id
    
    
    # user defined method
    def calculate_after(self):
        try:
            # Process in parts of 100 milliseconds to repeatedly check the termination condition
            nexttime = (self.time_next - (time.time() - self.realtime_starttime) / self.realtime_scale) * self.realtime_scale
            x =  min(0.1, nexttime)
            return x
        except (TypeError, AttributeError) as e:
            # We are probably not simulating in realtime...
            return float('inf')
    
    
    # user defined method
    def parse_options(self, configuration):
        self.realtime_scale = 1.0 if "realtime_scale" not in configuration else 1.0/configuration["realtime_scale"]
        # Subtract the current simulation time to allow for pausing
        self.realtime_starttime = (time.time() - self.simulation_time * self.realtime_scale)
        # Reset the time used in the waiting, as it might not get recomputed
        self.the_time = 0.00001
    
    
    # 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/simulation_flow
        self.states["/main/simulation_flow"] = State(2, "/main/simulation_flow", self)
        
        # state /main/simulation_flow/initialize
        self.states["/main/simulation_flow/initialize"] = State(3, "/main/simulation_flow/initialize", self)
        
        # state /main/simulation_flow/check_termination
        self.states["/main/simulation_flow/check_termination"] = State(4, "/main/simulation_flow/check_termination", self)
        self.states["/main/simulation_flow/check_termination"].setExit(self._main_simulation_flow_check_termination_exit)
        
        # state /main/simulation_flow/check_termination/workaround
        self.states["/main/simulation_flow/check_termination/workaround"] = State(5, "/main/simulation_flow/check_termination/workaround", self)
        self.states["/main/simulation_flow/check_termination/workaround"].setEnter(self._main_simulation_flow_check_termination_workaround_enter)
        self.states["/main/simulation_flow/check_termination/workaround"].setExit(self._main_simulation_flow_check_termination_workaround_exit)
        
        # state /main/simulation_flow/check_termination/wait
        self.states["/main/simulation_flow/check_termination/wait"] = State(6, "/main/simulation_flow/check_termination/wait", self)
        self.states["/main/simulation_flow/check_termination/wait"].setEnter(self._main_simulation_flow_check_termination_wait_enter)
        self.states["/main/simulation_flow/check_termination/wait"].setExit(self._main_simulation_flow_check_termination_wait_exit)
        
        # state /main/simulation_flow/check_termination/check_termination
        self.states["/main/simulation_flow/check_termination/check_termination"] = State(7, "/main/simulation_flow/check_termination/check_termination", self)
        self.states["/main/simulation_flow/check_termination/check_termination"].setEnter(self._main_simulation_flow_check_termination_check_termination_enter)
        
        # state /main/simulation_flow/execute_step
        self.states["/main/simulation_flow/execute_step"] = State(8, "/main/simulation_flow/execute_step", self)
        self.states["/main/simulation_flow/execute_step"].setEnter(self._main_simulation_flow_execute_step_enter)
        
        # state /main/simulation_flow/execute_step/executing_step
        self.states["/main/simulation_flow/execute_step/executing_step"] = State(9, "/main/simulation_flow/execute_step/executing_step", self)
        
        # state /main/simulation_state
        self.states["/main/simulation_state"] = State(10, "/main/simulation_state", self)
        
        # state /main/simulation_state/paused
        self.states["/main/simulation_state/paused"] = State(11, "/main/simulation_state/paused", self)
        
        # state /main/simulation_state/continuous
        self.states["/main/simulation_state/continuous"] = State(12, "/main/simulation_state/continuous", self)
        
        # state /main/simulation_state/realtime
        self.states["/main/simulation_state/realtime"] = State(13, "/main/simulation_state/realtime", self)
        
        # state /main/reset_manager
        self.states["/main/reset_manager"] = State(14, "/main/reset_manager", self)
        
        # state /main/reset_manager/listening
        self.states["/main/reset_manager/listening"] = State(15, "/main/reset_manager/listening", self)
        
        # state /main/breakpoint_manager
        self.states["/main/breakpoint_manager"] = State(16, "/main/breakpoint_manager", self)
        
        # state /main/breakpoint_manager/breakpoint_manage
        self.states["/main/breakpoint_manager/breakpoint_manage"] = State(17, "/main/breakpoint_manager/breakpoint_manage", self)
        
        # add children
        self.states[""].addChild(self.states["/main"])
        self.states["/main"].addChild(self.states["/main/simulation_flow"])
        self.states["/main"].addChild(self.states["/main/simulation_state"])
        self.states["/main"].addChild(self.states["/main/reset_manager"])
        self.states["/main"].addChild(self.states["/main/breakpoint_manager"])
        self.states["/main/simulation_flow"].addChild(self.states["/main/simulation_flow/initialize"])
        self.states["/main/simulation_flow"].addChild(self.states["/main/simulation_flow/check_termination"])
        self.states["/main/simulation_flow"].addChild(self.states["/main/simulation_flow/execute_step"])
        self.states["/main/simulation_flow/check_termination"].addChild(self.states["/main/simulation_flow/check_termination/workaround"])
        self.states["/main/simulation_flow/check_termination"].addChild(self.states["/main/simulation_flow/check_termination/wait"])
        self.states["/main/simulation_flow/check_termination"].addChild(self.states["/main/simulation_flow/check_termination/check_termination"])
        self.states["/main/simulation_flow/execute_step"].addChild(self.states["/main/simulation_flow/execute_step/executing_step"])
        self.states["/main/simulation_state"].addChild(self.states["/main/simulation_state/paused"])
        self.states["/main/simulation_state"].addChild(self.states["/main/simulation_state/continuous"])
        self.states["/main/simulation_state"].addChild(self.states["/main/simulation_state/realtime"])
        self.states["/main/reset_manager"].addChild(self.states["/main/reset_manager/listening"])
        self.states["/main/breakpoint_manager"].addChild(self.states["/main/breakpoint_manager/breakpoint_manage"])
        self.states[""].fixTree()
        self.states[""].default_state = self.states["/main"]
        self.states["/main/simulation_flow"].default_state = self.states["/main/simulation_flow/initialize"]
        self.states["/main/simulation_flow/check_termination"].default_state = self.states["/main/simulation_flow/check_termination/workaround"]
        self.states["/main/simulation_flow/execute_step"].default_state = self.states["/main/simulation_flow/execute_step/executing_step"]
        self.states["/main/simulation_state"].default_state = self.states["/main/simulation_state/paused"]
        self.states["/main/reset_manager"].default_state = self.states["/main/reset_manager/listening"]
        self.states["/main/breakpoint_manager"].default_state = self.states["/main/breakpoint_manager/breakpoint_manage"]
        
        # transition /main/simulation_flow/initialize
        _main_simulation_flow_initialize_0 = Transition(self, self.states["/main/simulation_flow/initialize"], [self.states["/main/simulation_flow/check_termination"]])
        _main_simulation_flow_initialize_0.setAction(self._main_simulation_flow_initialize_0_exec)
        _main_simulation_flow_initialize_0.setTrigger(None)
        self.states["/main/simulation_flow/initialize"].addTransition(_main_simulation_flow_initialize_0)
        
        # transition /main/simulation_flow/check_termination/workaround
        _main_simulation_flow_check_termination_workaround_0 = Transition(self, self.states["/main/simulation_flow/check_termination/workaround"], [self.states["/main/simulation_flow/check_termination/check_termination"]])
        _main_simulation_flow_check_termination_workaround_0.setTrigger(Event("_0after"))
        self.states["/main/simulation_flow/check_termination/workaround"].addTransition(_main_simulation_flow_check_termination_workaround_0)
        
        # transition /main/simulation_flow/check_termination/wait
        _main_simulation_flow_check_termination_wait_0 = Transition(self, self.states["/main/simulation_flow/check_termination/wait"], [self.states["/main/simulation_flow/check_termination/check_termination"]])
        _main_simulation_flow_check_termination_wait_0.setTrigger(Event("_1after"))
        self.states["/main/simulation_flow/check_termination/wait"].addTransition(_main_simulation_flow_check_termination_wait_0)
        _main_simulation_flow_check_termination_wait_1 = Transition(self, self.states["/main/simulation_flow/check_termination/wait"], [self.states["/main/simulation_flow/check_termination/check_termination"]])
        _main_simulation_flow_check_termination_wait_1.setTrigger(None)
        _main_simulation_flow_check_termination_wait_1.setGuard(self._main_simulation_flow_check_termination_wait_1_guard)
        self.states["/main/simulation_flow/check_termination/wait"].addTransition(_main_simulation_flow_check_termination_wait_1)
        
        # transition /main/simulation_flow/check_termination/check_termination
        _main_simulation_flow_check_termination_check_termination_0 = Transition(self, self.states["/main/simulation_flow/check_termination/check_termination"], [self.states["/main/simulation_flow/check_termination/workaround"]])
        _main_simulation_flow_check_termination_check_termination_0.setAction(self._main_simulation_flow_check_termination_check_termination_0_exec)
        _main_simulation_flow_check_termination_check_termination_0.setTrigger(None)
        _main_simulation_flow_check_termination_check_termination_0.setGuard(self._main_simulation_flow_check_termination_check_termination_0_guard)
        self.states["/main/simulation_flow/check_termination/check_termination"].addTransition(_main_simulation_flow_check_termination_check_termination_0)
        _main_simulation_flow_check_termination_check_termination_1 = Transition(self, self.states["/main/simulation_flow/check_termination/check_termination"], [self.states["/main/simulation_flow/execute_step"]])
        _main_simulation_flow_check_termination_check_termination_1.setTrigger(None)
        _main_simulation_flow_check_termination_check_termination_1.setGuard(self._main_simulation_flow_check_termination_check_termination_1_guard)
        self.states["/main/simulation_flow/check_termination/check_termination"].addTransition(_main_simulation_flow_check_termination_check_termination_1)
        _main_simulation_flow_check_termination_check_termination_2 = Transition(self, self.states["/main/simulation_flow/check_termination/check_termination"], [self.states["/main/simulation_flow/execute_step"]])
        _main_simulation_flow_check_termination_check_termination_2.setTrigger(Event("step", "request"))
        self.states["/main/simulation_flow/check_termination/check_termination"].addTransition(_main_simulation_flow_check_termination_check_termination_2)
        _main_simulation_flow_check_termination_check_termination_3 = Transition(self, self.states["/main/simulation_flow/check_termination/check_termination"], [self.states["/main/simulation_flow/check_termination/wait"]])
        _main_simulation_flow_check_termination_check_termination_3.setTrigger(None)
        _main_simulation_flow_check_termination_check_termination_3.setGuard(self._main_simulation_flow_check_termination_check_termination_3_guard)
        self.states["/main/simulation_flow/check_termination/check_termination"].addTransition(_main_simulation_flow_check_termination_check_termination_3)
        _main_simulation_flow_check_termination_check_termination_4 = Transition(self, self.states["/main/simulation_flow/check_termination/check_termination"], [self.states["/main/simulation_flow/execute_step"]])
        _main_simulation_flow_check_termination_check_termination_4.setTrigger(None)
        _main_simulation_flow_check_termination_check_termination_4.setGuard(self._main_simulation_flow_check_termination_check_termination_4_guard)
        self.states["/main/simulation_flow/check_termination/check_termination"].addTransition(_main_simulation_flow_check_termination_check_termination_4)
        _main_simulation_flow_check_termination_check_termination_5 = Transition(self, self.states["/main/simulation_flow/check_termination/check_termination"], [self.states["/main/simulation_flow/check_termination/workaround"]])
        _main_simulation_flow_check_termination_check_termination_5.setAction(self._main_simulation_flow_check_termination_check_termination_5_exec)
        _main_simulation_flow_check_termination_check_termination_5.setTrigger(Event("god_event", "request"))
        _main_simulation_flow_check_termination_check_termination_5.setGuard(self._main_simulation_flow_check_termination_check_termination_5_guard)
        self.states["/main/simulation_flow/check_termination/check_termination"].addTransition(_main_simulation_flow_check_termination_check_termination_5)
        
        # transition /main/simulation_flow/execute_step/executing_step
        _main_simulation_flow_execute_step_executing_step_0 = Transition(self, self.states["/main/simulation_flow/execute_step/executing_step"], [self.states["/main/simulation_flow/check_termination"]])
        _main_simulation_flow_execute_step_executing_step_0.setAction(self._main_simulation_flow_execute_step_executing_step_0_exec)
        _main_simulation_flow_execute_step_executing_step_0.setTrigger(None)
        _main_simulation_flow_execute_step_executing_step_0.setGuard(self._main_simulation_flow_execute_step_executing_step_0_guard)
        self.states["/main/simulation_flow/execute_step/executing_step"].addTransition(_main_simulation_flow_execute_step_executing_step_0)
        _main_simulation_flow_execute_step_executing_step_1 = Transition(self, self.states["/main/simulation_flow/execute_step/executing_step"], [self.states["/main/simulation_flow/check_termination"]])
        _main_simulation_flow_execute_step_executing_step_1.setAction(self._main_simulation_flow_execute_step_executing_step_1_exec)
        _main_simulation_flow_execute_step_executing_step_1.setTrigger(None)
        _main_simulation_flow_execute_step_executing_step_1.setGuard(self._main_simulation_flow_execute_step_executing_step_1_guard)
        self.states["/main/simulation_flow/execute_step/executing_step"].addTransition(_main_simulation_flow_execute_step_executing_step_1)
        
        # transition /main/simulation_state/paused
        _main_simulation_state_paused_0 = Transition(self, self.states["/main/simulation_state/paused"], [self.states["/main/simulation_state/continuous"]])
        _main_simulation_state_paused_0.setTrigger(Event("continuous", "request"))
        self.states["/main/simulation_state/paused"].addTransition(_main_simulation_state_paused_0)
        _main_simulation_state_paused_1 = Transition(self, self.states["/main/simulation_state/paused"], [self.states["/main/simulation_state/realtime"]])
        _main_simulation_state_paused_1.setAction(self._main_simulation_state_paused_1_exec)
        _main_simulation_state_paused_1.setTrigger(Event("realtime", "request"))
        self.states["/main/simulation_state/paused"].addTransition(_main_simulation_state_paused_1)
        
        # transition /main/simulation_state/continuous
        _main_simulation_state_continuous_0 = Transition(self, self.states["/main/simulation_state/continuous"], [self.states["/main/simulation_state/paused"]])
        _main_simulation_state_continuous_0.setAction(self._main_simulation_state_continuous_0_exec)
        _main_simulation_state_continuous_0.setTrigger(Event("pause", None))
        self.states["/main/simulation_state/continuous"].addTransition(_main_simulation_state_continuous_0)
        _main_simulation_state_continuous_1 = Transition(self, self.states["/main/simulation_state/continuous"], [self.states["/main/simulation_state/paused"]])
        _main_simulation_state_continuous_1.setAction(self._main_simulation_state_continuous_1_exec)
        _main_simulation_state_continuous_1.setTrigger(Event("termination_condition", None))
        self.states["/main/simulation_state/continuous"].addTransition(_main_simulation_state_continuous_1)
        
        # transition /main/simulation_state/realtime
        _main_simulation_state_realtime_0 = Transition(self, self.states["/main/simulation_state/realtime"], [self.states["/main/simulation_state/paused"]])
        _main_simulation_state_realtime_0.setAction(self._main_simulation_state_realtime_0_exec)
        _main_simulation_state_realtime_0.setTrigger(Event("pause", None))
        self.states["/main/simulation_state/realtime"].addTransition(_main_simulation_state_realtime_0)
        _main_simulation_state_realtime_1 = Transition(self, self.states["/main/simulation_state/realtime"], [self.states["/main/simulation_state/paused"]])
        _main_simulation_state_realtime_1.setAction(self._main_simulation_state_realtime_1_exec)
        _main_simulation_state_realtime_1.setTrigger(Event("termination_condition", None))
        self.states["/main/simulation_state/realtime"].addTransition(_main_simulation_state_realtime_1)
        
        # transition /main/reset_manager/listening
        _main_reset_manager_listening_0 = Transition(self, self.states["/main/reset_manager/listening"], [self.states["/main/reset_manager/listening"]])
        _main_reset_manager_listening_0.setAction(self._main_reset_manager_listening_0_exec)
        _main_reset_manager_listening_0.setTrigger(Event("reset", "request"))
        _main_reset_manager_listening_0.setGuard(self._main_reset_manager_listening_0_guard)
        self.states["/main/reset_manager/listening"].addTransition(_main_reset_manager_listening_0)
        
        # transition /main/breakpoint_manager/breakpoint_manage
        _main_breakpoint_manager_breakpoint_manage_0 = Transition(self, self.states["/main/breakpoint_manager/breakpoint_manage"], [self.states["/main/breakpoint_manager/breakpoint_manage"]])
        _main_breakpoint_manager_breakpoint_manage_0.setAction(self._main_breakpoint_manager_breakpoint_manage_0_exec)
        _main_breakpoint_manager_breakpoint_manage_0.setTrigger(Event("add_breakpoint", "request"))
        self.states["/main/breakpoint_manager/breakpoint_manage"].addTransition(_main_breakpoint_manager_breakpoint_manage_0)
        _main_breakpoint_manager_breakpoint_manage_1 = Transition(self, self.states["/main/breakpoint_manager/breakpoint_manage"], [self.states["/main/breakpoint_manager/breakpoint_manage"]])
        _main_breakpoint_manager_breakpoint_manage_1.setAction(self._main_breakpoint_manager_breakpoint_manage_1_exec)
        _main_breakpoint_manager_breakpoint_manage_1.setTrigger(Event("del_breakpoint", "request"))
        self.states["/main/breakpoint_manager/breakpoint_manage"].addTransition(_main_breakpoint_manager_breakpoint_manage_1)
        _main_breakpoint_manager_breakpoint_manage_2 = Transition(self, self.states["/main/breakpoint_manager/breakpoint_manage"], [self.states["/main/breakpoint_manager/breakpoint_manage"]])
        _main_breakpoint_manager_breakpoint_manage_2.setAction(self._main_breakpoint_manager_breakpoint_manage_2_exec)
        _main_breakpoint_manager_breakpoint_manage_2.setTrigger(Event("toggle_breakpoint", "request"))
        self.states["/main/breakpoint_manager/breakpoint_manage"].addTransition(_main_breakpoint_manager_breakpoint_manage_2)
    
    def _main_simulation_flow_check_termination_exit(self):
        self.simulation_time = self.time_next
    
    def _main_simulation_flow_execute_step_enter(self):
        self.link.command("go")
        self.new_turtles = self.get_all_turtles()
    
    def _main_simulation_flow_check_termination_workaround_enter(self):
        self.addTimer(0, self.sccd_yield())
    
    def _main_simulation_flow_check_termination_workaround_exit(self):
        self.removeTimer(0)
    
    def _main_simulation_flow_check_termination_wait_enter(self):
        self.addTimer(1, self.calculate_after())
    
    def _main_simulation_flow_check_termination_wait_exit(self):
        self.removeTimer(1)
        diff = time.time() - self.realtime_starttime
        self.simulation_time = diff / self.realtime_scale
    
    def _main_simulation_flow_check_termination_check_termination_enter(self):
        self.the_time = self.calculate_after()
    
    def _main_simulation_flow_initialize_0_exec(self, parameters):
        self.new_turtles = self.get_all_turtles()
        self.big_step.outputEvent(Event("all_states", "reply", [self.simulation_time, self.new_turtles, {'CREATED_TURTLES': self.turtles_created(self.new_turtles),                                                   'DELETED_TURTLES': self.turtles_deleted(self.new_turtles)}]))
        self.turtles = self.new_turtles
    
    def _main_simulation_flow_check_termination_wait_1_guard(self, parameters):
        return self.inState(["/main/simulation_state/paused"])
    
    def _main_simulation_flow_check_termination_check_termination_0_exec(self, parameters):
        breakpoint_id = self.process_breakpoints()
        self.big_step.outputEvent(Event("breakpoint_triggered", "reply", [breakpoint_id]))
        self.raiseInternalEvent(Event("termination_condition", None, []))
    
    def _main_simulation_flow_check_termination_check_termination_0_guard(self, parameters):
        return (not self.inState(["/main/simulation_state/paused"])) and (self.breakpoint_triggers() > -1)
    
    def _main_simulation_flow_check_termination_check_termination_1_guard(self, parameters):
        return self.inState(["/main/simulation_state/continuous"])
    
    def _main_simulation_flow_check_termination_check_termination_3_guard(self, parameters):
        return self.inState(["/main/simulation_state/realtime"]) and (self.the_time > 0.0)
    
    def _main_simulation_flow_check_termination_check_termination_4_guard(self, parameters):
        return self.inState(["/main/simulation_state/realtime"]) and (self.the_time <= 0.0)
    
    def _main_simulation_flow_check_termination_check_termination_5_exec(self, parameters):
        configuration = parameters[0]
        turtle_id = configuration["turtle"]
        state_attribute = configuration["attribute"]
        new_value = configuration["value"]
        askstring = "ask turtle %i [set %s %s]" % (int(turtle_id), state_attribute, new_value)
        self.link.command(askstring)
        self.new_turtles = self.get_all_turtles()
        self.big_step.outputEvent(Event("god_event_ok", "reply", []))
        self.big_step.outputEvent(Event("all_states", "reply", [self.simulation_time, self.new_turtles, {'CREATED_TURTLES': self.turtles_created(self.new_turtles),                                                       'DELETED_TURTLES': self.turtles_deleted(self.new_turtles)}]))
        self.turtles = self.new_turtles
    
    def _main_simulation_flow_check_termination_check_termination_5_guard(self, parameters):
        configuration = parameters[0]
        return self.inState(["/main/simulation_state/paused"])
    
    def _main_simulation_flow_execute_step_executing_step_0_exec(self, parameters):
        self.time_next = self.time_next + 1
    
    def _main_simulation_flow_execute_step_executing_step_0_guard(self, parameters):
        return self.inState(["/main/simulation_state/continuous"])
    
    def _main_simulation_flow_execute_step_executing_step_1_exec(self, parameters):
        self.big_step.outputEvent(Event("all_states", "reply", [self.simulation_time, self.new_turtles, {'CREATED_TURTLES': self.turtles_created(self.new_turtles),                                                       'DELETED_TURTLES': self.turtles_deleted(self.new_turtles)}]))
        self.time_next = self.time_next + 1
        self.turtles = self.new_turtles
    
    def _main_simulation_flow_execute_step_executing_step_1_guard(self, parameters):
        return "ELSE_GUARD"
    
    def _main_simulation_state_paused_1_exec(self, parameters):
        configuration = parameters[0]
        self.parse_options(configuration)
    
    def _main_simulation_state_continuous_0_exec(self, parameters):
        self.big_step.outputEvent(Event("all_states", "reply", [self.simulation_time, self.new_turtles, {'CREATED_TURTLES': self.turtles_created(self.new_turtles),                                                   'DELETED_TURTLES': self.turtles_deleted(self.new_turtles)}]))
        self.turtles = self.new_turtles
    
    def _main_simulation_state_continuous_1_exec(self, parameters):
        self.big_step.outputEvent(Event("terminate", "reply", [self.simulation_time]))
    
    def _main_simulation_state_realtime_0_exec(self, parameters):
        diff = time.time() - self.realtime_starttime
        self.simulation_time = diff / self.realtime_scale
        self.new_turtles = self.get_all_turtles()
        self.big_step.outputEvent(Event("all_states", "reply", [self.simulation_time, self.new_turtles, {'CREATED_TURTLES': self.turtles_created(self.new_turtles),                                                   'DELETED_TURTLES': self.turtles_deleted(self.new_turtles)}]))
        self.turtles = self.new_turtles
    
    def _main_simulation_state_realtime_1_exec(self, parameters):
        self.big_step.outputEvent(Event("terminate", "reply", [self.simulation_time]))
    
    def _main_reset_manager_listening_0_exec(self, parameters):
        self.link.command("setup")
        self.new_turtles = self.get_all_turtles()
        self.simulation_time = 0
        self.realtime_scale = 1.0
        self.realtime_starttime = None
        self.time_next = 1
        self.big_step.outputEvent(Event("all_states", "reply", [self.simulation_time, self.new_turtles, {'CREATED_TURTLES': self.turtles_created(self.new_turtles),                                                   'DELETED_TURTLES': self.turtles_deleted(self.new_turtles)}]))
        self.turtles = self.new_turtles
    
    def _main_reset_manager_listening_0_guard(self, parameters):
        return self.inState(["/main/simulation_state/paused"])
    
    def _main_breakpoint_manager_breakpoint_manage_0_exec(self, parameters):
        breakpoint_id = parameters[0]
        function = parameters[1]
        enabled = parameters[2]
        disable_on_trigger = parameters[3]
        self.breakpoints.append(Breakpoint(breakpoint_id, function, enabled, disable_on_trigger))
    
    def _main_breakpoint_manager_breakpoint_manage_1_exec(self, parameters):
        del_breakpoint_id = parameters[0]
        self.breakpoints = [breakpoint for breakpoint in self.breakpoints if breakpoint.id != del_breakpoint_id]
    
    def _main_breakpoint_manager_breakpoint_manage_2_exec(self, parameters):
        breakpoint_id = parameters[0]
        enabled = parameters[1]
        for breakpoint in self.breakpoints:
            if breakpoint.id == breakpoint_id:
                breakpoint.enabled = enabled
                break
    
    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 == "NetLogoSimulator":
            instance = NetLogoSimulator(self.controller, construct_params[0])
            instance.associations = {}
        else:
            raise Exception("Cannot instantiate class " + class_name)
        return instance

class Controller(ThreadsControllerBase):
    def __init__(self, model, keep_running = None, behind_schedule_callback = None):
        if keep_running == None: keep_running = True
        if behind_schedule_callback == None: behind_schedule_callback = None
        ThreadsControllerBase.__init__(self, ObjectManager(self), keep_running, behind_schedule_callback)
        self.addInputPort("request")
        self.addOutputPort("reply")
        self.object_manager.createInstance("NetLogoSimulator", [model])