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