use of org.logicng.formulas.Formula in project LogicNG by logic-ng.
the class VariablesFunction method apply.
@Override
public SortedSet<Variable> apply(final Formula formula, final boolean cache) {
final Object cached = formula.functionCacheEntry(VARIABLES);
if (cached != null) {
return (SortedSet<Variable>) cached;
}
SortedSet<Variable> result = new TreeSet<>();
switch(formula.type()) {
case FALSE:
case TRUE:
result = new TreeSet<>();
break;
case LITERAL:
final Literal lit = (Literal) formula;
result.add(lit.variable());
break;
case NOT:
final Not not = (Not) formula;
result = apply(not.operand(), cache);
break;
case IMPL:
case EQUIV:
final BinaryOperator binary = (BinaryOperator) formula;
result.addAll(apply(binary.left(), cache));
result.addAll(apply(binary.right(), cache));
break;
case OR:
case AND:
final NAryOperator nary = (NAryOperator) formula;
for (final Formula op : nary) {
result.addAll(apply(op, cache));
}
break;
case PBC:
final PBConstraint pbc = (PBConstraint) formula;
result = FormulaHelper.variables(pbc.literals());
break;
default:
throw new IllegalStateException("Unknown formula type " + formula.type());
}
result = Collections.unmodifiableSortedSet(result);
if (cache) {
formula.setFunctionCacheEntry(VARIABLES, result);
}
return result;
}
use of org.logicng.formulas.Formula in project LogicNG by logic-ng.
the class DnnfCompiler method conjoin.
protected Formula conjoin(final Formula implied, final DTreeNode tree, final int currentShannons) throws TimeoutException {
final Formula left;
final Formula right;
if (implied == this.f.falsum() || (left = cnfAux(tree.left(), currentShannons)) == this.f.falsum() || (right = cnfAux(tree.right(), currentShannons)) == this.f.falsum()) {
return this.f.falsum();
} else {
return this.f.and(implied, left, right);
}
}
use of org.logicng.formulas.Formula in project LogicNG by logic-ng.
the class DnnfCompiler method cnf2Ddnnf.
protected Formula cnf2Ddnnf(final DTree tree, final int currentShannons) throws TimeoutException {
final BitSet separator = tree.dynamicSeparator();
final Formula implied = this.newlyImpliedLiterals(tree.staticVarSet());
if (separator.isEmpty()) {
if (tree instanceof DTreeLeaf) {
return this.f.and(implied, leaf2Ddnnf((DTreeLeaf) tree));
} else {
return conjoin(implied, (DTreeNode) tree, currentShannons);
}
} else {
final int var = chooseShannonVariable(tree, separator, currentShannons);
if (this.handler != null && !this.handler.shannonExpansion()) {
throw new TimeoutException();
}
/* Positive branch */
Formula positiveDnnf = this.f.falsum();
if (this.solver.decide(var, true)) {
positiveDnnf = cnf2Ddnnf(tree, currentShannons + 1);
}
this.solver.undoDecide(var);
if (positiveDnnf == this.f.falsum()) {
if (this.solver.atAssertionLevel() && this.solver.assertCdLiteral()) {
return cnf2Ddnnf(tree);
} else {
return this.f.falsum();
}
}
/* Negative branch */
Formula negativeDnnf = this.f.falsum();
if (this.solver.decide(var, false)) {
negativeDnnf = cnf2Ddnnf(tree, currentShannons + 1);
}
this.solver.undoDecide(var);
if (negativeDnnf == this.f.falsum()) {
if (this.solver.atAssertionLevel() && this.solver.assertCdLiteral()) {
return cnf2Ddnnf(tree);
} else {
return this.f.falsum();
}
}
final Literal lit = this.solver.litForIdx(var);
final Formula positiveBranch = this.f.and(lit, positiveDnnf);
final Formula negativeBranch = this.f.and(lit.negate(), negativeDnnf);
return this.f.and(implied, this.f.or(positiveBranch, negativeBranch));
}
}
use of org.logicng.formulas.Formula in project LogicNG by logic-ng.
the class DnnfCompiler method compile.
protected Formula compile(final DTree dTree, final DnnfCompilationHandler handler) {
if (this.nonUnitClauses.isAtomicFormula()) {
return this.cnf;
}
if (!this.solver.start()) {
return this.f.falsum();
}
initializeCaches(dTree);
this.handler = handler;
start(handler);
Formula result;
try {
result = cnf2Ddnnf(dTree);
} catch (final TimeoutException e) {
result = null;
}
this.handler = null;
return result == null ? null : this.f.and(this.unitClauses, result);
}
use of org.logicng.formulas.Formula in project LogicNG by logic-ng.
the class DnnfFactory method compile.
/**
* Compiles the given formula to a DNNF instance.
* @param formula the formula
* @return the compiled DNNF
*/
public Dnnf compile(final Formula formula) {
final SortedSet<Variable> originalVariables = new TreeSet<>(formula.variables());
final Formula cnf = formula.cnf();
originalVariables.addAll(cnf.variables());
final Formula simplifedFormula = simplifyFormula(cnf);
final DnnfCompiler compiler = new DnnfCompiler(simplifedFormula);
final Formula dnnf = compiler.compile(new MinFillDTreeGenerator());
return new Dnnf(originalVariables, dnnf);
}
Aggregations