00001 package gov.nasa.arc.ase.util.graph; 00002 00003 import java.io.*; 00004 import java.util.BitSet; 00005 00006 public class SuperSetReduction { 00007 private static boolean included(boolean[] a, boolean[] b) { 00008 final int al = a.length; 00009 final int bl = b.length; 00010 00011 for(int i = 0; i < al; i++) 00012 if(a[i] && !b[i]) 00013 return false; 00014 00015 return true; 00016 } 00017 public static void main(String[] args) { 00018 if(args.length > 1) { 00019 System.out.println("usage:"); 00020 System.out.println("\tjava gov.nasa.arc.ase.util.graph.SuperSetReduction [<filename>]"); 00021 return; 00022 } 00023 00024 Graph g = null; 00025 00026 try { 00027 if(args.length == 0) 00028 g = Graph.load(); 00029 else 00030 g = Graph.load(args[0]); 00031 } catch(IOException e) { 00032 System.out.println("Can't load the graph."); 00033 return; 00034 } 00035 00036 g = reduce(g); 00037 00038 g.save(); 00039 } 00040 public static Graph reduce(Graph g) { 00041 final int nsets = g.getIntAttribute("nsets"); 00042 String type = g.getStringAttribute("type"); 00043 String ac = g.getStringAttribute("ac"); 00044 00045 if(!type.equals("gba")) 00046 throw new RuntimeException("invalid graph type: " + type); 00047 00048 if(ac.equals("nodes")) { 00049 final int nnodes = g.getNodeCount(); 00050 00051 final boolean[][] asets = new boolean[nsets][nnodes]; 00052 00053 g.forAllNodes(new EmptyVisitor() { 00054 public void visitNode(Node n) { 00055 for(int i = 0; i < nsets; i++) { 00056 String acc = "acc"+i; 00057 if(n.getBooleanAttribute(acc)) { 00058 asets[i][n.getId()] = true; 00059 n.setBooleanAttribute(acc, false); 00060 } 00061 } 00062 } 00063 }); 00064 00065 boolean[] remove = new boolean[nsets]; 00066 00067 for(int i = 0; i < nsets; i++) { 00068 for(int j = 0; j < nsets && !remove[i] ; j++) { 00069 if(i != j && !remove[j]) 00070 if(included(asets[j], asets[i])) 00071 remove[i] = true; 00072 } 00073 } 00074 00075 int n_nsets = 0; 00076 for(int i = 0; i < nsets; i++) 00077 if(!remove[i]) n_nsets++; 00078 00079 boolean[][] n_asets = new boolean[n_nsets][nnodes]; 00080 00081 n_nsets = 0; 00082 for(int i = 0; i < nsets; i++) 00083 if(!remove[i]) 00084 n_asets[n_nsets++] = asets[i]; 00085 00086 g.setIntAttribute("nsets", n_nsets); 00087 00088 for(int i = 0; i < nnodes; i++) { 00089 Node n = g.getNode(i); 00090 for(int j = 0; j < n_nsets; j++) 00091 if(n_asets[j][i]) 00092 n.setBooleanAttribute("acc"+j, true); 00093 } 00094 00095 return g; 00096 } else if(ac.equals("edges")) { 00097 final int nedges = g.getEdgeCount(); 00098 00099 final boolean[][] asets = new boolean[nsets][nedges]; 00100 final Edge[] edges = new Edge[nedges]; 00101 00102 g.forAllEdges(new EmptyVisitor(new Integer(0)) { 00103 public void visitEdge(Edge e) { 00104 int id = ((Integer)arg).intValue(); 00105 arg = new Integer(id+1); 00106 00107 edges[id] = e; 00108 00109 for(int i = 0; i < nsets; i++) { 00110 String acc = "acc"+i; 00111 if(e.getBooleanAttribute(acc)) { 00112 asets[i][id] = true; 00113 e.setBooleanAttribute(acc, false); 00114 } 00115 } 00116 } 00117 }); 00118 00119 boolean[] remove = new boolean[nsets]; 00120 00121 for(int i = 0; i < nsets; i++) { 00122 for(int j = 0; j < nsets && !remove[i] ; j++) { 00123 if(i != j && !remove[j]) 00124 if(included(asets[j], asets[i])) 00125 remove[i] = true; 00126 } 00127 } 00128 00129 int n_nsets = 0; 00130 for(int i = 0; i < nsets; i++) 00131 if(!remove[i]) n_nsets++; 00132 00133 boolean[][] n_asets = new boolean[n_nsets][nedges]; 00134 00135 n_nsets = 0; 00136 for(int i = 0; i < nsets; i++) 00137 if(!remove[i]) 00138 n_asets[n_nsets++] = asets[i]; 00139 00140 g.setIntAttribute("nsets", n_nsets); 00141 00142 for(int i = 0; i < nedges; i++) { 00143 Edge e = edges[i]; 00144 for(int j = 0; j < n_nsets; j++) 00145 if(n_asets[j][i]) 00146 e.setBooleanAttribute("acc"+j, true); 00147 } 00148 00149 return g; 00150 } else 00151 throw new RuntimeException("invalid accepting type: " + ac); 00152 } 00153 }