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

Date:   Fri Aug  5 15:59:05 2016

Model author: Yentl Van Tendeloo
Model name:   broken
Model description:
Broken!
"""

from sccd.runtime.statecharts_core import *

# package "broken"

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.SourceParent
        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):
        pass
    
    def user_defined_destructor(self):
        pass
    
    
    # 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)
        
        # state /ready
        self.states["/ready"] = State(2, self)
        self.states["/ready"].setEnter(self._ready_enter)
        
        # state /done
        self.states["/done"] = State(3, self)
        
        # add children
        self.states[""].addChild(self.states["/x"])
        self.states[""].addChild(self.states["/ready"])
        self.states[""].addChild(self.states["/done"])
        self.states[""].fixTree()
        self.states[""].default_state = self.states["/x"]
        
        # transition /x
        _x_0 = Transition(self, self.states["/x"], [self.states["/ready"]])
        _x_0.setAction(self._x_0_exec)
        _x_0.trigger = Event("instance_created", None)
        self.states["/x"].addTransition(_x_0)
        
        # transition /ready
        _ready_0 = Transition(self, self.states["/ready"], [self.states["/done"]])
        _ready_0.setAction(self._ready_0_exec)
        _ready_0.trigger = Event("close", None)
        self.states["/ready"].addTransition(_ready_0)
    
    def _x_enter(self):
        self.big_step.outputEventOM(Event("create_instance", None, [self, 'child', 'B']))
    
    def _ready_enter(self):
        self.big_step.outputEventOM(Event("start_instance", None, [self, self.instancename]))
    
    def _x_0_exec(self, parameters):
        instancename = parameters[0]
        self.instancename = instancename
        print(self.instancename)
    
    def _ready_0_exec(self, parameters):
        print("CLOSED")
        self.big_step.outputEventOM(Event("delete_instance", None, [self, self.instancename]))
    
    def initializeStatechart(self):
        # enter default state
        states = self.states["/x"].getEffectiveTargetStates()
        self.updateConfiguration(states)
        for state in states:
            if state.enter:
                state.enter()

class B(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
        B.user_defined_constructor(self)
    
    def user_defined_constructor(self):
        print("READY")
    
    def user_defined_destructor(self):
        pass
    
    
    # builds Statechart structure
    def build_statechart_structure(self):
        
        # state <root>
        self.states[""] = State(0, self)
        
        # state /z
        self.states["/z"] = State(1, self)
        self.states["/z"].setEnter(self._z_enter)
        self.states["/z"].setExit(self._z_exit)
        
        # add children
        self.states[""].addChild(self.states["/z"])
        self.states[""].fixTree()
        self.states[""].default_state = self.states["/z"]
        
        # transition /z
        _z_0 = Transition(self, self.states["/z"], [self.states["/z"]])
        _z_0.setAction(self._z_0_exec)
        _z_0.trigger = Event("_0after")
        self.states["/z"].addTransition(_z_0)
    
    def _z_enter(self):
        self.addTimer(0, 0)
        print("RUN")
    
    def _z_exit(self):
        self.removeTimer(0)
    
    def _z_0_exec(self, parameters):
        print("RAISE")
        self.big_step.outputEventOM(Event("broad_cast", None, [Event("close", None, [])]))
    
    def initializeStatechart(self):
        # enter default state
        states = self.states["/z"].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 == "A":
            instance = A(self.controller)
            instance.associations = {}
            instance.associations["child"] = Association("B", 0, 1)
        elif class_name == "B":
            instance = B(self.controller)
            instance.associations = {}
            instance.associations["parent"] = Association("A", 1, 1)
        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.object_manager.createInstance("A", [])