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