"""
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:06 2016

Model author: Glenn De Jonghe
Model name:   TestParallelHistory2
Model description:
Testing history where the history state is directly inside a parallel element (shallow).
"""

from sccd.runtime.statecharts_core import *

# package "TestParallelHistory2"

class TestClass(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
        TestClass.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 /parallel
        self.states["/parallel"] = ParallelState(1, self)
        
        # state /parallel/orthogonal_1
        self.states["/parallel/orthogonal_1"] = State(2, self)
        
        # state /parallel/orthogonal_1/orthogonal_inner_1
        self.states["/parallel/orthogonal_1/orthogonal_inner_1"] = State(3, self)
        
        # state /parallel/orthogonal_1/orthogonal_inner_1/state_1
        self.states["/parallel/orthogonal_1/orthogonal_inner_1/state_1"] = State(4, self)
        self.states["/parallel/orthogonal_1/orthogonal_inner_1/state_1"].setEnter(self._parallel_orthogonal_1_orthogonal_inner_1_state_1_enter)
        
        # state /parallel/orthogonal_1/orthogonal_inner_1/state_2
        self.states["/parallel/orthogonal_1/orthogonal_inner_1/state_2"] = State(5, self)
        self.states["/parallel/orthogonal_1/orthogonal_inner_1/state_2"].setEnter(self._parallel_orthogonal_1_orthogonal_inner_1_state_2_enter)
        
        # state /parallel/orthogonal_1/outer_1
        self.states["/parallel/orthogonal_1/outer_1"] = State(6, self)
        self.states["/parallel/orthogonal_1/outer_1"].setEnter(self._parallel_orthogonal_1_outer_1_enter)
        
        # state /parallel/orthogonal_2
        self.states["/parallel/orthogonal_2"] = State(7, self)
        
        # state /parallel/orthogonal_2/orthogonal_inner_2
        self.states["/parallel/orthogonal_2/orthogonal_inner_2"] = State(8, self)
        
        # state /parallel/orthogonal_2/orthogonal_inner_2/state_3
        self.states["/parallel/orthogonal_2/orthogonal_inner_2/state_3"] = State(9, self)
        self.states["/parallel/orthogonal_2/orthogonal_inner_2/state_3"].setEnter(self._parallel_orthogonal_2_orthogonal_inner_2_state_3_enter)
        
        # state /parallel/orthogonal_2/orthogonal_inner_2/state_4
        self.states["/parallel/orthogonal_2/orthogonal_inner_2/state_4"] = State(10, self)
        self.states["/parallel/orthogonal_2/orthogonal_inner_2/state_4"].setEnter(self._parallel_orthogonal_2_orthogonal_inner_2_state_4_enter)
        
        # state /parallel/orthogonal_2/outer_2
        self.states["/parallel/orthogonal_2/outer_2"] = State(11, self)
        self.states["/parallel/orthogonal_2/outer_2"].setEnter(self._parallel_orthogonal_2_outer_2_enter)
        
        # state /parallel/history_1
        self.states["/parallel/history_1"] = ShallowHistoryState(12, self)
        
        # state /next_to_parallel
        self.states["/next_to_parallel"] = State(13, self)
        self.states["/next_to_parallel"].setEnter(self._next_to_parallel_enter)
        
        # add children
        self.states[""].addChild(self.states["/parallel"])
        self.states[""].addChild(self.states["/next_to_parallel"])
        self.states["/parallel"].addChild(self.states["/parallel/orthogonal_1"])
        self.states["/parallel"].addChild(self.states["/parallel/orthogonal_2"])
        self.states["/parallel"].addChild(self.states["/parallel/history_1"])
        self.states["/parallel/orthogonal_1"].addChild(self.states["/parallel/orthogonal_1/orthogonal_inner_1"])
        self.states["/parallel/orthogonal_1"].addChild(self.states["/parallel/orthogonal_1/outer_1"])
        self.states["/parallel/orthogonal_1/orthogonal_inner_1"].addChild(self.states["/parallel/orthogonal_1/orthogonal_inner_1/state_1"])
        self.states["/parallel/orthogonal_1/orthogonal_inner_1"].addChild(self.states["/parallel/orthogonal_1/orthogonal_inner_1/state_2"])
        self.states["/parallel/orthogonal_2"].addChild(self.states["/parallel/orthogonal_2/orthogonal_inner_2"])
        self.states["/parallel/orthogonal_2"].addChild(self.states["/parallel/orthogonal_2/outer_2"])
        self.states["/parallel/orthogonal_2/orthogonal_inner_2"].addChild(self.states["/parallel/orthogonal_2/orthogonal_inner_2/state_3"])
        self.states["/parallel/orthogonal_2/orthogonal_inner_2"].addChild(self.states["/parallel/orthogonal_2/orthogonal_inner_2/state_4"])
        self.states[""].fixTree()
        self.states[""].default_state = self.states["/parallel"]
        self.states["/parallel/orthogonal_1"].default_state = self.states["/parallel/orthogonal_1/orthogonal_inner_1"]
        self.states["/parallel/orthogonal_1/orthogonal_inner_1"].default_state = self.states["/parallel/orthogonal_1/orthogonal_inner_1/state_1"]
        self.states["/parallel/orthogonal_2"].default_state = self.states["/parallel/orthogonal_2/orthogonal_inner_2"]
        self.states["/parallel/orthogonal_2/orthogonal_inner_2"].default_state = self.states["/parallel/orthogonal_2/orthogonal_inner_2/state_3"]
        
        # transition /parallel/orthogonal_1/orthogonal_inner_1/state_1
        _parallel_orthogonal_1_orthogonal_inner_1_state_1_0 = Transition(self, self.states["/parallel/orthogonal_1/orthogonal_inner_1/state_1"], [self.states["/parallel/orthogonal_1/orthogonal_inner_1/state_2"]])
        _parallel_orthogonal_1_orthogonal_inner_1_state_1_0.trigger = Event("to_state_2", "test_input")
        self.states["/parallel/orthogonal_1/orthogonal_inner_1/state_1"].addTransition(_parallel_orthogonal_1_orthogonal_inner_1_state_1_0)
        
        # transition /parallel/orthogonal_2/orthogonal_inner_2/state_3
        _parallel_orthogonal_2_orthogonal_inner_2_state_3_0 = Transition(self, self.states["/parallel/orthogonal_2/orthogonal_inner_2/state_3"], [self.states["/parallel/orthogonal_2/orthogonal_inner_2/state_4"]])
        _parallel_orthogonal_2_orthogonal_inner_2_state_3_0.trigger = Event("to_state_4", "test_input")
        self.states["/parallel/orthogonal_2/orthogonal_inner_2/state_3"].addTransition(_parallel_orthogonal_2_orthogonal_inner_2_state_3_0)
        
        # transition /next_to_parallel
        _next_to_parallel_0 = Transition(self, self.states["/next_to_parallel"], [self.states["/parallel/history_1"]])
        _next_to_parallel_0.trigger = Event("to_history_1", "test_input")
        self.states["/next_to_parallel"].addTransition(_next_to_parallel_0)
        
        # transition /parallel
        _parallel_0 = Transition(self, self.states["/parallel"], [self.states["/next_to_parallel"]])
        _parallel_0.trigger = Event("exit", "test_input")
        self.states["/parallel"].addTransition(_parallel_0)
        
        # transition /parallel/orthogonal_1/orthogonal_inner_1
        _parallel_orthogonal_1_orthogonal_inner_1_0 = Transition(self, self.states["/parallel/orthogonal_1/orthogonal_inner_1"], [self.states["/parallel/orthogonal_1/outer_1"]])
        _parallel_orthogonal_1_orthogonal_inner_1_0.trigger = Event("to_outer_1", "test_input")
        self.states["/parallel/orthogonal_1/orthogonal_inner_1"].addTransition(_parallel_orthogonal_1_orthogonal_inner_1_0)
        
        # transition /parallel/orthogonal_2/orthogonal_inner_2
        _parallel_orthogonal_2_orthogonal_inner_2_0 = Transition(self, self.states["/parallel/orthogonal_2/orthogonal_inner_2"], [self.states["/parallel/orthogonal_2/outer_2"]])
        _parallel_orthogonal_2_orthogonal_inner_2_0.trigger = Event("to_outer_2", "test_input")
        self.states["/parallel/orthogonal_2/orthogonal_inner_2"].addTransition(_parallel_orthogonal_2_orthogonal_inner_2_0)
    
    def _parallel_orthogonal_1_orthogonal_inner_1_state_1_enter(self):
        self.big_step.outputEvent(Event("in_state_1", "test_output", []))
    
    def _parallel_orthogonal_1_orthogonal_inner_1_state_2_enter(self):
        self.big_step.outputEvent(Event("in_state_2", "test_output", []))
    
    def _parallel_orthogonal_1_outer_1_enter(self):
        self.big_step.outputEvent(Event("in_outer_1", "test_output", []))
    
    def _parallel_orthogonal_2_orthogonal_inner_2_state_3_enter(self):
        self.big_step.outputEvent(Event("in_state_3", "test_output", []))
    
    def _parallel_orthogonal_2_orthogonal_inner_2_state_4_enter(self):
        self.big_step.outputEvent(Event("in_state_4", "test_output", []))
    
    def _parallel_orthogonal_2_outer_2_enter(self):
        self.big_step.outputEvent(Event("in_outer_2", "test_output", []))
    
    def _next_to_parallel_enter(self):
        self.big_step.outputEvent(Event("outside", "test_output", []))
    
    def initializeStatechart(self):
        # enter default state
        states = self.states["/parallel"].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 == "TestClass":
            instance = TestClass(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("TestClass", [])

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_outer_1", "test_input", [], 0.0), InputEvent("to_outer_2", "test_input", [], 0.0), InputEvent("exit", "test_input", [], 0.0), InputEvent("to_history_1", "test_input", [], 0.0)]
    expected_events = [[Event("in_state_1", "test_output", []), Event("in_state_3", "test_output", [])], [Event("in_outer_1", "test_output", [])], [Event("in_outer_2", "test_output", [])], [Event("outside", "test_output", [])], [Event("in_state_1", "test_output", []), Event("in_state_3", "test_output", [])]]