/**
   * This function performs an individual formatting test after the input and output streams have
   * been created.
   *
   * @param commands the input that decides which tests to perform
   * @return a String holding the error messages for any failed tests or null if no tests are
   *     failed.
   */
  private static /*@Nullable*/ String performTest(LineNumberReader commands) {
    StringBuffer output = new StringBuffer();
    //  List invariantTestCases = new Vector();
    boolean noTestFailed = true;

    while (true) {
      // Create a new test case
      //  FormatTestCase currentCase = FormatTestCase.instantiate(commands, generateGoals);

      // if (currentCase == null)
      //  break;
      //  else {
      //  invariantTestCases.add(currentCase);
      String results = AddAndCheckTestCase.runTest(commands);
      if (results == null) break;
      if (!(results.length() == 0)) {
        //  output.print(currentCase.getDiffString());
        output.append(results);
        noTestFailed = false;
      }
    }
    if (noTestFailed) {
      return null;
    } else {
      return output.toString();
    }
  }
  private static String generateCommands(LineNumberReader input) {
    StringBuffer output = new StringBuffer();

    while (true) {
      String commands = AddAndCheckTestCase.generateTest(input);
      if (commands == null) break;
      output.append(commands);
    }
    return output.toString();
  }
 static String combineDummy(String inv, String daikonStr, String esc, String simplify) {
   StringBuffer combined = new StringBuffer(inv);
   combined.append(lineSep + "\tDAIKON_FORMAT ");
   combined.append(daikonStr);
   combined.append(lineSep + "\tESC_FORMAT ");
   combined.append(esc);
   combined.append(lineSep + "\tSIMPLIFY_FORMAT ");
   combined.append(simplify);
   return combined.toString();
 }
  private String format_simplify() {
    if (intersect == null || intersect.length == 0) {
      return "(AND)";
    }
    String[] name = var().simplifyNameAndBounds();
    if (name == null) {
      return format_unimplemented(OutputFormat.SIMPLIFY);
    }
    String idx;
    if (!name[0].equals("|i|")) {
      idx = "i";
    } else {
      idx = "j";
    }
    StringBuffer pre_buf = new StringBuffer("");
    StringBuffer end_buf = new StringBuffer("");
    for (int i = 0; i < intersect.length; i++) {
      pre_buf.append("(EXISTS (" + idx + i + ") (AND ");
      pre_buf.append("(>= " + idx + i + " " + name[1] + ") ");
      pre_buf.append("(<= " + idx + i + " " + name[2] + ") ");

      // Based on the class name, I originally wrote this method as if
      // the invariant represented a common subsequence between two
      // sequences (i.e. where the match was required to be in
      // order). In case an invariant like that is added in the
      // future, use the following:
      //       if (i == 0)
      //         pre_buf.append("(>= "+idx+i + " 0) ");
      //       else if (i > 0)
      //          pre_buf.append("(> "+idx+i + " "+idx+(i-1) +") ");
      //       if (i == intersect.length - 1)
      //         pre_buf.append("(< "+idx+i + " (select arrayLength " + name[0] + ")) ");
      pre_buf.append(
          "(EQ (select (select elems "
              + name[0]
              + ") "
              + idx
              + i
              + ") "
              + simplify_format_double(intersect[i])
              + ")");
      if (i == intersect.length - 1) pre_buf.append(" ");
      end_buf.append("))");
    }
    pre_buf.append(end_buf);
    return pre_buf.toString();
  }
 /**
  * @return a String containing the proper add and check commands for this input lines of this
  *     test case.
  */
 public static String generateTest(LineNumberReader commands) {
   boolean endOfFile = initFields(commands, true);
   if (endOfFile) return null;
   while (true) {
     String commandLine = getNextLine(commands).trim();
     int lineNumber = commands.getLineNumber();
     if (InvariantAddAndCheckTester.isComment(commandLine)) {
       results.append(commandLine + lineSep);
     } else if (isTestTerminator(commandLine)) {
       results.append(commandLine + lineSep + lineSep);
       break;
     } else if (isAddCommand(commandLine) || isCheckCommand(commandLine)) {
       generateCheckOrAddCommand(commandLine, lineNumber);
     } else if (isCompareCommand(commandLine)) {
       // generateCompareCommand(commandLine);
     } else {
       throw new RuntimeException("unrecognized command");
     }
   }
   return results.toString();
 }
 /**
  * @return String containing error messages for any failed cases. In the case that there are no
  *     failed cases, the empty string is returned. In the case where commands is empty (there
  *     are no more test cases and the end of the file has been reached), null is returned.
  */
 public static String runTest(LineNumberReader commands) {
   boolean endOfFile = initFields(commands, false);
   if (endOfFile) {
     return null;
   }
   while (true) {
     String commandLine = getNextRealLine(commands);
     int lineNumber = commands.getLineNumber();
     if (InvariantAddAndCheckTester.isComment(commandLine)) {
       continue;
     } else if (isTestTerminator(commandLine)) {
       break;
     } else if (isAddCommand(commandLine) || isCheckCommand(commandLine)) {
       exicuteCheckOrAddCommand(commandLine, lineNumber);
     } else if (isCompareCommand(commandLine)) {
     } else {
       throw new RuntimeException("unrecognized command");
     }
   }
   return results.toString();
 }
  public static void extract_consequent(PptMap ppts) {
    // Retrieve Ppt objects in sorted order.
    // Use a custom comparator for a specific ordering
    Comparator<PptTopLevel> comparator = new Ppt.NameComparator();
    TreeSet<PptTopLevel> ppts_sorted = new TreeSet<PptTopLevel>(comparator);
    ppts_sorted.addAll(ppts.asCollection());

    for (PptTopLevel ppt : ppts_sorted) {
      extract_consequent_maybe(ppt, ppts);
    }

    PrintWriter pw = new PrintWriter(System.out, true);

    // All conditions at a program point.  A TreeSet to enable
    // deterministic output.
    TreeSet<String> allConds = new TreeSet<String>();
    for (String pptname : pptname_to_conditions.keySet()) {
      Map<String, Map<String, HashedConsequent>> cluster_to_conditions =
          pptname_to_conditions.get(pptname);
      for (Map.Entry</*@KeyFor("cluster_to_conditions")*/ String, Map<String, HashedConsequent>>
          entry : cluster_to_conditions.entrySet()) {
        String predicate = entry.getKey();
        Map<String, HashedConsequent> conditions = entry.getValue();
        StringBuffer conjunctionJava = new StringBuffer();
        StringBuffer conjunctionDaikon = new StringBuffer();
        StringBuffer conjunctionESC = new StringBuffer();
        StringBuffer conjunctionSimplify = new StringBuffer("(AND ");
        int count = 0;
        for (Map.Entry</*@KeyFor("conditions")*/ String, HashedConsequent> entry2 :
            conditions.entrySet()) {
          count++;
          String condIndex = entry2.getKey();
          HashedConsequent cond = entry2.getValue();
          if (cond.fakeFor != null) {
            count--;
            continue;
          }
          String javaStr = cond.inv.format_using(OutputFormat.JAVA);
          String daikonStr = cond.inv.format_using(OutputFormat.DAIKON);
          String escStr = cond.inv.format_using(OutputFormat.ESCJAVA);
          String simplifyStr = cond.inv.format_using(OutputFormat.SIMPLIFY);
          allConds.add(combineDummy(condIndex, "<dummy> " + daikonStr, escStr, simplifyStr));
          //           allConds.add(condIndex);
          if (count > 0) {
            conjunctionJava.append(" && ");
            conjunctionDaikon.append(" and ");
            conjunctionESC.append(" && ");
            conjunctionSimplify.append(" ");
          }
          conjunctionJava.append(javaStr);
          conjunctionDaikon.append(daikonStr);
          conjunctionESC.append(escStr);
          conjunctionSimplify.append(simplifyStr);
        }
        conjunctionSimplify.append(")");
        String conj = conjunctionJava.toString();
        // Avoid inserting self-contradictory conditions such as "x == 1 &&
        // x == 2", or conjunctions of only a single condition.
        if (count < 2
            || contradict_inv_pattern.matcher(conj).find()
            || useless_inv_pattern_1.matcher(conj).find()
            || useless_inv_pattern_2.matcher(conj).find()) {
          // System.out.println("Suppressing: " + conj);
        } else {
          allConds.add(
              combineDummy(
                  conjunctionJava.toString(),
                  conjunctionDaikon.toString(),
                  conjunctionESC.toString(),
                  conjunctionSimplify.toString()));
        }
      }

      if (allConds.size() > 0) {
        pw.println();
        pw.println("PPT_NAME " + pptname);
        for (String s : allConds) {
          pw.println(s);
        }
      }
      allConds.clear();
    }

    pw.flush();
  }