Main Page   Packages   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

Automaton.java

00001 package gov.nasa.arc.ase.ltl;
00002 
00003 // Written by Dimitra Giannakopoulou, 19 Jan 2001
00004 import java.util.*;
00005 import gov.nasa.arc.ase.util.graph.*;
00006 
00007 class Automaton
00008 {
00009 
00010   private LinkNode head;
00011   private LinkNode tail;
00012   private Node[] equivalence_classes; // array of representatives of equivalent states
00013 
00014   public Automaton ()
00015   {
00016     head = tail = null;
00017     equivalence_classes = null;
00018   }  
00019   public void add (Node nd)
00020   {
00021     LinkNode newNode = new LinkNode(nd, null);
00022     if (head == null) // set is currently empty
00023       head = tail = newNode;
00024     else // put element at end of list
00025     {
00026       tail.LinkWith(newNode);
00027       tail = newNode;
00028     }
00029   }  
00030   public Node alreadyThere (Node nd)
00031   {
00032     // is already there if old and next fields are the same
00033 
00034     LinkNode nextNd = head;
00035     while (nextNd != null)
00036     {
00037       Node currState = nextNd.getNode();
00038 
00039       if (currState.getField_next().equals(nd.getField_next())
00040       &&  currState.compare_accepting(nd))
00041       {
00042     //System.out.println("Match found");
00043     return currState;
00044       }
00045       else
00046     nextNd = nextNd.getNext();
00047 
00048     }
00049 
00050     //System.out.println("No match found for node " + nd.getNodeId());
00051     return null;
00052   }  
00053   public static void FSPoutput(State[] automaton)
00054   {
00055     boolean comma = false;
00056     if (automaton == null)
00057     {
00058       System.out.println("\n\nRES = STOP.");
00059       return;
00060     }
00061     else
00062       System.out.println("\n\nRES = S0,");
00063 
00064     int size = Pool.assign();
00065     for (int i = 0; i < size; i++)
00066     {
00067       if ((automaton[i] != null) && (i == automaton[i].get_representativeId())) // a representative so print
00068       {
00069     if (comma) System.out.println("),");
00070     comma = true;
00071     System.out.print("S" + automaton[i].get_representativeId());
00072     System.out.print("=");
00073     automaton[i].FSPoutput();
00074       }
00075     }
00076     System.out.println(").\n");
00077   }  
00078   /*    public int get_representative_id(int automaton_index, State[] automaton)
00079     {
00080     return equivalence_classes[automaton[automaton_index].get_equivalence_class()].getNodeId();
00081     }
00082    */
00083 
00084   public int index_equivalence (Node nd)
00085   {
00086     // check if next field of node is already represented
00087 
00088     int index;
00089 
00090     for (index=0; index < Pool.assign(); index++)
00091     {
00092       if (equivalence_classes[index] == null)
00093       {
00094     //  System.out.println("Null object");
00095     break;
00096       }
00097       else if (equivalence_classes[index].getField_next().equals(nd.getField_next()))
00098       {
00099     //  System.out.println("Successful merge");
00100     return(equivalence_classes[index].getNodeId());
00101       }
00102     }
00103 
00104     if (index == Pool.assign())
00105       System.out.println("ERROR - size of equivalence classes array was incorrect");
00106     equivalence_classes[index] = nd;
00107     return (equivalence_classes[index].getNodeId());
00108   }  
00109   public static Graph SMoutput(State[] automaton)
00110   {
00111     Graph g = new Graph();
00112     g.setStringAttribute("type", "gba");
00113     g.setStringAttribute("ac", "edges");
00114 
00115     if (automaton == null)
00116       return g;
00117 
00118     int size = Pool.assign();
00119     gov.nasa.arc.ase.util.graph.Node[] nodes = new gov.nasa.arc.ase.util.graph.Node[size];
00120     for(int i = 0; i < size; i++) {
00121       if ((automaton[i] != null) &&
00122       (i == automaton[i].get_representativeId())) {
00123     nodes[i] = new gov.nasa.arc.ase.util.graph.Node(g);
00124     nodes[i].setStringAttribute("label",
00125         "S"+automaton[i].get_representativeId());
00126       }
00127     }
00128 
00129     for(int i = 0; i < size; i++)
00130       if ((automaton[i] != null) &&
00131       (i == automaton[i].get_representativeId()))
00132     automaton[i].SMoutput(nodes, nodes[i]);
00133 
00134     if(Node.accepting_conds == 0)
00135       g.setIntAttribute("nsets", 1);
00136     else
00137       g.setIntAttribute("nsets", Node.accepting_conds);
00138 
00139     return g;
00140   }  
00141   public State[] structForRuntAnalysis()
00142   {
00143     // now also fixes equivalence classes
00144     Pool.stop();
00145     int automatonSize = Pool.assign();
00146     State[] RTstruct = new State[automatonSize];
00147     equivalence_classes = new Node[automatonSize];
00148 
00149     if (head == null) return RTstruct;
00150 
00151     LinkNode nextNd = head;
00152     Node current;
00153 
00154     while (nextNd != null)
00155     {
00156       current = nextNd.getNode();
00157       current.set_equivalenceId(index_equivalence(current));
00158       nextNd.getNode().RTstructure(RTstruct);
00159       nextNd = nextNd.getNext();
00160     }
00161 
00162     return RTstruct;
00163   }  
00164 }

Generated at Thu Feb 7 06:40:22 2002 for Bandera by doxygen1.2.10 written by Dimitri van Heesch, © 1997-2001