Search in sources :

Example 11 with CompiledProgram

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

the class NaiveGrounderTest method testPermissiveGrounderHeuristicTolerance.

/**
 * Tests if {@link NaiveGrounder#getGroundInstantiations(InternalRule, RuleGroundingOrder, Substitution, Assignment)}
 * produces ground instantiations for the rule with ID {@code ruleID} in {@code program} when {@code startingLiteral}
 * unified with the numeric instance {@code startingInstance} is used as starting literal and the following
 * additional conditions are established:
 * <ul>
 * <li>The atoms {@code b([startingInstance], 1), ..., b([startingInstance], n)} are added to the grounder's
 * working memory without changing the assignment, where {@code arityOfB-1} occurences of {@code startingInstance}
 * are used instead of {@code [startingInstance]} and {@code n} is the length of the {@code truthsOfB} array.
 * For example, if the length of {@code truthsOfB} is 2 and {@code arityOfB} is also 2, these atoms are
 * {@code b(1,1), b(1,2)}.
 * </li>
 * <li>The same atoms are assigned the truth values in the {@code truthsOfB} array.</li>
 * </ul>
 * It is asserted that ground instantiations are produced if and only if {@code expectNoGoods} is true.
 * If ground instantiations are produced, it is also asserted that the numbers of unassigned positive body atoms
 * determined by {@code getGroundInstantiations} match those given in {@code expectedNumbersOfUnassignedPositiveBodyAtoms}.
 */
private void testPermissiveGrounderHeuristicTolerance(ASPCore2Program program, int ruleID, Literal startingLiteral, int startingInstance, int tolerance, ThriceTruth[] truthsOfB, int arityOfB, boolean expectNoGoods, List<Integer> expectedNumbersOfUnassignedPositiveBodyAtoms) {
    CompiledProgram internalPrg = InternalProgram.fromNormalProgram(NORMALIZE_TRANSFORM.apply(program));
    AtomStore atomStore = new AtomStoreImpl();
    TrailAssignment currentAssignment = new TrailAssignment(atomStore);
    GrounderHeuristicsConfiguration heuristicConfiguration = GrounderHeuristicsConfiguration.getInstance(tolerance, tolerance);
    NaiveGrounder grounder = (NaiveGrounder) GrounderFactory.getInstance("naive", internalPrg, atomStore, p -> true, heuristicConfiguration, true);
    int[] bAtomIDs = new int[truthsOfB.length];
    for (int i = 0; i < truthsOfB.length; i++) {
        int[] bTerms = new int[arityOfB];
        for (int n = 0; n < arityOfB; n++) {
            bTerms[n] = (n == arityOfB - 1) ? i + 1 : startingInstance;
        }
        bAtomIDs[i] = atomStore.putIfAbsent(atom("b", bTerms));
    }
    addAtomsToWorkingMemoryWithoutChangingTheAssignment(atomStore, grounder, bAtomIDs);
    assign(currentAssignment, bAtomIDs, truthsOfB);
    grounder.bootstrap();
    final CompiledRule nonGroundRule = grounder.getNonGroundRule(ruleID);
    final Substitution substStartingLiteral = BasicSubstitution.specializeSubstitution(startingLiteral, new Instance(Terms.newConstant(startingInstance)), BasicSubstitution.EMPTY_SUBSTITUTION);
    final BindingResult bindingResult = grounder.getGroundInstantiations(nonGroundRule, nonGroundRule.getGroundingInfo().orderStartingFrom(startingLiteral), substStartingLiteral, currentAssignment);
    assertEquals(expectNoGoods, bindingResult.size() > 0);
    if (bindingResult.size() > 0) {
        assertEquals(expectedNumbersOfUnassignedPositiveBodyAtoms, bindingResult.getNumbersOfUnassignedPositiveBodyAtoms());
    } else {
        assertTrue(bindingResult.getNumbersOfUnassignedPositiveBodyAtoms().isEmpty());
    }
}
Also used : Assertions.fail(org.junit.jupiter.api.Assertions.fail) InternalProgram(at.ac.tuwien.kr.alpha.core.programs.InternalProgram) BeforeEach(org.junit.jupiter.api.BeforeEach) Substitution(at.ac.tuwien.kr.alpha.api.grounder.Substitution) Arrays(java.util.Arrays) TrailAssignment(at.ac.tuwien.kr.alpha.core.solver.TrailAssignment) BasicSubstitution(at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution) AnalyzedProgram(at.ac.tuwien.kr.alpha.core.programs.AnalyzedProgram) ThriceTruth(at.ac.tuwien.kr.alpha.core.solver.ThriceTruth) ProgramParser(at.ac.tuwien.kr.alpha.api.programs.ProgramParser) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) NoGood(at.ac.tuwien.kr.alpha.core.common.NoGood) Disabled(org.junit.jupiter.api.Disabled) NormalProgram(at.ac.tuwien.kr.alpha.api.programs.NormalProgram) Terms(at.ac.tuwien.kr.alpha.commons.terms.Terms) ProgramParserImpl(at.ac.tuwien.kr.alpha.core.parser.ProgramParserImpl) Map(java.util.Map) BindingResult(at.ac.tuwien.kr.alpha.core.grounder.instantiation.BindingResult) CompiledRule(at.ac.tuwien.kr.alpha.core.rules.CompiledRule) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Literals(at.ac.tuwien.kr.alpha.core.atoms.Literals) CompiledProgram(at.ac.tuwien.kr.alpha.core.programs.CompiledProgram) NormalizeProgramTransformation(at.ac.tuwien.kr.alpha.core.programs.transformation.NormalizeProgramTransformation) Collection(java.util.Collection) GrounderHeuristicsConfiguration(at.ac.tuwien.kr.alpha.api.config.GrounderHeuristicsConfiguration) StratifiedEvaluation(at.ac.tuwien.kr.alpha.core.programs.transformation.StratifiedEvaluation) ProgramPartParser(at.ac.tuwien.kr.alpha.core.parser.ProgramPartParser) TestUtils.atom(at.ac.tuwien.kr.alpha.core.test.util.TestUtils.atom) Test(org.junit.jupiter.api.Test) List(java.util.List) Assignment(at.ac.tuwien.kr.alpha.core.common.Assignment) Instance(at.ac.tuwien.kr.alpha.commons.substitutions.Instance) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) SystemConfig(at.ac.tuwien.kr.alpha.api.config.SystemConfig) ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) AtomStore(at.ac.tuwien.kr.alpha.core.common.AtomStore) InternalRule(at.ac.tuwien.kr.alpha.core.rules.InternalRule) Collections(java.util.Collections) AtomStoreImpl(at.ac.tuwien.kr.alpha.core.common.AtomStoreImpl) BindingResult(at.ac.tuwien.kr.alpha.core.grounder.instantiation.BindingResult) Instance(at.ac.tuwien.kr.alpha.commons.substitutions.Instance) 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) 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) GrounderHeuristicsConfiguration(at.ac.tuwien.kr.alpha.api.config.GrounderHeuristicsConfiguration) CompiledRule(at.ac.tuwien.kr.alpha.core.rules.CompiledRule)

Example 12 with CompiledProgram

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

the class RuleGroundingOrderTest method testPositionFromWhichAllVarsAreBound_simpleNonGround.

@Test
public void testPositionFromWhichAllVarsAreBound_simpleNonGround() {
    String aspStr = "a(X) :- b(X), not c(X).";
    CompiledProgram internalPrg = PARSE_AND_PREPROCESS.apply(aspStr);
    RuleGroundingInfo rgo0 = computeGroundingOrdersForRule(internalPrg, 0);
    assertEquals(1, rgo0.getStartingLiterals().size());
    for (Literal startingLiteral : rgo0.getStartingLiterals()) {
        assertEquals(0, rgo0.orderStartingFrom(startingLiteral).getPositionFromWhichAllVarsAreBound());
    }
}
Also used : Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) CompiledProgram(at.ac.tuwien.kr.alpha.core.programs.CompiledProgram) Test(org.junit.jupiter.api.Test)

Example 13 with CompiledProgram

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

the class RuleGroundingOrderTest method groundingOrder.

@Test
public void groundingOrder() {
    String aspStr = "h(X,C) :- p(X,Y), q(A,B), r(Y,A), s(C)." + "j(A,B,X,Y) :- r1(A,B), r1(X,Y), r1(A,X), r1(B,Y), A = B." + "p(a) :- b = a.";
    CompiledProgram prog = PARSE_AND_PREPROCESS.apply(aspStr);
    CompiledRule rule0 = prog.getRules().get(0);
    RuleGroundingInfo rgo0 = new RuleGroundingInfoImpl(rule0);
    rgo0.computeGroundingOrders();
    assertEquals(4, rgo0.getStartingLiterals().size());
    CompiledRule rule1 = prog.getRules().get(1);
    RuleGroundingInfo rgo1 = new RuleGroundingInfoImpl(rule1);
    rgo1.computeGroundingOrders();
    assertEquals(4, rgo1.getStartingLiterals().size());
    CompiledRule rule2 = prog.getRules().get(2);
    RuleGroundingInfo rgo2 = new RuleGroundingInfoImpl(rule2);
    rgo2.computeGroundingOrders();
    assertTrue(rgo2.hasFixedInstantiation());
}
Also used : CompiledRule(at.ac.tuwien.kr.alpha.core.rules.CompiledRule) CompiledProgram(at.ac.tuwien.kr.alpha.core.programs.CompiledProgram) Test(org.junit.jupiter.api.Test)

Example 14 with CompiledProgram

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

the class RuleGroundingOrderTest method testPositionFromWhichAllVarsAreBound_joinedNonGround.

@Test
public void testPositionFromWhichAllVarsAreBound_joinedNonGround() {
    String aspStr = "a(X) :- b(X), c(X,Y), d(X,Z), not e(X).";
    CompiledProgram internalPrg = PARSE_AND_PREPROCESS.apply(aspStr);
    RuleGroundingInfo rgo0 = computeGroundingOrdersForRule(internalPrg, 0);
    final Literal litBX = PROGRAM_PART_PARSER.parseLiteral("b(X)");
    final Literal litCXY = PROGRAM_PART_PARSER.parseLiteral("c(X,Y)");
    final Literal litDXZ = PROGRAM_PART_PARSER.parseLiteral("d(X,Z)");
    assertTrue(2 <= rgo0.orderStartingFrom(litBX).getPositionFromWhichAllVarsAreBound());
    assertTrue(1 <= rgo0.orderStartingFrom(litCXY).getPositionFromWhichAllVarsAreBound());
    assertTrue(1 <= rgo0.orderStartingFrom(litDXZ).getPositionFromWhichAllVarsAreBound());
}
Also used : Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) CompiledProgram(at.ac.tuwien.kr.alpha.core.programs.CompiledProgram) Test(org.junit.jupiter.api.Test)

Example 15 with CompiledProgram

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

the class ChoiceManagerTests method setUp.

@BeforeEach
public void setUp() {
    String testProgram = "h :- b1, b2, not b3, not b4.";
    ASPCore2Program parsedProgram = new ProgramParserImpl().parse(testProgram);
    CompiledProgram internalProgram = InternalProgram.fromNormalProgram(new NormalizeProgramTransformation(SystemConfig.DEFAULT_AGGREGATE_REWRITING_CONFIG).apply(parsedProgram));
    atomStore = new AtomStoreImpl();
    grounder = new NaiveGrounder(internalProgram, atomStore, true);
    WritableAssignment assignment = new TrailAssignment(atomStore);
    NoGoodStore store = new NoGoodStoreAlphaRoaming(assignment);
    choiceManager = new ChoiceManager(assignment, store);
}
Also used : ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) NaiveGrounder(at.ac.tuwien.kr.alpha.core.grounder.NaiveGrounder) 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) NormalizeProgramTransformation(at.ac.tuwien.kr.alpha.core.programs.transformation.NormalizeProgramTransformation) BeforeEach(org.junit.jupiter.api.BeforeEach)

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