use of org.logicng.formulas.Not 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.Not 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.Not in project LogicNG by logic-ng.
the class AIGPredicate method test.
@Override
public boolean test(final Formula formula, final boolean cache) {
final Tristate cached = formula.predicateCacheEntry(IS_AIG);
if (cached != Tristate.UNDEF) {
return cached == Tristate.TRUE;
}
boolean result;
switch(formula.type()) {
case FALSE:
case TRUE:
case LITERAL:
result = true;
break;
case IMPL:
case EQUIV:
case OR:
case PBC:
result = false;
break;
case NOT:
result = test(((Not) formula).operand(), cache);
break;
case AND:
result = true;
for (final Formula op : formula) {
if (!test(op, cache)) {
result = false;
break;
}
}
break;
default:
throw new IllegalArgumentException("Cannot compute AIG predicate on " + formula.type());
}
if (cache) {
formula.setPredicateCacheEntry(IS_AIG, result);
}
return result;
}
Aggregations