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

Date:   Mon Aug 08 09:49:26 2016

Model author: Herr Joeri Exelmans
Model name:   source_parent
Model description:
'Source Parent' priority-semantics: If 2 transitions are enabled, and the source state of the first is an ancestor of the second, only the first is executed.
"""

from sccd.runtime.statecharts_core import *

# package "source_parent"

class c(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
        c.user_defined_constructor(self)
    
    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 /parent
        self.states["/parent"] = State(1, self)
        self.states["/parent"].setEnter(self._parent_enter)
        
        # state /parent/a
        self.states["/parent/a"] = State(2, self)
        self.states["/parent/a"].setEnter(self._parent_a_enter)
        
        # state /parent/b
        self.states["/parent/b"] = State(3, self)
        self.states["/parent/b"].setEnter(self._parent_b_enter)
        
        # state /c
        self.states["/c"] = State(4, self)
        self.states["/c"].setEnter(self._c_enter)
        
        # add children
        self.states[""].addChild(self.states["/parent"])
        self.states[""].addChild(self.states["/c"])
        self.states["/parent"].addChild(self.states["/parent/a"])
        self.states["/parent"].addChild(self.states["/parent/b"])
        self.states[""].fixTree()
        self.states[""].default_state = self.states["/parent"]
        self.states["/parent"].default_state = self.states["/parent/a"]
        
        # transition /parent/a
        _parent_a_0 = Transition(self, self.states["/parent/a"], [self.states["/parent/b"]])
        _parent_a_0.setTrigger(None)
        self.states["/parent/a"].addTransition(_parent_a_0)
        
        # transition /parent
        _parent_0 = Transition(self, self.states["/parent"], [self.states["/c"]])
        _parent_0.setTrigger(None)
        self.states["/parent"].addTransition(_parent_0)
    
    def _parent_enter(self):
        self.big_step.outputEvent(Event("entered_parent", "out", []))
    
    def _parent_a_enter(self):
        self.big_step.outputEvent(Event("entered_a", "out", []))
    
    def _parent_b_enter(self):
        self.big_step.outputEvent(Event("entered_b", "out", []))
    
    def _c_enter(self):
        self.big_step.outputEvent(Event("entered_c", "out", []))
    
    def initializeStatechart(self):
        # enter default state
        states = self.states["/parent"].getEffectiveTargetStates()
        self.updateConfiguration(states)
        for state in states:
            if state.enter:
                state.enter()

class ObjectManager(ObjectManagerBase):
    def __init__(self, controller):
        ObjectManagerBase.__init__(self, controller)
    
    def instantiate(self, class_name, construct_params):
        if class_name == "c":
            instance = c(self.controller)
            instance.associations = {}
        else:
            raise Exception("Cannot instantiate class " + class_name)
        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("in")
        self.addOutputPort("out")
        self.object_manager.createInstance("c", [])

class InputEvent:
    def __init__(self, name, port, parameters, time_offset):
        self.name = name
        self.port = port
        self.parameters = parameters
        self.time_offset = time_offset

class Test:
    def __init__(self):
        pass
    input_events = []
    expected_events = [[Event("entered_parent", "out", []), Event("entered_a", "out", [])], [Event("entered_c", "out", [])]]