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

Date:   Wed Jun 14 14:49:34 2017

Model name:   multiple-afters-running-behind

"""

from sccd.runtime.statecharts_core import *

# package "multiple-afters-running-behind"

class Main(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
        Main.user_defined_constructor(self)
    
    def user_defined_constructor(self):
        pass
    
    def user_defined_destructor(self):
        pass
    
    
    # user defined method
    def cond(self):
        return True
    
    
    # builds Statechart structure
    def build_statechart_structure(self):
        
        # state <root>
        self.states[""] = State(0, self)
        
        # state /x
        self.states["/x"] = State(1, self)
        self.states["/x"].setEnter(self._x_enter)
        self.states["/x"].setExit(self._x_exit)
        
        # add children
        self.states[""].addChild(self.states["/x"])
        self.states[""].fixTree()
        self.states[""].default_state = self.states["/x"]
        
        # transition /x
        _x_0 = Transition(self, self.states["/x"], [self.states["/x"]])
        _x_0.setTrigger(Event("_0after"))
        _x_0.setGuard(self._x_0_guard)
        self.states["/x"].addTransition(_x_0)
        _x_1 = Transition(self, self.states["/x"], [self.states["/x"]])
        _x_1.setTrigger(Event("_1after"))
        _x_1.setGuard(self._x_1_guard)
        self.states["/x"].addTransition(_x_1)
        _x_2 = Transition(self, self.states["/x"], [self.states["/x"]])
        _x_2.setTrigger(Event("_2after"))
        _x_2.setGuard(self._x_2_guard)
        self.states["/x"].addTransition(_x_2)
    
    def _x_enter(self):
        import time
        print 'time.time() = %s' % time.time()
        time.sleep(0.1)
        print self.sccd_yield()
        self.addTimer(0, self.sccd_yield() + 0.5)
        self.addTimer(1, self.sccd_yield() + 0.5)
        self.addTimer(2, self.sccd_yield() + 0.5)
    
    def _x_exit(self):
        self.removeTimer(0)
        self.removeTimer(1)
        self.removeTimer(2)
    
    def _x_0_guard(self, parameters):
        return self.cond()
    
    def _x_1_guard(self, parameters):
        return self.cond()
    
    def _x_2_guard(self, parameters):
        return self.cond()
    
    def initializeStatechart(self):
        # enter default state
        self.default_targets = self.states["/x"].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 == "Main":
            instance = Main(self.controller)
            instance.associations = {}
        else:
            raise Exception("Cannot instantiate class " + class_name)
        return instance

class Controller(ThreadsControllerBase):
    def __init__(self, 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.object_manager.createInstance("Main", [])