3c26256491e48c9cecfb131378af4e749c5e16ae
[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             requiredsymbols.addAll(rule.getInclusion().getRequiredDescriptors());
61
62             for (int j = 0; j < rules.size(); j++) {
63
64                 if (j == i) {
65                     continue; 
66                 }
67                 
68                 Rule otherrule = (Rule) rules.elementAt(j);
69                 Inclusion inclusion = otherrule.getInclusion();
70                 Iterator targets = inclusion.getTargetDescriptors().iterator();
71                 GraphNode otherrulenode = (GraphNode) rulenodes.get(otherrule.getLabel());
72
73                 while (targets.hasNext()) {
74                     Descriptor d = (Descriptor) targets.next();
75
76                     if (requiredsymbols.contains(d)) { /* rule->rule dependency */
77                         otherrulenode.addEdge(new GraphNode.Edge(d.getSymbol(), rulenode));
78                     }
79                 }
80             }           
81         }
82
83         /* build constraint->rule dependencies */
84         for (int i = 0; i < constraints.size(); i++) {           
85             Constraint constraint = (Constraint) constraints.elementAt(i);
86             GraphNode constraintnode = (GraphNode) constraintnodes.get(constraint.getLabel());
87             Set requiredsymbols = constraint.getRequiredDescriptorsFromLogicStatement();
88             Set requiredquantifiers = constraint.getRequiredDescriptorsFromQuantifiers();
89  
90             for (int j = 0; j < rules.size(); j++) {                
91                 Rule otherrule = (Rule) rules.elementAt(j);
92                 Inclusion inclusion = otherrule.getInclusion();
93                 Iterator targets = inclusion.getTargetDescriptors().iterator();
94                 GraphNode otherrulenode = (GraphNode) rulenodes.get(otherrule.getLabel());
95
96                 while (targets.hasNext()) {
97                     Descriptor d = (Descriptor) targets.next();
98
99                     if (requiredsymbols.contains(d)) { /* logic->rule dependency */
100                         GraphNode.Edge edge = new GraphNode.Edge(d.getSymbol(), constraintnode);
101                         //edge.setDotNodeParameters("style=bold");
102                         otherrulenode.addEdge(edge);
103                     }
104
105                     if (requiredquantifiers.contains(d)) { /* quantifier-> dependency */
106                         GraphNode.Edge edge = new GraphNode.Edge(d.getSymbol(), constraintnode);
107                         edge.setDotNodeParameters("style=dotted");
108                         otherrulenode.addEdge(edge);
109                     }
110                 }               
111             }           
112         }
113
114         /* store results in state */
115         state.rulenodes = rulenodes;
116         state.constraintnodes = constraintnodes;
117     }
118
119     static class IntegerLattice {
120
121         boolean top;
122         boolean isNum;
123         int num;
124
125         public static final IntegerLattice TOP = new IntegerLattice(true);
126         public static final IntegerLattice BOT = new IntegerLattice(false);
127
128         private IntegerLattice(boolean top) {
129             this.top = top;
130             isNum = false;
131         }
132
133         public IntegerLattice(int num) {
134             isNum = true;
135             this.num = num;
136         }
137
138     }
139
140     public IntegerLattice setSize(SetDescriptor sd) {
141         String setname = sd.getSymbol();
142
143         if (setname.equals("Block")) {
144             return IntegerLattice.TOP;
145         } else if (setname.equals("UsedBlock")) {
146             return IntegerLattice.TOP;
147         } else if (setname.equals("FreeBlock")) {
148             return IntegerLattice.TOP;
149         } else if (setname.equals("Inode")) {
150             return IntegerLattice.TOP;
151         } else if (setname.equals("UsedInode")) {
152             return IntegerLattice.TOP;
153         } else if (setname.equals("FileInode")) {
154             return IntegerLattice.TOP;
155         } else if (setname.equals("DirectoryInode")) {
156             return new IntegerLattice(1);
157         } else if (setname.equals("RootDirectoryInode")) {
158             return new IntegerLattice(1);
159         } else if (setname.equals("SuperBlock")) {
160             return new IntegerLattice(1);
161         } else if (setname.equals("GroupBlock")) {
162             return new IntegerLattice(1);
163         } else if (setname.equals("FileDirectoryBlock")) {
164             return IntegerLattice.TOP;
165         } else if (setname.equals("InodeTableBlock")) {
166             return new IntegerLattice(1);
167         } else if (setname.equals("InodeBitmapBlock")) {
168             return new IntegerLattice(1);
169         } else if (setname.equals("BlockBitmapBlock")) {
170             return new IntegerLattice(1);
171         } else if (setname.equals("DirectoryBlock")) {
172             return new IntegerLattice(0);
173         } else if (setname.equals("FileBlock")) {
174             return IntegerLattice.TOP;
175         } else if (setname.equals("DirectoryEntry")) {
176             return IntegerLattice.TOP;
177         } else {
178             throw new IRException();
179         }
180             
181     }
182
183 }