Search in sources :

Example 11 with AnalyzedProgram

use of at.ac.tuwien.kr.alpha.core.programs.AnalyzedProgram in project Alpha by alpha-asp.

the class DependencyGraphImplTest method reachabilityCheckWithHopsTest.

@Test
public void reachabilityCheckWithHopsTest() {
    StringBuilder bld = new StringBuilder();
    bld.append("b :- a.").append("\n");
    bld.append("c :- b.").append("\n");
    bld.append("d :- c.").append("\n");
    ASPCore2Program prog = parser.parse(bld.toString());
    NormalProgram normalProg = normalizeTransform.apply(prog);
    AnalyzedProgram analyzed = AnalyzedProgram.analyzeNormalProgram(normalProg);
    DependencyGraph dg = analyzed.getDependencyGraph();
    Node a = dg.getNodeForPredicate(Predicates.getPredicate("a", 0));
    Node b = dg.getNodeForPredicate(Predicates.getPredicate("b", 0));
    Node c = dg.getNodeForPredicate(Predicates.getPredicate("c", 0));
    Node d = dg.getNodeForPredicate(Predicates.getPredicate("d", 0));
    assertTrue(DependencyGraphUtils.isReachableFrom(d, a, dg));
    assertTrue(DependencyGraphUtils.isReachableFrom(c, a, dg));
    assertTrue(DependencyGraphUtils.isReachableFrom(b, a, dg));
    assertTrue(DependencyGraphUtils.isReachableFrom(a, a, dg));
    assertTrue(DependencyGraphUtils.isReachableFrom(d, b, dg));
    assertTrue(DependencyGraphUtils.isReachableFrom(c, b, dg));
    assertTrue(DependencyGraphUtils.isReachableFrom(b, b, dg));
    assertTrue(DependencyGraphUtils.isReachableFrom(d, c, dg));
    assertTrue(DependencyGraphUtils.isReachableFrom(c, c, dg));
    assertFalse(DependencyGraphUtils.isReachableFrom(a, d, dg));
    assertFalse(DependencyGraphUtils.isReachableFrom(a, c, dg));
    assertFalse(DependencyGraphUtils.isReachableFrom(a, b, dg));
}
Also used : ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) AnalyzedProgram(at.ac.tuwien.kr.alpha.core.programs.AnalyzedProgram) Node(at.ac.tuwien.kr.alpha.api.programs.analysis.DependencyGraph.Node) NormalProgram(at.ac.tuwien.kr.alpha.api.programs.NormalProgram) DependencyGraph(at.ac.tuwien.kr.alpha.api.programs.analysis.DependencyGraph) Test(org.junit.jupiter.api.Test)

Example 12 with AnalyzedProgram

use of at.ac.tuwien.kr.alpha.core.programs.AnalyzedProgram in project Alpha by alpha-asp.

the class StratificationAlgorithmTest method stratifyLargeGraphTest.

@Test
public void stratifyLargeGraphTest() {
    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);
    assertTrue(predicateIsBeforePredicateInOrder(a, h, strata));
    assertTrue(predicateIsBeforePredicateInOrder(b, h, strata));
    assertTrue(predicateIsBeforePredicateInOrder(c, h, strata));
    assertTrue(predicateIsBeforePredicateInOrder(a, f, strata));
    assertTrue(predicateIsBeforePredicateInOrder(b, f, strata));
    assertTrue(predicateIsBeforePredicateInOrder(c, f, strata));
    assertTrue(predicateIsBeforePredicateInOrder(d, f, strata));
    assertTrue(predicateIsBeforePredicateInOrder(e, 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 13 with AnalyzedProgram

use of at.ac.tuwien.kr.alpha.core.programs.AnalyzedProgram 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 14 with AnalyzedProgram

use of at.ac.tuwien.kr.alpha.core.programs.AnalyzedProgram 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 15 with AnalyzedProgram

use of at.ac.tuwien.kr.alpha.core.programs.AnalyzedProgram 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)

Aggregations

AnalyzedProgram (at.ac.tuwien.kr.alpha.core.programs.AnalyzedProgram)17 DependencyGraph (at.ac.tuwien.kr.alpha.api.programs.analysis.DependencyGraph)14 ASPCore2Program (at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program)13 NormalProgram (at.ac.tuwien.kr.alpha.api.programs.NormalProgram)13 Test (org.junit.jupiter.api.Test)13 ComponentGraph (at.ac.tuwien.kr.alpha.api.programs.analysis.ComponentGraph)9 Predicate (at.ac.tuwien.kr.alpha.api.programs.Predicate)8 SCComponent (at.ac.tuwien.kr.alpha.api.programs.analysis.ComponentGraph.SCComponent)7 Node (at.ac.tuwien.kr.alpha.api.programs.analysis.DependencyGraph.Node)5 ArrayList (java.util.ArrayList)3 AnswerSet (at.ac.tuwien.kr.alpha.api.AnswerSet)2 Solver (at.ac.tuwien.kr.alpha.api.Solver)2 ProgramParser (at.ac.tuwien.kr.alpha.api.programs.ProgramParser)2 ProgramParserImpl (at.ac.tuwien.kr.alpha.core.parser.ProgramParserImpl)2 CompiledProgram (at.ac.tuwien.kr.alpha.core.programs.CompiledProgram)2 InternalProgram (at.ac.tuwien.kr.alpha.core.programs.InternalProgram)2 StratifiedEvaluation (at.ac.tuwien.kr.alpha.core.programs.transformation.StratifiedEvaluation)2 HashMap (java.util.HashMap)2 List (java.util.List)2 DebugSolvingContext (at.ac.tuwien.kr.alpha.api.DebugSolvingContext)1