Search in sources :

Example 1 with NormalProgram

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

the class ArithmeticTermsRewritingTest method rewriteRule.

@Test
public void rewriteRule() {
    NormalProgram inputProgram = NormalProgramImpl.fromInputProgram(parser.parse("p(X+1) :- q(Y/2), r(f(X*2),Y), X-2 = Y*3, X = 0..9."));
    assertEquals(1, inputProgram.getRules().size());
    ArithmeticTermsRewriting arithmeticTermsRewriting = new ArithmeticTermsRewriting();
    NormalProgram rewrittenProgram = arithmeticTermsRewriting.apply(inputProgram);
    // Expect the rewritten program to be one rule with: p(_A0) :- _A0 = X+1,  _A1 = Y/2, q(_A1), _A2 = X*2, r(f(_A2),Y), X-2 = Y*3, X = 0..9.
    assertEquals(1, rewrittenProgram.getRules().size());
    NormalRule rewrittenRule = rewrittenProgram.getRules().get(0);
    assertTrue(rewrittenRule.getHeadAtom().getTerms().get(0) instanceof VariableTerm);
    assertEquals(7, rewrittenRule.getBody().size());
}
Also used : NormalProgram(at.ac.tuwien.kr.alpha.api.programs.NormalProgram) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) NormalRule(at.ac.tuwien.kr.alpha.api.rules.NormalRule) Test(org.junit.jupiter.api.Test)

Example 2 with NormalProgram

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

the class ArithmeticTermsRewritingTest method rewriteExternalAtom.

@Test
public void rewriteExternalAtom() {
    NormalProgram inputProgram = NormalProgramImpl.fromInputProgram(parser.parse("p :- Y = 13, &extArithTest[Y*5](Y-4)."));
    assertEquals(1, inputProgram.getRules().size());
    ArithmeticTermsRewriting arithmeticTermsRewriting = new ArithmeticTermsRewriting();
    NormalProgram rewrittenProgram = arithmeticTermsRewriting.apply(inputProgram);
    assertEquals(1, rewrittenProgram.getRules().size());
    NormalRule rewrittenRule = rewrittenProgram.getRules().get(0);
    assertEquals(4, rewrittenRule.getBody().size());
    List<Literal> externalLiterals = rewrittenRule.getBody().stream().filter(lit -> lit instanceof ExternalLiteral).collect(toList());
    assertEquals(1, externalLiterals.size());
    ExternalAtom rewrittenExternal = ((ExternalLiteral) externalLiterals.get(0)).getAtom();
    assertEquals(1, rewrittenExternal.getInput().size());
    assertTrue(rewrittenExternal.getInput().get(0) instanceof VariableTerm);
    assertEquals(1, rewrittenExternal.getOutput().size());
    assertTrue(rewrittenExternal.getOutput().get(0) instanceof VariableTerm);
}
Also used : NormalRule(at.ac.tuwien.kr.alpha.api.rules.NormalRule) ProgramParser(at.ac.tuwien.kr.alpha.api.programs.ProgramParser) ExternalAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.ExternalAtom) Set(java.util.Set) ExternalLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.ExternalLiteral) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) Predicate(at.ac.tuwien.kr.alpha.api.externals.Predicate) NormalProgram(at.ac.tuwien.kr.alpha.api.programs.NormalProgram) ConstantTerm(at.ac.tuwien.kr.alpha.api.terms.ConstantTerm) Externals(at.ac.tuwien.kr.alpha.core.externals.Externals) Test(org.junit.jupiter.api.Test) Terms(at.ac.tuwien.kr.alpha.commons.terms.Terms) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) PredicateInterpretation(at.ac.tuwien.kr.alpha.api.common.fixedinterpretations.PredicateInterpretation) ProgramParserImpl(at.ac.tuwien.kr.alpha.core.parser.ProgramParserImpl) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Map(java.util.Map) NormalProgramImpl(at.ac.tuwien.kr.alpha.core.programs.NormalProgramImpl) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Collections(java.util.Collections) ExternalLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.ExternalLiteral) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) NormalProgram(at.ac.tuwien.kr.alpha.api.programs.NormalProgram) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) NormalRule(at.ac.tuwien.kr.alpha.api.rules.NormalRule) ExternalAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.ExternalAtom) ExternalLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.ExternalLiteral) Test(org.junit.jupiter.api.Test)

Example 3 with NormalProgram

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

the class NaiveGrounderTest method groundConstraintAlreadyGround.

/**
 * Asserts that a ground constraint whose positive body is not satisfied by the empty assignment
 * is grounded immediately.
 */
@Test
public void groundConstraintAlreadyGround() {
    ASPCore2Program program = PROGRAM_PARSER.parse("a :- not b. " + "b :- not a. " + ":- b.");
    NormalProgram normal = NORMALIZE_TRANSFORM.apply(program);
    InternalProgram prog = new StratifiedEvaluation().apply(AnalyzedProgram.analyzeNormalProgram(normal));
    AtomStore atomStore = new AtomStoreImpl();
    Grounder grounder = GrounderFactory.getInstance("naive", prog, atomStore, true);
    Map<Integer, NoGood> noGoods = grounder.getNoGoods(new TrailAssignment(atomStore));
    int litB = Literals.atomToLiteral(atomStore.get(PROGRAM_PART_PARSER.parseBasicAtom("b")));
    assertTrue(noGoods.containsValue(NoGood.fromConstraint(Collections.singletonList(litB), Collections.emptyList())));
}
Also used : ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) AtomStore(at.ac.tuwien.kr.alpha.core.common.AtomStore) NoGood(at.ac.tuwien.kr.alpha.core.common.NoGood) AtomStoreImpl(at.ac.tuwien.kr.alpha.core.common.AtomStoreImpl) NormalProgram(at.ac.tuwien.kr.alpha.api.programs.NormalProgram) InternalProgram(at.ac.tuwien.kr.alpha.core.programs.InternalProgram) StratifiedEvaluation(at.ac.tuwien.kr.alpha.core.programs.transformation.StratifiedEvaluation) TrailAssignment(at.ac.tuwien.kr.alpha.core.solver.TrailAssignment) Test(org.junit.jupiter.api.Test)

Example 4 with NormalProgram

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

the class NaiveGrounderTest method groundRuleWithLongerBodyAlreadyGround.

/**
 * Asserts that a ground rule whose positive non-unary body is not satisfied by the empty assignment
 * is grounded immediately.
 */
@Test
public void groundRuleWithLongerBodyAlreadyGround() {
    ASPCore2Program program = PROGRAM_PARSER.parse("a :- not b. " + "b :- not a. " + "c :- b. " + "d :- b, c. ");
    NormalProgram normal = NORMALIZE_TRANSFORM.apply(program);
    InternalProgram prog = new StratifiedEvaluation().apply(AnalyzedProgram.analyzeNormalProgram(normal));
    AtomStore atomStore = new AtomStoreImpl();
    Grounder grounder = GrounderFactory.getInstance("naive", prog, atomStore, true);
    Map<Integer, NoGood> noGoods = grounder.getNoGoods(new TrailAssignment(atomStore));
    int litANeg = Literals.atomToLiteral(atomStore.get(PROGRAM_PART_PARSER.parseBasicAtom("a")), false);
    int litBNeg = Literals.atomToLiteral(atomStore.get(PROGRAM_PART_PARSER.parseBasicAtom("b")), false);
    int litCNeg = Literals.atomToLiteral(atomStore.get(PROGRAM_PART_PARSER.parseBasicAtom("c")), false);
    int litDNeg = Literals.atomToLiteral(atomStore.get(PROGRAM_PART_PARSER.parseBasicAtom("d")), false);
    assertExistsNoGoodContaining(noGoods.values(), litANeg);
    assertExistsNoGoodContaining(noGoods.values(), litBNeg);
    assertExistsNoGoodContaining(noGoods.values(), litCNeg);
    assertExistsNoGoodContaining(noGoods.values(), litDNeg);
}
Also used : ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) AtomStore(at.ac.tuwien.kr.alpha.core.common.AtomStore) NoGood(at.ac.tuwien.kr.alpha.core.common.NoGood) AtomStoreImpl(at.ac.tuwien.kr.alpha.core.common.AtomStoreImpl) NormalProgram(at.ac.tuwien.kr.alpha.api.programs.NormalProgram) InternalProgram(at.ac.tuwien.kr.alpha.core.programs.InternalProgram) StratifiedEvaluation(at.ac.tuwien.kr.alpha.core.programs.transformation.StratifiedEvaluation) TrailAssignment(at.ac.tuwien.kr.alpha.core.solver.TrailAssignment) Test(org.junit.jupiter.api.Test)

Example 5 with NormalProgram

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

the class TestUtils method buildSolverFromSystemConfig.

private static Solver buildSolverFromSystemConfig(ASPCore2Program prog, SystemConfig cfg) {
    AtomStore atomStore = new AtomStoreImpl();
    NormalProgram normalProg = new NormalizeProgramTransformation(cfg.getAggregateRewritingConfig()).apply(prog);
    InternalProgram preprocessed = cfg.isEvaluateStratifiedPart() ? new StratifiedEvaluation().apply(AnalyzedProgram.analyzeNormalProgram(normalProg)) : InternalProgram.fromNormalProgram(normalProg);
    return SolverFactory.getInstance(cfg, atomStore, GrounderFactory.getInstance(cfg.getGrounderName(), preprocessed, atomStore, cfg.isDebugInternalChecks()));
}
Also used : AtomStore(at.ac.tuwien.kr.alpha.core.common.AtomStore) AtomStoreImpl(at.ac.tuwien.kr.alpha.core.common.AtomStoreImpl) NormalProgram(at.ac.tuwien.kr.alpha.api.programs.NormalProgram) InternalProgram(at.ac.tuwien.kr.alpha.core.programs.InternalProgram) NormalizeProgramTransformation(at.ac.tuwien.kr.alpha.core.programs.transformation.NormalizeProgramTransformation) StratifiedEvaluation(at.ac.tuwien.kr.alpha.core.programs.transformation.StratifiedEvaluation)

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