+package edu.uci.eecs.specCompiler.specExtraction;
+
+import java.util.HashMap;
+import java.util.HashSet;
+
+public class GlobalConstruct extends Construct {
+ public final String code;
+ private final HashMap<String, HashSet<ConditionalInterface>> interfaceCluster;
+ private final HashMap<ConditionalInterface, HashSet<ConditionalInterface>> originalHBRelations;
+ public final HashMap<ConditionalInterface, HashSet<ConditionalInterface>> hbRelations;
+
+ public GlobalConstruct(String code) {
+ this.code = code;
+ this.interfaceCluster = new HashMap<String, HashSet<ConditionalInterface>>();
+ this.originalHBRelations = new HashMap<ConditionalInterface, HashSet<ConditionalInterface>>();
+ this.hbRelations = new HashMap<ConditionalInterface, HashSet<ConditionalInterface>>();
+ }
+
+ public void addInterface2Cluster(String clusterName, ConditionalInterface condInterface) {
+ if (!interfaceCluster.containsKey(clusterName)) {
+ interfaceCluster.put(clusterName, new HashSet<ConditionalInterface>());
+ }
+ HashSet<ConditionalInterface> set = interfaceCluster.get(clusterName);
+ set.add(condInterface);
+ }
+
+ public void addHBCondition(ConditionalInterface left, ConditionalInterface right) {
+ if (!originalHBRelations.containsKey(left)) {
+ originalHBRelations.put(left, new HashSet<ConditionalInterface>());
+ }
+ HashSet<ConditionalInterface> set = originalHBRelations.get(left);
+ set.add(right);
+ }
+
+ private void addUnfoldedHBCondition(ConditionalInterface left, ConditionalInterface right) {
+ if (!hbRelations.containsKey(left)) {
+ hbRelations.put(left, new HashSet<ConditionalInterface>());
+ }
+ HashSet<ConditionalInterface> set = hbRelations.get(left);
+ set.add(right);
+ }
+
+ private HashSet<ConditionalInterface> getByName(ConditionalInterface condInterface) {
+ if (interfaceCluster.containsKey(condInterface.interfaceName))
+ return interfaceCluster.get(condInterface.interfaceName);
+ HashSet<ConditionalInterface> res = new HashSet<ConditionalInterface>();
+ res.add(condInterface);
+ return res;
+ }
+
+ public void unfoldInterfaceCluster() {
+ for (ConditionalInterface left : originalHBRelations.keySet()) {
+ HashSet<ConditionalInterface> rights = originalHBRelations.get(left);
+ for (ConditionalInterface right : rights) {
+ HashSet<ConditionalInterface> leftCondInterfaces = getByName(left),
+ rightCondInterfaces = getByName(right);
+ for (ConditionalInterface l : leftCondInterfaces) {
+ for (ConditionalInterface r : rightCondInterfaces) {
+ addUnfoldedHBCondition(l, r);
+ }
+ }
+ }
+ }
+ }
+
+ public String toString() {
+ StringBuilder sb = new StringBuilder();
+ sb.append("Code:\n");
+ sb.append(code);
+
+ sb.append("Happens_before:\n");
+ for (ConditionalInterface left : hbRelations.keySet()) {
+ HashSet<ConditionalInterface> rights = hbRelations.get(left);
+ sb.append(left + " -> ");
+ for (ConditionalInterface right : rights) {
+ sb.append(right + " | ");
+ }
+ sb.append(".\n");
+ }
+
+ return sb.toString();
+ }
+}