use of org.logicng.formulas.Formula in project LogicNG by logic-ng.
the class FormulaDepthFunctionTest method testCache.
@Test
public void testCache() throws ParserException {
final FormulaFactory f = new FormulaFactory();
final Formula formula = f.parse("A & B | C");
assertThat(formula.functionCacheEntry(FunctionCacheEntry.DEPTH)).isNull();
assertThat(formula.apply(new FormulaDepthFunction())).isEqualTo(2);
assertThat(formula.functionCacheEntry(FunctionCacheEntry.DEPTH)).isEqualTo(2);
assertThat(f.variable("A").functionCacheEntry(FunctionCacheEntry.DEPTH)).isEqualTo(0);
formula.setFunctionCacheEntry(FunctionCacheEntry.DEPTH, 3);
assertThat(formula.apply(new FormulaDepthFunction())).isEqualTo(3);
}
use of org.logicng.formulas.Formula in project LogicNG by logic-ng.
the class FormulaDepthFunctionTest method testDeeperFormulas.
@Test
public void testDeeperFormulas() {
Formula formula = this.PBC1;
for (int i = 0; i < 10; i++) {
final Variable var = this.f.variable("X" + i);
formula = i % 2 == 0 ? this.f.or(formula, var) : this.f.and(formula, var);
}
assertThat(formula.apply(new FormulaDepthFunction())).isEqualTo(10);
}
use of org.logicng.formulas.Formula in project LogicNG by logic-ng.
the class FactorOutSimplifier method applyRec.
private Formula applyRec(final Formula formula, final boolean cache) {
switch(formula.type()) {
case OR:
case AND:
final List<Formula> newOps = new ArrayList<>();
for (final Formula op : formula) {
newOps.add(apply(op, cache));
}
final Formula newFormula = formula.factory().naryOperator(formula.type(), newOps);
return newFormula instanceof NAryOperator ? simplify((NAryOperator) newFormula) : newFormula;
case NOT:
return apply(((Not) formula).operand(), cache).negate();
case FALSE:
case TRUE:
case LITERAL:
case IMPL:
case EQUIV:
case PBC:
return formula;
default:
throw new IllegalStateException("Unknown formula type: " + formula.type());
}
}
use of org.logicng.formulas.Formula in project LogicNG by logic-ng.
the class FactorOutSimplifier method computeMaxOccurringSubformula.
private static Formula computeMaxOccurringSubformula(final NAryOperator formula) {
final Map<Formula, Integer> formulaCounts = new HashMap<>();
for (final Formula operand : formula) {
if (operand.type() == FType.LITERAL) {
formulaCounts.merge(operand, 1, Integer::sum);
} else if (operand.type() == FType.AND || operand.type() == FType.OR) {
for (final Formula subOperand : operand) {
formulaCounts.merge(subOperand, 1, Integer::sum);
}
}
}
final Pair<Formula, Integer> max = formulaCounts.entrySet().stream().max(Comparator.comparingInt(Map.Entry::getValue)).map(e -> new Pair<>(e.getKey(), e.getValue())).orElse(new Pair<>(null, 0));
return max.second() < 2 ? null : max.first();
}
use of org.logicng.formulas.Formula in project LogicNG by logic-ng.
the class FactorOutSimplifier method factorOut.
private static Formula factorOut(final NAryOperator formula) {
final Formula factorOutFormula = computeMaxOccurringSubformula(formula);
if (factorOutFormula == null) {
return null;
}
final FormulaFactory f = formula.factory();
final FType type = formula.type();
final List<Formula> formulasWithRemoved = new ArrayList<>();
final List<Formula> unchangedFormulas = new ArrayList<>();
for (final Formula operand : formula) {
if (operand.type() == FType.LITERAL) {
if (operand.equals(factorOutFormula)) {
formulasWithRemoved.add(f.constant(type == FType.OR));
} else {
unchangedFormulas.add(operand);
}
} else if (operand.type() == FType.AND || operand.type() == FType.OR) {
boolean removed = false;
final List<Formula> newOps = new ArrayList<>();
for (final Formula op : operand) {
if (!op.equals(factorOutFormula)) {
newOps.add(op);
} else {
removed = true;
}
}
(removed ? formulasWithRemoved : unchangedFormulas).add(f.naryOperator(operand.type(), newOps));
} else {
unchangedFormulas.add(operand);
}
}
return f.naryOperator(type, f.naryOperator(type, unchangedFormulas), f.naryOperator(dual(type), factorOutFormula, f.naryOperator(type, formulasWithRemoved)));
}
Aggregations