use of org.logicng.formulas.Formula in project LogicNG by logic-ng.
the class CNFFactorization method applyRec.
private Formula applyRec(final Formula formula, final boolean cache) {
if (!this.proceed) {
return null;
}
if (formula.type().precedence() >= LITERAL.precedence()) {
return formula;
}
Formula cached = formula.transformationCacheEntry(FACTORIZED_CNF);
if (cached != null) {
return cached;
}
switch(formula.type()) {
case NOT:
case IMPL:
case EQUIV:
cached = this.applyRec(formula.nnf(), cache);
break;
case OR:
LinkedHashSet<Formula> nops = new LinkedHashSet<>();
for (final Formula op : formula) {
if (!this.proceed) {
return null;
}
nops.add(this.applyRec(op, cache));
}
final Iterator<Formula> it = nops.iterator();
cached = it.next();
while (it.hasNext()) {
if (!this.proceed) {
return null;
}
cached = this.distribute(cached, it.next());
}
break;
case AND:
nops = new LinkedHashSet<>();
for (final Formula op : formula) {
final Formula apply = this.applyRec(op, cache);
if (!this.proceed) {
return null;
}
nops.add(apply);
}
cached = formula.factory().and(nops);
break;
case PBC:
cached = formula.nnf();
break;
default:
throw new IllegalArgumentException("Could not process the formula type " + formula.type());
}
if (this.proceed) {
if (cache) {
formula.setTransformationCacheEntry(FACTORIZED_CNF, cached);
}
return cached;
}
return null;
}
use of org.logicng.formulas.Formula in project LogicNG by logic-ng.
the class CNFFactorization method distribute.
/**
* Computes the distribution (factorization) of two formulas.
* @param f1 the first formula
* @param f2 the second formula
* @return the distribution of the two formulas
*/
private Formula distribute(final Formula f1, final Formula f2) {
if (this.handler != null) {
this.proceed = this.handler.performedDistribution();
}
if (this.proceed) {
final FormulaFactory f = f1.factory();
if (f1.type() == AND || f2.type() == AND) {
final LinkedHashSet<Formula> nops = new LinkedHashSet<>();
for (final Formula op : f1.type() == AND ? f1 : f2) {
final Formula distribute = this.distribute(op, f1.type() == AND ? f2 : f1);
if (!this.proceed) {
return null;
}
nops.add(distribute);
}
return f.and(nops);
}
final Formula clause = f.or(f1, f2);
if (this.handler != null) {
this.proceed = this.handler.createdClause(clause);
}
return clause;
}
return null;
}
use of org.logicng.formulas.Formula in project LogicNG by logic-ng.
the class TimeoutBDDHandlerTest method testTimeoutHandlerSingleTimeout.
@Test
public void testTimeoutHandlerSingleTimeout() {
final Formula formula = pg.generate(10);
final VariableOrderingProvider provider = VariableOrdering.BFS.provider();
final BDDKernel kernel = new BDDKernel(this.f, provider.getOrder(formula), 100, 100);
final TimeoutBDDHandler handler = new TimeoutBDDHandler(100L);
final BDD result = BDDFactory.build(formula, kernel, handler);
assertThat(handler.aborted).isTrue();
assertThat(result).isEqualTo(new BDD(BDDKernel.BDD_ABORT, kernel));
}
use of org.logicng.formulas.Formula in project LogicNG by logic-ng.
the class TimeoutBDDHandlerTest method testThatMethodsAreCalled.
@Test
public void testThatMethodsAreCalled() throws ParserException {
final Formula formula = f.parse("(A => ~B) & ((A & C) | (D & ~C)) & (A | Y | X)");
final VariableOrderingProvider provider = VariableOrdering.BFS.provider();
final BDDKernel kernel = new BDDKernel(this.f, provider.getOrder(formula), 100, 100);
final TimeoutBDDHandler handler = Mockito.mock(TimeoutBDDHandler.class);
BDDFactory.build(formula, kernel, handler);
verify(handler, times(1)).started();
verify(handler, atLeast(1)).newRefAdded();
}
use of org.logicng.formulas.Formula in project LogicNG by logic-ng.
the class TimeoutBDDHandlerTest method testThatNewRefAddedHandledProperly.
@Test
public void testThatNewRefAddedHandledProperly() throws ParserException {
final Formula formula = f.parse("(A => ~B) & ((A & C) | ~(D & ~C)) & (A | Y | X)");
final VariableOrderingProvider provider = VariableOrdering.BFS.provider();
final BDDKernel kernel = new BDDKernel(this.f, provider.getOrder(formula), 100, 100);
final TimeoutBDDHandler handler = Mockito.mock(TimeoutBDDHandler.class);
final AtomicInteger count = new AtomicInteger(0);
when(handler.newRefAdded()).then(invocationOnMock -> count.addAndGet(1) < 5);
final BDD result = BDDFactory.build(formula, kernel, handler);
assertThat(result).isEqualTo(new BDD(BDDKernel.BDD_ABORT, kernel));
verify(handler, times(1)).started();
verify(handler, times(5)).newRefAdded();
}
Aggregations