コード例 #1
0
 public static void main(String args[]) throws IOException {
   // Main array where we will store our initial clauses.
   ArrayList<Clause> Clauses = new ArrayList<Clause>();
   BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
   // Creating data structure.
   System.out.println("How many clauses does your formula in the Conjuctive Normal Form have?");
   int nClauses = Integer.parseInt(in.readLine());
   System.out.println("Please introduce your clauses.");
   System.out.println("(Input description in the readme file.");
   for (int i = 0; i < nClauses; i++) {
     String literalsList = in.readLine();
     String[] literals = literalsList.split(" ");
     Clause clause = new Clause();
     for (int i1 = 0; i1 < literals.length; i1++) {
       clause.addLiteral(literals[i1]);
     }
     Clauses.add(clause);
   }
   // End of data structure
   if (DLL(Clauses)) {
     System.out.println("Result: The formula is satisfactable.");
   } else {
     System.out.println("Result: The formula is  not satisfactable.");
   }
 }
コード例 #2
0
  /*	This is the main algorithm.
   * 	It receives a formula and processes it until
   * 	it can return true or false.
   */
  static boolean DLL(ArrayList<Clause> Clauses) {
    // Unitary Propagation
    while (true) {
      String literalToRemove = searchSingleLiteral(Clauses);
      if (!literalToRemove.equals("NotFoundYet")) {
        printClauses(Clauses);
        System.out.println("Performing unitary propagation with: " + literalToRemove);
        removeClauses(literalToRemove, Clauses);
        cutClauses(literalToRemove, Clauses);
        printClauses(Clauses);
        if (Clauses.size() == 0) {
          System.out.println("All clauses removed. Returning true.");
          return true;
        }
        if (hasFalsehood(Clauses)) {
          System.out.println("Falsehood detected. Returning false.");
          return false;
        } else if (hasEmptyClause(Clauses)) {
          System.out.println("Empty clause detected. Returning false.");
          return false;
        }
      } else {
        System.out.println("No single literals.");
        System.out.println("Cannot perform unitary propagation.");
        break;
      }
    }
    ArrayList<Clause> copy1 = new ArrayList<Clause>();
    ArrayList<Clause> copy2 = new ArrayList<Clause>();
    for (Clause c : Clauses) {
      Clause c2 = new Clause();
      for (String s : c.literals) {
        c2.addLiteral(s);
      }
      copy1.add(c2);
    }
    for (Clause c : Clauses) {
      Clause c2 = new Clause();
      for (String s : c.literals) {
        c2.addLiteral(s);
      }
      copy2.add(c2);
    }
    Clause clause1 = new Clause();
    Clause clause2 = new Clause();
    String l1 = pickLiteral(Clauses);
    String l2 = "";

    if (l1.startsWith("-")) l2 = l1.substring(1);
    else l2 = "-" + l1;
    clause1.addLiteral(l1);
    clause2.addLiteral(l2);
    copy1.add(clause1);
    copy2.add(clause2);
    // Moment of the truth
    System.out.println("Adding clause: [" + l1 + "]");
    if (DLL(copy1) == true) {
      return true;
    } else {
      System.out.println("Trying opposite clause: [" + l2 + "]");
      return DLL(copy2);
    }
  }