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