"""
Generated by Statechart compiler by Glenn De Jonghe and Joeri Exelmans

Date:   Wed Jul 20 12:52:08 2016

Model author: Simon Van Mierlo
Model name:   Timer (Threaded Version)

"""

from python_runtime.statecharts_core import *
from python_runtime.ordered_set import OrderedSet
from python_runtime.libs.ui import ui
from python_runtime.accurate_time import time

# package "Timer (Threaded 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.SourceChild
        self.semantics.concurrency = StatechartSemantics.Single
        
        # state /running
        self.states["/running"] = ParallelState(0)
        
        # state /running/print_simulated_time
        self.states["/running/print_simulated_time"] = State(1)
        self.states["/running"].children.add(self.states["/running/print_simulated_time"])
        
        # state /running/print_wct_time
        self.states["/running/print_wct_time"] = State(2)
        self.states["/running"].children.add(self.states["/running/print_wct_time"])
        
        # state /running/print_simulated_time/print_simulated_time
        self.states["/running/print_simulated_time/print_simulated_time"] = State(3)
        self.states["/running/print_simulated_time"].children.add(self.states["/running/print_simulated_time/print_simulated_time"])
        
        def running_print_simulated_time_print_simulated_time_enter(self):
            self.addTimer(0, 1)
        self.states["/running/print_simulated_time/print_simulated_time"].enter = running_print_simulated_time_print_simulated_time_enter
        def running_print_simulated_time_print_simulated_time_exit(self):
            self.removeTimer(0)
        self.states["/running/print_simulated_time/print_simulated_time"].exit = running_print_simulated_time_print_simulated_time_exit
        
        # state /running/print_wct_time/print_wct_time
        self.states["/running/print_wct_time/print_wct_time"] = State(4)
        self.states["/running/print_wct_time"].children.add(self.states["/running/print_wct_time/print_wct_time"])
        
        def running_print_wct_time_print_wct_time_enter(self):
            self.addTimer(1, 1)
        self.states["/running/print_wct_time/print_wct_time"].enter = running_print_wct_time_print_wct_time_enter
        def running_print_wct_time_print_wct_time_exit(self):
            self.removeTimer(1)
        self.states["/running/print_wct_time/print_wct_time"].exit = running_print_wct_time_print_wct_time_exit
       
        # state /interrupted
        self.states["/interrupted"] = State(5)
        
        # fix ancestors and descendants
        self.states["/running"].fixTree()
        self.states["/interrupted"].fixTree()
        
        # defaults
        self.states["/running/print_simulated_time"].default = self.states["/running/print_simulated_time/print_simulated_time"]
        self.states["/running/print_wct_time"].default = self.states["/running/print_wct_time/print_wct_time"]
        
        # transitions /running        
        running_0 = Transition(self, self.states["/running"], self.states["/interrupted"])
        def running_0_exec(self):
            self.print_simulated_time()
            self.print_wct_time()
        running_0.executable_content = running_0_exec
        running_0.trigger = Event("interrupt", "input")
        self.states["/running"].transitions.add(running_0)
        
        # transitions /running/print_simulated_time/print_simulated_time
        running_print_simulated_time_print_simulated_time_0 = Transition(self, self.states["/running/print_simulated_time/print_simulated_time"], self.states["/running/print_simulated_time/print_simulated_time"])
        def running_print_simulated_time_print_simulated_time_0_exec(self):
            self.print_simulated_time()
        running_print_simulated_time_print_simulated_time_0.executable_content = running_print_simulated_time_print_simulated_time_0_exec
        running_print_simulated_time_print_simulated_time_0.trigger = Event("_0after")
        self.states["/running/print_simulated_time/print_simulated_time"].transitions.add(running_print_simulated_time_print_simulated_time_0)
        
        running_print_simulated_time_print_simulated_time_1 = Transition(self, self.states["/running/print_simulated_time/print_simulated_time"], self.states["/running/print_simulated_time/print_simulated_time"])
        def running_print_simulated_time_print_simulated_time_1_exec(self):
            print 'going nowhere!'
        running_print_simulated_time_print_simulated_time_1.executable_content = running_print_simulated_time_print_simulated_time_1_exec
        running_print_simulated_time_print_simulated_time_1.trigger = Event("interrupt", "input")        
        self.states["/running/print_simulated_time/print_simulated_time"].transitions.add(running_print_simulated_time_print_simulated_time_1)
        
        # transitions /running/print_wct_time/print_wct_time
        running_print_wct_time_print_wct_time_0 = Transition(self, self.states["/running/print_wct_time/print_wct_time"], self.states["/running/print_wct_time/print_wct_time"])
        def running_print_wct_time_print_wct_time_0_exec(self):
            self.print_wct_time()
        running_print_wct_time_print_wct_time_0.executable_content = running_print_wct_time_print_wct_time_0_exec
        running_print_wct_time_print_wct_time_0.trigger = Event("_1after")
        self.states["/running/print_wct_time/print_wct_time"].transitions.add(running_print_wct_time_print_wct_time_0)
        
        # transitions /interrupted
        interrupted_0 = Transition(self, self.states["/interrupted"], self.states["/interrupted"])
        def interrupted_0_exec(self):
            self.print_simulated_time()
            self.print_wct_time()
        interrupted_0.executable_content = interrupted_0_exec
        interrupted_0.trigger = Event("interrupt", "input")
        self.states["/interrupted"].transitions.add(interrupted_0)
        
        interrupted_1 = Transition(self, self.states["/interrupted"], self.states["/running"])
        def interrupted_1_exec(self):
            self.print_simulated_time()
            self.print_wct_time()
        interrupted_1.executable_content = interrupted_1_exec
        interrupted_1.trigger = Event("continue", "input")
        self.states["/interrupted"].transitions.add(interrupted_1)
        
        # call user defined constructor
        MainApp.user_defined_constructor(self)
    
    def user_defined_constructor(self):
        pass
    
    def user_defined_destructor(self):
        pass
    
    # user defined method
    def print_simulated_time(self):
        print 'SIMTIME = %.3f' % get_simulated_time()
        
    def print_wct_time(self):
        print 'ACTTIME = %.3f' % time()
    
    def initializeStatechart(self):        
        # enter default state
        states = self.states["/running"].getEffectiveTargetStates()
        self.configuration.update(states)
        for state in states:
            if state.enter:
                state.enter(self)
    
    # generate transition candidates for current small step
    def generateCandidates(self):
        enabledEvents = self.getEnabledEvents()
        enabledTransitions = OrderedSet()
        for s in self.configuration:
            if not s in self.combo_step.changed:
                for t in s.transitions:
                    if t.isEnabled(enabledEvents):
                        enabledTransitions.add(t)
        return enabledTransitions

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 = {}
        return instance

class Controller(ThreadsControllerBase):
    def __init__(self, keep_running = None):
        if keep_running == None: keep_running = True
        ThreadsControllerBase.__init__(self, ObjectManager(self), keep_running)
        self.addInputPort("input")
        self.object_manager.createInstance("MainApp", [])