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

Date:   Tue Oct 11 13:15:37 2016

Model author: Sadaf Mustafiz and Bruno Barroca and Claudio Gomes
Model name:   FSASimulator
Model description:
An FSA simulator written in SCCD. It supports after and events.
"""

from sccd.runtime.statecharts_core import *
from sccd.runtime.libs.ui import *
from sccd.runtime.libs.utils import *
import fsaclasses

# package "FSASimulator"

class FSASimulator(RuntimeClassBase):
    def __init__(self, controller, amodel, events):
        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()
        
        # user defined attributes
        self.elapsed = None
        self.super_step_completed = None
        self.model = None
        self.logicalTime = None
        self.timestep = None
        self.currentEvent = None
        self.selectedTransition = None
        self.eventList = None
        self.currentState = None
        
        # call user defined constructor
        FSASimulator.user_defined_constructor(self, amodel, events)
    
    def user_defined_constructor(self, amodel, events):
        self.model = amodel
        self.eventList = events
    
    def user_defined_destructor(self):
        pass
    
    
    # user defined method
    def processEvent(self, event):
        if (event != None):
        	self.eventList.popEvent(event)
        	event.processed = True
    
    
    # user defined method
    def getInputEventAt(self, time):
        return self.eventList.getInputAt(time)
    
    
    # builds Statechart structure
    def build_statechart_structure(self):
        
        # state <root>
        self.states[""] = State(0, self)
        
        # state /CheckTermination
        self.states["/CheckTermination"] = State(1, self)
        
        # state /CheckTermination/MacroStepProcessed
        self.states["/CheckTermination/MacroStepProcessed"] = State(2, self)
        
        # state /DoSimulation
        self.states["/DoSimulation"] = State(3, self)
        
        # state /DoSimulation/MicroStepPrepared
        self.states["/DoSimulation/MicroStepPrepared"] = State(4, self)
        
        # state /DoSimulation/MicroStepProcessed
        self.states["/DoSimulation/MicroStepProcessed"] = State(5, self)
        
        # state /DoSimulation/MacroStepPrepared
        self.states["/DoSimulation/MacroStepPrepared"] = State(6, self)
        
        # state /Initialized
        self.states["/Initialized"] = State(7, self)
        
        # state /Started
        self.states["/Started"] = State(8, self)
        
        # state /End
        self.states["/End"] = State(9, self)
        
        # add children
        self.states[""].addChild(self.states["/CheckTermination"])
        self.states[""].addChild(self.states["/DoSimulation"])
        self.states[""].addChild(self.states["/Initialized"])
        self.states[""].addChild(self.states["/Started"])
        self.states[""].addChild(self.states["/End"])
        self.states["/CheckTermination"].addChild(self.states["/CheckTermination/MacroStepProcessed"])
        self.states["/DoSimulation"].addChild(self.states["/DoSimulation/MicroStepPrepared"])
        self.states["/DoSimulation"].addChild(self.states["/DoSimulation/MicroStepProcessed"])
        self.states["/DoSimulation"].addChild(self.states["/DoSimulation/MacroStepPrepared"])
        self.states[""].fixTree()
        self.states[""].default_state = self.states["/Started"]
        self.states["/CheckTermination"].default_state = self.states["/CheckTermination/MacroStepProcessed"]
        self.states["/DoSimulation"].default_state = self.states["/DoSimulation/MacroStepPrepared"]
        
        # transition /CheckTermination/MacroStepProcessed
        _CheckTermination_MacroStepProcessed_0 = Transition(self, self.states["/CheckTermination/MacroStepProcessed"], [self.states["/End"]])
        _CheckTermination_MacroStepProcessed_0.setAction(self._CheckTermination_MacroStepProcessed_0_exec)
        _CheckTermination_MacroStepProcessed_0.setTrigger(None)
        _CheckTermination_MacroStepProcessed_0.setGuard(self._CheckTermination_MacroStepProcessed_0_guard)
        self.states["/CheckTermination/MacroStepProcessed"].addTransition(_CheckTermination_MacroStepProcessed_0)
        _CheckTermination_MacroStepProcessed_1 = Transition(self, self.states["/CheckTermination/MacroStepProcessed"], [self.states["/DoSimulation"]])
        _CheckTermination_MacroStepProcessed_1.setAction(self._CheckTermination_MacroStepProcessed_1_exec)
        _CheckTermination_MacroStepProcessed_1.setTrigger(None)
        _CheckTermination_MacroStepProcessed_1.setGuard(self._CheckTermination_MacroStepProcessed_1_guard)
        self.states["/CheckTermination/MacroStepProcessed"].addTransition(_CheckTermination_MacroStepProcessed_1)
        
        # transition /DoSimulation/MicroStepPrepared
        _DoSimulation_MicroStepPrepared_0 = Transition(self, self.states["/DoSimulation/MicroStepPrepared"], [self.states["/DoSimulation/MicroStepProcessed"]])
        _DoSimulation_MicroStepPrepared_0.setAction(self._DoSimulation_MicroStepPrepared_0_exec)
        _DoSimulation_MicroStepPrepared_0.setTrigger(None)
        self.states["/DoSimulation/MicroStepPrepared"].addTransition(_DoSimulation_MicroStepPrepared_0)
        
        # transition /DoSimulation/MicroStepProcessed
        _DoSimulation_MicroStepProcessed_0 = Transition(self, self.states["/DoSimulation/MicroStepProcessed"], [self.states["/DoSimulation/MicroStepPrepared"]])
        _DoSimulation_MicroStepProcessed_0.setAction(self._DoSimulation_MicroStepProcessed_0_exec)
        _DoSimulation_MicroStepProcessed_0.setTrigger(None)
        _DoSimulation_MicroStepProcessed_0.setGuard(self._DoSimulation_MicroStepProcessed_0_guard)
        self.states["/DoSimulation/MicroStepProcessed"].addTransition(_DoSimulation_MicroStepProcessed_0)
        _DoSimulation_MicroStepProcessed_1 = Transition(self, self.states["/DoSimulation/MicroStepProcessed"], [self.states["/CheckTermination"]])
        _DoSimulation_MicroStepProcessed_1.setAction(self._DoSimulation_MicroStepProcessed_1_exec)
        _DoSimulation_MicroStepProcessed_1.setTrigger(None)
        _DoSimulation_MicroStepProcessed_1.setGuard(self._DoSimulation_MicroStepProcessed_1_guard)
        self.states["/DoSimulation/MicroStepProcessed"].addTransition(_DoSimulation_MicroStepProcessed_1)
        
        # transition /DoSimulation/MacroStepPrepared
        _DoSimulation_MacroStepPrepared_0 = Transition(self, self.states["/DoSimulation/MacroStepPrepared"], [self.states["/DoSimulation/MicroStepProcessed"]])
        _DoSimulation_MacroStepPrepared_0.setAction(self._DoSimulation_MacroStepPrepared_0_exec)
        _DoSimulation_MacroStepPrepared_0.setTrigger(None)
        self.states["/DoSimulation/MacroStepPrepared"].addTransition(_DoSimulation_MacroStepPrepared_0)
        
        # transition /Initialized
        _Initialized_0 = Transition(self, self.states["/Initialized"], [self.states["/CheckTermination"]])
        _Initialized_0.setAction(self._Initialized_0_exec)
        _Initialized_0.setTrigger(None)
        self.states["/Initialized"].addTransition(_Initialized_0)
        
        # transition /Started
        _Started_0 = Transition(self, self.states["/Started"], [self.states["/Initialized"]])
        _Started_0.setAction(self._Started_0_exec)
        _Started_0.setTrigger(None)
        self.states["/Started"].addTransition(_Started_0)
    
    def _CheckTermination_MacroStepProcessed_0_exec(self, parameters):
        print('Going to End... ')
    
    def _CheckTermination_MacroStepProcessed_0_guard(self, parameters):
        return self.currentState.final
    
    def _CheckTermination_MacroStepProcessed_1_exec(self, parameters):
        print('Going to DoSimulation and reading events... ')
        self.currentEvent = self.getInputEventAt(self.logicalTime)
        self.selectedTransition = self.model.getTransitionFrom(self.currentState, self.currentEvent, self.elapsed)
        print(self.currentEvent)
        print(self.selectedTransition)
    
    def _CheckTermination_MacroStepProcessed_1_guard(self, parameters):
        return not self.currentState.final
    
    def _DoSimulation_MicroStepPrepared_0_exec(self, parameters):
        print('Going to MicroStepProcessed, taking transition and reading events... ')
        print('Transition to be taken: ' + str(self.selectedTransition))
        self.currentState = self.selectedTransition.target
        self.elapsed = 0
        self.processEvent(self.currentEvent)
        print('New state: ' + str(self.currentState))
        self.currentEvent = self.getInputEventAt(self.logicalTime)
        self.selectedTransition = self.model.getTransitionFrom(self.currentState, self.currentEvent, self.elapsed)
        print(self.currentEvent)
        print(self.selectedTransition)
    
    def _DoSimulation_MicroStepProcessed_0_exec(self, parameters):
        print('Going to MicroStepPrepared... ')
    
    def _DoSimulation_MicroStepProcessed_0_guard(self, parameters):
        return self.selectedTransition != None
    
    def _DoSimulation_MicroStepProcessed_1_exec(self, parameters):
        print('Going to CheckTermination and advancing time... ')
        self.logicalTime = self.logicalTime + self.timestep
        self.elapsed = self.elapsed + self.timestep
        print(self.logicalTime)
        print(self.elapsed)
    
    def _DoSimulation_MicroStepProcessed_1_guard(self, parameters):
        return self.selectedTransition == None
    
    def _DoSimulation_MacroStepPrepared_0_exec(self, parameters):
        print('Going to MicroStepProcessed... ')
    
    def _Initialized_0_exec(self, parameters):
        print('Going to MacroStepProcessed... ')
    
    def _Started_0_exec(self, parameters):
        print('Going to Initialized... ')
        self.logicalTime = 0
        self.elapsed = 0
        self.timestep = 1.0
        self.currentState = self.model.initialState
    
    def initializeStatechart(self):
        # enter default state
        self.default_targets = self.states["/Started"].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 == "FSASimulator":
            instance = FSASimulator(self.controller, construct_params[0], construct_params[1])
            instance.associations = {}
        else:
            raise Exception("Cannot instantiate class " + class_name)
        return instance

class Controller(ThreadsControllerBase):
    def __init__(self, amodel, events, 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.object_manager.createInstance("FSASimulator", [amodel, events])