Search in sources :

Example 71 with ASPCore2Program

use of at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program in project Alpha by alpha-asp.

the class StratificationAlgorithmTest method stratifyWithNegativeDependencyTest.

@Test
public void stratifyWithNegativeDependencyTest() {
    StringBuilder bld = new StringBuilder();
    bld.append("b :- a.").append("\n");
    bld.append("c :- b.").append("\n");
    bld.append("d :- not c.").append("\n");
    bld.append("e :- d.").append("\n");
    ASPCore2Program prog = parser.parse(bld.toString());
    NormalProgram normalProg = normalizeTransform.apply(prog);
    AnalyzedProgram analyzed = AnalyzedProgram.analyzeNormalProgram(normalProg);
    DependencyGraph dg = analyzed.getDependencyGraph();
    ComponentGraph cg = ComponentGraphImpl.buildComponentGraph(dg, StronglyConnectedComponentsAlgorithm.findStronglyConnectedComponents(dg));
    List<SCComponent> strata = StratificationAlgorithm.calculateStratification(cg);
    Predicate a = Predicates.getPredicate("a", 0);
    Predicate b = Predicates.getPredicate("b", 0);
    Predicate c = Predicates.getPredicate("c", 0);
    Predicate d = Predicates.getPredicate("d", 0);
    Predicate e = Predicates.getPredicate("e", 0);
    assertEquals(5, strata.size());
    assertTrue(predicateIsBeforePredicateInOrder(a, b, strata));
    assertTrue(predicateIsBeforePredicateInOrder(b, c, strata));
    assertTrue(predicateIsBeforePredicateInOrder(c, d, strata));
    assertTrue(predicateIsBeforePredicateInOrder(d, e, strata));
}
Also used : ComponentGraph(at.ac.tuwien.kr.alpha.api.programs.analysis.ComponentGraph) ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) SCComponent(at.ac.tuwien.kr.alpha.api.programs.analysis.ComponentGraph.SCComponent) AnalyzedProgram(at.ac.tuwien.kr.alpha.core.programs.AnalyzedProgram) NormalProgram(at.ac.tuwien.kr.alpha.api.programs.NormalProgram) DependencyGraph(at.ac.tuwien.kr.alpha.api.programs.analysis.DependencyGraph) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate) Test(org.junit.jupiter.api.Test)

Example 72 with ASPCore2Program

use of at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program in project Alpha by alpha-asp.

the class StratificationAlgorithmTest method stratifyWithPositiveCycleTest.

@Test
public void stratifyWithPositiveCycleTest() {
    StringBuilder bld = new StringBuilder();
    bld.append("ancestor_of(X, Y) :- parent_of(X, Y).");
    bld.append("ancestor_of(X, Z) :- parent_of(X, Y), ancestor_of(Y, Z).");
    ASPCore2Program prog = parser.parse(bld.toString());
    NormalProgram normalProg = normalizeTransform.apply(prog);
    AnalyzedProgram analyzed = AnalyzedProgram.analyzeNormalProgram(normalProg);
    DependencyGraph dg = analyzed.getDependencyGraph();
    ComponentGraph cg = ComponentGraphImpl.buildComponentGraph(dg, StronglyConnectedComponentsAlgorithm.findStronglyConnectedComponents(dg));
    List<SCComponent> strata = StratificationAlgorithm.calculateStratification(cg);
    Predicate ancestorOf = Predicates.getPredicate("ancestor_of", 2);
    Predicate parentOf = Predicates.getPredicate("parent_of", 2);
    assertEquals(2, strata.size());
    assertTrue(predicateIsBeforePredicateInOrder(parentOf, ancestorOf, strata));
}
Also used : ComponentGraph(at.ac.tuwien.kr.alpha.api.programs.analysis.ComponentGraph) ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) SCComponent(at.ac.tuwien.kr.alpha.api.programs.analysis.ComponentGraph.SCComponent) AnalyzedProgram(at.ac.tuwien.kr.alpha.core.programs.AnalyzedProgram) NormalProgram(at.ac.tuwien.kr.alpha.api.programs.NormalProgram) DependencyGraph(at.ac.tuwien.kr.alpha.api.programs.analysis.DependencyGraph) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate) Test(org.junit.jupiter.api.Test)

Example 73 with ASPCore2Program

use of at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program in project Alpha by alpha-asp.

the class StratificationAlgorithmTest method stratifyAvoidDuplicatesTest.

@Test
public void stratifyAvoidDuplicatesTest() {
    StringBuilder bld = new StringBuilder();
    bld.append("b :- a.");
    bld.append("c :- b.");
    bld.append("d :- c.");
    bld.append("e :- d.");
    bld.append("f :- not e.");
    bld.append("g :- d, j, not f.");
    bld.append("h :- not c.");
    bld.append("i :- h, not j.");
    bld.append("j :- h, not i.");
    bld.append("k :- g, not l.");
    bld.append("l :- g, not k.");
    bld.append("m :- not k, not l.");
    bld.append("n :- m, not i, not j.");
    bld.append("p :- not m, not n.");
    ASPCore2Program prog = parser.parse(bld.toString());
    NormalProgram normalProg = normalizeTransform.apply(prog);
    AnalyzedProgram analyzed = AnalyzedProgram.analyzeNormalProgram(normalProg);
    DependencyGraph dg = analyzed.getDependencyGraph();
    ComponentGraph cg = ComponentGraphImpl.buildComponentGraph(dg, StronglyConnectedComponentsAlgorithm.findStronglyConnectedComponents(dg));
    List<SCComponent> strata = StratificationAlgorithm.calculateStratification(cg);
    Predicate a = Predicates.getPredicate("a", 0);
    Predicate b = Predicates.getPredicate("b", 0);
    Predicate c = Predicates.getPredicate("c", 0);
    Predicate d = Predicates.getPredicate("d", 0);
    Predicate e = Predicates.getPredicate("e", 0);
    Predicate f = Predicates.getPredicate("f", 0);
    Predicate h = Predicates.getPredicate("h", 0);
    assertEquals(7, strata.size());
    assertTrue(predicateIsBeforePredicateInOrder(a, b, strata));
    assertTrue(predicateIsBeforePredicateInOrder(b, c, strata));
    assertTrue(predicateIsBeforePredicateInOrder(c, h, strata));
    assertTrue(predicateIsBeforePredicateInOrder(c, d, strata));
    assertTrue(predicateIsBeforePredicateInOrder(d, e, strata));
    assertTrue(predicateIsBeforePredicateInOrder(e, f, strata));
    assertTrue(predicateIsBeforePredicateInOrder(d, f, strata));
}
Also used : ComponentGraph(at.ac.tuwien.kr.alpha.api.programs.analysis.ComponentGraph) ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) SCComponent(at.ac.tuwien.kr.alpha.api.programs.analysis.ComponentGraph.SCComponent) AnalyzedProgram(at.ac.tuwien.kr.alpha.core.programs.AnalyzedProgram) NormalProgram(at.ac.tuwien.kr.alpha.api.programs.NormalProgram) DependencyGraph(at.ac.tuwien.kr.alpha.api.programs.analysis.DependencyGraph) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate) Test(org.junit.jupiter.api.Test)

Example 74 with ASPCore2Program

use of at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program in project Alpha by alpha-asp.

the class AtomsTest method testAreBasicAtomsEqual.

@Test
public void testAreBasicAtomsEqual() {
    ASPCore2Program p1 = parser.parse("bla(blubb, foo(bar)). bla(blubb, foo(bar)).");
    Atom a1 = p1.getFacts().get(0);
    Atom a2 = p1.getFacts().get(1);
    assertEquals(a1, a2);
    ASPCore2Program p2 = parser.parse("foo(1, 2, 3, \"bar\"). foo(1, 2, 3, \"bar\").");
    Atom a3 = p2.getFacts().get(0);
    Atom a4 = p2.getFacts().get(1);
    assertEquals(a3, a4);
    ASPCore2Program p3 = parser.parse("foo(BAR). foo(BAR).");
    Atom a5 = p3.getFacts().get(0);
    Atom a6 = p3.getFacts().get(1);
    assertEquals(a5, a6);
    ASPCore2Program p4 = parser.parse("foo(b, a, r(\"bla\", BLUBB)). foo(b, a, r(\"bla\", BLUBB)).");
    Atom a7 = p4.getFacts().get(0);
    Atom a8 = p4.getFacts().get(1);
    assertEquals(a7, a8);
    assertFalse(a1.equals(a3));
    assertFalse(a3.equals(a1));
    assertFalse(a1.equals(a5));
    assertFalse(a5.equals(a1));
    assertFalse(a1.equals(a7));
    assertFalse(a7.equals(a1));
}
Also used : ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) ExternalAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.ExternalAtom) Test(org.junit.jupiter.api.Test)

Example 75 with ASPCore2Program

use of at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program in project Alpha by alpha-asp.

the class AtomsTest method testAreExternalAtomsEqual.

@Test
@SuppressWarnings("unlikely-arg-type")
public void testAreExternalAtomsEqual() {
    ASPCore2Program p1 = parser.parse("a :- &isFoo[1].", externals);
    Atom ext1 = p1.getRules().get(0).getBody().stream().findFirst().get().getAtom();
    ASPCore2Program p2 = parser.parse("a :- &isFoo[1].", externals);
    Atom ext2 = p2.getRules().get(0).getBody().stream().findFirst().get().getAtom();
    assertEquals(ext1, ext2);
    assertEquals(ext2, ext1);
    assertFalse(ext1.equals(null));
    assertFalse(ext1.equals("bla"));
    assertTrue(ext1.hashCode() == ext2.hashCode());
}
Also used : ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) ExternalAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.ExternalAtom) Test(org.junit.jupiter.api.Test)

Aggregations

ASPCore2Program (at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program)89 Test (org.junit.jupiter.api.Test)70 AnswerSet (at.ac.tuwien.kr.alpha.api.AnswerSet)23 NormalProgram (at.ac.tuwien.kr.alpha.api.programs.NormalProgram)22 Alpha (at.ac.tuwien.kr.alpha.api.Alpha)21 InputConfig (at.ac.tuwien.kr.alpha.api.config.InputConfig)19 AnalyzedProgram (at.ac.tuwien.kr.alpha.core.programs.AnalyzedProgram)15 ProgramParserImpl (at.ac.tuwien.kr.alpha.core.parser.ProgramParserImpl)14 DependencyGraph (at.ac.tuwien.kr.alpha.api.programs.analysis.DependencyGraph)12 CompiledProgram (at.ac.tuwien.kr.alpha.core.programs.CompiledProgram)11 Atom (at.ac.tuwien.kr.alpha.api.programs.atoms.Atom)10 Disabled (org.junit.jupiter.api.Disabled)9 SystemConfig (at.ac.tuwien.kr.alpha.api.config.SystemConfig)8 AtomStoreImpl (at.ac.tuwien.kr.alpha.core.common.AtomStoreImpl)8 InputProgram (at.ac.tuwien.kr.alpha.core.programs.InputProgram)8 HashSet (java.util.HashSet)8 Predicate (at.ac.tuwien.kr.alpha.api.programs.Predicate)7 ComponentGraph (at.ac.tuwien.kr.alpha.api.programs.analysis.ComponentGraph)7 SCComponent (at.ac.tuwien.kr.alpha.api.programs.analysis.ComponentGraph.SCComponent)7 AtomStore (at.ac.tuwien.kr.alpha.core.common.AtomStore)7