Fixing a typo in option name.
[jpf-core.git] / src / main / gov / nasa / jpf / listener / StateReducer.java
index 5dbe79dc4ab8f7c59b2f90b8ec423c8b7f217580..7d57f1d9044abaaad47bf309e2f4baeec1a60175 100644 (file)
@@ -33,145 +33,143 @@ import java.io.PrintWriter;
 
 import java.util.*;
 
+// TODO: Fix for Groovy's model-checking
+// TODO: This is a listener created to detect device conflicts and global variable conflicts
 /**
- * simple tool to log state changes
+ * Simple listener tool to track remove unwanted ChoiceGenerators that are registered
+ * while an existing ChoiceGenerator is being explored. Multiple ChoiceGenerators of the same
+ * type could be registered spuriously during a while(true) loop.
  */
 public class StateReducer extends ListenerAdapter {
 
+  private boolean debugMode;
+  private boolean stateReductionMode;
   private final PrintWriter out;
-  volatile private String valueVarLock = "";
-  volatile private String valueVarContact = "";
-  volatile private String operation;
-  volatile private String detail;
-  volatile private int depth;
-  volatile private int id;
-  Transition transition;
-  Object annotation;
-  String label;
-  String output;
-  int stateId;
-
-
-  public StateReducer (Config conf, JPF jpf) {
-    out = new PrintWriter(System.out, true);
+  private String detail;
+  private int depth;
+  private int id;
+  private Transition transition;
+
+  // Holds values that have appeared during CG advances
+  private HashSet<Integer> cgChoiceSet;
+  private Integer choiceUpperBound;
+  private boolean isInitialized;
+
+  public StateReducer (Config config, JPF jpf) {
+    debugMode = config.getBoolean("debug_state_transition", false);
+    stateReductionMode = config.getBoolean("activate_state_reduction", true);
+    if (debugMode) {
+      out = new PrintWriter(System.out, true);
+    } else {
+      out = null;
+    }
+    detail = null;
+    depth = 0;
+    id = 0;
+    transition = null;
+    cgChoiceSet = new HashSet<>();
+    choiceUpperBound = 0;
+    isInitialized = false;
   }
 
   @Override
   public void stateRestored(Search search) {
-    id = search.getStateId();
-    depth = search.getDepth();
-    operation = "restored";
-    transition = search.getTransition();
-    if (transition != null) {
-      annotation = transition.getAnnotation();
-      label = transition.getLabel();
-      output = transition.getOutput();
-      stateId = transition.getStateId();
+    if (debugMode) {
+      id = search.getStateId();
+      depth = search.getDepth();
+      transition = search.getTransition();
+      detail = null;
+      out.println("\n==> DEBUG: The state is restored to state with id: " + id + " -- Transition: " + transition +
+              " and depth: " + depth + "\n");
     }
-    detail = null;
-
-    out.println("\nThe state is restored to state with id: "+id+" Transition: "+transition+", with depth: "+depth+"##LockValue: "+valueVarLock+", ##ContactValue: "+valueVarContact +"\n");
   }
 
   //--- the ones we are interested in
   @Override
   public void searchStarted(Search search) {
-    out.println("----------------------------------- search started");
+    if (debugMode) {
+      out.println("\n==> DEBUG: ----------------------------------- search started" + "\n");
+    }
   }
 
-  // Holds values that have appeared during CG advances
-  private HashSet<Integer> cgChoiceSet = new HashSet<>();
-  private Integer choiceUpperBound = 0;
-  private boolean isInitialized = false;
-
   @Override
   public void choiceGeneratorSet (VM vm, ChoiceGenerator<?> newCG) {
-
-    // Initialize with necessary information from the CG
-    if (newCG instanceof IntIntervalGenerator) {
-      IntIntervalGenerator iigCG = (IntIntervalGenerator) newCG;
-      // Check if CG has been initialized, otherwise initialize it
-      if (!isInitialized) {
-        Integer[] choices = iigCG.getChoices();
-        // Get the upper bound from the last element of the choices
-        choiceUpperBound = choices[choices.length - 1];
-        isInitialized = true;
-      } else {
-        newCG.setDone();
+    if (stateReductionMode) {
+      // Initialize with necessary information from the CG
+      if (newCG instanceof IntIntervalGenerator) {
+        IntIntervalGenerator iigCG = (IntIntervalGenerator) newCG;
+        // Check if CG has been initialized, otherwise initialize it
+        if (!isInitialized) {
+          Integer[] choices = iigCG.getChoices();
+          // Get the upper bound from the last element of the choices
+          choiceUpperBound = choices[choices.length - 1];
+          isInitialized = true;
+        } else {
+          newCG.setDone();
+        }
       }
     }
   }
 
   @Override
   public void choiceGeneratorAdvanced (VM vm, ChoiceGenerator<?> currentCG) {
-    // Check every choice generated and make sure that all the available choices
-    // are chosen first before repeating the same choice of value twice!
-    if (currentCG instanceof IntIntervalGenerator) {
-      IntIntervalGenerator iigCG = (IntIntervalGenerator) currentCG;
-      Integer nextChoice = iigCG.getNextChoice();
-      if (!cgChoiceSet.contains(nextChoice)) {
-        cgChoiceSet.add(nextChoice);
-      }
-      // Allow reinitialization after an upper bound is hit
-      // This means all available choices have been explored once during this iteration
-      if (cgChoiceSet.contains(choiceUpperBound)) {
-        isInitialized = false;
-        cgChoiceSet.clear();
+    if (stateReductionMode) {
+      // Check every choice generated and make sure that all the available choices
+      // are chosen first before repeating the same choice of value twice!
+      if (currentCG instanceof IntIntervalGenerator) {
+        IntIntervalGenerator iigCG = (IntIntervalGenerator) currentCG;
+        Integer nextChoice = iigCG.getNextChoice();
+        if (!cgChoiceSet.contains(nextChoice)) {
+          cgChoiceSet.add(nextChoice);
+        }
+        // Allow reinitialization after an upper bound is hit
+        // This means all available choices have been explored once during this iteration
+        if (cgChoiceSet.contains(choiceUpperBound)) {
+          isInitialized = false;
+          cgChoiceSet.clear();
+        }
       }
     }
   }
 
   @Override
   public void stateAdvanced(Search search) {
-    String theEnd = null;
-    id = search.getStateId();
-    depth = search.getDepth();
-    operation = "forward";
-    transition = search.getTransition();
-    if (transition != null) {
-      annotation = transition.getAnnotation();
-      label = transition.getLabel();
-      output = transition.getOutput();
-      stateId = transition.getStateId();
-    }
-    if (search.isNewState()) {
-      detail = "new";
-    } else {
-      detail = "visited";
-    }
-
-    if (search.isEndState()) {
-      out.println("This is the last state!");
-      theEnd = "end";
-    }
-
-    //out.println("stateId: "+stateId+", output: "+output+", label: "+label);
-    out.println("\nThe state is forwarded to state with id: "+id+", with depth: "+depth+" which is "+detail+" Transition: "+transition+" state: "+"% "+theEnd+"##LockValue: "+valueVarLock+", ##ContactValue: "+valueVarContact +"\n");
+    if (debugMode) {
+      id = search.getStateId();
+      depth = search.getDepth();
+      transition = search.getTransition();
+      if (search.isNewState()) {
+        detail = "new";
+      } else {
+        detail = "visited";
+      }
 
-    if (search.isEndState()) {
-      detail += " end";
+      if (search.isEndState()) {
+        out.println("\n==> DEBUG: This is the last state!\n");
+        detail += " end";
+      }
+      out.println("\n==> DEBUG: The state is forwarded to state with id: " + id + " with depth: " + depth +
+              " which is " + detail + " Transition: " + transition + "\n");
     }
   }
 
   @Override
   public void stateBacktracked(Search search) {
-    id = search.getStateId();
-    depth = search.getDepth();
-    operation = "backtrack";
-    transition = search.getTransition();
-    if (transition != null) {
-      annotation = transition.getAnnotation();
-      label = transition.getLabel();
-      output = transition.getOutput();
-      stateId = transition.getStateId();
+    if (debugMode) {
+      id = search.getStateId();
+      depth = search.getDepth();
+      transition = search.getTransition();
+      detail = null;
+
+      out.println("\n==> DEBUG: The state is backtracked to state with id: " + id + " -- Transition: " + transition +
+              " and depth: " + depth + "\n");
     }
-    detail = null;
-
-    out.println("\nThe state is backtracked to state with id: "+id+" Transition: "+ transition+", with depth: "+depth+"##LockValue: "+valueVarLock+", ##ContactValue: "+valueVarContact +"\n");
   }
 
   @Override
   public void searchFinished(Search search) {
-    out.println("----------------------------------- search finished");
+    if (debugMode) {
+      out.println("\n==> DEBUG: ----------------------------------- search finished" + "\n");
+    }
   }
 }
\ No newline at end of file