Fixing a bug in manual_write-non_conflict + setSet conflicts
[jpf-core.git] / src / main / gov / nasa / jpf / listener / ConflictTracker.java
index ca405b25dacf78abaec4f885cbef1ef0c7852956..c8bb079c07248c0633e5ac67caf59863f668cb36 100644 (file)
@@ -37,7 +37,6 @@ import java.util.*;
  **/
 
 public class ConflictTracker extends ListenerAdapter {
-
   private final PrintWriter out;
   private final HashSet<String> conflictSet = new HashSet<String>(); // Variables we want to track
   private final HashSet<String> appSet = new HashSet<String>(); // Apps we want to find their conflicts
@@ -90,22 +89,57 @@ public class ConflictTracker extends ListenerAdapter {
 
   boolean propagateTheChange(Node currentNode) {
        HashSet<Node> changed = new HashSet<Node>(currentNode.getSuccessors());
+       HashMap<Node, HashSet<Node>> parentQueueMap = new HashMap<Node, HashSet<Node>>();
+       HashSet<Node> parents = new HashSet<Node>();
+       parents.add(currentNode);
+
+       for (Node node : currentNode.getSuccessors()) {
+               parentQueueMap.put(node, parents);
+       }
 
        while(!changed.isEmpty()) {
-               // Get the first element of HashSet and remove it from the changed set
+               // Get the first element of the changed set and remove it
                Node nodeToProcess = changed.iterator().next();
                changed.remove(nodeToProcess);
 
-               // Update the edge
-               boolean isChanged = updateEdge(currentNode, nodeToProcess);
+               // Update the changed parents
+               parents.clear();
+               parents = parentQueueMap.get(nodeToProcess);
+               boolean isChanged = false;
+
+               for (Node node : parents) {
+                       // Update the edge
+                       isChanged |= updateTheOutSet(node, nodeToProcess);
+               }
+
+               // Check for a conflict if the outSet of nodeToProcess is changed
+               if (isChanged) {
+                       for (Node node : nodeToProcess.getSuccessors()) {
+                               HashMap<Transition, ArrayList<NameValuePair>> setSets = nodeToProcess.getOutgoingEdges().get(node).getSetSetMap();
+                               for (Map.Entry mapElement : setSets.entrySet()) {
+                                       Transition transition = (Transition)mapElement.getKey();
+                                       if (checkForConflict(nodeToProcess, node, transition))
+                                               return true;
+                               }
+                       }
+               }
 
-               // Check for a conflict in this transition(currentNode -> nodeToProcess)
-               if (checkForConflict(nodeToProcess))
-                               return true;
+               // Update the parents list for the successors of the current node
+               parents.clear();
+               parents.add(nodeToProcess);
 
                // Checking if the out set has changed or not(Add its successors to the change list!)
                if (isChanged) {
-                       propagateTheChange(nodeToProcess);
+                       for (Node i : nodeToProcess.getSuccessors()) {
+                               if (!changed.contains(i))
+                                       changed.add(i);
+
+                               // Update the list of updated parents for the current node
+                               if (parentQueueMap.containsKey(i))
+                                       parentQueueMap.get(i).add(nodeToProcess);
+                               else
+                                       parentQueueMap.put(i, parents);
+                       }
                }
       }
 
@@ -115,118 +149,153 @@ public class ConflictTracker extends ListenerAdapter {
   String createErrorMessage(NameValuePair pair, HashMap<String, String> valueMap, HashMap<String, Integer> writerMap) {
        String message = "Conflict found between the two apps. App"+pair.getAppNum()+
                         " has written the value: "+pair.getValue()+
-                        " to the variable: "+pair.getVarName()+" while App"+
-                        writerMap.get(pair.getVarName())+" is overwriting the value: "
+                        " to the variable: "+pair.getVarName()+" while App"
+                        +writerMap.get(pair.getVarName())+" is overwriting the value: "
                         +valueMap.get(pair.getVarName())+" to the same variable!";
        System.out.println(message);    
        return message;
   }
 
-  boolean checkForConflict(Node nodeToProcess) {
+  boolean checkForConflict(Node parentNode, Node currentNode, Transition currentTransition) {
+       ArrayList<NameValuePair> setSet = parentNode.getOutgoingEdges().get(currentNode).getSetSetMap().get(currentTransition);
        HashMap<String, String> valueMap = new HashMap<String, String>(); // HashMap from varName to value
-       HashMap<String, Integer> writerMap = new HashMap<String, Integer>(); // HashMap from varName to appNum
-
-       // Update the valueMap
-       for (int i = 0;i < nodeToProcess.getSetSet().size();i++) {
-               NameValuePair nameValuePair = nodeToProcess.getSetSet().get(i);
-
-               if (valueMap.containsKey(nameValuePair.getVarName())) {
-                       // Check if we have a same writer
-                       if (!writerMap.get(nameValuePair.getVarName()).equals(nameValuePair.getAppNum())) {
-                               // Check if we have a conflict or not
-                               if (!valueMap.get(nameValuePair.getVarName()).equals(nameValuePair.getValue())) {
-                                       errorMessage = createErrorMessage(nameValuePair, valueMap, writerMap);
-                                       return true;
-                               } else { // We have two writers writing the same value
-                                       writerMap.put(nameValuePair.getVarName(), 3); // 3 represents both apps
-                               }       
-                       } else {
-                               // Check if we have more than one value with the same writer
-                               if (!valueMap.get(nameValuePair.getVarName()).equals(nameValuePair.getValue())) {
-                                       valueMap.put(nameValuePair.getVarName(), "twoValue"); // We have one writer writing more than one value in a same event
-                               }
-                       }       
-               } else {
-                       valueMap.put(nameValuePair.getVarName(), nameValuePair.getValue());
-                       writerMap.put(nameValuePair.getVarName(), nameValuePair.getAppNum());
-               }
-       }
-
-       // Comparing the outSet to setSet
-       for (NameValuePair i : nodeToProcess.getOutSet()) {
-               if (valueMap.containsKey(i.getVarName())) {
-                       String value = valueMap.get(i.getVarName());
-                       Integer writer = writerMap.get(i.getVarName());
-                       if ((value != null)&&(writer != null)) {
-                               if (!value.equals(i.getValue())&&!writer.equals(i.getAppNum())) { // We have different values
-                                       errorMessage = createErrorMessage(i, valueMap, writerMap);
-                                       return true;
-                               }
+       HashMap<String, Integer> writerMap = new HashMap<String, Integer>(); //  HashMap from varName to appNum
+       HashMap<String, Boolean> isManualMap = new HashMap<String, Boolean>(); // HashMap from varName to isManual
+
+       // Update the valueMap and writerMap + check for conflict between the elements of setSet
+       for (int i = 0;i < setSet.size();i++) {
+            NameValuePair nameValuePair = setSet.get(i);
+           String varName = nameValuePair.getVarName();
+           String value = nameValuePair.getValue();
+           Integer appNum = nameValuePair.getAppNum();
+           Boolean isManual = nameValuePair.getIsManual();
+
+           if (valueMap.containsKey(varName)) {
+               // Check if we have a same writer
+               if (!writerMap.get(varName).equals(appNum)) {
+                       // Check if we have a conflict or not
+                       if (!valueMap.get(varName).equals(value)) {
+                               errorMessage = createErrorMessage(nameValuePair, valueMap, writerMap);
+                               return true;
                        }
                }
+               valueMap.put(varName, value);
+               writerMap.put(varName, appNum);
+               isManualMap.put(varName, isManual);
+            } else {
+               valueMap.put(varName, value);
+               writerMap.put(varName, appNum);
+               isManualMap.put(varName, isManual);
+            }
        }
 
-       return false;
+       // Check for conflict between outSet and this transition setSet
+       for (NameValuePair i : parentNode.getOutSet()) {
+            if (valueMap.containsKey(i.getVarName())) {
+                String value = valueMap.get(i.getVarName());
+                Integer writer = writerMap.get(i.getVarName());
+                    if ((value != null)&&(writer != null)) {
+                        if (!value.equals(i.getValue())&&!writer.equals(i.getAppNum())
+                            &&!isManualMap.get(i.getVarName())) { 
+                            // We have different values and different writers
+                            errorMessage = createErrorMessage(i, valueMap, writerMap);
+                            return true;
+                        }
+                    }
+            }
+       }
+
+       return false;
   }
 
-  boolean updateEdge(Node parentNode, Node currentNode) {
-       ArrayList<NameValuePair> setSet = currentNode.getSetSetMap().get(parentNode);
+  boolean updateTheOutSet(Node parentNode, Node currentNode) {
+       HashMap<Transition, ArrayList<NameValuePair>> setSets = parentNode.getOutgoingEdges().get(currentNode).getSetSetMap();
        HashSet<String> updatedVarNames = new HashSet<String>();
+       Edge currentEdge = parentNode.getOutgoingEdges().get(currentNode);
+       HashMap<String, Integer> lastWriter = currentEdge.getLastWriter();
+       HashMap<String, String> lastValue = currentEdge.getLastValue();
+       HashMap<String, Integer> outSetVarMap = new HashMap<String, Integer>();
        boolean isChanged = false;
-       
-       if (setSet != null) {
-               for (int i = 0;i < setSet.size();i++) {
+
+        for (Map.Entry mapElement : setSets.entrySet()) {
+            ArrayList<NameValuePair> setSet = (ArrayList<NameValuePair>)mapElement.getValue();
+  
+            for (int i = 0;i < setSet.size();i++) {
                        updatedVarNames.add(setSet.get(i).getVarName());
-               }
-       }
+           }
+        }
+
 
        for (NameValuePair i : parentNode.getOutSet()) {
+               outSetVarMap.put(i.getVarName(), i.getAppNum());
                if (!updatedVarNames.contains(i.getVarName()))
                        isChanged |= currentNode.getOutSet().add(i);
        }
 
-       if (setSet != null) {
-               for (int i = 0;i < setSet.size();i++) {
-                       if (currentNode.getOutSet().contains(setSet.get(i)))
-                               currentNode.getOutSet().remove(setSet.get(i));
-                       isChanged |= currentNode.getOutSet().add(setSet.get(i));
-               }
-       }
+
+       for (Map.Entry mapElement : setSets.entrySet()) {
+            ArrayList<NameValuePair> setSet = (ArrayList<NameValuePair>)mapElement.getValue();
+  
+            for (int i = 0;i < setSet.size();i++) {
+                       String varName = setSet.get(i).getVarName();
+                       Integer writer = lastWriter.get(varName);
+                       String value = lastValue.get(varName);
+
+                       if (setSet.get(i).getAppNum().equals(writer) 
+                           && setSet.get(i).getValue().equals(value)) {
+                               if (outSetVarMap.containsKey(varName)) {
+                                       Integer hashCode = outSetVarMap.get(varName).hashCode() * 31 +
+                                                          varName.hashCode();
+                                       currentNode.getOutSet().remove(hashCode);
+                               }
+                               isChanged |= currentNode.getOutSet().add(setSet.get(i));
+                       }
+           }
+        }
 
        return isChanged;
   }
 
+  void updateTheEdge(Node currentNode, Transition transition) {
+       if (parentNode.getOutgoingEdges().containsKey(currentNode)) {
+               Edge currentEdge = parentNode.getOutgoingEdges().get(currentNode);
+               if (currentEdge.getSetSetMap().containsKey(transition)) { // Update the transition
+                       if (manual)
+                               currentEdge.getSetSetMap().put(transition, tempSetSet);
+                       else
+                               currentEdge.getSetSetMap().get(transition).addAll(tempSetSet);
+               } else { // Add a new transition
+                       currentEdge.getSetSetMap().put(transition, tempSetSet);
+               }
+        } else {
+               parentNode.getOutgoingEdges().put(currentNode, new Edge(parentNode, currentNode));
+               Edge currentEdge = parentNode.getOutgoingEdges().get(currentNode);
+               currentEdge.getSetSetMap().put(transition, tempSetSet);
+        }
+
+       // Update the last writer and last value for this edge for each varName
+       Edge currentEdge = parentNode.getOutgoingEdges().get(currentNode);
+       ArrayList<NameValuePair> setSet = currentEdge.getSetSetMap().get(transition);
+       for (int i = 0;i < setSet.size();i++) {
+               NameValuePair nameValuePair = setSet.get(i);
+               currentEdge.getLastWriter().put(nameValuePair.getVarName(), nameValuePair.getAppNum());
+               currentEdge.getLastValue().put(nameValuePair.getVarName(), nameValuePair.getValue());
+       }
+  }
+
+  
+
   static class Node {
         Integer id;
        HashSet<Node> predecessors = new HashSet<Node>();
        HashSet<Node> successors = new HashSet<Node>();
        HashSet<NameValuePair> outSet = new HashSet<NameValuePair>();
-       HashMap<Node, ArrayList<NameValuePair>> setSetMap = new HashMap<Node, ArrayList<NameValuePair>>();
-       ArrayList<NameValuePair> setSet = new ArrayList<NameValuePair>();
-
+       HashMap<Node, Edge> outgoingEdges = new HashMap<Node, Edge>();
 
        Node(Integer id) {
          this.id = id;
        }
 
-       void addPredecessor(Node node) {
-         predecessors.add(node);
-       }
-
-       void addSuccessor(Node node) {
-         successors.add(node);
-       }
-
-       void setSetSet(ArrayList<NameValuePair> setSet, boolean isManual) {
-         if (isManual)
-           this.setSet = new ArrayList<NameValuePair>();
-
-         for (int i = 0;i < setSet.size();i++) {
-           this.setSet.add(new NameValuePair(setSet.get(i).getAppNum(), setSet.get(i).getValue(), 
-                                             setSet.get(i).getVarName(), setSet.get(i).getIsManual()));
-           }
-       }
-
        Integer getId() {
                return id;
        }
@@ -239,17 +308,45 @@ public class ConflictTracker extends ListenerAdapter {
                return successors;
        }
 
-       ArrayList<NameValuePair> getSetSet() {
-               return setSet;
-       }
-
        HashSet<NameValuePair> getOutSet() {
                return outSet;
        }
 
-       HashMap<Node, ArrayList<NameValuePair>> getSetSetMap() {
+       HashMap<Node, Edge> getOutgoingEdges() {
+               return outgoingEdges;
+       }
+  }
+
+  static class Edge {
+       Node source, destination;
+       HashMap<String, Integer> lastWriter = new HashMap<String, Integer>();
+       HashMap<String, String> lastValue = new HashMap<String, String>();
+       HashMap<Transition, ArrayList<NameValuePair>> setSetMap = new HashMap<Transition, ArrayList<NameValuePair>>();
+
+       Edge(Node source, Node destination) {
+               this.source = source;
+               this.destination = destination;
+       }
+
+       Node getSource() {
+               return source;
+       }
+
+       Node getDestination() {
+               return destination;
+       }
+
+       HashMap<Transition, ArrayList<NameValuePair>> getSetSetMap() {
                return setSetMap;
        }
+
+       HashMap<String, Integer> getLastWriter() {
+               return lastWriter;
+       }
+
+       HashMap<String, String> getLastValue() {
+               return lastValue;
+       }
   }
 
   static class NameValuePair {
@@ -277,7 +374,7 @@ public class ConflictTracker extends ListenerAdapter {
                this.varName = varName;
        }
 
-    void setIsManual(String varName) {
+        void setIsManual(String varName) {
                this.isManual = isManual;
        }
 
@@ -339,6 +436,7 @@ public class ConflictTracker extends ListenerAdapter {
   @Override
   public void stateAdvanced(Search search) {
     String theEnd = null;
+    Transition transition = search.getTransition();
     id = search.getStateId();
     depth = search.getDepth();
     operation = "forward";
@@ -349,11 +447,16 @@ public class ConflictTracker extends ListenerAdapter {
 
     Node currentNode = nodes.get(id);
 
-    // Update the setSet for this new node
-    currentNode.setSetSet(tempSetSet, manual);
-    tempSetSet = new ArrayList<NameValuePair>(); 
+    // Update the edge based on the current transition
+    updateTheEdge(currentNode, transition);
+
+    // Reset the temporary variables and flags
+    tempSetSet = new ArrayList<NameValuePair>();
     manual = false;
 
+    // Check for the conflict in this transition
+    conflictFound = checkForConflict(parentNode, currentNode, transition);
+
     if (search.isNewState()) {
       detail = "new";
     } else {
@@ -370,29 +473,27 @@ public class ConflictTracker extends ListenerAdapter {
     // Updating the predecessors for this node
     // Check if parent node is already in successors of the current node or not
     if (!(currentNode.getPredecessors().contains(parentNode)))
-       currentNode.addPredecessor(parentNode);
+       currentNode.getPredecessors().add(parentNode);
 
     // Update the successors for this node
     // Check if current node is already in successors of the parent node or not
     if (!(parentNode.getSuccessors().contains(currentNode)))
-        parentNode.addSuccessor(currentNode);
-
+        parentNode.getSuccessors().add(currentNode);
 
-    // Update the setSetMap of the current node
-    for (Node i : currentNode.getPredecessors()) {
-       currentNode.getSetSetMap().put(i, i.getSetSet());
-    }
+    // Update the outset of the current node and check if it is changed or not to propagate the change
+    boolean isChanged = updateTheOutSet(parentNode, currentNode);
 
-    // Update the edge and check if the outset of the current node is changed or not to propagate the change
-    boolean isChanged = updateEdge(parentNode, currentNode);
-
-    // Check for the conflict in this edge
-    conflictFound = checkForConflict(currentNode);
-    
     // Check if the outSet of this state has changed, update all of its successors' sets if any
-    if (isChanged)
+    if (isChanged) {
+       for (Node node : currentNode.getSuccessors()) {
+               HashMap<Transition, ArrayList<NameValuePair>> setSets = currentNode.getOutgoingEdges().get(node).getSetSetMap();
+               for (Map.Entry mapElement : setSets.entrySet()) {
+                       Transition currentTransition = (Transition)mapElement.getKey();
+                       conflictFound = conflictFound || checkForConflict(currentNode, node, currentTransition);
+               }
+       }
        conflictFound = conflictFound || propagateTheChange(currentNode);
-
+    }
     // Update the parent node
     if (nodes.containsKey(id)) {
          parentNode = nodes.get(id);