Search in sources :

Example 11 with ASPCore2Program

use of at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program 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 12 with ASPCore2Program

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

the class NaiveGrounderTest method testGroundingOfRuleNotSwitchedOffByTruePositiveBody.

@Test
public void testGroundingOfRuleNotSwitchedOffByTruePositiveBody() {
    ASPCore2Program program = PROGRAM_PARSER.parse("a(1). " + "c(X) :- a(X), b(X). " + "b(X) :- something(X). ");
    testIfGrounderGroundsRule(program, 0, litAX, 1, ThriceTruth.TRUE, true);
}
Also used : ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) Test(org.junit.jupiter.api.Test)

Example 13 with ASPCore2Program

use of at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program 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 14 with ASPCore2Program

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

the class NaiveGrounderTest method testPermissiveGrounderHeuristicTolerance_0_reject.

@Test
public void testPermissiveGrounderHeuristicTolerance_0_reject() {
    ASPCore2Program program = PROGRAM_PARSER.parse("a(1). " + "c(X) :- a(X), b(X). " + "b(X) :- something(X).");
    testPermissiveGrounderHeuristicTolerance(program, 0, litAX, 1, 0, false, Arrays.asList(1));
}
Also used : ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) Test(org.junit.jupiter.api.Test)

Example 15 with ASPCore2Program

use of at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program 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

ASPCore2Program (at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program)89 Test (org.junit.jupiter.api.Test)70 AnswerSet (at.ac.tuwien.kr.alpha.api.AnswerSet)23 NormalProgram (at.ac.tuwien.kr.alpha.api.programs.NormalProgram)22 Alpha (at.ac.tuwien.kr.alpha.api.Alpha)21 InputConfig (at.ac.tuwien.kr.alpha.api.config.InputConfig)19 AnalyzedProgram (at.ac.tuwien.kr.alpha.core.programs.AnalyzedProgram)15 ProgramParserImpl (at.ac.tuwien.kr.alpha.core.parser.ProgramParserImpl)14 DependencyGraph (at.ac.tuwien.kr.alpha.api.programs.analysis.DependencyGraph)12 CompiledProgram (at.ac.tuwien.kr.alpha.core.programs.CompiledProgram)11 Atom (at.ac.tuwien.kr.alpha.api.programs.atoms.Atom)10 Disabled (org.junit.jupiter.api.Disabled)9 SystemConfig (at.ac.tuwien.kr.alpha.api.config.SystemConfig)8 AtomStoreImpl (at.ac.tuwien.kr.alpha.core.common.AtomStoreImpl)8 InputProgram (at.ac.tuwien.kr.alpha.core.programs.InputProgram)8 HashSet (java.util.HashSet)8 Predicate (at.ac.tuwien.kr.alpha.api.programs.Predicate)7 ComponentGraph (at.ac.tuwien.kr.alpha.api.programs.analysis.ComponentGraph)7 SCComponent (at.ac.tuwien.kr.alpha.api.programs.analysis.ComponentGraph.SCComponent)7 AtomStore (at.ac.tuwien.kr.alpha.core.common.AtomStore)7