Search in sources :

Example 31 with ASPCore2Program

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

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

the class RuleToStringTest method parseSingleRule.

private Rule<Head> parseSingleRule(String rule) {
    ASPCore2Program program = parser.parse(rule);
    List<Rule<Head>> rules = program.getRules();
    assertEquals(1, rules.size(), "Number of rules");
    return rules.get(0);
}
Also used : ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) Rule(at.ac.tuwien.kr.alpha.api.rules.Rule) CompiledRule(at.ac.tuwien.kr.alpha.core.rules.CompiledRule) BasicRule(at.ac.tuwien.kr.alpha.core.rules.BasicRule) InternalRule(at.ac.tuwien.kr.alpha.core.rules.InternalRule)

Example 33 with ASPCore2Program

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

the class AtomsTest method testExternalHasOutput.

@Test
public void testExternalHasOutput() {
    ASPCore2Program p = parser.parse("a:- &extWithOutput[1](OUT).", externals);
    Atom ext = p.getRules().get(0).getBody().stream().findFirst().get().getAtom();
    assertExternalAtomGround(ext, false);
    assertTrue(((ExternalAtom) ext).hasOutput());
}
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 34 with ASPCore2Program

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

the class RuleTest method testRulesEqual.

@Test
public void testRulesEqual() {
    ASPCore2Program p1 = parser.parse("p(X, Y) :- bla(X), blub(Y), foo(X, Y), not bar(X).");
    Rule<Head> r1 = p1.getRules().get(0);
    ASPCore2Program p2 = parser.parse("p(X, Y) :- bla(X), blub(Y), foo(X, Y), not bar(X).");
    Rule<Head> r2 = p2.getRules().get(0);
    ASPCore2Program p3 = parser.parse("p(X, Y) :- bla(X), blub(X), foo(X, X), not bar(X).");
    Rule<Head> r3 = p3.getRules().get(0);
    assertTrue(r1.equals(r2));
    assertTrue(r2.equals(r1));
    assertTrue(r1.hashCode() == r2.hashCode());
    assertFalse(r1.equals(r3));
    assertFalse(r3.equals(r1));
    assertTrue(r1.hashCode() != r3.hashCode());
    assertFalse(r2.equals(r3));
    assertFalse(r3.equals(r2));
    assertTrue(r2.hashCode() != r3.hashCode());
}
Also used : ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) Head(at.ac.tuwien.kr.alpha.api.rules.heads.Head) Test(org.junit.jupiter.api.Test)

Example 35 with ASPCore2Program

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

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