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

Date:   Wed Oct 04 16:05:54 2017

Model name:   sourcechildbug

"""

from sccd.runtime.statecharts_core import *

# package "sourcechildbug"

class A(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
        
        # build Statechart structure
        self.build_statechart_structure()
        
        # call user defined constructor
        A.user_defined_constructor(self)
    
    def user_defined_constructor(self):
        self.a = 5
    
    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/child1
        self.states["/running/child1"] = State(2, "/running/child1", self)
        
        # state /running/child2
        self.states["/running/child2"] = State(3, "/running/child2", self)
        
        # state /running/child3
        self.states["/running/child3"] = State(4, "/running/child3", self)
        
        # add children
        self.states[""].addChild(self.states["/running"])
        self.states["/running"].addChild(self.states["/running/child1"])
        self.states["/running"].addChild(self.states["/running/child2"])
        self.states["/running"].addChild(self.states["/running/child3"])
        self.states[""].fixTree()
        self.states[""].default_state = self.states["/running"]
        self.states["/running"].default_state = self.states["/running/child1"]
        
        # transition /running/child1
        _running_child1_0 = Transition(self, self.states["/running/child1"], [self.states["/running/child2"]])
        _running_child1_0.setAction(self._running_child1_0_exec)
        _running_child1_0.setTrigger(None)
        _running_child1_0.setGuard(self._running_child1_0_guard)
        self.states["/running/child1"].addTransition(_running_child1_0)
        _running_child1_1 = Transition(self, self.states["/running/child1"], [self.states["/running/child2"]])
        _running_child1_1.setAction(self._running_child1_1_exec)
        _running_child1_1.setTrigger(None)
        _running_child1_1.setGuard(self._running_child1_1_guard)
        self.states["/running/child1"].addTransition(_running_child1_1)
        _running_child1_2 = Transition(self, self.states["/running/child1"], [self.states["/running/child2"]])
        _running_child1_2.setAction(self._running_child1_2_exec)
        _running_child1_2.setTrigger(None)
        _running_child1_2.setGuard(self._running_child1_2_guard)
        self.states["/running/child1"].addTransition(_running_child1_2)
        
        # transition /running
        _running_0 = Transition(self, self.states["/running"], [self.states["/running/child3"]])
        _running_0.setAction(self._running_0_exec)
        _running_0.setTrigger(None)
        _running_0.setGuard(self._running_0_guard)
        self.states["/running"].addTransition(_running_0)
    
    def _running_0_exec(self, parameters):
        print('taking outer transition')
        self.a = -1
    
    def _running_0_guard(self, parameters):
        return self.a == 5
    
    def _running_child1_0_exec(self, parameters):
        print('taking second inner transition')
        self.a = -1
    
    def _running_child1_0_guard(self, parameters):
        return self.a == 5
    
    def _running_child1_1_exec(self, parameters):
        print('taking third inner transition')
        self.a = -1
    
    def _running_child1_1_guard(self, parameters):
        return self.a == 3
    
    def _running_child1_2_exec(self, parameters):
        print('taking fourth inner transition')
        self.a = -1
    
    def _running_child1_2_guard(self, parameters):
        return self.a == 1
    
    def initializeStatechart(self):
        # enter default state
        self.default_targets = self.states["/running"].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 == "A":
            instance = A(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.addInputPort("input")
        self.object_manager.createInstance("A", [])