use of org.logicng.formulas.Not in project LogicNG by logic-ng.
the class FormulaStringRepresentation method toInnerString.
/**
* Returns the string representation of the given formula.
* <p>
* This method is used for recursive calls in order to format the sub-formulas.
* @param formula the formula
* @return the string representation of the formula
*/
protected String toInnerString(final Formula formula) {
switch(formula.type()) {
case FALSE:
return this.falsum();
case TRUE:
return this.verum();
case LITERAL:
final Literal lit = (Literal) formula;
return lit.phase() ? lit.name() : this.negation() + lit.name();
case NOT:
final Not not = (Not) formula;
return this.negation() + this.bracket(not.operand());
case IMPL:
case EQUIV:
final BinaryOperator binary = (BinaryOperator) formula;
String op = formula.type() == FType.IMPL ? this.implication() : this.equivalence();
return this.binaryOperator(binary, op);
case AND:
case OR:
final NAryOperator nary = (NAryOperator) formula;
op = formula.type() == FType.AND ? this.and() : this.or();
return this.naryOperator(nary, String.format("%s", op));
case PBC:
final PBConstraint pbc = (PBConstraint) formula;
return String.format("%s%s%d", this.pbLhs(pbc.operands(), pbc.coefficients()), this.pbComparator(pbc.comparator()), pbc.rhs());
default:
throw new IllegalArgumentException("Cannot print the unknown formula type " + formula.type());
}
}
use of org.logicng.formulas.Not in project LogicNG by logic-ng.
the class SortedStringRepresentation method toInnerString.
/**
* Returns the sorted string representation of the given formula.
* @param formula the formula
* @return the sorted string representation of the formula with regard to the variable ordering
*/
@Override
public String toInnerString(final Formula formula) {
switch(formula.type()) {
case FALSE:
return falsum();
case TRUE:
return verum();
case LITERAL:
final Literal lit = (Literal) formula;
return lit.phase() ? lit.name() : negation() + lit.name();
case NOT:
final Not not = (Not) formula;
return negation() + bracket(not.operand());
case IMPL:
return binaryOperator((BinaryOperator) formula, implication());
case EQUIV:
return sortedEquivalence((Equivalence) formula);
case AND:
case OR:
final NAryOperator nary = (NAryOperator) formula;
final String op = formula.type() == FType.AND ? and() : or();
return naryOperator(nary, String.format("%s", op));
case PBC:
final PBConstraint pbc = (PBConstraint) formula;
return String.format("%s%s%d", pbLhs(pbc.operands(), pbc.coefficients()), pbComparator(pbc.comparator()), pbc.rhs());
default:
throw new IllegalArgumentException("Cannot print the unknown formula type " + formula.type());
}
}
use of org.logicng.formulas.Not in project LogicNG by logic-ng.
the class VariablesFunction method apply.
@Override
public SortedSet<Variable> apply(final Formula formula, final boolean cache) {
final Object cached = formula.functionCacheEntry(VARIABLES);
if (cached != null) {
return (SortedSet<Variable>) cached;
}
SortedSet<Variable> result = new TreeSet<>();
switch(formula.type()) {
case FALSE:
case TRUE:
result = new TreeSet<>();
break;
case LITERAL:
final Literal lit = (Literal) formula;
result.add(lit.variable());
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.variables(pbc.literals());
break;
default:
throw new IllegalStateException("Unknown formula type " + formula.type());
}
result = Collections.unmodifiableSortedSet(result);
if (cache) {
formula.setFunctionCacheEntry(VARIABLES, result);
}
return result;
}
use of org.logicng.formulas.Not 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.Not in project LogicNG by logic-ng.
the class PureExpansionTransformation method apply.
@Override
public Formula apply(final Formula formula, final boolean cache) {
final FormulaFactory f = formula.factory();
switch(formula.type()) {
case FALSE:
case TRUE:
case LITERAL:
return formula;
case NOT:
final Not not = (Not) formula;
return f.not(apply(not.operand(), cache));
case OR:
case AND:
final NAryOperator nary = (NAryOperator) formula;
final List<Formula> newOps = new ArrayList<>(nary.numberOfOperands());
for (final Formula op : nary) {
newOps.add(apply(op, cache));
}
return f.naryOperator(formula.type(), newOps);
case IMPL:
case EQUIV:
final BinaryOperator binary = (BinaryOperator) formula;
final Formula newLeft = apply(binary.left(), cache);
final Formula newRight = apply(binary.right(), cache);
return f.binaryOperator(formula.type(), newLeft, newRight);
case PBC:
final PBConstraint pbc = (PBConstraint) formula;
if (pbc.isAmo() || pbc.isExo()) {
final EncodingResult encodingResult = EncodingResult.resultForFormula(f);
final Variable[] vars = literalsAsVariables(pbc.operands());
this.amoEncoder.build(encodingResult, vars);
final List<Formula> encoding = encodingResult.result();
if (pbc.isExo()) {
encoding.add(f.or(vars));
}
return f.and(encoding);
} else {
throw new UnsupportedOperationException("Pure encoding for a PBC of type other than AMO or EXO is currently not supported.");
}
default:
throw new IllegalStateException("Unknown formula type: " + formula.type());
}
}
Aggregations