Search in sources :

Example 1 with Equivalence

use of org.logicng.formulas.Equivalence in project LogicNG by logic-ng.

the class NNFTransformation method applyRec.

private Formula applyRec(final Formula formula, final boolean polarity) {
    final FormulaFactory f = formula.factory();
    Formula nnf;
    if (polarity) {
        nnf = formula.transformationCacheEntry(NNF);
        if (nnf != null) {
            return nnf;
        }
    }
    final FType type = formula.type();
    switch(type) {
        case TRUE:
        case FALSE:
        case LITERAL:
            nnf = polarity ? formula : formula.negate();
            break;
        case NOT:
            nnf = applyRec(((Not) formula).operand(), !polarity);
            break;
        case OR:
        case AND:
            nnf = applyRec(formula.iterator(), formula.type(), polarity, f);
            break;
        case EQUIV:
            final Equivalence equiv = (Equivalence) formula;
            if (polarity) {
                nnf = f.and(f.or(applyRec(equiv.left(), false), applyRec(equiv.right(), true)), f.or(applyRec(equiv.left(), true), applyRec(equiv.right(), false)));
            } else {
                nnf = f.and(f.or(applyRec(equiv.left(), false), applyRec(equiv.right(), false)), f.or(applyRec(equiv.left(), true), applyRec(equiv.right(), true)));
            }
            break;
        case IMPL:
            final Implication impl = (Implication) formula;
            if (polarity) {
                nnf = f.or(applyRec(impl.left(), false), applyRec(impl.right(), true));
            } else {
                nnf = f.and(applyRec(impl.left(), true), applyRec(impl.right(), false));
            }
            break;
        case PBC:
            final PBConstraint pbc = (PBConstraint) formula;
            if (polarity) {
                final List<Formula> encoding = pbc.getEncoding();
                nnf = applyRec(encoding.iterator(), FType.AND, true, f);
            } else {
                nnf = applyRec(pbc.negate(), true);
            }
            break;
        default:
            throw new IllegalStateException("Unknown formula type = " + type);
    }
    if (polarity) {
        formula.setTransformationCacheEntry(NNF, nnf);
    }
    return nnf;
}
Also used : Formula(org.logicng.formulas.Formula) FormulaFactory(org.logicng.formulas.FormulaFactory) FType(org.logicng.formulas.FType) Not(org.logicng.formulas.Not) Equivalence(org.logicng.formulas.Equivalence) Implication(org.logicng.formulas.Implication) PBConstraint(org.logicng.formulas.PBConstraint)

Example 2 with Equivalence

use of org.logicng.formulas.Equivalence in project LogicNG by logic-ng.

the class FormulaFactoryImporter method apply.

@Override
public Formula apply(final Formula formula, final boolean cache) {
    if (formula.factory() == this.newFormulaFactory) {
        return formula;
    }
    switch(formula.type()) {
        case TRUE:
            return this.newFormulaFactory.verum();
        case FALSE:
            return this.newFormulaFactory.falsum();
        case LITERAL:
            final Literal literal = (Literal) formula;
            return this.newFormulaFactory.literal(literal.name(), literal.phase());
        case NOT:
            final Not not = (Not) formula;
            return this.newFormulaFactory.not(apply(not.operand(), cache));
        case IMPL:
            final Implication implication = (Implication) formula;
            return this.newFormulaFactory.implication(apply(implication.left(), cache), apply(implication.right(), cache));
        case EQUIV:
            final Equivalence equivalence = (Equivalence) formula;
            return this.newFormulaFactory.equivalence(apply(equivalence.left(), cache), apply(equivalence.right(), cache));
        case OR:
            final Or or = (Or) formula;
            return this.newFormulaFactory.or(gatherAppliedOperands(or));
        case AND:
            final And and = (And) formula;
            return this.newFormulaFactory.and(gatherAppliedOperands(and));
        case PBC:
            final PBConstraint pbc = (PBConstraint) formula;
            final Literal[] literals = new Literal[pbc.operands().length];
            for (int i = 0; i < pbc.operands().length; i++) {
                literals[i] = (Literal) apply(pbc.operands()[i], cache);
            }
            return this.newFormulaFactory.pbc(pbc.comparator(), pbc.rhs(), literals, pbc.coefficients());
        default:
            throw new IllegalArgumentException("Unknown LogicNG formula type: " + formula.type());
    }
}
Also used : Not(org.logicng.formulas.Not) Or(org.logicng.formulas.Or) Equivalence(org.logicng.formulas.Equivalence) And(org.logicng.formulas.And) Literal(org.logicng.formulas.Literal) Implication(org.logicng.formulas.Implication) PBConstraint(org.logicng.formulas.PBConstraint) PBConstraint(org.logicng.formulas.PBConstraint)

Example 3 with Equivalence

use of org.logicng.formulas.Equivalence in project LogicNG by logic-ng.

the class DistributiveSimplifier method apply.

@Override
public Formula apply(final Formula formula, final boolean cache) {
    final FormulaFactory f = formula.factory();
    final Formula result;
    switch(formula.type()) {
        case FALSE:
        case TRUE:
        case LITERAL:
        case PBC:
            result = formula;
            break;
        case EQUIV:
            final Equivalence equiv = (Equivalence) formula;
            result = f.equivalence(this.apply(equiv.left(), cache), this.apply(equiv.right(), cache));
            break;
        case IMPL:
            final Implication impl = (Implication) formula;
            result = f.implication(this.apply(impl.left(), cache), this.apply(impl.right(), cache));
            break;
        case NOT:
            final Not not = (Not) formula;
            result = f.not(this.apply(not.operand(), cache));
            break;
        case OR:
        case AND:
            result = distributeNAry(formula, cache, f);
            break;
        default:
            throw new IllegalStateException("Unknown formula type: " + formula.type());
    }
    if (cache) {
        formula.setTransformationCacheEntry(TransformationCacheEntry.DISTRIBUTIVE_SIMPLIFICATION, result);
    }
    return result;
}
Also used : Formula(org.logicng.formulas.Formula) FormulaFactory(org.logicng.formulas.FormulaFactory) Not(org.logicng.formulas.Not) Equivalence(org.logicng.formulas.Equivalence) Implication(org.logicng.formulas.Implication)

Aggregations

Equivalence (org.logicng.formulas.Equivalence)3 Implication (org.logicng.formulas.Implication)3 Not (org.logicng.formulas.Not)3 Formula (org.logicng.formulas.Formula)2 FormulaFactory (org.logicng.formulas.FormulaFactory)2 PBConstraint (org.logicng.formulas.PBConstraint)2 And (org.logicng.formulas.And)1 FType (org.logicng.formulas.FType)1 Literal (org.logicng.formulas.Literal)1 Or (org.logicng.formulas.Or)1