from MetaEditAPI_services import *
import sys

class LocateInitial:
  def __init__(self, name='Locateinitial'):
    self.name = name

  def __str__(self):
    return self.name

  def execute(self, listNodes):
    service = MetaEditAPILocator()
    kw = { 'tracefile' : sys.stdout }
    port = service.getMetaEditAPIPortType(**kw)

    graphType = ns0.METype_Def("FSA")
    graphType._name = "FiniteStateAutomata"

    agir = allGoodInstancesRequest()
    agir._receiver = graphType
    graph = port.allGoodInstances(agir)

    """
        Establishes the graph's initial state
    """
    index = 0
    for i in range(0, len(listNodes['State'])):
        request = valueAtRequest()
        request._receiver = listNodes['State'][i]
        request._valueAt = 2
        zxc = port.valueAt(request)

        if(zxc._result._meValue == "true"):
            index = i
            break

    """
        Animates the current state
    """
    animate = animateRequest()
    animate._receiver = graph._result[0]
    animate._animate = listNodes['State'][index]
    listNodes['Current'] = listNodes['State'][index]
    animateResult = port.animate(animate)
    print "Animate State %s" % listNodes['Current']._objectID

    return listNodes

