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

Date:   Thu Aug 04 12:51:05 2016

Model author: Glenn De Jonghe
Model name:   TestEnterExitHierarchy
Model description:
Testing the enter and exit hierarchy.
"""

from sccd.runtime.statecharts_core import *

# package "TestEnterExitHierarchy"

class Class1(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
        Class1.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 /outside
        self.states["/outside"] = State(1, self)
        
        # state /composite
        self.states["/composite"] = State(2, self)
        
        # state /composite/state1
        self.states["/composite/state1"] = State(3, self)
        self.states["/composite/state1"].setEnter(self._composite_state1_enter)
        self.states["/composite/state1"].setExit(self._composite_state1_exit)
        
        # state /composite/state1/inner1
        self.states["/composite/state1/inner1"] = State(4, self)
        self.states["/composite/state1/inner1"].setEnter(self._composite_state1_inner1_enter)
        self.states["/composite/state1/inner1"].setExit(self._composite_state1_inner1_exit)
        
        # state /composite/state1/inner2
        self.states["/composite/state1/inner2"] = State(5, self)
        self.states["/composite/state1/inner2"].setEnter(self._composite_state1_inner2_enter)
        self.states["/composite/state1/inner2"].setExit(self._composite_state1_inner2_exit)
        
        # state /composite/state2
        self.states["/composite/state2"] = State(6, self)
        self.states["/composite/state2"].setEnter(self._composite_state2_enter)
        self.states["/composite/state2"].setExit(self._composite_state2_exit)
        
        # state /composite/state2/inner3
        self.states["/composite/state2/inner3"] = State(7, self)
        self.states["/composite/state2/inner3"].setEnter(self._composite_state2_inner3_enter)
        self.states["/composite/state2/inner3"].setExit(self._composite_state2_inner3_exit)
        
        # state /composite/state2/inner4
        self.states["/composite/state2/inner4"] = State(8, self)
        self.states["/composite/state2/inner4"].setEnter(self._composite_state2_inner4_enter)
        self.states["/composite/state2/inner4"].setExit(self._composite_state2_inner4_exit)
        
        # add children
        self.states[""].addChild(self.states["/outside"])
        self.states[""].addChild(self.states["/composite"])
        self.states["/composite"].addChild(self.states["/composite/state1"])
        self.states["/composite"].addChild(self.states["/composite/state2"])
        self.states["/composite/state1"].addChild(self.states["/composite/state1/inner1"])
        self.states["/composite/state1"].addChild(self.states["/composite/state1/inner2"])
        self.states["/composite/state2"].addChild(self.states["/composite/state2/inner3"])
        self.states["/composite/state2"].addChild(self.states["/composite/state2/inner4"])
        self.states[""].fixTree()
        self.states[""].default_state = self.states["/outside"]
        self.states["/composite"].default_state = self.states["/composite/state1"]
        self.states["/composite/state1"].default_state = self.states["/composite/state1/inner1"]
        self.states["/composite/state2"].default_state = self.states["/composite/state2/inner3"]
        
        # transition /outside
        _outside_0 = Transition(self, self.states["/outside"], [self.states["/composite"]])
        _outside_0.trigger = Event("to_composite", "test_input")
        self.states["/outside"].addTransition(_outside_0)
        _outside_1 = Transition(self, self.states["/outside"], [self.states["/composite/state2/inner3"]])
        _outside_1.trigger = Event("to_inner3", "test_input")
        self.states["/outside"].addTransition(_outside_1)
        _outside_2 = Transition(self, self.states["/outside"], [self.states["/composite/state2/inner4"]])
        _outside_2.trigger = Event("to_inner4", "test_input")
        self.states["/outside"].addTransition(_outside_2)
        
        # transition /composite/state1/inner1
        _composite_state1_inner1_0 = Transition(self, self.states["/composite/state1/inner1"], [self.states["/composite/state1/inner2"]])
        _composite_state1_inner1_0.trigger = Event("to_inner2", "test_input")
        self.states["/composite/state1/inner1"].addTransition(_composite_state1_inner1_0)
        
        # transition /composite
        _composite_0 = Transition(self, self.states["/composite"], [self.states["/outside"]])
        _composite_0.trigger = Event("to_outside", "test_input")
        self.states["/composite"].addTransition(_composite_0)
    
    def _composite_state1_enter(self):
        self.big_step.outputEvent(Event("enter_state1", "test_output", []))
    
    def _composite_state1_exit(self):
        self.big_step.outputEvent(Event("exit_state1", "test_output", []))
    
    def _composite_state2_enter(self):
        self.big_step.outputEvent(Event("enter_state2", "test_output", []))
    
    def _composite_state2_exit(self):
        self.big_step.outputEvent(Event("exit_state2", "test_output", []))
    
    def _composite_state1_inner1_enter(self):
        self.big_step.outputEvent(Event("enter_inner1", "test_output", []))
    
    def _composite_state1_inner1_exit(self):
        self.big_step.outputEvent(Event("exit_inner1", "test_output", []))
    
    def _composite_state1_inner2_enter(self):
        self.big_step.outputEvent(Event("enter_inner2", "test_output", []))
    
    def _composite_state1_inner2_exit(self):
        self.big_step.outputEvent(Event("exit_inner2", "test_output", []))
    
    def _composite_state2_inner3_enter(self):
        self.big_step.outputEvent(Event("enter_inner3", "test_output", []))
    
    def _composite_state2_inner3_exit(self):
        self.big_step.outputEvent(Event("exit_inner3", "test_output", []))
    
    def _composite_state2_inner4_enter(self):
        self.big_step.outputEvent(Event("enter_inner4", "test_output", []))
    
    def _composite_state2_inner4_exit(self):
        self.big_step.outputEvent(Event("exit_inner4", "test_output", []))
    
    def initializeStatechart(self):
        # enter default state
        states = self.states["/outside"].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 == "Class1":
            instance = Class1(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("test_input")
        self.addOutputPort("test_output")
        self.object_manager.createInstance("Class1", [])

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 = [InputEvent("to_composite", "test_input", [], 0.0), InputEvent("to_inner2", "test_input", [], 0.0), InputEvent("to_outside", "test_input", [], 0.0), InputEvent("to_inner3", "test_input", [], 0.0), InputEvent("to_outside", "test_input", [], 0.0), InputEvent("to_inner4", "test_input", [], 0.0)]
    expected_events = [[Event("enter_state1", "test_output", [])], [Event("enter_inner1", "test_output", [])], [Event("exit_inner1", "test_output", [])], [Event("enter_inner2", "test_output", [])], [Event("exit_inner2", "test_output", [])], [Event("exit_state1", "test_output", [])], [Event("enter_state2", "test_output", [])], [Event("enter_inner3", "test_output", [])], [Event("exit_inner3", "test_output", [])], [Event("exit_state2", "test_output", [])], [Event("enter_state2", "test_output", [])], [Event("enter_inner4", "test_output", [])]]