Search in sources :

Example 21 with Formula

use of org.logicng.formulas.Formula in project LogicNG by logic-ng.

the class DNFPredicate method test.

@Override
public boolean test(final Formula formula, final boolean cache) {
    final Tristate cached = formula.predicateCacheEntry(IS_DNF);
    if (cached != Tristate.UNDEF) {
        return cached == Tristate.TRUE;
    }
    boolean result;
    switch(formula.type()) {
        case FALSE:
        case TRUE:
        case LITERAL:
            return true;
        case NOT:
        case IMPL:
        case EQUIV:
        case PBC:
            return false;
        case OR:
            result = true;
            for (final Formula op : formula) {
                if (!getMaxtermPredicate().test(op, false)) {
                    result = false;
                }
            }
            break;
        case AND:
            result = getMaxtermPredicate().test(formula, false);
            break;
        default:
            throw new IllegalArgumentException("Cannot compute DNF predicate on " + formula.type());
    }
    if (cache) {
        formula.setPredicateCacheEntry(IS_DNF, result);
    }
    return result;
}
Also used : Formula(org.logicng.formulas.Formula) Tristate(org.logicng.datastructures.Tristate)

Example 22 with Formula

use of org.logicng.formulas.Formula in project LogicNG by logic-ng.

the class EvaluatesToConstantPredicate method handleEquivalence.

private Formula handleEquivalence(final Equivalence formula, final boolean topLevel) {
    final FormulaFactory f = formula.factory();
    final Formula left = formula.left();
    final Formula right = formula.right();
    final Formula leftResult = innerTest(left, false);
    if (leftResult.isConstantFormula()) {
        return isVerum(leftResult) ? innerTest(right, topLevel) : innerTest(f.not(right), topLevel);
    }
    final Formula rightResult = innerTest(right, false);
    if (rightResult.isConstantFormula()) {
        if (topLevel) {
            return f.constant(!this.evaluatesToTrue);
        }
        return isVerum(rightResult) ? leftResult : f.not(leftResult);
    }
    return f.equivalence(leftResult, rightResult);
}
Also used : Formula(org.logicng.formulas.Formula) FormulaFactory(org.logicng.formulas.FormulaFactory)

Example 23 with Formula

use of org.logicng.formulas.Formula in project LogicNG by logic-ng.

the class EvaluatesToConstantPredicate method handleNot.

private Formula handleNot(final Not formula, final boolean topLevel) {
    final FormulaFactory f = formula.factory();
    final Formula opResult = innerTest(formula.operand(), false);
    if (topLevel && !opResult.isConstantFormula()) {
        return f.constant(!this.evaluatesToTrue);
    }
    return opResult.isConstantFormula() ? f.constant(isFalsum(opResult)) : f.not(opResult);
}
Also used : Formula(org.logicng.formulas.Formula) FormulaFactory(org.logicng.formulas.FormulaFactory)

Example 24 with Formula

use of org.logicng.formulas.Formula in project LogicNG by logic-ng.

the class EvaluatesToConstantPredicate method handleImplication.

private Formula handleImplication(final Implication formula, final boolean topLevel) {
    final FormulaFactory f = formula.factory();
    final Formula left = formula.left();
    final Formula right = formula.right();
    final Formula leftResult = innerTest(left, false);
    if (leftResult.isConstantFormula()) {
        if (this.evaluatesToTrue) {
            return isFalsum(leftResult) ? f.verum() : innerTest(right, topLevel);
        } else {
            return isVerum(leftResult) ? innerTest(right, topLevel) : f.verum();
        }
    }
    if (!this.evaluatesToTrue && topLevel) {
        return f.verum();
    }
    final Formula rightResult = innerTest(right, false);
    if (rightResult.isConstantFormula()) {
        return isVerum(rightResult) ? f.verum() : f.not(leftResult);
    }
    return f.implication(leftResult, rightResult);
}
Also used : Formula(org.logicng.formulas.Formula) FormulaFactory(org.logicng.formulas.FormulaFactory)

Example 25 with Formula

use of org.logicng.formulas.Formula in project LogicNG by logic-ng.

the class EvaluatesToConstantPredicate method handleAnd.

private Formula handleAnd(final And formula, final boolean topLevel) {
    final FormulaFactory f = formula.factory();
    final List<Formula> nops = new ArrayList<>();
    for (final Formula op : formula) {
        final Formula opResult = innerTest(op, this.evaluatesToTrue && topLevel);
        if (isFalsum(opResult)) {
            return f.falsum();
        }
        if (!opResult.isConstantFormula()) {
            if (this.evaluatesToTrue && topLevel) {
                return f.falsum();
            }
            nops.add(opResult);
        }
    }
    return f.and(nops);
}
Also used : Formula(org.logicng.formulas.Formula) FormulaFactory(org.logicng.formulas.FormulaFactory) ArrayList(java.util.ArrayList)

Aggregations

Formula (org.logicng.formulas.Formula)349 Test (org.junit.jupiter.api.Test)190 FormulaFactory (org.logicng.formulas.FormulaFactory)129 ArrayList (java.util.ArrayList)58 Variable (org.logicng.formulas.Variable)55 Literal (org.logicng.formulas.Literal)54 Assignment (org.logicng.datastructures.Assignment)50 PropositionalParser (org.logicng.io.parsers.PropositionalParser)50 PBConstraint (org.logicng.formulas.PBConstraint)45 SATSolver (org.logicng.solvers.SATSolver)36 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)25 CardinalityConstraint (org.logicng.formulas.CardinalityConstraint)25 MethodSource (org.junit.jupiter.params.provider.MethodSource)24 TreeSet (java.util.TreeSet)21 BDDKernel (org.logicng.knowledgecompilation.bdds.jbuddy.BDDKernel)20 TautologyPredicate (org.logicng.predicates.satisfiability.TautologyPredicate)20 HashMap (java.util.HashMap)17 LinkedHashSet (java.util.LinkedHashSet)17 List (java.util.List)17 LogicNGTest (org.logicng.LogicNGTest)16