Search in sources :

Example 11 with NormalProgram

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

the class DependencyGraphImplTest method stronglyConnectedComponentsSimpleTest.

@Test
public void stronglyConnectedComponentsSimpleTest() {
    StringBuilder bld = new StringBuilder();
    bld.append("b :- a.").append("\n");
    bld.append("a :- b.").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));
    List<Node> componentA = new ArrayList<>();
    componentA.add(a);
    assertTrue(DependencyGraphUtils.areStronglyConnected(componentA, dg));
    assertFalse(DependencyGraphUtils.isStronglyConnectedComponent(componentA, dg));
    List<Node> componentB = new ArrayList<>();
    componentB.add(b);
    assertTrue(DependencyGraphUtils.areStronglyConnected(componentB, dg));
    assertFalse(DependencyGraphUtils.isStronglyConnectedComponent(componentB, dg));
    List<Node> componentAll = new ArrayList<>();
    componentAll.add(a);
    componentAll.add(b);
    assertTrue(DependencyGraphUtils.areStronglyConnected(componentAll, dg));
    assertTrue(DependencyGraphUtils.isStronglyConnectedComponent(componentAll, 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) ArrayList(java.util.ArrayList) 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 NormalProgram

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

the class DependencyGraphImplTest method stronglyConnectedComponentsMultipleComponentsTest.

@Test
public void stronglyConnectedComponentsMultipleComponentsTest() {
    String inputProgram = "f0.\n" + "f1.\n" + "f2.\n" + "f3.\n" + "a :- f0, f1, not b.\n" + "b :- f0, f1, not a.\n" + "c :- f2, f3, not d.\n" + "d :- f2, f3, not c.\n" + "x :- a, c, y.\n" + "y :- b, d, x.\n" + "z :- x, y, z.";
    ASPCore2Program prog = parser.parse(inputProgram);
    NormalProgram normalProg = normalizeTransform.apply(prog);
    AnalyzedProgram analyzed = AnalyzedProgram.analyzeNormalProgram(normalProg);
    DependencyGraph dg = analyzed.getDependencyGraph();
    Node f0 = dg.getNodeForPredicate(Predicates.getPredicate("f0", 0));
    Node f1 = dg.getNodeForPredicate(Predicates.getPredicate("f1", 0));
    Node f2 = dg.getNodeForPredicate(Predicates.getPredicate("f2", 0));
    Node f3 = dg.getNodeForPredicate(Predicates.getPredicate("f3", 0));
    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));
    Node x = dg.getNodeForPredicate(Predicates.getPredicate("x", 0));
    Node y = dg.getNodeForPredicate(Predicates.getPredicate("y", 0));
    Node z = dg.getNodeForPredicate(Predicates.getPredicate("z", 0));
    StronglyConnectedComponentsAlgorithm.SccResult sccResult = StronglyConnectedComponentsAlgorithm.findStronglyConnectedComponents(dg);
    Map<Node, Integer> nodesByComponent = sccResult.nodesByComponentId;
    List<List<Node>> stronglyConnectedComponents = sccResult.stronglyConnectedComponents;
    assertEquals(8, stronglyConnectedComponents.size());
    for (int i = 0; i < stronglyConnectedComponents.size(); i++) {
        List<Node> stronglyConnectedComponent = stronglyConnectedComponents.get(i);
        assertTrue(DependencyGraphUtils.isStronglyConnectedComponent(stronglyConnectedComponent, dg));
        for (Node node : stronglyConnectedComponent) {
            assertEquals(Integer.valueOf(i), nodesByComponent.get(node));
        }
    }
    List<Node> c1 = new ArrayList<>();
    c1.add(a);
    c1.add(b);
    assertTrue(DependencyGraphUtils.isStronglyConnectedComponent(c1, dg));
    assertEquals(nodesByComponent.get(a), nodesByComponent.get(b));
    List<Node> c2 = new ArrayList<>();
    c2.add(c);
    c2.add(d);
    assertTrue(DependencyGraphUtils.isStronglyConnectedComponent(c2, dg));
    assertEquals(nodesByComponent.get(c), nodesByComponent.get(d));
    List<Node> c3 = new ArrayList<>();
    c3.add(x);
    c3.add(y);
    assertTrue(DependencyGraphUtils.isStronglyConnectedComponent(c3, dg));
    assertEquals(nodesByComponent.get(x), nodesByComponent.get(y));
    List<Node> c4 = new ArrayList<>();
    c4.add(z);
    assertTrue(DependencyGraphUtils.isStronglyConnectedComponent(c4, dg));
    List<Node> c5 = new ArrayList<>();
    c5.add(f0);
    assertTrue(DependencyGraphUtils.isStronglyConnectedComponent(c5, dg));
    List<Node> c6 = new ArrayList<>();
    c6.add(f1);
    assertTrue(DependencyGraphUtils.isStronglyConnectedComponent(c6, dg));
    List<Node> c7 = new ArrayList<>();
    c7.add(f2);
    assertTrue(DependencyGraphUtils.isStronglyConnectedComponent(c7, dg));
    List<Node> c8 = new ArrayList<>();
    c8.add(f3);
    assertTrue(DependencyGraphUtils.isStronglyConnectedComponent(c8, 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) ArrayList(java.util.ArrayList) DependencyGraph(at.ac.tuwien.kr.alpha.api.programs.analysis.DependencyGraph) NormalProgram(at.ac.tuwien.kr.alpha.api.programs.NormalProgram) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.jupiter.api.Test)

Example 13 with NormalProgram

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

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

the class NormalizeProgramTransformation method apply.

@Override
public NormalProgram apply(ASPCore2Program inputProgram) {
    ASPCore2Program tmpPrg;
    // Remove variable equalities.
    tmpPrg = new VariableEqualityRemoval().apply(inputProgram);
    // Transform choice rules.
    tmpPrg = new ChoiceHeadToNormal().apply(tmpPrg);
    // Transform aggregates.
    tmpPrg = new AggregateRewriting(aggregateRewritingCfg.isUseSortingGridEncoding(), aggregateRewritingCfg.isSupportNegativeValuesInSums()).apply(tmpPrg);
    // Transform enumeration atoms.
    tmpPrg = new EnumerationRewriting().apply(tmpPrg);
    EnumerationAtom.resetEnumerations();
    // Construct the normal program.
    NormalProgram retVal = NormalProgramImpl.fromInputProgram(tmpPrg);
    // Transform intervals.
    retVal = new IntervalTermToIntervalAtom().apply(retVal);
    // Rewrite ArithmeticTerms.
    retVal = new ArithmeticTermsRewriting().apply(retVal);
    return retVal;
}
Also used : ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) AggregateRewriting(at.ac.tuwien.kr.alpha.core.programs.transformation.aggregates.AggregateRewriting) NormalProgram(at.ac.tuwien.kr.alpha.api.programs.NormalProgram)

Example 15 with NormalProgram

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

the class NaiveGrounderTest method groundRuleAlreadyGround.

/**
 * Asserts that a ground rule whose positive body is not satisfied by the empty assignment
 * is grounded immediately.
 */
@Test
public void groundRuleAlreadyGround() {
    ASPCore2Program program = PROGRAM_PARSER.parse("a :- not b. " + "b :- not a. " + "c :- b.");
    NormalProgram normal = NORMALIZE_TRANSFORM.apply(program);
    CompiledProgram prog = new StratifiedEvaluation().apply(AnalyzedProgram.analyzeNormalProgram(normal));
    AtomStore atomStore = new AtomStoreImpl();
    Grounder grounder = GrounderFactory.getInstance("naive", prog, atomStore, true);
    Map<Integer, NoGood> noGoods = grounder.getNoGoods(new TrailAssignment(atomStore));
    int litCNeg = Literals.atomToLiteral(atomStore.get(PROGRAM_PART_PARSER.parseBasicAtom("c")), false);
    int litB = Literals.atomToLiteral(atomStore.get(PROGRAM_PART_PARSER.parseBasicAtom("b")));
    assertExistsNoGoodContaining(noGoods.values(), litCNeg);
    assertExistsNoGoodContaining(noGoods.values(), litB);
}
Also used : ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) AtomStore(at.ac.tuwien.kr.alpha.core.common.AtomStore) NoGood(at.ac.tuwien.kr.alpha.core.common.NoGood) AtomStoreImpl(at.ac.tuwien.kr.alpha.core.common.AtomStoreImpl) NormalProgram(at.ac.tuwien.kr.alpha.api.programs.NormalProgram) CompiledProgram(at.ac.tuwien.kr.alpha.core.programs.CompiledProgram) StratifiedEvaluation(at.ac.tuwien.kr.alpha.core.programs.transformation.StratifiedEvaluation) TrailAssignment(at.ac.tuwien.kr.alpha.core.solver.TrailAssignment) Test(org.junit.jupiter.api.Test)

Aggregations

NormalProgram (at.ac.tuwien.kr.alpha.api.programs.NormalProgram)23 Test (org.junit.jupiter.api.Test)20 ASPCore2Program (at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program)19 DependencyGraph (at.ac.tuwien.kr.alpha.api.programs.analysis.DependencyGraph)13 AnalyzedProgram (at.ac.tuwien.kr.alpha.core.programs.AnalyzedProgram)13 ComponentGraph (at.ac.tuwien.kr.alpha.api.programs.analysis.ComponentGraph)8 Predicate (at.ac.tuwien.kr.alpha.api.programs.Predicate)7 SCComponent (at.ac.tuwien.kr.alpha.api.programs.analysis.ComponentGraph.SCComponent)7 Node (at.ac.tuwien.kr.alpha.api.programs.analysis.DependencyGraph.Node)5 AtomStore (at.ac.tuwien.kr.alpha.core.common.AtomStore)5 AtomStoreImpl (at.ac.tuwien.kr.alpha.core.common.AtomStoreImpl)5 StratifiedEvaluation (at.ac.tuwien.kr.alpha.core.programs.transformation.StratifiedEvaluation)5 CompiledProgram (at.ac.tuwien.kr.alpha.core.programs.CompiledProgram)4 NoGood (at.ac.tuwien.kr.alpha.core.common.NoGood)3 InternalProgram (at.ac.tuwien.kr.alpha.core.programs.InternalProgram)3 TrailAssignment (at.ac.tuwien.kr.alpha.core.solver.TrailAssignment)3 SystemConfig (at.ac.tuwien.kr.alpha.api.config.SystemConfig)2 NormalRule (at.ac.tuwien.kr.alpha.api.rules.NormalRule)2 VariableTerm (at.ac.tuwien.kr.alpha.api.terms.VariableTerm)2 ArrayList (java.util.ArrayList)2