Committing changes to leftsize->rightSize, more comments, and handling
[repair.git] / Repair / RepairCompiler / MCC / IR / DependencyBuilder.java
1 package MCC.IR;
2
3 import MCC.State;
4 import java.util.*;
5
6 public class DependencyBuilder {
7
8     Hashtable constraintnodes = new Hashtable(); 
9     Hashtable rulenodes = new Hashtable();
10     State state;
11
12     public DependencyBuilder(State state) {
13         this.state = state;
14     }
15
16     public void calculate() {
17         /* reinitialize (clear) nodes */
18         constraintnodes = new Hashtable();
19         rulenodes = new Hashtable();
20
21         /* load up the rules and constraints */
22         Vector rules = state.vRules;        
23         Vector constraints = state.vConstraints;
24
25         /* build up graph rulenodes (not edges yet) */
26         for (int i = 0; i < rules.size(); i++) {
27             Rule rule = (Rule) rules.elementAt(i);
28             assert rule != null;
29             assert rule.getLabel() != null;
30
31             Inclusion inclusion = rule.getInclusion();
32             Iterator targets = inclusion.getTargetDescriptors().iterator();
33             String additionallabel = new String();
34
35             /* #ATTN#: is this meant to be while, not if? */
36             /* perhaps there is only one descriptor for targets */
37             if (targets.hasNext()) {
38                 Descriptor d = (Descriptor)targets.next();
39                 additionallabel = "\\n" + d.getSymbol();
40             } 
41             
42             GraphNode gn = new GraphNode(rule.getLabel(), rule.getLabel() + additionallabel, rule);
43             rulenodes.put(rule.getLabel(), gn);
44         } 
45
46         /* build up graph constraintnodes (not edges yet) */
47         for (int i = 0; i < constraints.size(); i++) {
48             Constraint constraint = (Constraint) constraints.elementAt(i);
49             assert constraint != null;
50             assert constraint.getLabel() != null;
51             GraphNode gn = new GraphNode(constraint.getLabel(), constraint);
52             gn.setDotNodeParameters("shape=box");
53             constraintnodes.put(constraint.getLabel(), gn);
54         } 
55
56         /* calculate rule->rule dependencies */        
57         for (int i = 0; i < rules.size(); i++) {
58             Rule rule = (Rule) rules.elementAt(i);
59             GraphNode rulenode = (GraphNode) rulenodes.get(rule.getLabel());
60             Set requiredsymbols = rule.getRequiredDescriptors();
61             requiredsymbols.addAll(rule.getInclusion().getRequiredDescriptors());
62
63             for (int j = 0; j < rules.size(); j++) {
64
65                 if (j == i) {
66                     continue; 
67                 }
68                 
69                 Rule otherrule = (Rule) rules.elementAt(j);
70                 Inclusion inclusion = otherrule.getInclusion();
71                 Iterator targets = inclusion.getTargetDescriptors().iterator();
72                 GraphNode otherrulenode = (GraphNode) rulenodes.get(otherrule.getLabel());
73
74                 while (targets.hasNext()) {
75                     Descriptor d = (Descriptor) targets.next();
76
77                     if (requiredsymbols.contains(d)) { /* rule->rule dependency */
78                         otherrulenode.addEdge(new GraphNode.Edge(d.getSymbol(), rulenode));
79                     }
80                 }
81             }
82         }
83
84         /* build constraint->rule dependencies */
85         for (int i = 0; i < constraints.size(); i++) {           
86             Constraint constraint = (Constraint) constraints.elementAt(i);
87             GraphNode constraintnode = (GraphNode) constraintnodes.get(constraint.getLabel());
88             Set requiredsymbols = constraint.getRequiredDescriptorsFromLogicStatement();
89             Set requiredquantifiers = constraint.getRequiredDescriptorsFromQuantifiers();
90  
91             for (int j = 0; j < rules.size(); j++) {                
92                 Rule otherrule = (Rule) rules.elementAt(j);
93                 Inclusion inclusion = otherrule.getInclusion();
94                 Iterator targets = inclusion.getTargetDescriptors().iterator();
95                 GraphNode otherrulenode = (GraphNode) rulenodes.get(otherrule.getLabel());
96
97                 while (targets.hasNext()) {
98                     Descriptor d = (Descriptor) targets.next();
99
100                     if (requiredsymbols.contains(d)) { /* logic->rule dependency */
101                         GraphNode.Edge edge = new GraphNode.Edge(d.getSymbol(), constraintnode);
102                         //edge.setDotNodeParameters("style=bold");
103                         otherrulenode.addEdge(edge);
104                     }
105
106                     if (requiredquantifiers.contains(d)) { /* quantifier-> dependency */
107                         GraphNode.Edge edge = new GraphNode.Edge(d.getSymbol(), constraintnode);
108                         edge.setDotNodeParameters("style=dotted");
109                         otherrulenode.addEdge(edge);
110                     }
111                 }
112             }
113         }
114
115         /* store results in state */
116         state.rulenodes = rulenodes;
117         state.constraintnodes = constraintnodes;
118     }
119
120     static class IntegerLattice {
121
122         boolean top;
123         boolean isNum;
124         int num;
125
126         public static final IntegerLattice TOP = new IntegerLattice(true);
127         public static final IntegerLattice BOT = new IntegerLattice(false);
128
129         private IntegerLattice(boolean top) {
130             this.top = top;
131             isNum = false;
132         }
133
134         public IntegerLattice(int num) {
135             isNum = true;
136             this.num = num;
137         }
138
139     }
140
141     public IntegerLattice setSize(SetDescriptor sd) {
142         String setname = sd.getSymbol();
143
144         if (setname.equals("Block")) {
145             return IntegerLattice.TOP;
146         } else if (setname.equals("UsedBlock")) {
147             return IntegerLattice.TOP;
148         } else if (setname.equals("FreeBlock")) {
149             return IntegerLattice.TOP;
150         } else if (setname.equals("Inode")) {
151             return IntegerLattice.TOP;
152         } else if (setname.equals("UsedInode")) {
153             return IntegerLattice.TOP;
154         } else if (setname.equals("FileInode")) {
155             return IntegerLattice.TOP;
156         } else if (setname.equals("DirectoryInode")) {
157             return new IntegerLattice(1);
158         } else if (setname.equals("RootDirectoryInode")) {
159             return new IntegerLattice(1);
160         } else if (setname.equals("SuperBlock")) {
161             return new IntegerLattice(1);
162         } else if (setname.equals("GroupBlock")) {
163             return new IntegerLattice(1);
164         } else if (setname.equals("FileDirectoryBlock")) {
165             return IntegerLattice.TOP;
166         } else if (setname.equals("InodeTableBlock")) {
167             return new IntegerLattice(1);
168         } else if (setname.equals("InodeBitmapBlock")) {
169             return new IntegerLattice(1);
170         } else if (setname.equals("BlockBitmapBlock")) {
171             return new IntegerLattice(1);
172         } else if (setname.equals("DirectoryBlock")) {
173             return new IntegerLattice(0);
174         } else if (setname.equals("FileBlock")) {
175             return IntegerLattice.TOP;
176         } else if (setname.equals("DirectoryEntry")) {
177             return IntegerLattice.TOP;
178         } else {
179             throw new IRException();
180         }
181             
182     }
183
184 }