use of org.logicng.formulas.Formula in project LogicNG by logic-ng.
the class VariableProfileTest method testNAryOperator.
@Test
public void testNAryOperator() throws ParserException {
final Map<Literal, Integer> expected = new HashMap<>();
expected.put(this.f2.variable("a"), 1);
expected.put(this.f2.variable("b"), 2);
expected.put(this.f2.variable("c"), 3);
final PropositionalParser p = new PropositionalParser(this.f);
final Formula formula = p.parse("a & (b | c) & (~b | ~c) & c");
assertThat(formula.apply(this.varProfile, true)).isEqualTo(expected);
assertThat(formula.apply(this.varProfile, false)).isEqualTo(expected);
}
use of org.logicng.formulas.Formula in project LogicNG by logic-ng.
the class VariableProfileTest method testNot.
@Test
public void testNot() throws ParserException {
final Map<Literal, Integer> expected = new HashMap<>();
expected.put(this.f2.variable("a"), 1);
expected.put(this.f2.variable("b"), 2);
expected.put(this.f2.variable("c"), 3);
final PropositionalParser p = new PropositionalParser(this.f);
final Formula formula = p.parse("~(a & (b | c) & ((~b | ~c) => c))");
assertThat(formula.apply(this.varProfile, true)).isEqualTo(expected);
assertThat(formula.apply(this.varProfile, false)).isEqualTo(expected);
}
use of org.logicng.formulas.Formula in project LogicNG by logic-ng.
the class VariableProfileTest method testBinaryOperator.
@Test
public void testBinaryOperator() throws ParserException {
final Map<Literal, Integer> expected = new HashMap<>();
expected.put(this.f2.variable("a"), 1);
expected.put(this.f2.variable("b"), 2);
expected.put(this.f2.variable("c"), 3);
final PropositionalParser p = new PropositionalParser(this.f);
final Formula impl = p.parse("(a & (b | c) & (~b | ~c)) => c");
final Formula equiv = p.parse("(a & (b | c) & (~b | ~c)) <=> c");
assertThat(impl.apply(this.varProfile, true)).isEqualTo(expected);
assertThat(impl.apply(this.varProfile, false)).isEqualTo(expected);
assertThat(equiv.apply(this.varProfile, true)).isEqualTo(expected);
assertThat(equiv.apply(this.varProfile, false)).isEqualTo(expected);
}
use of org.logicng.formulas.Formula in project LogicNG by logic-ng.
the class ConnectedComponentsComputerTest method testFormulaSplit.
@Test
public void testFormulaSplit() throws IOException, ParserException {
final FormulaFactory f = new FormulaFactory();
f.putConfiguration(CCConfig.builder().amoEncoding(CCConfig.AMO_ENCODER.PURE).build());
final Formula parsed = FormulaReader.readPseudoBooleanFormula("src/test/resources/formulas/formula1.txt", f);
final List<Formula> formulas = new ArrayList<>();
final List<Formula> originalFormulas = new ArrayList<>();
for (final Formula formula : parsed) {
originalFormulas.add(formula);
if (formula instanceof PBConstraint) {
formulas.add(formula);
} else {
formulas.add(formula.transform(new CNFFactorization()));
}
}
final Graph<Variable> constraintGraph = ConstraintGraphGenerator.generateFromCnf(formulas);
final Set<Set<Node<Variable>>> ccs = ConnectedComponentsComputation.compute(constraintGraph);
final List<List<Formula>> split = ConnectedComponentsComputation.splitFormulasByComponent(originalFormulas, ccs);
assertThat(split).hasSize(4);
assertThat(split.get(0)).hasSize(1899);
assertThat(split.get(1)).hasSize(3);
assertThat(split.get(2)).hasSize(3);
assertThat(split.get(3)).hasSize(3);
}
use of org.logicng.formulas.Formula in project LogicNG by logic-ng.
the class GraphTest method testFormulaGraph.
@Test
public void testFormulaGraph() {
final FormulaFactory f = new FormulaFactory();
final Graph<Formula> g = new Graph<>("Graph with Formula nodes.");
final Variable a = f.variable("A");
final Node<Formula> an = g.node(a);
final Variable b = f.variable("B");
final Node<Formula> bn = g.node(b);
final Formula aNb = f.and(a, b);
final Node<Formula> aNbn = g.node(aNb);
g.connect(aNbn, an);
g.connect(aNbn, bn);
assertThat(aNbn.neighbours().size()).isEqualTo(2);
assertThat(an.neighbours().contains(aNbn)).isTrue();
assertThat(bn.neighbours().contains(aNbn)).isTrue();
assertThat(an.graph().name()).isEqualTo(g.name());
g.disconnect(aNbn, an);
assertThat(an.neighbours().isEmpty()).isTrue();
assertThat(g.nodes().size()).isEqualTo(3);
}
Aggregations