A minor change in ConflictTracker.java
[jpf-core.git] / src / main / gov / nasa / jpf / listener / ConflictTracker.java
index 1aa50b92ef37ed2aad2b1b126e46fd1f706e5f40..a0ecc4bb6721597ba8631d83deeb8929feb207f8 100644 (file)
@@ -43,7 +43,7 @@ public class ConflictTracker extends ListenerAdapter {
   private final HashSet<String> appSet = new HashSet<String>(); // Apps we want to find their conflicts
   private final HashSet<String> manualSet = new HashSet<String>(); // Writer classes with manual inputs to detect direct-direct(No Conflict) interactions
   private final HashMap<Integer, Node> nodes = new HashMap<Integer, Node>(); // Nodes of a graph
-  private HashSet<NameValuePair> tempSetSet = new HashSet<NameValuePair>();
+  private ArrayList<NameValuePair> tempSetSet = new ArrayList<NameValuePair>();
   private long timeout;
   private long startTime;
   private Node parentNode = new Node(-2);
@@ -53,7 +53,6 @@ public class ConflictTracker extends ListenerAdapter {
   private int depth;
   private int id;
   private boolean conflictFound = false;
-  private boolean isSet = false;
   private boolean manual = false;
 
   private final String SET_LOCATION_METHOD = "setLocationMode";
@@ -101,15 +100,12 @@ public class ConflictTracker extends ListenerAdapter {
                boolean isChanged = updateEdge(currentNode, nodeToProcess);
 
                // Check for a conflict in this transition(currentNode -> nodeToProcess)
-               if (checkForConflict(currentNode))
+               if (checkForConflict(nodeToProcess))
                                return true;
 
                // Checking if the out set has changed or not(Add its successors to the change list!)
                if (isChanged) {
-                       for (Node i : nodeToProcess.getSuccessors()) {
-                               if (!changed.contains(i))
-                                       changed.add(i);
-                       }
+                       propagateTheChange(nodeToProcess);
                }
       }
 
@@ -122,6 +118,7 @@ public class ConflictTracker extends ListenerAdapter {
                         " 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;
   }
 
@@ -129,21 +126,29 @@ public class ConflictTracker extends ListenerAdapter {
        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 and check if we have conflict in the setSet
-       for (NameValuePair i : nodeToProcess.getSetSet()) {
-               if (i.getIsManual()) // Manual input: we have no conflict
-                       continue; // If this manual write is the last write, this is actually a break
-               
-               if (valueMap.containsKey(i.getVarName())) {
-                       if (!valueMap.get(i.getVarName()).equals(i.getValue())) { // Check if their value is different we have a conflict
-                               errorMessage = createErrorMessage(i, valueMap, writerMap);                              
-                               return true;
-                       }
-                       else // We have two writers writing the same value
-                               writerMap.put(i.getVarName(), i.getAppNum()+writerMap.get(i.getVarName()));
+       // 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(i.getVarName(), i.getValue());
-                       writerMap.put(i.getVarName(), i.getAppNum());
+                       valueMap.put(nameValuePair.getVarName(), nameValuePair.getValue());
+                       writerMap.put(nameValuePair.getVarName(), nameValuePair.getAppNum());
                }
        }
 
@@ -165,14 +170,17 @@ public class ConflictTracker extends ListenerAdapter {
   }
 
   boolean updateEdge(Node parentNode, Node currentNode) {
-       HashSet<NameValuePair> setSet = currentNode.getSetSetMap().get(parentNode);
+       ArrayList<NameValuePair> setSet = currentNode.getSetSetMap().get(parentNode);
        HashSet<String> updatedVarNames = new HashSet<String>();
-       HashMap<String, Integer> lastWriter = new HashMap<String, Integer>(); // Store the last writer of each variable
-       boolean isChanged = false;
+       HashMap<Integer, String> writerLastValue = new HashMap<Integer, String>();
 
-       for (NameValuePair i : setSet) {
-               updatedVarNames.add(i.getVarName());
-               lastWriter.put(i.getVarName(), i.getAppNum());
+       boolean isChanged = false;
+       
+       if (setSet != null) {
+               for (int i = 0;i < setSet.size();i++) {
+                       updatedVarNames.add(setSet.get(i).getVarName());
+                       writerLastValue.put(setSet.get(i).getAppNum(), setSet.get(i).getValue());
+               }
        }
 
        for (NameValuePair i : parentNode.getOutSet()) {
@@ -180,41 +188,35 @@ public class ConflictTracker extends ListenerAdapter {
                        isChanged |= currentNode.getOutSet().add(i);
        }
 
-       for (NameValuePair i : setSet) {
-               if (i.getAppNum().equals(lastWriter.get(i.getVarName()))) // Add the last writer of each variable to outSet
-                       isChanged |= currentNode.getOutSet().add(i);
+       if (setSet != null) {
+               for (int i = 0;i < setSet.size();i++) {
+                       if (setSet.get(i).getValue().equals(writerLastValue.get(setSet.get(i).getAppNum()))) {
+                               isChanged |= currentNode.getOutSet().add(setSet.get(i));
+                       }
+               }
        }
 
        return isChanged;
   }
 
   static class Node {
-       Integer id;
+        Integer id;
        HashSet<Node> predecessors = new HashSet<Node>();
        HashSet<Node> successors = new HashSet<Node>();
-       HashSet<NameValuePair> setSet = new HashSet<NameValuePair>();
        HashSet<NameValuePair> outSet = new HashSet<NameValuePair>();
-       HashMap<Node, HashSet<NameValuePair>> setSetMap = new HashMap<Node, HashSet<NameValuePair>>();
-
+        ArrayList<NameValuePair> setSet = new ArrayList<NameValuePair>();
+       HashMap<Node, ArrayList<NameValuePair>> setSetMap = new HashMap<Node, ArrayList<NameValuePair>>();
 
        Node(Integer id) {
-               this.id = id;
+         this.id = id;
        }
 
        void addPredecessor(Node node) {
-               predecessors.add(node);
+         predecessors.add(node);
        }
 
        void addSuccessor(Node node) {
-               successors.add(node);
-       }
-
-       void setSetSet(HashSet<NameValuePair> setSet, boolean isManual) {
-             if (isManual)
-               this.setSet = new HashSet<NameValuePair>();
-             for (NameValuePair i : setSet) {
-               this.setSet.add(new NameValuePair(i.getAppNum(), i.getValue(), i.getVarName(), i.getIsManual()));
-             }
+         successors.add(node);
        }
 
        Integer getId() {
@@ -229,15 +231,15 @@ public class ConflictTracker extends ListenerAdapter {
                return successors;
        }
 
-       HashSet<NameValuePair> getSetSet() {
-               return setSet;
-       }
-
        HashSet<NameValuePair> getOutSet() {
                return outSet;
        }
 
-       HashMap<Node, HashSet<NameValuePair>> getSetSetMap() {
+        ArrayList<NameValuePair> getSetSet() {
+               return setSet;
+        }
+
+       HashMap<Node, ArrayList<NameValuePair>> getSetSetMap() {
                return setSetMap;
        }
   }
@@ -267,7 +269,7 @@ public class ConflictTracker extends ListenerAdapter {
                this.varName = varName;
        }
 
-    void setIsManual(String varName) {
+        void setIsManual(String varName) {
                this.isManual = isManual;
        }
 
@@ -333,20 +335,23 @@ public class ConflictTracker extends ListenerAdapter {
     depth = search.getDepth();
     operation = "forward";
 
-    // Check for the conflict in this new transition
-    conflictFound = checkForConflict(parentNode);
+    // Add the node to the list of nodes
+    if (nodes.get(id) == null)
+       nodes.put(id, new Node(id));
 
-    // Add the node to the list of nodes       
-    nodes.put(id, new Node(id));
     Node currentNode = nodes.get(id);
 
-    // Update the setSet for this new node
-    if (isSet) {
-      currentNode.setSetSet(tempSetSet, manual);
-      tempSetSet = new HashSet<NameValuePair>(); 
-      isSet = false;
-      manual = false;
-    }
+    if ((currentNode.getSetSetMap().get(parentNode) == null) || manual)
+       currentNode.getSetSetMap().put(parentNode, new ArrayList<NameValuePair>());
+
+    // Update the setSet for the edge
+    currentNode.getSetSetMap().get(parentNode).addAll(tempSetSet);
+    parentNode.getSetSet().addAll(tempSetSet);
+    tempSetSet = new ArrayList<NameValuePair>();
+    manual = false;
+
+    // Check for the conflict in this edge
+    conflictFound = checkForConflict(parentNode);
 
     if (search.isNewState()) {
       detail = "new";
@@ -371,15 +376,9 @@ public class ConflictTracker extends ListenerAdapter {
     if (!(parentNode.getSuccessors().contains(currentNode)))
         parentNode.addSuccessor(currentNode);
 
-
-    // Update the setSetMap of the current node
-    for (Node i : currentNode.getPredecessors()) {
-       currentNode.getSetSetMap().put(i, i.getSetSet());
-    }
-
     // 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 if the outSet of this state has changed, update all of its successors' sets if any
     if (isChanged)
        conflictFound = conflictFound || propagateTheChange(currentNode);
@@ -554,16 +553,11 @@ public class ConflictTracker extends ListenerAdapter {
     if (writer.equals("App2"))
        temp = new NameValuePair(2, value, var, manual);
     
-    if (tempSetSet.contains(temp))
-       tempSetSet.remove(temp);
     tempSetSet.add(temp);
-    // Set isSet to true
-    isSet = true;
   }
 
   @Override
   public void instructionExecuted(VM vm, ThreadInfo ti, Instruction nextInsn, Instruction executedInsn) {
-    // Instantiate timeoutTimer
     if (timeout > 0) {
       if (System.currentTimeMillis() - startTime > timeout) {
         StringBuilder sbTimeOut = new StringBuilder();
@@ -597,6 +591,7 @@ public class ConflictTracker extends ListenerAdapter {
       } else {
         if (executedInsn instanceof WriteInstruction) {
           String varId = ((WriteInstruction) executedInsn).getFieldInfo().getFullName();
+
           for (String var : conflictSet) {
             if (varId.contains(var)) {
               // Get variable info