Search in sources :

Example 11 with Not

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;
}
Also used : Formula(org.logicng.formulas.Formula) Not(org.logicng.formulas.Not) BinaryOperator(org.logicng.formulas.BinaryOperator) NAryOperator(org.logicng.formulas.NAryOperator)

Example 12 with Not

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;
}
Also used : Formula(org.logicng.formulas.Formula) Not(org.logicng.formulas.Not) TreeSet(java.util.TreeSet) Literal(org.logicng.formulas.Literal) BinaryOperator(org.logicng.formulas.BinaryOperator) SortedSet(java.util.SortedSet) NAryOperator(org.logicng.formulas.NAryOperator) PBConstraint(org.logicng.formulas.PBConstraint)

Example 13 with Not

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;
}
Also used : Formula(org.logicng.formulas.Formula) Not(org.logicng.formulas.Not) Tristate(org.logicng.datastructures.Tristate)

Aggregations

Not (org.logicng.formulas.Not)13 Formula (org.logicng.formulas.Formula)9 PBConstraint (org.logicng.formulas.PBConstraint)8 BinaryOperator (org.logicng.formulas.BinaryOperator)7 Literal (org.logicng.formulas.Literal)7 NAryOperator (org.logicng.formulas.NAryOperator)7 FormulaFactory (org.logicng.formulas.FormulaFactory)4 Implication (org.logicng.formulas.Implication)4 ArrayList (java.util.ArrayList)3 Equivalence (org.logicng.formulas.Equivalence)3 SortedSet (java.util.SortedSet)2 TreeSet (java.util.TreeSet)2 And (org.logicng.formulas.And)2 Variable (org.logicng.formulas.Variable)2 Iterator (java.util.Iterator)1 EncodingResult (org.logicng.datastructures.EncodingResult)1 Tristate (org.logicng.datastructures.Tristate)1 FType (org.logicng.formulas.FType)1 Or (org.logicng.formulas.Or)1