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 degenSynchronousProduct {
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
00036
00037 int n1id = n1.getIntAttribute("lower_bound");
00038
00039
00040 if (i >= n1id)
00041 {
00042
00043 boolean b0 = e0.getBooleanAttribute("acc"+i);
00044 boolean b1 = e1.getBooleanAttribute("acc"+i);
00045
00046 if(b0 != b1) {
00047 found = false;
00048 break;
00049 }
00050 }
00051 }
00052 }
00053
00054 if(found) theEdge = e1;
00055 }
00056
00057 if(theEdge != null) {
00058 Node next1 = theEdge.getNext();
00059 boolean newNext = isNew(nodes, next0, next1);
00060 Node next = get(g, nodes, next0, next1);
00061 Edge e = new Edge(n, next, e0.getGuard(), theEdge.getAction(), null);
00062
00063 if(newNext)
00064 dfs(g, nodes, nsets, next0, next1);
00065 }
00066 }
00067 }
00068 private static Node get(Graph g, Node[][] nodes, Node n0, Node n1) {
00069 if(nodes[n0.getId()][n1.getId()] == null) {
00070 Node n = new Node(g);
00071 String label0 = n0.getStringAttribute("label");
00072 String label1 = n1.getStringAttribute("label");
00073 if(label0 == null) label0 = Integer.toString(n0.getId());
00074 if(label1 == null) label1 = Integer.toString(n1.getId());
00075 n.setStringAttribute(
00076 "label",
00077 label0 +
00078 "+" +
00079 label1);
00080 if(n1.getBooleanAttribute("accepting"))
00081 n.setBooleanAttribute("accepting", true);
00082
00083 return nodes[n0.getId()][n1.getId()] = n;
00084 }
00085
00086 return nodes[n0.getId()][n1.getId()];
00087 }
00088 private static boolean isNew(Node[][] nodes, Node n0, Node n1) {
00089 return nodes[n0.getId()][n1.getId()] == null;
00090 }
00091 public static void main(String[] args) {
00092 Graph g0, g1;
00093 try {
00094 g0 = Graph.load(args[0]);
00095 g1 = Graph.load(args[1]);
00096 } catch(IOException e) {
00097 System.err.println("Can't load automata");
00098 System.exit(1);
00099 return;
00100 }
00101
00102 Graph g = product(g0, g1);
00103
00104 g.save();
00105 }
00106 public static Graph product(Graph g0, Graph g1) {
00107 int nsets = g0.getIntAttribute("nsets");
00108
00109 if(nsets != g1.getIntAttribute("nsets")) {
00110 System.err.println("Different number of accepting sets");
00111 System.exit(1);
00112 }
00113
00114 Node[][] nodes;
00115 Graph g = new Graph();
00116 g.setStringAttribute("type", "ba");
00117 g.setStringAttribute("ac", "nodes");
00118
00119 nodes = new Node[g0.getNodeCount()][g1.getNodeCount()];
00120
00121 dfs(g, nodes, nsets, g0.getInit(), g1.getInit());
00122
00123 return g;
00124 }
00125 }