Search in sources :

Example 6 with CompiledProgram

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

the class StratifiedEvaluationTest method testNonGroundableRule.

@Test
public void testNonGroundableRule() {
    String asp = "p(a). q(a, b). s(X, Y) :- p(X), q(X, Y), r(Y).";
    CompiledProgram evaluated = parseAndEvaluate.apply(asp);
    Set<AnswerSet> answerSets = solveCompiledProg.apply(evaluated);
    TestUtils.assertAnswerSetsEqual("p(a), q(a,b)", 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 7 with CompiledProgram

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

the class StratifiedEvaluationTest method testNegatedFixedInterpretationLiteral.

@Test
public void testNegatedFixedInterpretationLiteral() {
    String asp = "stuff(1). stuff(2). smallStuff(X) :- stuff(X), not X > 1.";
    CompiledProgram evaluated = parseAndEvaluate.apply(asp);
    Set<AnswerSet> answerSets = solveCompiledProg.apply(evaluated);
    TestUtils.assertAnswerSetsEqual("stuff(1), stuff(2), smallStuff(1)", 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 8 with CompiledProgram

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

the class AnalyzeUnjustifiedTest method justifyLargerRules.

@Test
public void justifyLargerRules() {
    String program = "p(X) :- q(X,Y), r(Y), not s(X,Y)." + "{ q(1,X)} :- dom(X)." + "dom(1..3)." + "{r(X)} :- p(X)." + "{r(2)}." + "{s(1,2)}." + ":- not p(1).";
    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 p1 = parser.parse("p(1).").getFacts().get(0);
    Atom r2 = parser.parse("r(2).").getFacts().get(0);
    Atom s12 = parser.parse("s(1,2).").getFacts().get(0);
    Atom q11 = parser.parse("q(1,1).").getFacts().get(0);
    Atom q12 = parser.parse("q(1,2).").getFacts().get(0);
    Atom q13 = parser.parse("q(1,3).").getFacts().get(0);
    int p1Id = atomStore.get(p1);
    int r2Id = atomStore.get(r2);
    int s12Id = atomStore.get(s12);
    int q11Id = atomStore.get(q11);
    int q12Id = atomStore.get(q12);
    int q13Id = atomStore.get(q13);
    assignment.growForMaxAtomId();
    assignment.assign(p1Id, ThriceTruth.MBT);
    assignment.assign(r2Id, ThriceTruth.TRUE);
    assignment.assign(s12Id, ThriceTruth.TRUE);
    assignment.assign(q11Id, ThriceTruth.TRUE);
    assignment.assign(q12Id, ThriceTruth.TRUE);
    assignment.assign(q13Id, ThriceTruth.FALSE);
    Set<Literal> reasons = grounder.justifyAtom(p1Id, 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) Test(org.junit.jupiter.api.Test)

Example 9 with CompiledProgram

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

the class AnalyzeUnjustifiedTest method justifyNegatedFactsRemovedFromReasons.

@Test
public void justifyNegatedFactsRemovedFromReasons() {
    String program = "forbidden(2,9). forbidden(1,9)." + "p(X) :- q(X)." + "q(X) :- p(X)." + "q(5) :- r." + "r :- not nr, not forbidden(2,9), not forbidden(1,9)." + "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());
    for (Literal literal : reasons) {
        // Check that facts are not present in justification.
        assertNotEquals(literal.getPredicate(), Predicates.getPredicate("forbidden", 2));
    }
}
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 10 with CompiledProgram

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

the class StratifiedEvaluationRegressionTest method runTest.

@ParameterizedTest
@MethodSource("at.ac.tuwien.kr.alpha.core.programs.transformation.StratifiedEvaluationRegressionTest#params")
public void runTest(String aspString, Consumer<CompiledProgram> programVerifier, Consumer<Set<AnswerSet>> resultVerifier) {
    // Parse and pre-evaulate program
    ProgramParser parser = new ProgramParserImpl();
    ASPCore2Program prog = parser.parse(aspString);
    AnalyzedProgram analyzed = AnalyzedProgram.analyzeNormalProgram(new NormalizeProgramTransformation(SystemConfig.DEFAULT_AGGREGATE_REWRITING_CONFIG).apply(prog));
    CompiledProgram evaluated = new StratifiedEvaluation().apply(analyzed);
    // Verify stratified evaluation result
    programVerifier.accept(evaluated);
    // Solve remaining program
    AtomStore atomStore = new AtomStoreImpl();
    Grounder grounder = GrounderFactory.getInstance("naive", evaluated, atomStore, false);
    Solver solver = SolverFactory.getInstance(new SystemConfig(), atomStore, grounder);
    Set<AnswerSet> answerSets = solver.collectSet();
    resultVerifier.accept(answerSets);
}
Also used : ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) Solver(at.ac.tuwien.kr.alpha.api.Solver) SystemConfig(at.ac.tuwien.kr.alpha.api.config.SystemConfig) AnalyzedProgram(at.ac.tuwien.kr.alpha.core.programs.AnalyzedProgram) ProgramParser(at.ac.tuwien.kr.alpha.api.programs.ProgramParser) AnswerSet(at.ac.tuwien.kr.alpha.api.AnswerSet) AtomStoreImpl(at.ac.tuwien.kr.alpha.core.common.AtomStoreImpl) ProgramParserImpl(at.ac.tuwien.kr.alpha.core.parser.ProgramParserImpl) CompiledProgram(at.ac.tuwien.kr.alpha.core.programs.CompiledProgram) AtomStore(at.ac.tuwien.kr.alpha.core.common.AtomStore) Grounder(at.ac.tuwien.kr.alpha.core.grounder.Grounder) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

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