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;
}
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());
}
}
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;
}
Aggregations