Exemple #1
0
  public FormulaAnalysis(Formula formula) {
    f = formula;

    /*
     * Converts the Formula to Disjunctive Normal Form and collects the clauses
     */
    formula = formula.getDNF();
    Formula[] rawClauses;
    if (formula instanceof Disjunction) {
      Disjunction disj = ((Disjunction) formula).flatten();
      rawClauses = new Formula[disj.getNoFormulas()];
      for (int i = 0; i < rawClauses.length; i++) rawClauses[i] = disj.get(i);
    } else {
      rawClauses = new Formula[] {formula};
    }

    /*
     * Processes each clause
     */
    clauses = new ArrayList<DNFClause>(rawClauses.length);

    List<Atom> posLiterals = new ArrayList<Atom>(4);
    List<Atom> negLiterals = new ArrayList<Atom>(4);

    for (int i = 0; i < rawClauses.length; i++) {
      /*
       * Extracts the positive and negative literals from the clause
       */
      if (rawClauses[i] instanceof Conjunction) {
        Conjunction c = ((Conjunction) rawClauses[i]).flatten();
        for (int j = 0; j < c.getNoFormulas(); j++) {
          if (c.get(j) instanceof Atom) {
            posLiterals.add((Atom) c.get(j));
          } else if (c.get(j) instanceof Negation) {
            Negation n = (Negation) c.get(j);
            if (n.getFormula() instanceof Atom) {
              negLiterals.add((Atom) n.getFormula());
            } else {
              throw new IllegalStateException(
                  "Unexpected sub-Formula. Formula was not in flattened Disjunctive Normal Form.");
            }
          } else {
            throw new IllegalStateException(
                "Unexpected sub-Formula. Formula was not in flattened Disjunctive Normal Form.");
          }
        }
      } else if (rawClauses[i] instanceof Atom) {
        posLiterals.add((Atom) rawClauses[i]);
      } else if (rawClauses[i] instanceof Negation) {
        Negation n = (Negation) rawClauses[i];
        if (n.getFormula() instanceof Atom) {
          negLiterals.add((Atom) n.getFormula());
        } else {
          throw new IllegalStateException(
              "Unexpected sub-Formula. Formula was not in flattened Disjunctive Normal Form.");
        }
      } else {
        throw new IllegalStateException(
            "Unexpected sub-Formula. Formula was not in flattened Disjunctive Normal Form.");
      }

      /*
       * Stores the DNFClause
       */
      clauses.add(new DNFClause(posLiterals, negLiterals));
      posLiterals.clear();
      negLiterals.clear();
    }
  }