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;
}
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);
}
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);
}
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);
}
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);
}
Aggregations