use of org.logicng.formulas.BinaryOperator in project LogicNG by logic-ng.
the class BDDFactory method buildRec.
/**
* Recursive build procedure for the BDD.
* <p>
* If a BDD handler is given and the BDD generation is aborted due to the handler, the method will return
* {@link BDDKernel#BDD_ABORT} as result. If {@code null} is passed as handler, the generation will continue without
* interruption.
* @param formula the formula
* @param kernel the BDD kernel
* @param construction the BDD construction instance
* @param handler the BDD handler
* @return the BDD index or {@link BDDKernel#BDD_ABORT} if the computation was aborted
*/
private static int buildRec(final Formula formula, final BDDKernel kernel, final BDDConstruction construction, final BDDHandler handler) {
switch(formula.type()) {
case FALSE:
return BDDKernel.BDD_FALSE;
case TRUE:
return BDDKernel.BDD_TRUE;
case LITERAL:
final Literal lit = (Literal) formula;
final int idx = kernel.getOrAddVarIndex(lit.variable());
return lit.phase() ? construction.ithVar(idx) : construction.nithVar(idx);
case NOT:
{
final Not not = (Not) formula;
final int operand = buildRec(not.operand(), kernel, construction, handler);
if (operand == BDDKernel.BDD_ABORT) {
return BDDKernel.BDD_ABORT;
}
final int res = kernel.addRef(construction.not(operand), handler);
kernel.delRef(operand);
return res;
}
case IMPL:
case EQUIV:
final BinaryOperator binary = (BinaryOperator) formula;
final int left = buildRec(binary.left(), kernel, construction, handler);
if (left == BDDKernel.BDD_ABORT) {
return BDDKernel.BDD_ABORT;
}
final int right = buildRec(binary.right(), kernel, construction, handler);
if (right == BDDKernel.BDD_ABORT) {
return BDDKernel.BDD_ABORT;
}
int res = kernel.addRef(binary instanceof Implication ? construction.implication(left, right) : construction.equivalence(left, right), handler);
kernel.delRef(left);
kernel.delRef(right);
return res;
case AND:
case OR:
{
final Iterator<Formula> it = formula.iterator();
res = buildRec(it.next(), kernel, construction, handler);
if (res == BDDKernel.BDD_ABORT) {
return BDDKernel.BDD_ABORT;
}
while (it.hasNext()) {
final int operand = buildRec(it.next(), kernel, construction, handler);
if (operand == BDDKernel.BDD_ABORT) {
return BDDKernel.BDD_ABORT;
}
final int previous = res;
res = formula instanceof And ? kernel.addRef(construction.and(res, operand), handler) : kernel.addRef(construction.or(res, operand), handler);
kernel.delRef(previous);
kernel.delRef(operand);
}
return res;
}
case PBC:
return buildRec(formula.nnf(), kernel, construction, handler);
default:
throw new IllegalArgumentException("Unsupported operator for BDD generation: " + formula.type());
}
}
use of org.logicng.formulas.BinaryOperator in project LogicNG by logic-ng.
the class NumberOfAtomsFunction method apply.
@Override
public Long apply(final Formula formula, final boolean cache) {
final Object cached = formula.functionCacheEntry(NUMBER_OF_ATOMS);
if (cached != null) {
return (Long) cached;
}
long result = 0L;
switch(formula.type()) {
case FALSE:
case TRUE:
case LITERAL:
case PBC:
result = 1L;
break;
case NOT:
result = apply(((Not) formula).operand(), cache);
break;
case IMPL:
case EQUIV:
final BinaryOperator binary = (BinaryOperator) formula;
result = apply(binary.left(), cache) + apply(binary.right(), cache);
break;
case OR:
case AND:
final NAryOperator nary = (NAryOperator) formula;
for (final Formula op : nary) {
result += apply(op, cache);
}
break;
default:
throw new IllegalStateException("Unknown formula type " + formula.type());
}
if (cache) {
formula.setFunctionCacheEntry(NUMBER_OF_ATOMS, result);
}
return result;
}
use of org.logicng.formulas.BinaryOperator in project LogicNG by logic-ng.
the class NumberOfNodesFunction method apply.
@Override
public Long apply(final Formula formula, final boolean cache) {
final Object cached = formula.functionCacheEntry(NUMBER_OF_NODES);
if (cached != null) {
return (Long) cached;
}
long result;
switch(formula.type()) {
case FALSE:
case TRUE:
case LITERAL:
result = 1L;
break;
case NOT:
result = apply(((Not) formula).operand(), cache) + 1L;
break;
case IMPL:
case EQUIV:
final BinaryOperator binary = (BinaryOperator) formula;
result = apply(binary.left(), cache) + apply(binary.right(), cache) + 1L;
break;
case OR:
case AND:
final NAryOperator nary = (NAryOperator) formula;
result = 1L;
for (final Formula op : nary) {
result += apply(op, cache);
}
break;
case PBC:
final PBConstraint pbc = (PBConstraint) formula;
result = 1L + pbc.operands().length;
break;
default:
throw new IllegalStateException("Unknown formula type " + formula.type());
}
if (cache) {
formula.setFunctionCacheEntry(NUMBER_OF_NODES, result);
}
return result;
}
use of org.logicng.formulas.BinaryOperator in project LogicNG by logic-ng.
the class LiteralsFunction method apply.
@Override
public SortedSet<Literal> apply(final Formula formula, final boolean cache) {
final Object cached = formula.functionCacheEntry(LITERALS);
if (cached != null) {
return (SortedSet<Literal>) cached;
}
SortedSet<Literal> result = new TreeSet<>();
switch(formula.type()) {
case FALSE:
case TRUE:
result = new TreeSet<>();
break;
case LITERAL:
final Literal lit = (Literal) formula;
result.add(lit);
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.literals(pbc.operands());
break;
default:
throw new IllegalStateException("Unknown formula type " + formula.type());
}
result = Collections.unmodifiableSortedSet(result);
if (cache) {
formula.setFunctionCacheEntry(LITERALS, result);
}
return result;
}
use of org.logicng.formulas.BinaryOperator in project LogicNG by logic-ng.
the class BFSOrdering method bfs.
private LinkedHashSet<Variable> bfs(final Formula formula) {
final LinkedHashSet<Variable> variables = new LinkedHashSet<>();
final Queue<Formula> queue = new LinkedList<>();
queue.add(formula);
while (!queue.isEmpty()) {
final Formula current = queue.remove();
switch(current.type()) {
case LITERAL:
final Literal lit = (Literal) current;
if (lit.phase()) {
variables.add(lit.variable());
} else {
queue.add(lit.variable());
}
break;
case NOT:
queue.add(((Not) current).operand());
break;
case IMPL:
case EQUIV:
final BinaryOperator op = (BinaryOperator) current;
queue.add(op.left());
queue.add(op.right());
break;
case AND:
case OR:
for (final Formula operand : current) {
queue.add(operand);
}
break;
case PBC:
final PBConstraint pbc = (PBConstraint) current;
for (final Literal literal : pbc.operands()) {
variables.add(literal.variable());
}
break;
}
}
return variables;
}
Aggregations