Search in sources :

Example 1 with PropositionalParser

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")));
}
Also used : FormulaFactory(org.logicng.formulas.FormulaFactory) Variable(org.logicng.formulas.Variable) PropositionalParser(org.logicng.io.parsers.PropositionalParser) Test(org.junit.jupiter.api.Test)

Example 2 with PropositionalParser

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");
    }
}
Also used : FormulaFactory(org.logicng.formulas.FormulaFactory) PropositionalParser(org.logicng.io.parsers.PropositionalParser) Test(org.junit.jupiter.api.Test)

Example 3 with PropositionalParser

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)));
}
Also used : FormulaFactory(org.logicng.formulas.FormulaFactory) Variable(org.logicng.formulas.Variable) HypergraphNode(org.logicng.graphs.datastructures.HypergraphNode) PropositionalParser(org.logicng.io.parsers.PropositionalParser) Test(org.junit.jupiter.api.Test)

Example 4 with PropositionalParser

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();
}
Also used : PropositionalParser(org.logicng.io.parsers.PropositionalParser) Test(org.junit.jupiter.api.Test)

Example 5 with PropositionalParser

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);
}
Also used : PropositionalParser(org.logicng.io.parsers.PropositionalParser) Test(org.junit.jupiter.api.Test)

Aggregations

PropositionalParser (org.logicng.io.parsers.PropositionalParser)97 Test (org.junit.jupiter.api.Test)94 Formula (org.logicng.formulas.Formula)50 FormulaFactory (org.logicng.formulas.FormulaFactory)42 Variable (org.logicng.formulas.Variable)13 Literal (org.logicng.formulas.Literal)8 HashMap (java.util.HashMap)7 Assignment (org.logicng.datastructures.Assignment)6 BDDKernel (org.logicng.knowledgecompilation.bdds.jbuddy.BDDKernel)6 BufferedReader (java.io.BufferedReader)5 TautologyPredicate (org.logicng.predicates.satisfiability.TautologyPredicate)5 File (java.io.File)4 SATSolver (org.logicng.solvers.SATSolver)4 FileReader (java.io.FileReader)3 LogicNGTest (org.logicng.LogicNGTest)3 FileInputStream (java.io.FileInputStream)2 InputStreamReader (java.io.InputStreamReader)2 ArrayList (java.util.ArrayList)2 TreeSet (java.util.TreeSet)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2