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);
}
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);
}
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());
}
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));
}
}
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);
}
Aggregations