edits
[cdsspec-compiler.git] / src / edu / uci / eecs / specCompiler / specExtraction / GlobalConstruct.java
1 package edu.uci.eecs.specCompiler.specExtraction;
2
3 import java.io.File;
4 import java.util.ArrayList;
5 import java.util.HashMap;
6 import java.util.HashSet;
7
8 public class GlobalConstruct extends Construct {
9         public final SequentialDefineSubConstruct code;
10         private final HashMap<String, HashSet<ConditionalInterface>> interfaceCluster;
11         private final HashMap<ConditionalInterface, HashSet<ConditionalInterface>> originalHBRelations;
12         public final HashMap<ConditionalInterface, HashSet<ConditionalInterface>> hbRelations;
13         public final ArrayList<CommutativityRule> commutativityRules;
14         public final HashMap<String, String> options;
15
16         public GlobalConstruct(File file, int beginLineNum,
17                         SequentialDefineSubConstruct code, HashMap<String, String> options) {
18                 super(file, beginLineNum);
19                 this.code = code;
20                 this.interfaceCluster = new HashMap<String, HashSet<ConditionalInterface>>();
21                 this.originalHBRelations = new HashMap<ConditionalInterface, HashSet<ConditionalInterface>>();
22                 this.hbRelations = new HashMap<ConditionalInterface, HashSet<ConditionalInterface>>();
23                 this.options = options;
24                 this.commutativityRules = new ArrayList<CommutativityRule>();
25         }
26
27         public void addInterface2Cluster(String clusterName,
28                         ConditionalInterface condInterface) {
29                 if (!interfaceCluster.containsKey(clusterName)) {
30                         interfaceCluster.put(clusterName,
31                                         new HashSet<ConditionalInterface>());
32                 }
33                 HashSet<ConditionalInterface> set = interfaceCluster.get(clusterName);
34                 set.add(condInterface);
35         }
36
37         public void addHBCondition(ConditionalInterface left,
38                         ConditionalInterface right) {
39                 if (!originalHBRelations.containsKey(left)) {
40                         originalHBRelations.put(left, new HashSet<ConditionalInterface>());
41                 }
42                 HashSet<ConditionalInterface> set = originalHBRelations.get(left);
43                 set.add(right);
44         }
45         
46         public void addCommutativityRule(String method1, String method2, String condition) {
47                 CommutativityRule rule = new CommutativityRule(method1, method2, condition);
48                 if (!commutativityRules.contains(rule)) {
49                         commutativityRules.add(rule);
50                 }
51         }
52
53         private void addUnfoldedHBCondition(ConditionalInterface left,
54                         ConditionalInterface right) {
55                 if (!hbRelations.containsKey(left)) {
56                         hbRelations.put(left, new HashSet<ConditionalInterface>());
57                 }
58                 HashSet<ConditionalInterface> set = hbRelations.get(left);
59                 set.add(right);
60         }
61
62         private HashSet<ConditionalInterface> getByName(
63                         ConditionalInterface condInterface) {
64                 if (interfaceCluster.containsKey(condInterface.interfaceName))
65                         return interfaceCluster.get(condInterface.interfaceName);
66                 HashSet<ConditionalInterface> res = new HashSet<ConditionalInterface>();
67                 res.add(condInterface);
68                 return res;
69         }
70
71         public void unfoldInterfaceCluster() {
72                 for (ConditionalInterface left : originalHBRelations.keySet()) {
73                         HashSet<ConditionalInterface> rights = originalHBRelations
74                                         .get(left);
75                         for (ConditionalInterface right : rights) {
76                                 HashSet<ConditionalInterface> leftCondInterfaces = getByName(left), rightCondInterfaces = getByName(right);
77                                 for (ConditionalInterface l : leftCondInterfaces) {
78                                         for (ConditionalInterface r : rightCondInterfaces) {
79                                                 addUnfoldedHBCondition(l, r);
80                                         }
81                                 }
82                         }
83                 }
84         }
85
86         public String toString() {
87                 StringBuilder sb = new StringBuilder("GlobalConstruct:\n");
88                 sb.append("@Code:\n");
89                 sb.append(code);
90
91                 sb.append("@Happens_before:\n");
92                 for (ConditionalInterface left : hbRelations.keySet()) {
93                         HashSet<ConditionalInterface> rights = hbRelations.get(left);
94                         sb.append(left + " -> ");
95                         for (ConditionalInterface right : rights) {
96                                 sb.append(right + " | ");
97                         }
98                         sb.append(".\n");
99                 }
100
101                 return sb.toString();
102         }
103 }