00001 package gov.nasa.arc.ase.util.graph;
00002
00003 import java.io.IOException;
00004
00005
00006
00007
00008 import java.util.Iterator;
00009 import java.util.List;
00010
00011
00012 public class SynchronousProduct {
00013 public static void dfs(Graph g, Node[][] nodes, int nsets, Node n0, Node n1) {
00014 Node n = get(g, nodes, n0, n1);
00015
00016 List t0 = n0.getOutgoingEdges();
00017 List t1 = n1.getOutgoingEdges();
00018
00019 for(Iterator i0 = t0.iterator(); i0.hasNext(); ) {
00020 Edge e0 = (Edge)i0.next();
00021 Node next0 = e0.getNext();
00022 Edge theEdge = null;
00023
00024 boolean found = false;
00025
00026 for(Iterator i1 = t1.iterator(); i1.hasNext() && !found; ) {
00027 Edge e1 = (Edge)i1.next();
00028
00029 if(e1.getBooleanAttribute("else")) {
00030 if(theEdge == null)
00031 theEdge = e1;
00032 } else {
00033 found = true;
00034 for(int i = 0; i < nsets; i++) {
00035 boolean b0 = e0.getBooleanAttribute("acc"+i);
00036 boolean b1 = e1.getBooleanAttribute("acc"+i);
00037
00038 if(b0 != b1) {
00039 found = false;
00040 break;
00041 }
00042 }
00043 }
00044
00045 if(found) theEdge = e1;
00046 }
00047
00048 if(theEdge != null) {
00049 Node next1 = theEdge.getNext();
00050 boolean newNext = isNew(nodes, next0, next1);
00051 Node next = get(g, nodes, next0, next1);
00052 Edge e = new Edge(n, next, e0.getGuard(), theEdge.getAction(), null);
00053
00054 if(newNext)
00055 dfs(g, nodes, nsets, next0, next1);
00056 }
00057 }
00058 }
00059 private static Node get(Graph g, Node[][] nodes, Node n0, Node n1) {
00060 if(nodes[n0.getId()][n1.getId()] == null) {
00061 Node n = new Node(g);
00062 String label0 = n0.getStringAttribute("label");
00063 String label1 = n1.getStringAttribute("label");
00064 if(label0 == null) label0 = Integer.toString(n0.getId());
00065 if(label1 == null) label1 = Integer.toString(n1.getId());
00066 n.setStringAttribute(
00067 "label",
00068 label0 +
00069 "+" +
00070 label1);
00071 if(n1.getBooleanAttribute("accepting"))
00072 n.setBooleanAttribute("accepting", true);
00073
00074 return nodes[n0.getId()][n1.getId()] = n;
00075 }
00076
00077 return nodes[n0.getId()][n1.getId()];
00078 }
00079 private static boolean isNew(Node[][] nodes, Node n0, Node n1) {
00080 return nodes[n0.getId()][n1.getId()] == null;
00081 }
00082 public static void main(String[] args) {
00083 Graph g0, g1;
00084 try {
00085 g0 = Graph.load(args[0]);
00086 g1 = Graph.load(args[1]);
00087 } catch(IOException e) {
00088 System.err.println("Can't load automata");
00089 System.exit(1);
00090 return;
00091 }
00092
00093 Graph g = product(g0, g1);
00094
00095 g.save();
00096 }
00097 public static Graph product(Graph g0, Graph g1) {
00098 int nsets = g0.getIntAttribute("nsets");
00099
00100 if(nsets != g1.getIntAttribute("nsets")) {
00101 System.err.println("Different number of accepting sets");
00102 System.exit(1);
00103 }
00104
00105 Node[][] nodes;
00106 Graph g = new Graph();
00107 g.setStringAttribute("type", "ba");
00108 g.setStringAttribute("ac", "nodes");
00109
00110 nodes = new Node[g0.getNodeCount()][g1.getNodeCount()];
00111
00112 dfs(g, nodes, nsets, g0.getInit(), g1.getInit());
00113
00114 return g;
00115 }
00116 }