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