use of org.logicng.io.parsers.PropositionalParser in project LogicNG by logic-ng.
the class ConstraintGraphGeneratorTest method testSimple.
@Test
public void testSimple() throws ParserException {
final FormulaFactory f = new FormulaFactory();
final PropositionalParser p = new PropositionalParser(f);
assertThat(ConstraintGraphGenerator.generateFromCnf(f.falsum()).nodes()).isEmpty();
assertThat(ConstraintGraphGenerator.generateFromCnf(f.verum()).nodes()).isEmpty();
Graph<Variable> graph = ConstraintGraphGenerator.generateFromCnf(p.parse("a"));
assertThat(graph.nodes()).containsExactly(graph.node(f.variable("a")));
graph = ConstraintGraphGenerator.generateFromCnf(p.parse("~a"));
assertThat(graph.nodes()).containsExactly(graph.node(f.variable("a")));
}
use of org.logicng.io.parsers.PropositionalParser in project LogicNG by logic-ng.
the class HypergraphGeneratorTest method testNonCNF.
@Test
public void testNonCNF() throws ParserException {
final FormulaFactory f = new FormulaFactory();
final PropositionalParser p = new PropositionalParser(f);
try {
HypergraphGenerator.fromCNF(p.parse("A => B"));
} catch (final IllegalArgumentException e) {
assertThat(e).hasMessage("Cannot generate a hypergraph from a non-cnf formula");
}
}
use of org.logicng.io.parsers.PropositionalParser in project LogicNG by logic-ng.
the class HypergraphGeneratorTest method testCNF.
@Test
public void testCNF() throws ParserException {
final FormulaFactory f = new FormulaFactory();
final PropositionalParser p = new PropositionalParser(f);
assertThat(HypergraphGenerator.fromCNF(p.parse("$false")).nodes()).isEmpty();
assertThat(HypergraphGenerator.fromCNF(p.parse("$false")).edges()).isEmpty();
assertThat(HypergraphGenerator.fromCNF(p.parse("$true")).nodes()).isEmpty();
assertThat(HypergraphGenerator.fromCNF(p.parse("$true")).edges()).isEmpty();
Hypergraph<Variable> hypergraph = HypergraphGenerator.fromCNF(p.parse("A"));
HypergraphNode<Variable> nodeA = new HypergraphNode<>(hypergraph, f.variable("A"));
assertThat(hypergraph.nodes()).containsExactly(nodeA);
assertThat(hypergraph.edges()).containsExactly(new HypergraphEdge<>(Collections.singletonList(nodeA)));
hypergraph = HypergraphGenerator.fromCNF(p.parse("A | B | ~C"));
nodeA = new HypergraphNode<>(hypergraph, f.variable("A"));
HypergraphNode<Variable> nodeB = new HypergraphNode<>(hypergraph, f.variable("B"));
HypergraphNode<Variable> nodeC = new HypergraphNode<>(hypergraph, f.variable("C"));
assertThat(hypergraph.nodes()).containsExactlyInAnyOrder(nodeA, nodeB, nodeC);
assertThat(hypergraph.edges()).containsExactlyInAnyOrder(new HypergraphEdge<>(Arrays.asList(nodeA, nodeB, nodeC)));
hypergraph = HypergraphGenerator.fromCNF(p.parse("(A | B | ~C) & (B | ~D) & (C | ~E) & (~B | ~D | E) & X & ~Y"));
nodeA = new HypergraphNode<>(hypergraph, f.variable("A"));
nodeB = new HypergraphNode<>(hypergraph, f.variable("B"));
nodeC = new HypergraphNode<>(hypergraph, f.variable("C"));
final HypergraphNode<Variable> nodeD = new HypergraphNode<>(hypergraph, f.variable("D"));
final HypergraphNode<Variable> nodeE = new HypergraphNode<>(hypergraph, f.variable("E"));
final HypergraphNode<Variable> nodeX = new HypergraphNode<>(hypergraph, f.variable("X"));
final HypergraphNode<Variable> nodeY = new HypergraphNode<>(hypergraph, f.variable("Y"));
assertThat(hypergraph.nodes()).containsExactlyInAnyOrder(nodeA, nodeB, nodeC, nodeD, nodeE, nodeX, nodeY);
assertThat(hypergraph.edges()).containsExactlyInAnyOrder(new HypergraphEdge<>(Arrays.asList(nodeA, nodeB, nodeC)), new HypergraphEdge<>(Arrays.asList(nodeB, nodeD)), new HypergraphEdge<>(Arrays.asList(nodeC, nodeE)), new HypergraphEdge<>(Arrays.asList(nodeB, nodeD, nodeE)), new HypergraphEdge<>(Collections.singletonList(nodeX)), new HypergraphEdge<>(Collections.singletonList(nodeY)));
}
use of org.logicng.io.parsers.PropositionalParser in project LogicNG by logic-ng.
the class AndTest method testContains.
@Test
public void testContains() throws ParserException {
assertThat(this.AND3.containsVariable(this.f.variable("x"))).isTrue();
assertThat(this.AND3.containsVariable(this.f.variable("a"))).isFalse();
final PropositionalParser parser = new PropositionalParser(this.f);
final Formula contAnd = parser.parse("a & b & (c | (d & e))");
assertThat(contAnd.containsNode(parser.parse("d & e"))).isTrue();
}
use of org.logicng.io.parsers.PropositionalParser in project LogicNG by logic-ng.
the class EquivalenceTest method testNumberOfInternalNodes.
@Test
public void testNumberOfInternalNodes() throws ParserException {
final Formula eq = new PropositionalParser(this.f).parse("a & (b | c) <=> (d => (b | c))");
assertThat(this.EQ4.numberOfInternalNodes()).isEqualTo(7);
assertThat(eq.numberOfInternalNodes()).isEqualTo(8);
}
Aggregations