Search in sources :

Example 16 with CompiledProgram

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

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

the class StratifiedEvaluationTest method testAggregateSpecial.

@Test
public void testAggregateSpecial() {
    String asp = "thing(1..3).\n" + "% choose exactly one - since Alpha doesn't support bounds,\n" + "% this needs two constraints\n" + "{ chosenThing(X) : thing(X) }.\n" + "chosenSomething :- chosenThing(X).\n" + ":- not chosenSomething.\n" + ":- chosenThing(X), chosenThing(Y), X != Y.\n" + "allThings :- 3 <= #count{ X : thing(X)}. \n" + "chosenMaxThing :- allThings, chosenThing(3).\n" + ":- not chosenMaxThing.";
    CompiledProgram evaluated = parseAndEvaluate.apply(asp);
    assertTrue(evaluated.getFacts().contains(TestUtils.basicAtomWithSymbolicTerms("allThings")));
    Set<AnswerSet> answerSets = solveCompiledProg.apply(evaluated);
    TestUtils.assertAnswerSetsEqual("allThings, thing(1), thing(2), thing(3), chosenMaxThing, chosenSomething, chosenThing(3)", answerSets);
}
Also used : AnswerSet(at.ac.tuwien.kr.alpha.api.AnswerSet) CompiledProgram(at.ac.tuwien.kr.alpha.core.programs.CompiledProgram) Test(org.junit.jupiter.api.Test)

Example 18 with CompiledProgram

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

the class StratifiedEvaluationTest method testDuplicateFacts.

@Test
public void testDuplicateFacts() {
    String aspStr = "p(a). p(b). q(b). q(X) :- p(X).";
    CompiledProgram evaluated = parseAndEvaluate.apply(aspStr);
    Instance qOfB = new Instance(TestUtils.basicAtomWithSymbolicTerms("q", "b").getTerms());
    Set<Instance> facts = evaluated.getFactsByPredicate().get(Predicates.getPredicate("q", 1));
    int numQOfB = 0;
    for (Instance at : facts) {
        if (at.equals(qOfB)) {
            numQOfB++;
        }
    }
    assertEquals(1, numQOfB);
}
Also used : Instance(at.ac.tuwien.kr.alpha.commons.substitutions.Instance) CompiledProgram(at.ac.tuwien.kr.alpha.core.programs.CompiledProgram) Test(org.junit.jupiter.api.Test)

Example 19 with CompiledProgram

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

the class AnalyzeUnjustifiedTest method justifySimpleRules.

@Test
public void justifySimpleRules() {
    String program = "p(X) :- q(X)." + "q(X) :- p(X)." + "q(5) :- r." + "r :- not nr." + "nr :- not r." + ":- not p(5).";
    CompiledProgram internalProgram = parseAndPreprocess.apply(program);
    AtomStore atomStore = new AtomStoreImpl();
    NaiveGrounder grounder = new NaiveGrounder(internalProgram, atomStore, true);
    grounder.getNoGoods(null);
    TrailAssignment assignment = new TrailAssignment(atomStore);
    int rId = atomStore.get(Atoms.newBasicAtom(Predicates.getPredicate("r", 0)));
    int nrId = atomStore.get(Atoms.newBasicAtom(Predicates.getPredicate("nr", 0)));
    assignment.growForMaxAtomId();
    assignment.assign(rId, ThriceTruth.FALSE);
    assignment.assign(nrId, ThriceTruth.TRUE);
    BasicAtom p5 = Atoms.newBasicAtom(Predicates.getPredicate("p", 1), Collections.singletonList(Terms.newConstant(5)));
    assignment.assign(atomStore.get(p5), ThriceTruth.MBT);
    Set<Literal> reasons = grounder.justifyAtom(atomStore.get(p5), assignment);
    assertFalse(reasons.isEmpty());
}
Also used : NaiveGrounder(at.ac.tuwien.kr.alpha.core.grounder.NaiveGrounder) AtomStore(at.ac.tuwien.kr.alpha.core.common.AtomStore) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) AtomStoreImpl(at.ac.tuwien.kr.alpha.core.common.AtomStoreImpl) CompiledProgram(at.ac.tuwien.kr.alpha.core.programs.CompiledProgram) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) TrailAssignment(at.ac.tuwien.kr.alpha.core.solver.TrailAssignment) Test(org.junit.jupiter.api.Test)

Example 20 with CompiledProgram

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

the class AnalyzeUnjustifiedTest method justifyMultipleReasons.

@Test
public void justifyMultipleReasons() {
    String program = "n(a). n(b). n(c). n(d). n(e)." + "s(a,b). s(b,c). s(c,d). s(d,e)." + "{ q(X) } :- n(X)." + "p(X) :- q(X)." + "p(X) :- p(Y), s(Y,X)." + ":- not p(c).";
    CompiledProgram internalProgram = parseAndPreprocess.apply(program);
    AtomStore atomStore = new AtomStoreImpl();
    NaiveGrounder grounder = new NaiveGrounder(internalProgram, atomStore, true);
    grounder.getNoGoods(null);
    TrailAssignment assignment = new TrailAssignment(atomStore);
    Atom qa = parser.parse("q(a).").getFacts().get(0);
    Atom qb = parser.parse("q(b).").getFacts().get(0);
    Atom qc = parser.parse("q(c).").getFacts().get(0);
    Atom qd = parser.parse("q(d).").getFacts().get(0);
    Atom qe = parser.parse("q(e).").getFacts().get(0);
    int qaId = atomStore.get(qa);
    int qbId = atomStore.get(qb);
    int qcId = atomStore.get(qc);
    int qdId = atomStore.get(qd);
    int qeId = atomStore.get(qe);
    assignment.growForMaxAtomId();
    assignment.assign(qaId, ThriceTruth.FALSE);
    assignment.assign(qbId, ThriceTruth.FALSE);
    assignment.assign(qcId, ThriceTruth.FALSE);
    assignment.assign(qdId, ThriceTruth.FALSE);
    assignment.assign(qeId, ThriceTruth.FALSE);
    Predicate nq = Predicates.getPredicate("_nq", 2, true);
    Atom nqa = Atoms.newBasicAtom(nq, Arrays.asList(Terms.newConstant("1"), Terms.newSymbolicConstant("a")));
    Atom nqb = Atoms.newBasicAtom(nq, Arrays.asList(Terms.newConstant("1"), Terms.newSymbolicConstant("b")));
    Atom nqc = Atoms.newBasicAtom(nq, Arrays.asList(Terms.newConstant("1"), Terms.newSymbolicConstant("c")));
    Atom nqd = Atoms.newBasicAtom(nq, Arrays.asList(Terms.newConstant("1"), Terms.newSymbolicConstant("d")));
    Atom nqe = Atoms.newBasicAtom(nq, Arrays.asList(Terms.newConstant("1"), Terms.newSymbolicConstant("e")));
    int nqaId = atomStore.get(nqa);
    int nqbId = atomStore.get(nqb);
    int nqcId = atomStore.get(nqc);
    int nqdId = atomStore.get(nqd);
    int nqeId = atomStore.get(nqe);
    assignment.growForMaxAtomId();
    assignment.assign(nqaId, ThriceTruth.TRUE);
    assignment.assign(nqbId, ThriceTruth.TRUE);
    assignment.assign(nqcId, ThriceTruth.TRUE);
    assignment.assign(nqdId, ThriceTruth.TRUE);
    assignment.assign(nqeId, ThriceTruth.TRUE);
    Atom pc = parser.parse("p(c).").getFacts().get(0);
    Set<Literal> reasons = grounder.justifyAtom(atomStore.get(pc), assignment);
    assertFalse(reasons.isEmpty());
}
Also used : NaiveGrounder(at.ac.tuwien.kr.alpha.core.grounder.NaiveGrounder) AtomStore(at.ac.tuwien.kr.alpha.core.common.AtomStore) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) AtomStoreImpl(at.ac.tuwien.kr.alpha.core.common.AtomStoreImpl) CompiledProgram(at.ac.tuwien.kr.alpha.core.programs.CompiledProgram) TrailAssignment(at.ac.tuwien.kr.alpha.core.solver.TrailAssignment) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate) Test(org.junit.jupiter.api.Test)

Aggregations

CompiledProgram (at.ac.tuwien.kr.alpha.core.programs.CompiledProgram)33 Test (org.junit.jupiter.api.Test)30 AtomStoreImpl (at.ac.tuwien.kr.alpha.core.common.AtomStoreImpl)12 ASPCore2Program (at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program)11 Literal (at.ac.tuwien.kr.alpha.api.programs.literals.Literal)11 AtomStore (at.ac.tuwien.kr.alpha.core.common.AtomStore)10 AnswerSet (at.ac.tuwien.kr.alpha.api.AnswerSet)9 TrailAssignment (at.ac.tuwien.kr.alpha.core.solver.TrailAssignment)9 NormalProgram (at.ac.tuwien.kr.alpha.api.programs.NormalProgram)7 SystemConfig (at.ac.tuwien.kr.alpha.api.config.SystemConfig)6 BasicAtom (at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom)6 NaiveGrounder (at.ac.tuwien.kr.alpha.core.grounder.NaiveGrounder)6 ProgramParserImpl (at.ac.tuwien.kr.alpha.core.parser.ProgramParserImpl)6 ProgramParser (at.ac.tuwien.kr.alpha.api.programs.ProgramParser)5 AnalyzedProgram (at.ac.tuwien.kr.alpha.core.programs.AnalyzedProgram)5 CompiledRule (at.ac.tuwien.kr.alpha.core.rules.CompiledRule)5 BeforeEach (org.junit.jupiter.api.BeforeEach)5 Substitution (at.ac.tuwien.kr.alpha.api.grounder.Substitution)4 BasicSubstitution (at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution)4 Instance (at.ac.tuwien.kr.alpha.commons.substitutions.Instance)4