KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gov > nasa > ltl > graph > SuperSetReduction


1
2 //
3
// Copyright (C) 2005 United States Government as represented by the
4
// Administrator of the National Aeronautics and Space Administration
5
// (NASA). All Rights Reserved.
6
//
7
// This software is distributed under the NASA Open Source Agreement
8
// (NOSA), version 1.3. The NOSA has been approved by the Open Source
9
// Initiative. See the file NOSA-1.3-JPF at the top of the distribution
10
// directory tree for the complete NOSA document.
11
//
12
// THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF ANY
13
// KIND, EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT
14
// LIMITED TO, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL CONFORM TO
15
// SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR
16
// A PARTICULAR PURPOSE, OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY THAT
17
// THE SUBJECT SOFTWARE WILL BE ERROR FREE, OR ANY WARRANTY THAT
18
// DOCUMENTATION, IF PROVIDED, WILL CONFORM TO THE SUBJECT SOFTWARE.
19
//
20
package gov.nasa.ltl.graph;
21
22 import java.io.*;
23
24 /**
25  * DOCUMENT ME!
26  */

27 public class SuperSetReduction {
28   public static void main (String JavaDoc[] args) {
29     if (args.length > 1) {
30       System.out.println("usage:");
31       System.out.println(
32             "\tjava gov.nasa.ltl.graph.SuperSetReduction [<filename>]");
33
34       return;
35     }
36
37     Graph g = null;
38
39     try {
40       if (args.length == 0) {
41         g = Graph.load();
42       } else {
43         g = Graph.load(args[0]);
44       }
45     } catch (IOException e) {
46       System.out.println("Can't load the graph.");
47
48       return;
49     }
50
51     g = reduce(g);
52
53     g.save();
54   }
55
56   public static Graph reduce (Graph g) {
57     final int nsets = g.getIntAttribute("nsets");
58     String JavaDoc type = g.getStringAttribute("type");
59     String JavaDoc ac = g.getStringAttribute("ac");
60
61     if (!type.equals("gba")) {
62       throw new RuntimeException JavaDoc("invalid graph type: " + type);
63     }
64
65     if (ac.equals("nodes")) {
66       final int nnodes = g.getNodeCount();
67
68       final boolean[][] asets = new boolean[nsets][nnodes];
69
70       g.forAllNodes(new EmptyVisitor() {
71         public void visitNode (Node n) {
72           for (int i = 0; i < nsets; i++) {
73             String JavaDoc acc = "acc" + i;
74
75             if (n.getBooleanAttribute(acc)) {
76               asets[i][n.getId()] = true;
77               n.setBooleanAttribute(acc, false);
78             }
79           }
80         }
81       });
82
83       boolean[] remove = new boolean[nsets];
84
85       for (int i = 0; i < nsets; i++) {
86         for (int j = 0; (j < nsets) && !remove[i]; j++) {
87           if ((i != j) && !remove[j]) {
88             if (included(asets[j], asets[i])) {
89               remove[i] = true;
90             }
91           }
92         }
93       }
94
95       int n_nsets = 0;
96
97       for (int i = 0; i < nsets; i++) {
98         if (!remove[i]) {
99           n_nsets++;
100         }
101       }
102
103       boolean[][] n_asets = new boolean[n_nsets][nnodes];
104
105       n_nsets = 0;
106
107       for (int i = 0; i < nsets; i++) {
108         if (!remove[i]) {
109           n_asets[n_nsets++] = asets[i];
110         }
111       }
112
113       g.setIntAttribute("nsets", n_nsets);
114
115       for (int i = 0; i < nnodes; i++) {
116         Node n = g.getNode(i);
117
118         for (int j = 0; j < n_nsets; j++) {
119           if (n_asets[j][i]) {
120             n.setBooleanAttribute("acc" + j, true);
121           }
122         }
123       }
124
125       return g;
126     } else if (ac.equals("edges")) {
127       final int nedges = g.getEdgeCount();
128
129       final boolean[][] asets = new boolean[nsets][nedges];
130       final Edge[] edges = new Edge[nedges];
131
132       g.forAllEdges(new EmptyVisitor(new Integer JavaDoc(0)) {
133         public void visitEdge (Edge e) {
134           int id = ((Integer JavaDoc) arg).intValue();
135           arg = new Integer JavaDoc(id + 1);
136
137           edges[id] = e;
138
139           for (int i = 0; i < nsets; i++) {
140             String JavaDoc acc = "acc" + i;
141
142             if (e.getBooleanAttribute(acc)) {
143               asets[i][id] = true;
144               e.setBooleanAttribute(acc, false);
145             }
146           }
147         }
148       });
149
150       boolean[] remove = new boolean[nsets];
151
152       for (int i = 0; i < nsets; i++) {
153         for (int j = 0; (j < nsets) && !remove[i]; j++) {
154           if ((i != j) && !remove[j]) {
155             if (included(asets[j], asets[i])) {
156               remove[i] = true;
157             }
158           }
159         }
160       }
161
162       int n_nsets = 0;
163
164       for (int i = 0; i < nsets; i++) {
165         if (!remove[i]) {
166           n_nsets++;
167         }
168       }
169
170       boolean[][] n_asets = new boolean[n_nsets][nedges];
171
172       n_nsets = 0;
173
174       for (int i = 0; i < nsets; i++) {
175         if (!remove[i]) {
176           n_asets[n_nsets++] = asets[i];
177         }
178       }
179
180       g.setIntAttribute("nsets", n_nsets);
181
182       for (int i = 0; i < nedges; i++) {
183         Edge e = edges[i];
184
185         for (int j = 0; j < n_nsets; j++) {
186           if (n_asets[j][i]) {
187             e.setBooleanAttribute("acc" + j, true);
188           }
189         }
190       }
191
192       return g;
193     } else {
194       throw new RuntimeException JavaDoc("invalid accepting type: " + ac);
195     }
196   }
197
198   private static boolean included (boolean[] a, boolean[] b) {
199     final int al = a.length;
200     final int bl = b.length;
201
202     for (int i = 0; i < al; i++) {
203       if (a[i] && !b[i]) {
204         return false;
205       }
206     }
207
208     return true;
209   }
210 }
Popular Tags