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

Date:   Mon Aug 28 10:53:51 2017

Model author: Yentl Van Tendeloo
Model name:   Logging
Model description:
For testing: append all input to a log until finished
"""

from sccd.runtime.statecharts_core import *

# package "Logging"

class Logging(RuntimeClassBase):
    def __init__(self, controller, log):
        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
        Logging.user_defined_constructor(self, log)
    
    def user_defined_constructor(self, log):
        self.log = log
    
    def user_defined_destructor(self):
        pass
    
    
    # builds Statechart structure
    def build_statechart_structure(self):
        
        # state <root>
        self.states[""] = State(0, "", self)
        
        # state /init
        self.states["/init"] = State(1, "/init", self)
        self.states["/init"].setEnter(self._init_enter)
        self.states["/init"].setExit(self._init_exit)
        
        # state /finished
        self.states["/finished"] = State(2, "/finished", self)
        
        # add children
        self.states[""].addChild(self.states["/init"])
        self.states[""].addChild(self.states["/finished"])
        self.states[""].fixTree()
        self.states[""].default_state = self.states["/init"]
        
        # transition /init
        _init_0 = Transition(self, self.states["/init"], [self.states["/init"]])
        _init_0.setAction(self._init_0_exec)
        _init_0.setTrigger(Event("input", "inp"))
        self.states["/init"].addTransition(_init_0)
        _init_1 = Transition(self, self.states["/init"], [self.states["/init"]])
        _init_1.setTrigger(Event("_0after"))
        self.states["/init"].addTransition(_init_1)
        _init_2 = Transition(self, self.states["/init"], [self.states["/finished"]])
        _init_2.setTrigger(Event("terminate", "inp"))
        self.states["/init"].addTransition(_init_2)
    
    def _init_enter(self):
        self.addTimer(0, 0.1)
    
    def _init_exit(self):
        self.removeTimer(0)
    
    def _init_0_exec(self, parameters):
        value = parameters[0]
        self.log.append(value)
        print("Got value: " + str(value))
    
    def initializeStatechart(self):
        # enter default state
        self.default_targets = self.states["/init"].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 == "Logging":
            instance = Logging(self.controller, construct_params[0])
            instance.associations = {}
        else:
            raise Exception("Cannot instantiate class " + class_name)
        return instance

class Controller(ThreadsControllerBase):
    def __init__(self, log, 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("inp")
        self.addOutputPort("outp")
        self.object_manager.createInstance("Logging", [log])