# Python source code generated by SCC (StateChart Compiler) 0.3, written by Thomas Feng
#   Source: F:/McGill/763B MODELLING/project\ballCSULR_MDL.des
#   Date:   April 30, 2008
#   Time:   0:39:26


# Header Section -- definition and module importation used by the following parts
import sys
import copy
import time
import string
import thread
import threading
import time


class State:

  def __init__(self):

    self.StateID=-1
    self.Next=None

  def copy(self):

    s=State()
    s.StateID=self.StateID
    if self.Next!=None:
      s.Next=self.Next.copy()
    return s


class History:

  def __init__(self):

    self.States=None
    self.Times=None
    self.Submodel=None


class EventList:

  def __init__(self):

    self.Event=None
    self.Next=None

  def Append(self, e):

    if isinstance(e, str):
      el=EventList()
      el.Event=e
      cur=this
      while cur.Next!=None and cur.Event!=e:
        cur=cur.Next
      if cur.Event!=e:
        cur.Next=el

    elif isinstance(e, EventList):
      el=e
      while el!=None:
        self.Append(el.Event)
        el=el.Next


class StringList:

  def __init__(self, str=""):

    self.str=str
    self.Next=None

  def sort(self):

    if self.Next!=None:
      self.Next=self.Next.sort()
      if self.Next.str<self.str:
        first=self.Next
        prev=self.Next
        next=self.Next.Next
        while next!=None and next.str<self.str:
          prev=next
          next=next.Next
        self.Next=next
        prev.Next=self
        return first
    return self


class IntList:

  def __init__(self, int=0):

    self.int=int
    self.Next=None


class Hierarchy:

  def __init__(self):

    self.StateName=None
    self.PathName=None
    self.StateNum=-1
    self.Level=-1
    self.Next=None


class StateMachine:

  def eventStr2Int(self, event):

    return -1

  def getCurrentStateList(self):

    return None

  def getCurrentState(self):

    return "[]"

  def getEnabledEvents(self):

    return None

  def initModel(self, run_initializer=1, run_enter_actions=1):

    pass

  def isInState(self, s, check_substate=1, use_backup=1):

    return 0

  def getParentState(self, state):

    return -1

  def isHistoryState(self, state):

    return 0

  def isLeafState(self, state):

    return 0

  def getHierarchy(self, start_level=0, state_prefix=None):

    return None

  def topLevelHistory(self):

    pass

  def recordAllEnteredStates(self):

    pass

  def runAllEnterActions(self):

    pass

  def runInitializer(self):

    pass

  def runFinalizer(self):

    pass

  def runInteractor(self):

    pass

  def state(self):

    pass

  def handleEvent(self, se, params=[], cond=None, scheduler=None):

    return 0

  def event(self, e, params=[], cond=None, scheduler=None):

    pass


def startsWith(s, ss):

  return ss==None or (s!=None and len(s)>=len(ss) and s[0:len(ss)]==ss)


# used only when --ext is set
from code import InteractiveInterpreter


# Main Class -- the top level model that is executed from the command line
class ballCSULR_MDL(StateMachine):

  # Constants for this model
  StateNum=14
  EventNames=["__INTERNAL_0_TIME_0",
              "__INTERNAL_0_TIME_1",
              "__INTERNAL_0_TIME_2",
              "__INTERNAL_0_TIME_3",
              "__INTERNAL_0_TIME_4",
              "__INTERNAL_0_TIME_5",
              "start" 
             ]
  StateNames=["compo_0",
              "compo_0.color_comp",
              "compo_0.color_comp.color",
              "compo_0.left_comp",
              "compo_0.left_comp.blowLeft",
              "compo_0.move_comp",
              "compo_0.move_comp.moving",
              "compo_0.right_comp",
              "compo_0.right_comp.blowRight",
              "compo_0.shape_comp",
              "compo_0.shape_comp.shape",
              "compo_0.up_comp",
              "compo_0.up_comp.blowUp",
              "init" 
             ]
  ParentTable=[-1,  # compo_0 -- parent (None)
               0,  # compo_0.color_comp -- parent compo_0
               1,  # compo_0.color_comp.color -- parent compo_0.color_comp
               0,  # compo_0.left_comp -- parent compo_0
               3,  # compo_0.left_comp.blowLeft -- parent compo_0.left_comp
               0,  # compo_0.move_comp -- parent compo_0
               5,  # compo_0.move_comp.moving -- parent compo_0.move_comp
               0,  # compo_0.right_comp -- parent compo_0
               7,  # compo_0.right_comp.blowRight -- parent compo_0.right_comp
               0,  # compo_0.shape_comp -- parent compo_0
               9,  # compo_0.shape_comp.shape -- parent compo_0.shape_comp
               0,  # compo_0.up_comp -- parent compo_0
               11,  # compo_0.up_comp.blowUp -- parent compo_0.up_comp
               -1   # init -- parent (None)
              ]
  HistoryStateTable=[0,
                     0,
                     0,
                     0,
                     0,
                     0,
                     0,
                     0,
                     0,
                     0,
                     0,
                     0,
                     0,
                     0 
                    ]
  LeafStateTable=[None,
                  None,
                  "compo_0.color_comp.color",
                  None,
                  "compo_0.left_comp.blowLeft",
                  None,
                  "compo_0.move_comp.moving",
                  None,
                  "compo_0.right_comp.blowRight",
                  None,
                  "compo_0.shape_comp.shape",
                  None,
                  "compo_0.up_comp.blowUp",
                  "init" 
                 ]
  OrthogonalInBetween=[[0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0],
                       [0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0],
                       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 
                      ]
  OrthogonalTable=[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                   [0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
                   [0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
                   [0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0],
                   [0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0],
                   [0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0],
                   [0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0],
                   [0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0],
                   [0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0],
                   [0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0],
                   [0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0],
                   [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0],
                   [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0],
                   [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 
                  ]
  IntervalTable=["0.01",
                 "0.03",
                 "0.01",
                 "10",
                 "15",
                 "23",
                 None 
                ]
  RescheduleTable=[6,
                   2,
                   10,
                   12,
                   8,
                   4,
                   -1 
                  ]
  Hierarchy=[[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],  # substates of state compo_0
             [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],  # substates of state compo_0.color_comp
             [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],  # substates of state compo_0.color_comp.color
             [0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],  # substates of state compo_0.left_comp
             [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],  # substates of state compo_0.left_comp.blowLeft
             [0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0],  # substates of state compo_0.move_comp
             [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],  # substates of state compo_0.move_comp.moving
             [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0],  # substates of state compo_0.right_comp
             [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],  # substates of state compo_0.right_comp.blowRight
             [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0],  # substates of state compo_0.shape_comp
             [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],  # substates of state compo_0.shape_comp.shape
             [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0],  # substates of state compo_0.up_comp
             [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],  # substates of state compo_0.up_comp.blowUp
             [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]   # substates of state init
            ]
  CommonStateTable=[[-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1],
                    [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1],
                    [0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1],
                    [0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, -1],
                    [0, 0, 0, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0, -1],
                    [0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, -1],
                    [0, 0, 0, 0, 0, 5, 5, 0, 0, 0, 0, 0, 0, -1],
                    [0, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, -1],
                    [0, 0, 0, 0, 0, 0, 0, 7, 7, 0, 0, 0, 0, -1],
                    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, -1],
                    [0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 9, 0, 0, -1],
                    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11, -1],
                    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11, 11, -1],
                    [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1] 
                   ]
  Description=None

  ##Lock=thread.allocate_lock()
  CurrentModel=None

  def __init__(self, Interpreter=None, Parent=None, OldInstance=None):

    # Variables
    self.Parent=Parent
    self.HistoryCount=0
    self.state=None
    self.BackState=None
    if OldInstance!=None:
      self.Submodels=OldInstance.Submodels
    else:
      self.Submodels=[]
      for i in range(ballCSULR_MDL.StateNum):
        self.Submodels.append(None)
    self.history=[]
    for i in range(ballCSULR_MDL.StateNum):
      self.history.append(None)

    # Constructor
    for i in range(ballCSULR_MDL.StateNum):
      self.history[i]=History()
      self.history[i].States=[]
      self.history[i].Times=[]
      for j in range(ballCSULR_MDL.StateNum):
        self.history[i].States.append(-1)
        self.history[i].Times.append(-1)

    self.TimedTransitions=[] # used only when --ext is set
    for i in range(ballCSULR_MDL.StateNum):
      self.TimedTransitions.append(None)

    self.clearEnteredStates()
    self.HasInteractor=0

    # Interpreter of action code
    if self.Parent==None:  # Top-level model
      if Interpreter:
        self.DefaultInterpreter=Interpreter
      else:
        self.DefaultInterpreter=InteractiveInterpreter()
      self.setupInterpreter()

      ##self.EventsCond=threading.Condition()
      ##self.SchedulerCond=threading.Condition()
      self.Schedules=[]
      self.PendingEvents=None
      self.PendingEventsTail=None
    else:
      self.DefaultInterpreter=Interpreter
    self.Started=0
    self.Stopped=0

    self.description=ballCSULR_MDL.Description

  # Methods
  def isParent(self, sp, sc):

    return sc>=0 and (sp<0 or ballCSULR_MDL.Hierarchy[sp][sc])

  def isInState(self, s, check_substate=1, use_backup=1):

    if isinstance(s, int):
      if use_backup:
        st=self.BackState
      else:
        st=self.state
      while st!=None:
        if st.StateID==s or (check_substate and self.isParent(s, st.StateID)):
          return 1
        else:
          st=st.Next
      return 0

    elif isinstance(s, str):
      i=0
      while i<ballCSULR_MDL.StateNum:
        if s==ballCSULR_MDL.StateNames[i]:
          return self.isInState(i, check_substate, use_backup)
        i=i+1
      i=0
      while i<ballCSULR_MDL.StateNum:
        stname=ballCSULR_MDL.StateNames[i]
        if self.Submodels[i]!=None and startsWith(s, stname+"."):
          pos=len(stname)+1
          SubmodelState=s[pos:]
          return self.isInState(i, 0, use_backup) and self.Submodels[i].isInState(SubmodelState, check_substate, use_backup);
        i=i+1
      return 0

    else:
      return 0

  class main_callable:
    def __call__(self, argv):
      model=ballCSULR_MDL()
      cmd=""
      model.initModel()
      if model.HasInteractor:
        model.runInteractor()
      else:
        ##cond=thread.allocate_lock()
        while cmd!="exit":
          sys.__stdout__.write(model.getCurrentState()+" > ")
          cmd=string.strip(sys.__stdin__.readline())
          if cmd=="exit":
            break
          if not model.Stopped:
            ##cond.acquire()
            ##model.event(cmd, [], cond)
            model.event(cmd, [])
            ##cond.acquire()
            ##cond.release()
      model.runFinalizer()
  main=main_callable()

  def initModel(self, run_initializer=1, run_enter_actions=1, TkInstance = None ):

    self.clearEnteredStates()
    if self.Parent==None and ballCSULR_MDL.Description!=None:
      sys.__stdout__.write(ballCSULR_MDL.Description+"\n")
    self.addInState(13) # init state "init"
    self.recordEnteredState(13)
    if run_initializer:
      self.runInitializer()
    if not self.HasInteractor:
      self.start(None, run_enter_actions, TkInstance )

  def applyMask(self, mask, dest):

    i=0
    while i<ballCSULR_MDL.StateNum:
      dest[i]=dest[i] and mask[i]
      i=i+1

  def handleEvent(self, se, params=[], cond=None, scheduler=None):

    if not self.Started:
      if cond:
        cond.release()
      return 0
    self.params=params
    handled=0
    table=[]
    i=0
    while i<ballCSULR_MDL.StateNum:
      table.append(1)
      i=i+1
    if self.state:
      self.BackState=self.state.copy()
    else:
      self.BackState=None
    e=self.eventStr2Int(se)
    if e==0: # event "__INTERNAL_0_TIME_0"
      if table[6] and self.isInState(6) and self.testCondition(0):
        if (scheduler==self or scheduler==None) and table[6]:
          self.runActionCode(2) # output action(s)
          self.runExitActionsForStates(5)
	  self.clearEnteredStates()
          self.changeState(6, 6)
          self.runEnterActionsForStates(self.StatesEntered, 1)
          self.applyMask(ballCSULR_MDL.OrthogonalTable[6], table)
          handled=1
    elif e==1: # event "__INTERNAL_0_TIME_1"
      if table[2] and self.isInState(2) and self.testCondition(1):
        if (scheduler==self or scheduler==None) and table[2]:
          self.runActionCode(3) # output action(s)
          self.runExitActionsForStates(1)
	  self.clearEnteredStates()
          self.changeState(2, 2)
          self.runEnterActionsForStates(self.StatesEntered, 1)
          self.applyMask(ballCSULR_MDL.OrthogonalTable[2], table)
          handled=1
    elif e==2: # event "__INTERNAL_0_TIME_2"
      if table[10] and self.isInState(10) and self.testCondition(2):
        if (scheduler==self or scheduler==None) and table[10]:
          self.runActionCode(4) # output action(s)
          self.runExitActionsForStates(9)
	  self.clearEnteredStates()
          self.changeState(10, 10)
          self.runEnterActionsForStates(self.StatesEntered, 1)
          self.applyMask(ballCSULR_MDL.OrthogonalTable[10], table)
          handled=1
    elif e==3: # event "__INTERNAL_0_TIME_3"
      if table[12] and self.isInState(12) and self.testCondition(3):
        if (scheduler==self or scheduler==None) and table[12]:
          self.runActionCode(5) # output action(s)
          self.runExitActionsForStates(11)
	  self.clearEnteredStates()
          self.changeState(12, 12)
          self.runEnterActionsForStates(self.StatesEntered, 1)
          self.applyMask(ballCSULR_MDL.OrthogonalTable[12], table)
          handled=1
    elif e==4: # event "__INTERNAL_0_TIME_4"
      if table[8] and self.isInState(8) and self.testCondition(4):
        if (scheduler==self or scheduler==None) and table[8]:
          self.runActionCode(6) # output action(s)
          self.runExitActionsForStates(7)
	  self.clearEnteredStates()
          self.changeState(8, 8)
          self.runEnterActionsForStates(self.StatesEntered, 1)
          self.applyMask(ballCSULR_MDL.OrthogonalTable[8], table)
          handled=1
    elif e==5: # event "__INTERNAL_0_TIME_5"
      if table[4] and self.isInState(4) and self.testCondition(5):
        if (scheduler==self or scheduler==None) and table[4]:
          self.runActionCode(7) # output action(s)
          self.runExitActionsForStates(3)
	  self.clearEnteredStates()
          self.changeState(4, 4)
          self.runEnterActionsForStates(self.StatesEntered, 1)
          self.applyMask(ballCSULR_MDL.OrthogonalTable[4], table)
          handled=1
    elif e==6: # event "start"
      if table[13] and self.isInState(13) and self.testCondition(6):
        if (scheduler==self or scheduler==None) and table[13]:
          self.runActionCode(8) # output action(s)
          self.runExitActionsForStates(-1)
	  self.clearEnteredStates()
          self.changeState(13, 0)
          self.runEnterActionsForStates(self.StatesEntered, 1)
          self.applyMask(ballCSULR_MDL.OrthogonalTable[13], table)
          handled=1
    if cond and self.Parent==None:  # Top-level model
      cond.release()
    if not handled and e>=0 and (scheduler==self or scheduler==None) and ballCSULR_MDL.RescheduleTable[e]>=0:
      self.addSchedule(ballCSULR_MDL.RescheduleTable[e], ballCSULR_MDL.IntervalTable[e], ballCSULR_MDL.EventNames[e], scheduler)
    return handled

  def forceIntoState(self, s):

    changed=0
    s2=self.state
    while s2!=None:
      HasCommonParent=0
      for i in range(ballCSULR_MDL.StateNum):
        if self.isParent(i, s2.StateID) and self.isParent(i, s):
          HasCommonParent=1
          if not self.hasOrthogonalStateInBetween(i, s2.StateID):
            self.changeState(s2.StateID, s)
            changed=1
      if not HasCommonParent:
        self.changeState(s2.StateID, s)
        changed=1
      s2=s2.Next
    if not changed:
      self.addInState(s)

  def changeState(self, s1, s2, check_history=0, top_level=0):

    # t1=common(s1, s2)
    t1=ballCSULR_MDL.CommonStateTable[s1][s2]
    self.recordHistory(t1)
    if t1>=0:
      self.removeOutStates(t1)
    else:
      self.state=None
    # t2=history(s2)
    t2=ballCSULR_MDL.HistoryStateTable[s2]
    if t2==0: # no history
      self.generateStates(t1, s2)
    elif t2==1: # normal history
      if not check_history:
        self.generateStates(t1, s2)
      elif self.hasHistoryRecorded(s2) and self.Submodels[s2]==None:
        self.generateStates(t1, self.history[s2].States[s2])
      else:
        self.generateStates(t1, s2, 1)
    elif t2==2: # deep history
      if check_history and self.hasHistoryRecorded(s2):
        if self.Submodels[s2]:
          self.recordEnteredState(s2, 1, 1, t1)
          self.addInState(s2)
        else:
          for i in range(ballCSULR_MDL.StateNum):
            hs=self.history[s2].States[i]
            if hs>=0 and self.isLeafState(hs):
              self.recordEnteredState(hs, 1, 1, t1)
              self.addInState(hs)
      else:
        self.generateStates(t1, s2)

  def addInState(self, s):

    if not self.isInState(s, 1, 0):
      st=State()
      st.StateID=s
      st.Next=self.state
      self.state=st
      return 1
    else:
      return 0

  def generateStates(self, common, dest, history_type=0):

    if common==-1:
      if dest==0:
        if history_type!=2 or self.check_history(-1):
          if history_type!=2 or self.check_history(0):
            self.recordEnteredState(0)
            if history_type!=2 or self.check_history(1):
              self.recordEnteredState(1)
              if history_type!=2 or self.check_history(2):
                self.recordEnteredState(2)
                self.addInState(2)  # move into leaf state "compo_0.color_comp.color"
            if history_type!=2 or self.check_history(3):
              self.recordEnteredState(3)
              if history_type!=2 or self.check_history(4):
                self.recordEnteredState(4)
                self.addInState(4)  # move into leaf state "compo_0.left_comp.blowLeft"
            if history_type!=2 or self.check_history(5):
              self.recordEnteredState(5)
              if history_type!=2 or self.check_history(6):
                self.recordEnteredState(6)
                self.addInState(6)  # move into leaf state "compo_0.move_comp.moving"
            if history_type!=2 or self.check_history(7):
              self.recordEnteredState(7)
              if history_type!=2 or self.check_history(8):
                self.recordEnteredState(8)
                self.addInState(8)  # move into leaf state "compo_0.right_comp.blowRight"
            if history_type!=2 or self.check_history(9):
              self.recordEnteredState(9)
              if history_type!=2 or self.check_history(10):
                self.recordEnteredState(10)
                self.addInState(10)  # move into leaf state "compo_0.shape_comp.shape"
            if history_type!=2 or self.check_history(11):
              self.recordEnteredState(11)
              if history_type!=2 or self.check_history(12):
                self.recordEnteredState(12)
                self.addInState(12)  # move into leaf state "compo_0.up_comp.blowUp"
      elif dest==1:
        if history_type!=2 or self.check_history(-1):
          if history_type!=2 or self.check_history(0):
            self.recordEnteredState(0)
            if history_type!=2 or self.check_history(1):
              self.recordEnteredState(1)
              if history_type!=2 or self.check_history(2):
                self.recordEnteredState(2)
                self.addInState(2)  # move into leaf state "compo_0.color_comp.color"
            if history_type!=2 or self.check_history(3):
              self.recordEnteredState(3)
              if history_type!=2 or self.check_history(4):
                self.recordEnteredState(4)
                self.addInState(4)  # move into leaf state "compo_0.left_comp.blowLeft"
            if history_type!=2 or self.check_history(5):
              self.recordEnteredState(5)
              if history_type!=2 or self.check_history(6):
                self.recordEnteredState(6)
                self.addInState(6)  # move into leaf state "compo_0.move_comp.moving"
            if history_type!=2 or self.check_history(7):
              self.recordEnteredState(7)
              if history_type!=2 or self.check_history(8):
                self.recordEnteredState(8)
                self.addInState(8)  # move into leaf state "compo_0.right_comp.blowRight"
            if history_type!=2 or self.check_history(9):
              self.recordEnteredState(9)
              if history_type!=2 or self.check_history(10):
                self.recordEnteredState(10)
                self.addInState(10)  # move into leaf state "compo_0.shape_comp.shape"
            if history_type!=2 or self.check_history(11):
              self.recordEnteredState(11)
              if history_type!=2 or self.check_history(12):
                self.recordEnteredState(12)
                self.addInState(12)  # move into leaf state "compo_0.up_comp.blowUp"
      elif dest==2:
        if history_type!=2 or self.check_history(-1):
          if history_type!=2 or self.check_history(0):
            self.recordEnteredState(0)
            if history_type!=2 or self.check_history(1):
              self.recordEnteredState(1)
              if history_type!=2 or self.check_history(2):
                self.recordEnteredState(2)
                self.addInState(2)  # move into leaf state "compo_0.color_comp.color"
            if history_type!=2 or self.check_history(3):
              self.recordEnteredState(3)
              if history_type!=2 or self.check_history(4):
                self.recordEnteredState(4)
                self.addInState(4)  # move into leaf state "compo_0.left_comp.blowLeft"
            if history_type!=2 or self.check_history(5):
              self.recordEnteredState(5)
              if history_type!=2 or self.check_history(6):
                self.recordEnteredState(6)
                self.addInState(6)  # move into leaf state "compo_0.move_comp.moving"
            if history_type!=2 or self.check_history(7):
              self.recordEnteredState(7)
              if history_type!=2 or self.check_history(8):
                self.recordEnteredState(8)
                self.addInState(8)  # move into leaf state "compo_0.right_comp.blowRight"
            if history_type!=2 or self.check_history(9):
              self.recordEnteredState(9)
              if history_type!=2 or self.check_history(10):
                self.recordEnteredState(10)
                self.addInState(10)  # move into leaf state "compo_0.shape_comp.shape"
            if history_type!=2 or self.check_history(11):
              self.recordEnteredState(11)
              if history_type!=2 or self.check_history(12):
                self.recordEnteredState(12)
                self.addInState(12)  # move into leaf state "compo_0.up_comp.blowUp"
      elif dest==3:
        if history_type!=2 or self.check_history(-1):
          if history_type!=2 or self.check_history(0):
            self.recordEnteredState(0)
            if history_type!=2 or self.check_history(1):
              self.recordEnteredState(1)
              if history_type!=2 or self.check_history(2):
                self.recordEnteredState(2)
                self.addInState(2)  # move into leaf state "compo_0.color_comp.color"
            if history_type!=2 or self.check_history(3):
              self.recordEnteredState(3)
              if history_type!=2 or self.check_history(4):
                self.recordEnteredState(4)
                self.addInState(4)  # move into leaf state "compo_0.left_comp.blowLeft"
            if history_type!=2 or self.check_history(5):
              self.recordEnteredState(5)
              if history_type!=2 or self.check_history(6):
                self.recordEnteredState(6)
                self.addInState(6)  # move into leaf state "compo_0.move_comp.moving"
            if history_type!=2 or self.check_history(7):
              self.recordEnteredState(7)
              if history_type!=2 or self.check_history(8):
                self.recordEnteredState(8)
                self.addInState(8)  # move into leaf state "compo_0.right_comp.blowRight"
            if history_type!=2 or self.check_history(9):
              self.recordEnteredState(9)
              if history_type!=2 or self.check_history(10):
                self.recordEnteredState(10)
                self.addInState(10)  # move into leaf state "compo_0.shape_comp.shape"
            if history_type!=2 or self.check_history(11):
              self.recordEnteredState(11)
              if history_type!=2 or self.check_history(12):
                self.recordEnteredState(12)
                self.addInState(12)  # move into leaf state "compo_0.up_comp.blowUp"
      elif dest==4:
        if history_type!=2 or self.check_history(-1):
          if history_type!=2 or self.check_history(0):
            self.recordEnteredState(0)
            if history_type!=2 or self.check_history(1):
              self.recordEnteredState(1)
              if history_type!=2 or self.check_history(2):
                self.recordEnteredState(2)
                self.addInState(2)  # move into leaf state "compo_0.color_comp.color"
            if history_type!=2 or self.check_history(3):
              self.recordEnteredState(3)
              if history_type!=2 or self.check_history(4):
                self.recordEnteredState(4)
                self.addInState(4)  # move into leaf state "compo_0.left_comp.blowLeft"
            if history_type!=2 or self.check_history(5):
              self.recordEnteredState(5)
              if history_type!=2 or self.check_history(6):
                self.recordEnteredState(6)
                self.addInState(6)  # move into leaf state "compo_0.move_comp.moving"
            if history_type!=2 or self.check_history(7):
              self.recordEnteredState(7)
              if history_type!=2 or self.check_history(8):
                self.recordEnteredState(8)
                self.addInState(8)  # move into leaf state "compo_0.right_comp.blowRight"
            if history_type!=2 or self.check_history(9):
              self.recordEnteredState(9)
              if history_type!=2 or self.check_history(10):
                self.recordEnteredState(10)
                self.addInState(10)  # move into leaf state "compo_0.shape_comp.shape"
            if history_type!=2 or self.check_history(11):
              self.recordEnteredState(11)
              if history_type!=2 or self.check_history(12):
                self.recordEnteredState(12)
                self.addInState(12)  # move into leaf state "compo_0.up_comp.blowUp"
      elif dest==5:
        if history_type!=2 or self.check_history(-1):
          if history_type!=2 or self.check_history(0):
            self.recordEnteredState(0)
            if history_type!=2 or self.check_history(1):
              self.recordEnteredState(1)
              if history_type!=2 or self.check_history(2):
                self.recordEnteredState(2)
                self.addInState(2)  # move into leaf state "compo_0.color_comp.color"
            if history_type!=2 or self.check_history(3):
              self.recordEnteredState(3)
              if history_type!=2 or self.check_history(4):
                self.recordEnteredState(4)
                self.addInState(4)  # move into leaf state "compo_0.left_comp.blowLeft"
            if history_type!=2 or self.check_history(5):
              self.recordEnteredState(5)
              if history_type!=2 or self.check_history(6):
                self.recordEnteredState(6)
                self.addInState(6)  # move into leaf state "compo_0.move_comp.moving"
            if history_type!=2 or self.check_history(7):
              self.recordEnteredState(7)
              if history_type!=2 or self.check_history(8):
                self.recordEnteredState(8)
                self.addInState(8)  # move into leaf state "compo_0.right_comp.blowRight"
            if history_type!=2 or self.check_history(9):
              self.recordEnteredState(9)
              if history_type!=2 or self.check_history(10):
                self.recordEnteredState(10)
                self.addInState(10)  # move into leaf state "compo_0.shape_comp.shape"
            if history_type!=2 or self.check_history(11):
              self.recordEnteredState(11)
              if history_type!=2 or self.check_history(12):
                self.recordEnteredState(12)
                self.addInState(12)  # move into leaf state "compo_0.up_comp.blowUp"
      elif dest==6:
        if history_type!=2 or self.check_history(-1):
          if history_type!=2 or self.check_history(0):
            self.recordEnteredState(0)
            if history_type!=2 or self.check_history(1):
              self.recordEnteredState(1)
              if history_type!=2 or self.check_history(2):
                self.recordEnteredState(2)
                self.addInState(2)  # move into leaf state "compo_0.color_comp.color"
            if history_type!=2 or self.check_history(3):
              self.recordEnteredState(3)
              if history_type!=2 or self.check_history(4):
                self.recordEnteredState(4)
                self.addInState(4)  # move into leaf state "compo_0.left_comp.blowLeft"
            if history_type!=2 or self.check_history(5):
              self.recordEnteredState(5)
              if history_type!=2 or self.check_history(6):
                self.recordEnteredState(6)
                self.addInState(6)  # move into leaf state "compo_0.move_comp.moving"
            if history_type!=2 or self.check_history(7):
              self.recordEnteredState(7)
              if history_type!=2 or self.check_history(8):
                self.recordEnteredState(8)
                self.addInState(8)  # move into leaf state "compo_0.right_comp.blowRight"
            if history_type!=2 or self.check_history(9):
              self.recordEnteredState(9)
              if history_type!=2 or self.check_history(10):
                self.recordEnteredState(10)
                self.addInState(10)  # move into leaf state "compo_0.shape_comp.shape"
            if history_type!=2 or self.check_history(11):
              self.recordEnteredState(11)
              if history_type!=2 or self.check_history(12):
                self.recordEnteredState(12)
                self.addInState(12)  # move into leaf state "compo_0.up_comp.blowUp"
      elif dest==7:
        if history_type!=2 or self.check_history(-1):
          if history_type!=2 or self.check_history(0):
            self.recordEnteredState(0)
            if history_type!=2 or self.check_history(1):
              self.recordEnteredState(1)
              if history_type!=2 or self.check_history(2):
                self.recordEnteredState(2)
                self.addInState(2)  # move into leaf state "compo_0.color_comp.color"
            if history_type!=2 or self.check_history(3):
              self.recordEnteredState(3)
              if history_type!=2 or self.check_history(4):
                self.recordEnteredState(4)
                self.addInState(4)  # move into leaf state "compo_0.left_comp.blowLeft"
            if history_type!=2 or self.check_history(5):
              self.recordEnteredState(5)
              if history_type!=2 or self.check_history(6):
                self.recordEnteredState(6)
                self.addInState(6)  # move into leaf state "compo_0.move_comp.moving"
            if history_type!=2 or self.check_history(7):
              self.recordEnteredState(7)
              if history_type!=2 or self.check_history(8):
                self.recordEnteredState(8)
                self.addInState(8)  # move into leaf state "compo_0.right_comp.blowRight"
            if history_type!=2 or self.check_history(9):
              self.recordEnteredState(9)
              if history_type!=2 or self.check_history(10):
                self.recordEnteredState(10)
                self.addInState(10)  # move into leaf state "compo_0.shape_comp.shape"
            if history_type!=2 or self.check_history(11):
              self.recordEnteredState(11)
              if history_type!=2 or self.check_history(12):
                self.recordEnteredState(12)
                self.addInState(12)  # move into leaf state "compo_0.up_comp.blowUp"
      elif dest==8:
        if history_type!=2 or self.check_history(-1):
          if history_type!=2 or self.check_history(0):
            self.recordEnteredState(0)
            if history_type!=2 or self.check_history(1):
              self.recordEnteredState(1)
              if history_type!=2 or self.check_history(2):
                self.recordEnteredState(2)
                self.addInState(2)  # move into leaf state "compo_0.color_comp.color"
            if history_type!=2 or self.check_history(3):
              self.recordEnteredState(3)
              if history_type!=2 or self.check_history(4):
                self.recordEnteredState(4)
                self.addInState(4)  # move into leaf state "compo_0.left_comp.blowLeft"
            if history_type!=2 or self.check_history(5):
              self.recordEnteredState(5)
              if history_type!=2 or self.check_history(6):
                self.recordEnteredState(6)
                self.addInState(6)  # move into leaf state "compo_0.move_comp.moving"
            if history_type!=2 or self.check_history(7):
              self.recordEnteredState(7)
              if history_type!=2 or self.check_history(8):
                self.recordEnteredState(8)
                self.addInState(8)  # move into leaf state "compo_0.right_comp.blowRight"
            if history_type!=2 or self.check_history(9):
              self.recordEnteredState(9)
              if history_type!=2 or self.check_history(10):
                self.recordEnteredState(10)
                self.addInState(10)  # move into leaf state "compo_0.shape_comp.shape"
            if history_type!=2 or self.check_history(11):
              self.recordEnteredState(11)
              if history_type!=2 or self.check_history(12):
                self.recordEnteredState(12)
                self.addInState(12)  # move into leaf state "compo_0.up_comp.blowUp"
      elif dest==9:
        if history_type!=2 or self.check_history(-1):
          if history_type!=2 or self.check_history(0):
            self.recordEnteredState(0)
            if history_type!=2 or self.check_history(1):
              self.recordEnteredState(1)
              if history_type!=2 or self.check_history(2):
                self.recordEnteredState(2)
                self.addInState(2)  # move into leaf state "compo_0.color_comp.color"
            if history_type!=2 or self.check_history(3):
              self.recordEnteredState(3)
              if history_type!=2 or self.check_history(4):
                self.recordEnteredState(4)
                self.addInState(4)  # move into leaf state "compo_0.left_comp.blowLeft"
            if history_type!=2 or self.check_history(5):
              self.recordEnteredState(5)
              if history_type!=2 or self.check_history(6):
                self.recordEnteredState(6)
                self.addInState(6)  # move into leaf state "compo_0.move_comp.moving"
            if history_type!=2 or self.check_history(7):
              self.recordEnteredState(7)
              if history_type!=2 or self.check_history(8):
                self.recordEnteredState(8)
                self.addInState(8)  # move into leaf state "compo_0.right_comp.blowRight"
            if history_type!=2 or self.check_history(9):
              self.recordEnteredState(9)
              if history_type!=2 or self.check_history(10):
                self.recordEnteredState(10)
                self.addInState(10)  # move into leaf state "compo_0.shape_comp.shape"
            if history_type!=2 or self.check_history(11):
              self.recordEnteredState(11)
              if history_type!=2 or self.check_history(12):
                self.recordEnteredState(12)
                self.addInState(12)  # move into leaf state "compo_0.up_comp.blowUp"
      elif dest==10:
        if history_type!=2 or self.check_history(-1):
          if history_type!=2 or self.check_history(0):
            self.recordEnteredState(0)
            if history_type!=2 or self.check_history(1):
              self.recordEnteredState(1)
              if history_type!=2 or self.check_history(2):
                self.recordEnteredState(2)
                self.addInState(2)  # move into leaf state "compo_0.color_comp.color"
            if history_type!=2 or self.check_history(3):
              self.recordEnteredState(3)
              if history_type!=2 or self.check_history(4):
                self.recordEnteredState(4)
                self.addInState(4)  # move into leaf state "compo_0.left_comp.blowLeft"
            if history_type!=2 or self.check_history(5):
              self.recordEnteredState(5)
              if history_type!=2 or self.check_history(6):
                self.recordEnteredState(6)
                self.addInState(6)  # move into leaf state "compo_0.move_comp.moving"
            if history_type!=2 or self.check_history(7):
              self.recordEnteredState(7)
              if history_type!=2 or self.check_history(8):
                self.recordEnteredState(8)
                self.addInState(8)  # move into leaf state "compo_0.right_comp.blowRight"
            if history_type!=2 or self.check_history(9):
              self.recordEnteredState(9)
              if history_type!=2 or self.check_history(10):
                self.recordEnteredState(10)
                self.addInState(10)  # move into leaf state "compo_0.shape_comp.shape"
            if history_type!=2 or self.check_history(11):
              self.recordEnteredState(11)
              if history_type!=2 or self.check_history(12):
                self.recordEnteredState(12)
                self.addInState(12)  # move into leaf state "compo_0.up_comp.blowUp"
      elif dest==11:
        if history_type!=2 or self.check_history(-1):
          if history_type!=2 or self.check_history(0):
            self.recordEnteredState(0)
            if history_type!=2 or self.check_history(1):
              self.recordEnteredState(1)
              if history_type!=2 or self.check_history(2):
                self.recordEnteredState(2)
                self.addInState(2)  # move into leaf state "compo_0.color_comp.color"
            if history_type!=2 or self.check_history(3):
              self.recordEnteredState(3)
              if history_type!=2 or self.check_history(4):
                self.recordEnteredState(4)
                self.addInState(4)  # move into leaf state "compo_0.left_comp.blowLeft"
            if history_type!=2 or self.check_history(5):
              self.recordEnteredState(5)
              if history_type!=2 or self.check_history(6):
                self.recordEnteredState(6)
                self.addInState(6)  # move into leaf state "compo_0.move_comp.moving"
            if history_type!=2 or self.check_history(7):
              self.recordEnteredState(7)
              if history_type!=2 or self.check_history(8):
                self.recordEnteredState(8)
                self.addInState(8)  # move into leaf state "compo_0.right_comp.blowRight"
            if history_type!=2 or self.check_history(9):
              self.recordEnteredState(9)
              if history_type!=2 or self.check_history(10):
                self.recordEnteredState(10)
                self.addInState(10)  # move into leaf state "compo_0.shape_comp.shape"
            if history_type!=2 or self.check_history(11):
              self.recordEnteredState(11)
              if history_type!=2 or self.check_history(12):
                self.recordEnteredState(12)
                self.addInState(12)  # move into leaf state "compo_0.up_comp.blowUp"
      elif dest==12:
        if history_type!=2 or self.check_history(-1):
          if history_type!=2 or self.check_history(0):
            self.recordEnteredState(0)
            if history_type!=2 or self.check_history(1):
              self.recordEnteredState(1)
              if history_type!=2 or self.check_history(2):
                self.recordEnteredState(2)
                self.addInState(2)  # move into leaf state "compo_0.color_comp.color"
            if history_type!=2 or self.check_history(3):
              self.recordEnteredState(3)
              if history_type!=2 or self.check_history(4):
                self.recordEnteredState(4)
                self.addInState(4)  # move into leaf state "compo_0.left_comp.blowLeft"
            if history_type!=2 or self.check_history(5):
              self.recordEnteredState(5)
              if history_type!=2 or self.check_history(6):
                self.recordEnteredState(6)
                self.addInState(6)  # move into leaf state "compo_0.move_comp.moving"
            if history_type!=2 or self.check_history(7):
              self.recordEnteredState(7)
              if history_type!=2 or self.check_history(8):
                self.recordEnteredState(8)
                self.addInState(8)  # move into leaf state "compo_0.right_comp.blowRight"
            if history_type!=2 or self.check_history(9):
              self.recordEnteredState(9)
              if history_type!=2 or self.check_history(10):
                self.recordEnteredState(10)
                self.addInState(10)  # move into leaf state "compo_0.shape_comp.shape"
            if history_type!=2 or self.check_history(11):
              self.recordEnteredState(11)
              if history_type!=2 or self.check_history(12):
                self.recordEnteredState(12)
                self.addInState(12)  # move into leaf state "compo_0.up_comp.blowUp"
      elif dest==13:
        if history_type!=2 or self.check_history(-1):
          if history_type!=2 or self.check_history(13):
            self.recordEnteredState(13)
            self.addInState(13)  # move into leaf state "init"
    elif common==0:
      if dest==0:
        if history_type!=2 or self.check_history(0):
          if history_type!=2 or self.check_history(1):
            self.recordEnteredState(1)
            if history_type!=2 or self.check_history(2):
              self.recordEnteredState(2)
              self.addInState(2)  # move into leaf state "compo_0.color_comp.color"
          if history_type!=2 or self.check_history(3):
            self.recordEnteredState(3)
            if history_type!=2 or self.check_history(4):
              self.recordEnteredState(4)
              self.addInState(4)  # move into leaf state "compo_0.left_comp.blowLeft"
          if history_type!=2 or self.check_history(5):
            self.recordEnteredState(5)
            if history_type!=2 or self.check_history(6):
              self.recordEnteredState(6)
              self.addInState(6)  # move into leaf state "compo_0.move_comp.moving"
          if history_type!=2 or self.check_history(7):
            self.recordEnteredState(7)
            if history_type!=2 or self.check_history(8):
              self.recordEnteredState(8)
              self.addInState(8)  # move into leaf state "compo_0.right_comp.blowRight"
          if history_type!=2 or self.check_history(9):
            self.recordEnteredState(9)
            if history_type!=2 or self.check_history(10):
              self.recordEnteredState(10)
              self.addInState(10)  # move into leaf state "compo_0.shape_comp.shape"
          if history_type!=2 or self.check_history(11):
            self.recordEnteredState(11)
            if history_type!=2 or self.check_history(12):
              self.recordEnteredState(12)
              self.addInState(12)  # move into leaf state "compo_0.up_comp.blowUp"
      elif dest==1:
        if history_type!=2 or self.check_history(0):
          if history_type!=2 or self.check_history(1):
            self.recordEnteredState(1)
            if history_type!=2 or self.check_history(2):
              self.recordEnteredState(2)
              self.addInState(2)  # move into leaf state "compo_0.color_comp.color"
          if history_type!=2 or self.check_history(3):
            self.recordEnteredState(3)
            if history_type!=2 or self.check_history(4):
              self.recordEnteredState(4)
              self.addInState(4)  # move into leaf state "compo_0.left_comp.blowLeft"
          if history_type!=2 or self.check_history(5):
            self.recordEnteredState(5)
            if history_type!=2 or self.check_history(6):
              self.recordEnteredState(6)
              self.addInState(6)  # move into leaf state "compo_0.move_comp.moving"
          if history_type!=2 or self.check_history(7):
            self.recordEnteredState(7)
            if history_type!=2 or self.check_history(8):
              self.recordEnteredState(8)
              self.addInState(8)  # move into leaf state "compo_0.right_comp.blowRight"
          if history_type!=2 or self.check_history(9):
            self.recordEnteredState(9)
            if history_type!=2 or self.check_history(10):
              self.recordEnteredState(10)
              self.addInState(10)  # move into leaf state "compo_0.shape_comp.shape"
          if history_type!=2 or self.check_history(11):
            self.recordEnteredState(11)
            if history_type!=2 or self.check_history(12):
              self.recordEnteredState(12)
              self.addInState(12)  # move into leaf state "compo_0.up_comp.blowUp"
      elif dest==2:
        if history_type!=2 or self.check_history(0):
          if history_type!=2 or self.check_history(1):
            self.recordEnteredState(1)
            if history_type!=2 or self.check_history(2):
              self.recordEnteredState(2)
              self.addInState(2)  # move into leaf state "compo_0.color_comp.color"
          if history_type!=2 or self.check_history(3):
            self.recordEnteredState(3)
            if history_type!=2 or self.check_history(4):
              self.recordEnteredState(4)
              self.addInState(4)  # move into leaf state "compo_0.left_comp.blowLeft"
          if history_type!=2 or self.check_history(5):
            self.recordEnteredState(5)
            if history_type!=2 or self.check_history(6):
              self.recordEnteredState(6)
              self.addInState(6)  # move into leaf state "compo_0.move_comp.moving"
          if history_type!=2 or self.check_history(7):
            self.recordEnteredState(7)
            if history_type!=2 or self.check_history(8):
              self.recordEnteredState(8)
              self.addInState(8)  # move into leaf state "compo_0.right_comp.blowRight"
          if history_type!=2 or self.check_history(9):
            self.recordEnteredState(9)
            if history_type!=2 or self.check_history(10):
              self.recordEnteredState(10)
              self.addInState(10)  # move into leaf state "compo_0.shape_comp.shape"
          if history_type!=2 or self.check_history(11):
            self.recordEnteredState(11)
            if history_type!=2 or self.check_history(12):
              self.recordEnteredState(12)
              self.addInState(12)  # move into leaf state "compo_0.up_comp.blowUp"
      elif dest==3:
        if history_type!=2 or self.check_history(0):
          if history_type!=2 or self.check_history(1):
            self.recordEnteredState(1)
            if history_type!=2 or self.check_history(2):
              self.recordEnteredState(2)
              self.addInState(2)  # move into leaf state "compo_0.color_comp.color"
          if history_type!=2 or self.check_history(3):
            self.recordEnteredState(3)
            if history_type!=2 or self.check_history(4):
              self.recordEnteredState(4)
              self.addInState(4)  # move into leaf state "compo_0.left_comp.blowLeft"
          if history_type!=2 or self.check_history(5):
            self.recordEnteredState(5)
            if history_type!=2 or self.check_history(6):
              self.recordEnteredState(6)
              self.addInState(6)  # move into leaf state "compo_0.move_comp.moving"
          if history_type!=2 or self.check_history(7):
            self.recordEnteredState(7)
            if history_type!=2 or self.check_history(8):
              self.recordEnteredState(8)
              self.addInState(8)  # move into leaf state "compo_0.right_comp.blowRight"
          if history_type!=2 or self.check_history(9):
            self.recordEnteredState(9)
            if history_type!=2 or self.check_history(10):
              self.recordEnteredState(10)
              self.addInState(10)  # move into leaf state "compo_0.shape_comp.shape"
          if history_type!=2 or self.check_history(11):
            self.recordEnteredState(11)
            if history_type!=2 or self.check_history(12):
              self.recordEnteredState(12)
              self.addInState(12)  # move into leaf state "compo_0.up_comp.blowUp"
      elif dest==4:
        if history_type!=2 or self.check_history(0):
          if history_type!=2 or self.check_history(1):
            self.recordEnteredState(1)
            if history_type!=2 or self.check_history(2):
              self.recordEnteredState(2)
              self.addInState(2)  # move into leaf state "compo_0.color_comp.color"
          if history_type!=2 or self.check_history(3):
            self.recordEnteredState(3)
            if history_type!=2 or self.check_history(4):
              self.recordEnteredState(4)
              self.addInState(4)  # move into leaf state "compo_0.left_comp.blowLeft"
          if history_type!=2 or self.check_history(5):
            self.recordEnteredState(5)
            if history_type!=2 or self.check_history(6):
              self.recordEnteredState(6)
              self.addInState(6)  # move into leaf state "compo_0.move_comp.moving"
          if history_type!=2 or self.check_history(7):
            self.recordEnteredState(7)
            if history_type!=2 or self.check_history(8):
              self.recordEnteredState(8)
              self.addInState(8)  # move into leaf state "compo_0.right_comp.blowRight"
          if history_type!=2 or self.check_history(9):
            self.recordEnteredState(9)
            if history_type!=2 or self.check_history(10):
              self.recordEnteredState(10)
              self.addInState(10)  # move into leaf state "compo_0.shape_comp.shape"
          if history_type!=2 or self.check_history(11):
            self.recordEnteredState(11)
            if history_type!=2 or self.check_history(12):
              self.recordEnteredState(12)
              self.addInState(12)  # move into leaf state "compo_0.up_comp.blowUp"
      elif dest==5:
        if history_type!=2 or self.check_history(0):
          if history_type!=2 or self.check_history(1):
            self.recordEnteredState(1)
            if history_type!=2 or self.check_history(2):
              self.recordEnteredState(2)
              self.addInState(2)  # move into leaf state "compo_0.color_comp.color"
          if history_type!=2 or self.check_history(3):
            self.recordEnteredState(3)
            if history_type!=2 or self.check_history(4):
              self.recordEnteredState(4)
              self.addInState(4)  # move into leaf state "compo_0.left_comp.blowLeft"
          if history_type!=2 or self.check_history(5):
            self.recordEnteredState(5)
            if history_type!=2 or self.check_history(6):
              self.recordEnteredState(6)
              self.addInState(6)  # move into leaf state "compo_0.move_comp.moving"
          if history_type!=2 or self.check_history(7):
            self.recordEnteredState(7)
            if history_type!=2 or self.check_history(8):
              self.recordEnteredState(8)
              self.addInState(8)  # move into leaf state "compo_0.right_comp.blowRight"
          if history_type!=2 or self.check_history(9):
            self.recordEnteredState(9)
            if history_type!=2 or self.check_history(10):
              self.recordEnteredState(10)
              self.addInState(10)  # move into leaf state "compo_0.shape_comp.shape"
          if history_type!=2 or self.check_history(11):
            self.recordEnteredState(11)
            if history_type!=2 or self.check_history(12):
              self.recordEnteredState(12)
              self.addInState(12)  # move into leaf state "compo_0.up_comp.blowUp"
      elif dest==6:
        if history_type!=2 or self.check_history(0):
          if history_type!=2 or self.check_history(1):
            self.recordEnteredState(1)
            if history_type!=2 or self.check_history(2):
              self.recordEnteredState(2)
              self.addInState(2)  # move into leaf state "compo_0.color_comp.color"
          if history_type!=2 or self.check_history(3):
            self.recordEnteredState(3)
            if history_type!=2 or self.check_history(4):
              self.recordEnteredState(4)
              self.addInState(4)  # move into leaf state "compo_0.left_comp.blowLeft"
          if history_type!=2 or self.check_history(5):
            self.recordEnteredState(5)
            if history_type!=2 or self.check_history(6):
              self.recordEnteredState(6)
              self.addInState(6)  # move into leaf state "compo_0.move_comp.moving"
          if history_type!=2 or self.check_history(7):
            self.recordEnteredState(7)
            if history_type!=2 or self.check_history(8):
              self.recordEnteredState(8)
              self.addInState(8)  # move into leaf state "compo_0.right_comp.blowRight"
          if history_type!=2 or self.check_history(9):
            self.recordEnteredState(9)
            if history_type!=2 or self.check_history(10):
              self.recordEnteredState(10)
              self.addInState(10)  # move into leaf state "compo_0.shape_comp.shape"
          if history_type!=2 or self.check_history(11):
            self.recordEnteredState(11)
            if history_type!=2 or self.check_history(12):
              self.recordEnteredState(12)
              self.addInState(12)  # move into leaf state "compo_0.up_comp.blowUp"
      elif dest==7:
        if history_type!=2 or self.check_history(0):
          if history_type!=2 or self.check_history(1):
            self.recordEnteredState(1)
            if history_type!=2 or self.check_history(2):
              self.recordEnteredState(2)
              self.addInState(2)  # move into leaf state "compo_0.color_comp.color"
          if history_type!=2 or self.check_history(3):
            self.recordEnteredState(3)
            if history_type!=2 or self.check_history(4):
              self.recordEnteredState(4)
              self.addInState(4)  # move into leaf state "compo_0.left_comp.blowLeft"
          if history_type!=2 or self.check_history(5):
            self.recordEnteredState(5)
            if history_type!=2 or self.check_history(6):
              self.recordEnteredState(6)
              self.addInState(6)  # move into leaf state "compo_0.move_comp.moving"
          if history_type!=2 or self.check_history(7):
            self.recordEnteredState(7)
            if history_type!=2 or self.check_history(8):
              self.recordEnteredState(8)
              self.addInState(8)  # move into leaf state "compo_0.right_comp.blowRight"
          if history_type!=2 or self.check_history(9):
            self.recordEnteredState(9)
            if history_type!=2 or self.check_history(10):
              self.recordEnteredState(10)
              self.addInState(10)  # move into leaf state "compo_0.shape_comp.shape"
          if history_type!=2 or self.check_history(11):
            self.recordEnteredState(11)
            if history_type!=2 or self.check_history(12):
              self.recordEnteredState(12)
              self.addInState(12)  # move into leaf state "compo_0.up_comp.blowUp"
      elif dest==8:
        if history_type!=2 or self.check_history(0):
          if history_type!=2 or self.check_history(1):
            self.recordEnteredState(1)
            if history_type!=2 or self.check_history(2):
              self.recordEnteredState(2)
              self.addInState(2)  # move into leaf state "compo_0.color_comp.color"
          if history_type!=2 or self.check_history(3):
            self.recordEnteredState(3)
            if history_type!=2 or self.check_history(4):
              self.recordEnteredState(4)
              self.addInState(4)  # move into leaf state "compo_0.left_comp.blowLeft"
          if history_type!=2 or self.check_history(5):
            self.recordEnteredState(5)
            if history_type!=2 or self.check_history(6):
              self.recordEnteredState(6)
              self.addInState(6)  # move into leaf state "compo_0.move_comp.moving"
          if history_type!=2 or self.check_history(7):
            self.recordEnteredState(7)
            if history_type!=2 or self.check_history(8):
              self.recordEnteredState(8)
              self.addInState(8)  # move into leaf state "compo_0.right_comp.blowRight"
          if history_type!=2 or self.check_history(9):
            self.recordEnteredState(9)
            if history_type!=2 or self.check_history(10):
              self.recordEnteredState(10)
              self.addInState(10)  # move into leaf state "compo_0.shape_comp.shape"
          if history_type!=2 or self.check_history(11):
            self.recordEnteredState(11)
            if history_type!=2 or self.check_history(12):
              self.recordEnteredState(12)
              self.addInState(12)  # move into leaf state "compo_0.up_comp.blowUp"
      elif dest==9:
        if history_type!=2 or self.check_history(0):
          if history_type!=2 or self.check_history(1):
            self.recordEnteredState(1)
            if history_type!=2 or self.check_history(2):
              self.recordEnteredState(2)
              self.addInState(2)  # move into leaf state "compo_0.color_comp.color"
          if history_type!=2 or self.check_history(3):
            self.recordEnteredState(3)
            if history_type!=2 or self.check_history(4):
              self.recordEnteredState(4)
              self.addInState(4)  # move into leaf state "compo_0.left_comp.blowLeft"
          if history_type!=2 or self.check_history(5):
            self.recordEnteredState(5)
            if history_type!=2 or self.check_history(6):
              self.recordEnteredState(6)
              self.addInState(6)  # move into leaf state "compo_0.move_comp.moving"
          if history_type!=2 or self.check_history(7):
            self.recordEnteredState(7)
            if history_type!=2 or self.check_history(8):
              self.recordEnteredState(8)
              self.addInState(8)  # move into leaf state "compo_0.right_comp.blowRight"
          if history_type!=2 or self.check_history(9):
            self.recordEnteredState(9)
            if history_type!=2 or self.check_history(10):
              self.recordEnteredState(10)
              self.addInState(10)  # move into leaf state "compo_0.shape_comp.shape"
          if history_type!=2 or self.check_history(11):
            self.recordEnteredState(11)
            if history_type!=2 or self.check_history(12):
              self.recordEnteredState(12)
              self.addInState(12)  # move into leaf state "compo_0.up_comp.blowUp"
      elif dest==10:
        if history_type!=2 or self.check_history(0):
          if history_type!=2 or self.check_history(1):
            self.recordEnteredState(1)
            if history_type!=2 or self.check_history(2):
              self.recordEnteredState(2)
              self.addInState(2)  # move into leaf state "compo_0.color_comp.color"
          if history_type!=2 or self.check_history(3):
            self.recordEnteredState(3)
            if history_type!=2 or self.check_history(4):
              self.recordEnteredState(4)
              self.addInState(4)  # move into leaf state "compo_0.left_comp.blowLeft"
          if history_type!=2 or self.check_history(5):
            self.recordEnteredState(5)
            if history_type!=2 or self.check_history(6):
              self.recordEnteredState(6)
              self.addInState(6)  # move into leaf state "compo_0.move_comp.moving"
          if history_type!=2 or self.check_history(7):
            self.recordEnteredState(7)
            if history_type!=2 or self.check_history(8):
              self.recordEnteredState(8)
              self.addInState(8)  # move into leaf state "compo_0.right_comp.blowRight"
          if history_type!=2 or self.check_history(9):
            self.recordEnteredState(9)
            if history_type!=2 or self.check_history(10):
              self.recordEnteredState(10)
              self.addInState(10)  # move into leaf state "compo_0.shape_comp.shape"
          if history_type!=2 or self.check_history(11):
            self.recordEnteredState(11)
            if history_type!=2 or self.check_history(12):
              self.recordEnteredState(12)
              self.addInState(12)  # move into leaf state "compo_0.up_comp.blowUp"
      elif dest==11:
        if history_type!=2 or self.check_history(0):
          if history_type!=2 or self.check_history(1):
            self.recordEnteredState(1)
            if history_type!=2 or self.check_history(2):
              self.recordEnteredState(2)
              self.addInState(2)  # move into leaf state "compo_0.color_comp.color"
          if history_type!=2 or self.check_history(3):
            self.recordEnteredState(3)
            if history_type!=2 or self.check_history(4):
              self.recordEnteredState(4)
              self.addInState(4)  # move into leaf state "compo_0.left_comp.blowLeft"
          if history_type!=2 or self.check_history(5):
            self.recordEnteredState(5)
            if history_type!=2 or self.check_history(6):
              self.recordEnteredState(6)
              self.addInState(6)  # move into leaf state "compo_0.move_comp.moving"
          if history_type!=2 or self.check_history(7):
            self.recordEnteredState(7)
            if history_type!=2 or self.check_history(8):
              self.recordEnteredState(8)
              self.addInState(8)  # move into leaf state "compo_0.right_comp.blowRight"
          if history_type!=2 or self.check_history(9):
            self.recordEnteredState(9)
            if history_type!=2 or self.check_history(10):
              self.recordEnteredState(10)
              self.addInState(10)  # move into leaf state "compo_0.shape_comp.shape"
          if history_type!=2 or self.check_history(11):
            self.recordEnteredState(11)
            if history_type!=2 or self.check_history(12):
              self.recordEnteredState(12)
              self.addInState(12)  # move into leaf state "compo_0.up_comp.blowUp"
      elif dest==12:
        if history_type!=2 or self.check_history(0):
          if history_type!=2 or self.check_history(1):
            self.recordEnteredState(1)
            if history_type!=2 or self.check_history(2):
              self.recordEnteredState(2)
              self.addInState(2)  # move into leaf state "compo_0.color_comp.color"
          if history_type!=2 or self.check_history(3):
            self.recordEnteredState(3)
            if history_type!=2 or self.check_history(4):
              self.recordEnteredState(4)
              self.addInState(4)  # move into leaf state "compo_0.left_comp.blowLeft"
          if history_type!=2 or self.check_history(5):
            self.recordEnteredState(5)
            if history_type!=2 or self.check_history(6):
              self.recordEnteredState(6)
              self.addInState(6)  # move into leaf state "compo_0.move_comp.moving"
          if history_type!=2 or self.check_history(7):
            self.recordEnteredState(7)
            if history_type!=2 or self.check_history(8):
              self.recordEnteredState(8)
              self.addInState(8)  # move into leaf state "compo_0.right_comp.blowRight"
          if history_type!=2 or self.check_history(9):
            self.recordEnteredState(9)
            if history_type!=2 or self.check_history(10):
              self.recordEnteredState(10)
              self.addInState(10)  # move into leaf state "compo_0.shape_comp.shape"
          if history_type!=2 or self.check_history(11):
            self.recordEnteredState(11)
            if history_type!=2 or self.check_history(12):
              self.recordEnteredState(12)
              self.addInState(12)  # move into leaf state "compo_0.up_comp.blowUp"
    elif common==1:
      if dest==1:
        if history_type!=2 or self.check_history(1):
          if history_type!=2 or self.check_history(2):
            self.recordEnteredState(2)
            self.addInState(2)  # move into leaf state "compo_0.color_comp.color"
      elif dest==2:
        if history_type!=2 or self.check_history(1):
          if history_type!=2 or self.check_history(2):
            self.recordEnteredState(2)
            self.addInState(2)  # move into leaf state "compo_0.color_comp.color"
    elif common==2:
      if dest==2:
        if history_type!=2 or self.check_history(2):
          self.addInState(2)  # move into leaf state "compo_0.color_comp.color"
    elif common==3:
      if dest==3:
        if history_type!=2 or self.check_history(3):
          if history_type!=2 or self.check_history(4):
            self.recordEnteredState(4)
            self.addInState(4)  # move into leaf state "compo_0.left_comp.blowLeft"
      elif dest==4:
        if history_type!=2 or self.check_history(3):
          if history_type!=2 or self.check_history(4):
            self.recordEnteredState(4)
            self.addInState(4)  # move into leaf state "compo_0.left_comp.blowLeft"
    elif common==4:
      if dest==4:
        if history_type!=2 or self.check_history(4):
          self.addInState(4)  # move into leaf state "compo_0.left_comp.blowLeft"
    elif common==5:
      if dest==5:
        if history_type!=2 or self.check_history(5):
          if history_type!=2 or self.check_history(6):
            self.recordEnteredState(6)
            self.addInState(6)  # move into leaf state "compo_0.move_comp.moving"
      elif dest==6:
        if history_type!=2 or self.check_history(5):
          if history_type!=2 or self.check_history(6):
            self.recordEnteredState(6)
            self.addInState(6)  # move into leaf state "compo_0.move_comp.moving"
    elif common==6:
      if dest==6:
        if history_type!=2 or self.check_history(6):
          self.addInState(6)  # move into leaf state "compo_0.move_comp.moving"
    elif common==7:
      if dest==7:
        if history_type!=2 or self.check_history(7):
          if history_type!=2 or self.check_history(8):
            self.recordEnteredState(8)
            self.addInState(8)  # move into leaf state "compo_0.right_comp.blowRight"
      elif dest==8:
        if history_type!=2 or self.check_history(7):
          if history_type!=2 or self.check_history(8):
            self.recordEnteredState(8)
            self.addInState(8)  # move into leaf state "compo_0.right_comp.blowRight"
    elif common==8:
      if dest==8:
        if history_type!=2 or self.check_history(8):
          self.addInState(8)  # move into leaf state "compo_0.right_comp.blowRight"
    elif common==9:
      if dest==9:
        if history_type!=2 or self.check_history(9):
          if history_type!=2 or self.check_history(10):
            self.recordEnteredState(10)
            self.addInState(10)  # move into leaf state "compo_0.shape_comp.shape"
      elif dest==10:
        if history_type!=2 or self.check_history(9):
          if history_type!=2 or self.check_history(10):
            self.recordEnteredState(10)
            self.addInState(10)  # move into leaf state "compo_0.shape_comp.shape"
    elif common==10:
      if dest==10:
        if history_type!=2 or self.check_history(10):
          self.addInState(10)  # move into leaf state "compo_0.shape_comp.shape"
    elif common==11:
      if dest==11:
        if history_type!=2 or self.check_history(11):
          if history_type!=2 or self.check_history(12):
            self.recordEnteredState(12)
            self.addInState(12)  # move into leaf state "compo_0.up_comp.blowUp"
      elif dest==12:
        if history_type!=2 or self.check_history(11):
          if history_type!=2 or self.check_history(12):
            self.recordEnteredState(12)
            self.addInState(12)  # move into leaf state "compo_0.up_comp.blowUp"
    elif common==12:
      if dest==12:
        if history_type!=2 or self.check_history(12):
          self.addInState(12)  # move into leaf state "compo_0.up_comp.blowUp"
    elif common==13:
      if dest==13:
        if history_type!=2 or self.check_history(13):
          self.addInState(13)  # move into leaf state "init"

  def removeOutStates(self, common_state):

    s=self.state
    prev=None
    while s!=None:
      if self.isParent(common_state, s.StateID):
        if prev==None:
          self.state=self.state.Next
        else:
          prev.Next=s.Next
      else:
        prev=s
      s=s.Next

  def eventStr2Int(self, event):

    for i in range(7):
      if event==ballCSULR_MDL.EventNames[i]:
        return i
    return -1

  def stateInt2Str(self, state):

    if state==-1:
      return ""
    else:
      return ballCSULR_MDL.StateNames[state]

  def getCurrentStateList(self):

    sl=StringList()
    slend=sl
    s=self.state
    while s!=None:
      sm=self.Submodels[s.StateID]
      curstate=self.stateInt2Str(s.StateID)
      if sm!=None:
        slend.Next=sm.getCurrentStateList()
        while slend.Next!=None:
          slend.Next.str=curstate+"."+slend.Next.str
          slend=slend.Next
      else:
        slend.Next=StringList(curstate)
        slend=slend.Next
      s=s.Next
    return sl.Next

  def getCurrentState(self, states=None):

    if states==None:
      states=self.getCurrentStateList()
      if states!=None:
        states=states.sort()
        strst="['%s'%s]" % (states.str, self.getCurrentState(states))
      else:
        strst="[]"
    else:
      if states.Next:
        strst=", '%s'%s" % (states.Next.str, self.getCurrentState(states.Next))
      else:
        strst=""
    return strst

  def getParentState(self, state):

    return ballCSULR_MDL.ParentTable[state]

  def getSubstates(self, state):

    substates=None
    if state==-1: # substates of ""
      # add substate "compo_0"
      st=IntList()
      st.int=0
      st.Next=substates
      substates=st
      # add substate "init"
      st=IntList()
      st.int=13
      st.Next=substates
      substates=st
    elif state==0: # substates of "compo_0"
      # add substate "compo_0.color_comp"
      st=IntList()
      st.int=1
      st.Next=substates
      substates=st
      # add substate "compo_0.left_comp"
      st=IntList()
      st.int=3
      st.Next=substates
      substates=st
      # add substate "compo_0.move_comp"
      st=IntList()
      st.int=5
      st.Next=substates
      substates=st
      # add substate "compo_0.right_comp"
      st=IntList()
      st.int=7
      st.Next=substates
      substates=st
      # add substate "compo_0.shape_comp"
      st=IntList()
      st.int=9
      st.Next=substates
      substates=st
      # add substate "compo_0.up_comp"
      st=IntList()
      st.int=11
      st.Next=substates
      substates=st
    elif state==1: # substates of "compo_0.color_comp"
      # add substate "compo_0.color_comp.color"
      st=IntList()
      st.int=2
      st.Next=substates
      substates=st
    elif state==2: # substates of "compo_0.color_comp.color"
      pass
    elif state==3: # substates of "compo_0.left_comp"
      # add substate "compo_0.left_comp.blowLeft"
      st=IntList()
      st.int=4
      st.Next=substates
      substates=st
    elif state==4: # substates of "compo_0.left_comp.blowLeft"
      pass
    elif state==5: # substates of "compo_0.move_comp"
      # add substate "compo_0.move_comp.moving"
      st=IntList()
      st.int=6
      st.Next=substates
      substates=st
    elif state==6: # substates of "compo_0.move_comp.moving"
      pass
    elif state==7: # substates of "compo_0.right_comp"
      # add substate "compo_0.right_comp.blowRight"
      st=IntList()
      st.int=8
      st.Next=substates
      substates=st
    elif state==8: # substates of "compo_0.right_comp.blowRight"
      pass
    elif state==9: # substates of "compo_0.shape_comp"
      # add substate "compo_0.shape_comp.shape"
      st=IntList()
      st.int=10
      st.Next=substates
      substates=st
    elif state==10: # substates of "compo_0.shape_comp.shape"
      pass
    elif state==11: # substates of "compo_0.up_comp"
      # add substate "compo_0.up_comp.blowUp"
      st=IntList()
      st.int=12
      st.Next=substates
      substates=st
    elif state==12: # substates of "compo_0.up_comp.blowUp"
      pass
    elif state==13: # substates of "init"
      pass
    return substates

  def isHistoryState(self, state):

    return ballCSULR_MDL.HistoryStateTable[state]>0

  def isLeafState(self, state):

    if isinstance(state, int):
      return ballCSULR_MDL.LeafStateTable[state]!=None

    elif isinstance(state, str):
      for i in range(ballCSULR_MDL.StateNum):
        if ballCSULR_MDL.LeafStateTable[i]==None:
          continue
        if state==ballCSULR_MDL.LeafStateTable[i] and self.Submodels[i]==None:
          return 1
        elif startsWith(state, ballCSULR_MDL.LeafStateTable[i]+".") and self.Submodels[i]!=None:
          SubmodelState=state[ballCSULR_MDL.LeafStateTable[i].length()+1:]
          return self.Submodels[i].isLeafState(SubmodelState)
    return 0

  def isHistoryUp2Date(self, state, time):

    for i in range(ballCSULR_MDL.StateNum):
      if self.history[state].Times[i]>=time:
        return 1
    return 0

  def mergeHistory(self, state, states, times):

    max=-1
    for i in range(ballCSULR_MDL.StateNum):
      if times[i]>max:
        max=times[i]
    if self.isHistoryUp2Date(state, max):
      for i in range(ballCSULR_MDL.StateNum):
        if times[i]>self.history[state].Times[i]:
          self.history[state].States[i]=states[i]
          self.history[state].Times[i]=times[i]
    else:
      self.history[state].States=copy.copy(states)
      self.history[state].Times=copy.copy(times)

  def recordHistory(self, top_state):

    curtime=self.HistoryCount
    self.HistoryCount=self.HistoryCount+1
    s=self.state
    while s!=None:
      child=s.StateID
      states=[]
      times=[]
      for i in range(ballCSULR_MDL.StateNum):
        states.append(-1)
        times.append(-1)
      states[child]=child
      times[child]=curtime
      if top_state<0 or self.isParent(top_state, child):
        parent=self.getParentState(child)
        if self.isHistoryState(child):
          self.history[child].Submodel=self.Submodels[child]
        while parent>=0 and times[parent]!=curtime:
          states[parent]=child
          times[parent]=curtime
          if self.isHistoryState(parent):
            self.mergeHistory(parent, states, times)
          if parent==top_state:
            break
          child=parent
          parent=self.getParentState(child)
      s=s.Next

  def hasHistoryRecorded(self, state):

    for i in range(ballCSULR_MDL.StateNum):
      if self.history[state].States[i]!=-1:
        return 1
      if self.Submodels[state]!=None:
        return 1
    return 0

  def hasOrthogonalStateInBetween(self, parent, leaf):

    return ballCSULR_MDL.OrthogonalInBetween[parent+1][leaf]

  def check_history(self, dest):

    s=self.state
    while s!=None:
      if self.isParent(dest, s.StateID) and not self.hasOrthogonalStateInBetween(dest, s.StateID):
        return 0
      s=s.Next
    return 1

  def getEnabledEvents(self):

    events=EventList()
    if self.isInState(6):
      events.Append("__INTERNAL_0_TIME_0")
    if self.isInState(2):
      events.Append("__INTERNAL_0_TIME_1")
    if self.isInState(10):
      events.Append("__INTERNAL_0_TIME_2")
    if self.isInState(12):
      events.Append("__INTERNAL_0_TIME_3")
    if self.isInState(8):
      events.Append("__INTERNAL_0_TIME_4")
    if self.isInState(4):
      events.Append("__INTERNAL_0_TIME_5")
    if self.isInState(13):
      events.Append("start")
    return events.Next

  def getHierarchy(self, start_level, state_prefix):

    h=Hierarchy()
    lasth=h
    # Generate state "compo_0" in the hierarchy table
    lasth.Next=Hierarchy()
    lasth.Next.StateName="compo_0"
    if state_prefix==None:
      lasth.Next.PathName="compo_0"
    else:
      lasth.Next.PathName=state_prefix+".compo_0"
    lasth.Next.StateNum=0
    lasth.Next.Level=start_level+0
    lasth=lasth.Next
    # Generate state "compo_0.color_comp" in the hierarchy table
    lasth.Next=Hierarchy()
    lasth.Next.StateName="color_comp"
    if state_prefix==None:
      lasth.Next.PathName="compo_0.color_comp"
    else:
      lasth.Next.PathName=state_prefix+".compo_0.color_comp"
    lasth.Next.StateNum=1
    lasth.Next.Level=start_level+1
    lasth=lasth.Next
    # Generate state "compo_0.color_comp.color" in the hierarchy table
    lasth.Next=Hierarchy()
    lasth.Next.StateName="color"
    if state_prefix==None:
      lasth.Next.PathName="compo_0.color_comp.color"
    else:
      lasth.Next.PathName=state_prefix+".compo_0.color_comp.color"
    lasth.Next.StateNum=2
    lasth.Next.Level=start_level+2
    lasth=lasth.Next
    # Generate state "compo_0.left_comp" in the hierarchy table
    lasth.Next=Hierarchy()
    lasth.Next.StateName="left_comp"
    if state_prefix==None:
      lasth.Next.PathName="compo_0.left_comp"
    else:
      lasth.Next.PathName=state_prefix+".compo_0.left_comp"
    lasth.Next.StateNum=3
    lasth.Next.Level=start_level+1
    lasth=lasth.Next
    # Generate state "compo_0.left_comp.blowLeft" in the hierarchy table
    lasth.Next=Hierarchy()
    lasth.Next.StateName="blowLeft"
    if state_prefix==None:
      lasth.Next.PathName="compo_0.left_comp.blowLeft"
    else:
      lasth.Next.PathName=state_prefix+".compo_0.left_comp.blowLeft"
    lasth.Next.StateNum=4
    lasth.Next.Level=start_level+2
    lasth=lasth.Next
    # Generate state "compo_0.move_comp" in the hierarchy table
    lasth.Next=Hierarchy()
    lasth.Next.StateName="move_comp"
    if state_prefix==None:
      lasth.Next.PathName="compo_0.move_comp"
    else:
      lasth.Next.PathName=state_prefix+".compo_0.move_comp"
    lasth.Next.StateNum=5
    lasth.Next.Level=start_level+1
    lasth=lasth.Next
    # Generate state "compo_0.move_comp.moving" in the hierarchy table
    lasth.Next=Hierarchy()
    lasth.Next.StateName="moving"
    if state_prefix==None:
      lasth.Next.PathName="compo_0.move_comp.moving"
    else:
      lasth.Next.PathName=state_prefix+".compo_0.move_comp.moving"
    lasth.Next.StateNum=6
    lasth.Next.Level=start_level+2
    lasth=lasth.Next
    # Generate state "compo_0.right_comp" in the hierarchy table
    lasth.Next=Hierarchy()
    lasth.Next.StateName="right_comp"
    if state_prefix==None:
      lasth.Next.PathName="compo_0.right_comp"
    else:
      lasth.Next.PathName=state_prefix+".compo_0.right_comp"
    lasth.Next.StateNum=7
    lasth.Next.Level=start_level+1
    lasth=lasth.Next
    # Generate state "compo_0.right_comp.blowRight" in the hierarchy table
    lasth.Next=Hierarchy()
    lasth.Next.StateName="blowRight"
    if state_prefix==None:
      lasth.Next.PathName="compo_0.right_comp.blowRight"
    else:
      lasth.Next.PathName=state_prefix+".compo_0.right_comp.blowRight"
    lasth.Next.StateNum=8
    lasth.Next.Level=start_level+2
    lasth=lasth.Next
    # Generate state "compo_0.shape_comp" in the hierarchy table
    lasth.Next=Hierarchy()
    lasth.Next.StateName="shape_comp"
    if state_prefix==None:
      lasth.Next.PathName="compo_0.shape_comp"
    else:
      lasth.Next.PathName=state_prefix+".compo_0.shape_comp"
    lasth.Next.StateNum=9
    lasth.Next.Level=start_level+1
    lasth=lasth.Next
    # Generate state "compo_0.shape_comp.shape" in the hierarchy table
    lasth.Next=Hierarchy()
    lasth.Next.StateName="shape"
    if state_prefix==None:
      lasth.Next.PathName="compo_0.shape_comp.shape"
    else:
      lasth.Next.PathName=state_prefix+".compo_0.shape_comp.shape"
    lasth.Next.StateNum=10
    lasth.Next.Level=start_level+2
    lasth=lasth.Next
    # Generate state "compo_0.up_comp" in the hierarchy table
    lasth.Next=Hierarchy()
    lasth.Next.StateName="up_comp"
    if state_prefix==None:
      lasth.Next.PathName="compo_0.up_comp"
    else:
      lasth.Next.PathName=state_prefix+".compo_0.up_comp"
    lasth.Next.StateNum=11
    lasth.Next.Level=start_level+1
    lasth=lasth.Next
    # Generate state "compo_0.up_comp.blowUp" in the hierarchy table
    lasth.Next=Hierarchy()
    lasth.Next.StateName="blowUp"
    if state_prefix==None:
      lasth.Next.PathName="compo_0.up_comp.blowUp"
    else:
      lasth.Next.PathName=state_prefix+".compo_0.up_comp.blowUp"
    lasth.Next.StateNum=12
    lasth.Next.Level=start_level+2
    lasth=lasth.Next
    # Generate state "init" in the hierarchy table
    lasth.Next=Hierarchy()
    lasth.Next.StateName="init"
    if state_prefix==None:
      lasth.Next.PathName="init"
    else:
      lasth.Next.PathName=state_prefix+".init"
    lasth.Next.StateNum=13
    lasth.Next.Level=start_level+0
    lasth=lasth.Next
    return h.Next

  def topLevelHistory(self):

    s=self.state.StateID
    t=self.getParentState(s)
    while t!=-1:
      s=t
      t=self.getParentState(s)
    self.changeState(s, s)

  def runActionCode(self, code_num):

    if code_num==0: # model finalizer
      pass
    elif code_num==1: # model initializer
      pass
    elif code_num==2: # output action(s) of a transition
      self.runCode("for b in balls: ctl.bounce(b, 0.01)")
      # repeated timed transition
      self.addSchedule(6, ballCSULR_MDL.IntervalTable[0], ballCSULR_MDL.EventNames[0], self)
    elif code_num==3: # output action(s) of a transition
      self.runCode("for b in balls: ctl.changeColor(b)")
      # repeated timed transition
      self.addSchedule(2, ballCSULR_MDL.IntervalTable[1], ballCSULR_MDL.EventNames[1], self)
    elif code_num==4: # output action(s) of a transition
      self.runCode("for b in balls: ctl.changeShape(b)")
      # repeated timed transition
      self.addSchedule(10, ballCSULR_MDL.IntervalTable[2], ballCSULR_MDL.EventNames[2], self)
    elif code_num==5: # output action(s) of a transition
      self.runCode("for b in balls: ctl.blowUp(b)")
      # repeated timed transition
      self.addSchedule(12, ballCSULR_MDL.IntervalTable[3], ballCSULR_MDL.EventNames[3], self)
    elif code_num==6: # output action(s) of a transition
      self.runCode("for b in balls: ctl.blowRight(b)")
      # repeated timed transition
      self.addSchedule(8, ballCSULR_MDL.IntervalTable[4], ballCSULR_MDL.EventNames[4], self)
    elif code_num==7: # output action(s) of a transition
      self.runCode("for b in balls: ctl.blowLeft(b)")
      # repeated timed transition
      self.addSchedule(4, ballCSULR_MDL.IntervalTable[5], ballCSULR_MDL.EventNames[5], self)
    elif code_num==8: # output action(s) of a transition
      self.runCode("ctl = eventhandler.get_event_params()")
      self.runCode("bid = counter")
      self.runCode("counter += 1")
      self.runCode("gid, pos, speed = ctl.init(bid)")
      self.runCode("balls[bid] = {\'pos\': pos, \'speed\': speed, \'gid\':gid}")
      self.runCode("width = ctl.getWidth()")
      self.runCode("height = ctl.getHeight()")
    elif code_num==9: # enter actions for state "compo_0.right_comp.blowRight"
      # a timed transition
      self.addSchedule(8, ballCSULR_MDL.IntervalTable[4], ballCSULR_MDL.EventNames[4], self)
    elif code_num==10: # enter actions for state "compo_0.color_comp.color"
      # a timed transition
      self.addSchedule(2, ballCSULR_MDL.IntervalTable[1], ballCSULR_MDL.EventNames[1], self)
    elif code_num==11: # enter actions for state "compo_0.left_comp.blowLeft"
      # a timed transition
      self.addSchedule(4, ballCSULR_MDL.IntervalTable[5], ballCSULR_MDL.EventNames[5], self)
    elif code_num==12: # enter actions for state "compo_0.shape_comp.shape"
      # a timed transition
      self.addSchedule(10, ballCSULR_MDL.IntervalTable[2], ballCSULR_MDL.EventNames[2], self)
    elif code_num==13: # enter actions for state "init"
      self.runCode("counter = 0")
      self.runCode("balls = {}")
    elif code_num==14: # enter actions for state "compo_0.up_comp.blowUp"
      # a timed transition
      self.addSchedule(12, ballCSULR_MDL.IntervalTable[3], ballCSULR_MDL.EventNames[3], self)
    elif code_num==15: # enter actions for state "compo_0.move_comp.moving"
      # a timed transition
      self.addSchedule(6, ballCSULR_MDL.IntervalTable[0], ballCSULR_MDL.EventNames[0], self)
    elif code_num==16: # exit actions for state "compo_0.right_comp.blowRight"
      # clean up timed transitions
      self.removeSchedule(8, self)
    elif code_num==17: # exit actions for state "compo_0.color_comp.color"
      # clean up timed transitions
      self.removeSchedule(2, self)
    elif code_num==18: # exit actions for state "compo_0.left_comp.blowLeft"
      # clean up timed transitions
      self.removeSchedule(4, self)
    elif code_num==19: # exit actions for state "compo_0.shape_comp.shape"
      # clean up timed transitions
      self.removeSchedule(10, self)
    elif code_num==20: # exit actions for state "compo_0.up_comp.blowUp"
      # clean up timed transitions
      self.removeSchedule(12, self)
    elif code_num==21: # exit actions for state "compo_0.move_comp.moving"
      # clean up timed transitions
      self.removeSchedule(6, self)

  def testCondition(self, cond_num):

    if cond_num==0 and \
       eval("(1)", self.DefaultInterpreter.locals):
      return 1
    elif cond_num==1 and \
       eval("(1)", self.DefaultInterpreter.locals):
      return 1
    elif cond_num==2 and \
       eval("(1)", self.DefaultInterpreter.locals):
      return 1
    elif cond_num==3 and \
       eval("(1)", self.DefaultInterpreter.locals):
      return 1
    elif cond_num==4 and \
       eval("(1)", self.DefaultInterpreter.locals):
      return 1
    elif cond_num==5 and \
       eval("(1)", self.DefaultInterpreter.locals):
      return 1
    elif cond_num==6 and \
       eval("(1)", self.DefaultInterpreter.locals):
      return 1
    return 0

  def runEnterActions(self, state):

    if state==8: # enter action(s) for state "compo_0.right_comp.blowRight"
      self.runActionCode(9)
    elif state==2: # enter action(s) for state "compo_0.color_comp.color"
      self.runActionCode(10)
    elif state==4: # enter action(s) for state "compo_0.left_comp.blowLeft"
      self.runActionCode(11)
    elif state==10: # enter action(s) for state "compo_0.shape_comp.shape"
      self.runActionCode(12)
    elif state==13: # enter action(s) for state "init"
      self.runActionCode(13)
    elif state==12: # enter action(s) for state "compo_0.up_comp.blowUp"
      self.runActionCode(14)
    elif state==6: # enter action(s) for state "compo_0.move_comp.moving"
      self.runActionCode(15)

  def runExitActions(self, state):

    if state==8: # exit action(s) for state "compo_0.right_comp.blowRight"
      self.runActionCode(16)
    elif state==2: # exit action(s) for state "compo_0.color_comp.color"
      self.runActionCode(17)
    elif state==4: # exit action(s) for state "compo_0.left_comp.blowLeft"
      self.runActionCode(18)
    elif state==10: # exit action(s) for state "compo_0.shape_comp.shape"
      self.runActionCode(19)
    elif state==12: # exit action(s) for state "compo_0.up_comp.blowUp"
      self.runActionCode(20)
    elif state==6: # exit action(s) for state "compo_0.move_comp.moving"
      self.runActionCode(21)

  def compareSchedule(self, sched_a, sched_b):

    return cmp(sched_a[1], sched_b[1])

  def addSchedule(self, id, interval, event, scheduler):

    if self.Parent!=None:  # Non-top-level model
      self.Parent.addSchedule(id, interval, event, scheduler)
      return
    f=eval(interval, self.DefaultInterpreter.locals)
    ##self.SchedulerCond.acquire()
    t=time.time()+f
    s=[id, t, interval, event, scheduler]
    self.Schedules.append(s)
    self.Schedules.sort(self.compareSchedule)
    ##self.SchedulerCond.notify()
    ##self.SchedulerCond.release()

  def removeSchedule(self, id, scheduler):

    if self.Parent!=None:  # Non-top-level model
      self.Parent.removeSchedule(id, scheduler)
      return
    ##self.SchedulerCond.acquire()
    i=0
    while i<len(self.Schedules):
      if self.Schedules[i][0]==id and self.Schedules[i][4]==scheduler:
        del self.Schedules[i]
      else:
        i=i+1
    ##self.SchedulerCond.release()

  def scheduler(self):

    if( not self.Schedules): return

    while( self.Schedules and self.Schedules[0][1]<=time.time()):
      this_sched=self.Schedules[0]
      del self.Schedules[0]
      self.event(this_sched[3], [], None, this_sched[4])

    if self.Schedules:
      t=self.Schedules[0][1]-time.time()

  def recordAllEnteredStates(self):

    st=self.state
    while st!=None:
      self.recordEnteredState(st.StateID, 1, 1)
      st=st.Next

  def recordEnteredState(self, s, superstates=0, submodel=0, commonstate=-1):

    # test if s is already recorded
    se=self.StatesEntered
    found=0
    while se!=None:
      if se.int==s:
        found=1
        break
      se=se.Next

    if not found:
      if superstates:
        parent=self.getParentState(s)
        if parent>=0 and parent!=commonstate:
          self.recordEnteredState(parent, 1, 0, commonstate)
      st=IntList()
      st.Next=self.StatesEntered
      st.int=s
      self.StatesEntered=st
      if submodel and self.Submodels[s]:
        self.Submodels[s].recordAllEnteredStates()

  def runAllEnterActions(self):

    self.runEnterActionsForStates(self.StatesEntered, 1)

  def runEnterActionsForStates(self, states, recursive=0):

    if states:
      self.runEnterActionsForStates(states.Next, 0)
      self.runEnterActions(states.int)
    if recursive:
      for s in self.Submodels:
        if s:
          s.runAllEnterActions()

  def runExitActionsForStates(self, common_state):

    substates=self.getSubstates(common_state)
    if substates==None:
      s=self.state
      while s!=None and s.StateID!=common_state:
        s=s.Next
      if s!=None and self.Submodels[s.StateID]:
        self.Submodels[s.StateID].runExitActionsForStates(-1)
      return s!=None
    else:
      has_current_substate=0
      while substates!=None:
        res=self.runExitActionsForStates(substates.int)
        has_current_substate=has_current_substate or res
        if res:
          self.runExitActions(substates.int)
        substates=substates.Next
      return has_current_substate

  def runInitializer(self):

    self.runActionCode(1)
    for s in self.Submodels:
      if s:
        s.runInitializer()

  def runFinalizer(self):

    if self.Started:
      self.Started=0
      for s in self.Submodels:
        if s:
          s.runFinalizer()
      self.runActionCode(0)
      if self.Parent==None:  # Top-level model
        ##self.EventsCond.acquire()
        ##self.SchedulerCond.acquire()
        self.Stopped=1
        ##self.EventsCond.notify()
        ##self.SchedulerCond.notify()
        ##self.SchedulerCond.release()
        ##self.EventsCond.release()
      else:
        self.Stopped=1

  def clearEnteredStates(self):

    self.StatesEntered=None
    for s in self.Submodels:
      if s:
        s.clearEnteredStates()

  def get_current_state(self):

    return self.getCurrentState()

  def runCode(self, c):

    self.DefaultInterpreter.runsource(c+"\n", "<input>", "exec")

  def setupInterpreter(self):

    self.DefaultInterpreter.locals["eventhandler"]=self
    self.DefaultInterpreter.locals["dump_message"]=self.dump_message

  def get_event_params(self):

    return self.params

  def dump_message(self, msg):

    print msg

  def is_in_state(self, s, check_substate=1):

    return self.isInState(s, check_substate)

  def start(self, lock=None, run_enter_actions=1, TkInstance = None):

    if self.Parent==None:  # Top-level model
      if run_enter_actions:
        self.runEnterActionsForStates(self.StatesEntered, 1)
      self.Started=1
      
      if( TkInstance ):
        self.sleepMethod = TkInstance.after 
        startTkMainloop = False
      else:
        import Tkinter
        TkInstance = Tkinter.Tk()
        self.sleepMethod = TkInstance.after 
        startTkMainloop = True
      
      self.pollingStation() 
      ##thread.start_new_thread(self.handleEvent_wrapper, ())
      ##thread.start_new_thread(self.scheduler, ())
      ## if lock:
        ## lock.release()
    else:
      self.Started=1
    for submodel in self.Submodels:
      if submodel!=None:
        submodel.start()

    if( self.Parent==None and startTkMainloop == None ): TkInstance.mainloop()

  def pollingStation(self):

    self.sleepMethod( 100, self.pollingStation )
    if self.Stopped: return 
    #self.handleEvent_wrapper()
    self.scheduler()
    #todo: pollingStation\,
  def shutdown(self):

    pass

  def handleEvent_wrapper(self):

    if( self.PendingEvents==None ): return
    event=self.PendingEvents
    self.PendingEvents=self.PendingEvents.Next
    if self.PendingEvents==None:
      self.PendingEventsTail=None
    event.next=None
    self.handleEvent(event.Event[0], event.Event[1], event.Event[2], event.Event[3])
    self.handleEvent_wrapper()

  def event(self, e, params=[], cond=None, scheduler=None):

    ev=EventList()
    ev.Event=[e, params, cond, scheduler]
    if self.PendingEventsTail!=None:
      self.PendingEventsTail.Next=ev
    else:
      self.PendingEvents=ev
    self.PendingEventsTail=ev
    self.handleEvent_wrapper()


# main
if __name__=="__main__":
  ballCSULR_MDL.main(sys.argv)