Search in sources :

Example 6 with NormalProgram

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

the class StratificationAlgorithmTest method stratifyTwoRulesTest.

@Test
public void stratifyTwoRulesTest() {
    StringBuilder bld = new StringBuilder();
    bld.append("b :- a.").append("\n");
    bld.append("c :- b.").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);
    assertEquals(3, strata.size());
    assertTrue(predicateIsBeforePredicateInOrder(a, b, strata));
    assertTrue(predicateIsBeforePredicateInOrder(b, c, strata));
    assertTrue(predicateIsBeforePredicateInOrder(a, c, 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 7 with NormalProgram

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

the class StratificationAlgorithmTest method avoidDuplicatesTest1.

@Test
public void avoidDuplicatesTest1() {
    StringBuilder bld = new StringBuilder();
    bld.append("b :- a.");
    bld.append("c :- b.");
    bld.append("c :- a.");
    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);
    assertTrue(predicateIsBeforePredicateInOrder(a, b, strata));
    assertTrue(predicateIsBeforePredicateInOrder(b, c, strata));
    assertTrue(predicateIsBeforePredicateInOrder(a, c, strata));
    assertEquals(3, strata.size());
}
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 8 with NormalProgram

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

the class StratificationAlgorithmTest method stratifyOneRuleTest.

@Test
public void stratifyOneRuleTest() {
    ASPCore2Program prog = parser.parse("a :- b.");
    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);
    assertEquals(2, strata.size());
    assertTrue(predicateIsBeforePredicateInOrder(b, a, 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 9 with NormalProgram

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

the class NoGoodGeneratorTest method collectNeg_ContainsOnlyPositiveLiterals.

/**
 * Calls {@link NoGoodGenerator#collectNegLiterals(InternalRule, Substitution)}, which puts the atom occurring
 * negatively in a rule into the atom store. It is then checked whether the atom in the atom store is positive.
 */
@Test
public void collectNeg_ContainsOnlyPositiveLiterals() {
    ASPCore2Program input = PARSER.parse("p(a,b). " + "q(a,b) :- not nq(a,b). " + "nq(a,b) :- not q(a,b).");
    NormalProgram normal = NORMALIZE_TRANSFORM.apply(input);
    CompiledProgram program = InternalProgram.fromNormalProgram(normal);
    CompiledRule rule = program.getRules().get(1);
    AtomStore atomStore = new AtomStoreImpl();
    Grounder grounder = GrounderFactory.getInstance("naive", program, atomStore, true);
    NoGoodGenerator noGoodGenerator = ((NaiveGrounder) grounder).noGoodGenerator;
    Substitution substitution = new BasicSubstitution();
    substitution.put(X, A);
    substitution.put(Y, B);
    List<Integer> collectedNeg = noGoodGenerator.collectNegLiterals(rule, substitution);
    assertEquals(1, collectedNeg.size());
    String negAtomString = atomStore.atomToString(Literals.atomOf(collectedNeg.get(0)));
    assertEquals("q(a, b)", negAtomString);
}
Also used : ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) BasicSubstitution(at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution) AtomStoreImpl(at.ac.tuwien.kr.alpha.core.common.AtomStoreImpl) CompiledProgram(at.ac.tuwien.kr.alpha.core.programs.CompiledProgram) AtomStore(at.ac.tuwien.kr.alpha.core.common.AtomStore) Substitution(at.ac.tuwien.kr.alpha.api.grounder.Substitution) BasicSubstitution(at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution) CompiledRule(at.ac.tuwien.kr.alpha.core.rules.CompiledRule) NormalProgram(at.ac.tuwien.kr.alpha.api.programs.NormalProgram) Test(org.junit.jupiter.api.Test)

Example 10 with NormalProgram

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

the class DependencyGraphImplTest method reachabilityWithCyclesTest.

@Test
public void reachabilityWithCyclesTest() {
    StringBuilder bld = new StringBuilder();
    bld.append("b :- a, f1.").append("\n");
    bld.append("c :- b.").append("\n");
    bld.append("d :- c.").append("\n");
    bld.append("a :- d.").append("\n");
    bld.append("x :- d, f1.");
    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));
    Node f1 = dg.getNodeForPredicate(Predicates.getPredicate("f1", 0));
    Node x = dg.getNodeForPredicate(Predicates.getPredicate("x", 0));
    Node notInGraph = new NodeImpl(Predicates.getPredicate("notInGraph", 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));
    assertTrue(DependencyGraphUtils.isReachableFrom(a, d, dg));
    assertTrue(DependencyGraphUtils.isReachableFrom(a, c, dg));
    assertTrue(DependencyGraphUtils.isReachableFrom(a, b, dg));
    assertTrue(DependencyGraphUtils.isReachableFrom(x, f1, dg));
    assertTrue(DependencyGraphUtils.isReachableFrom(c, f1, dg));
    assertFalse(DependencyGraphUtils.isReachableFrom(notInGraph, a, 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)

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