from MetaEditAPI_services import *
from LocateInitial import *
from Transition import *
import sys

def main():
    listNodes = getGraph()
    li = LocateInitial()
    tr = Transition()
    listNodes = li.execute(listNodes)

    while(True):
        readLine = raw_input("Enter the transition input: ")

        listNodes = tr.execute(listNodes, readLine)
        if ~readLine.find("q") :
            print "Exiting Simulation...."
            break

def getGraph():
    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)

    """
        Grabs all FSAStates that are drawn in the graph
        To grab the relationships/connections between states use MEOopArray relationshipSet(MEOop receiver)
    """
    objsr = objectSetRequest()
    objsr._receiver = graph._result[0]
    listOfObjs = port.objectSet(objsr)

    relsr = relationshipSetRequest()
    relsr._receiver = graph._result[0]
    listOfRels = port.relationshipSet(relsr)

    """
        Creates ATOM3 style graph
    """
    listNodes = {}
    listNodes['State'] = []
    listNodes['Transition'] = []

    for i in range(0, len(listOfObjs._result)):
        listNodes['State'].append(listOfObjs._result[i])

    for i in range(0, len(listOfRels._result)):
        listNodes['Transition'].append(listOfRels._result[i])

    return listNodes

if __name__ == "__main__":
    main()