Search in sources :

Example 6 with Substitution

use of at.ac.tuwien.kr.alpha.api.grounder.Substitution in project Alpha by alpha-asp.

the class StratifiedEvaluation method calcSubstitutionsWithGroundingOrder.

private List<Substitution> calcSubstitutionsWithGroundingOrder(RuleGroundingOrder groundingOrder, List<Substitution> startingSubstitutions) {
    // Iterate through the grounding order and whenever instantiation of a Literal with a given substitution
    // causes a result with a type other than CONTINUE, discard that substitution.
    // Note that this function uses a stack of partial substitutions to simulate a recursive function.
    // For speed, we really want ArrayLists on the stack.
    Stack<ArrayList<Substitution>> substitutionStack = new Stack<>();
    if (startingSubstitutions instanceof ArrayList) {
        substitutionStack.push((ArrayList<Substitution>) startingSubstitutions);
    } else {
        // Copy startingSubstitutions into ArrayList. Note: mostly happens for empty or
        substitutionStack.push(new ArrayList<>(startingSubstitutions));
    // singleton lists.
    }
    int currentOrderPosition = 0;
    List<Substitution> fullSubstitutions = new ArrayList<>();
    while (!substitutionStack.isEmpty()) {
        List<Substitution> currentSubstitutions = substitutionStack.peek();
        // If no more substitutions remain at current position, all have been processed, continue on next lower level.
        if (currentSubstitutions.isEmpty()) {
            substitutionStack.pop();
            currentOrderPosition--;
            continue;
        }
        // In case the full grounding order has been worked on, all current substitutions are full substitutions, add them to
        // result.
        Literal currentLiteral = groundingOrder.getLiteralAtOrderPosition(currentOrderPosition);
        if (currentLiteral == null) {
            fullSubstitutions.addAll(currentSubstitutions);
            currentSubstitutions.clear();
            // Continue on next lower level.
            substitutionStack.pop();
            currentOrderPosition--;
            continue;
        }
        // Take one substitution from the top-list of the stack and try extending it.
        // Work on last element (removing last element is
        Substitution currentSubstitution = currentSubstitutions.remove(currentSubstitutions.size() - 1);
        // O(1) for ArrayList).
        LiteralInstantiationResult currentLiteralResult = literalInstantiator.instantiateLiteral(currentLiteral, currentSubstitution);
        if (currentLiteralResult.getType() == LiteralInstantiationResult.Type.CONTINUE) {
            // The currentSubstitution could be extended, push the extensions on the stack and continue working on them.
            ArrayList<Substitution> furtheredSubstitutions = new ArrayList<>();
            for (ImmutablePair<Substitution, AssignmentStatus> resultSubstitution : currentLiteralResult.getSubstitutions()) {
                furtheredSubstitutions.add(resultSubstitution.left);
            }
            substitutionStack.push(furtheredSubstitutions);
            // Continue work on the higher level.
            currentOrderPosition++;
        }
    }
    return fullSubstitutions;
}
Also used : Substitution(at.ac.tuwien.kr.alpha.api.grounder.Substitution) BasicSubstitution(at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution) LiteralInstantiationResult(at.ac.tuwien.kr.alpha.core.grounder.instantiation.LiteralInstantiationResult) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) ArrayList(java.util.ArrayList) AssignmentStatus(at.ac.tuwien.kr.alpha.core.grounder.instantiation.AssignmentStatus) Stack(java.util.Stack)

Example 7 with Substitution

use of at.ac.tuwien.kr.alpha.api.grounder.Substitution 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 8 with Substitution

use of at.ac.tuwien.kr.alpha.api.grounder.Substitution in project Alpha by alpha-asp.

the class LiteralInstantiationStrategyTest method defaultLazyGroundingNoAssignmentSubstituteNonGroundLiteral.

/**
 * Uses {@link DefaultLazyGroundingInstantiationStrategy} to find ground
 * instances for the partially ground positive literal "q(a, X)".
 *
 * In this case, the instantiation strategy does not have an assignment set (as
 * is the case when {@link NaiveGrounder} is in bootstrap), so we expect the
 * assignment status (i.e. assignment status of the found ground instance)
 * passed back with the substitution to be TRUE. Furthermore, the stale atom set
 * (used by {@link NaiveGrounder} to clean up atoms that should be deleted from
 * working memory) must stay empty.
 */
@Test
public void defaultLazyGroundingNoAssignmentSubstituteNonGroundLiteral() {
    Predicate q = Predicates.getPredicate("q", 2);
    BasicAtom nonGroundAtom = Atoms.newBasicAtom(q, Terms.newSymbolicConstant("a"), Terms.newVariable("X"));
    WorkingMemory workingMemory = new WorkingMemory();
    workingMemory.initialize(q);
    workingMemory.addInstance(Atoms.newBasicAtom(q, Terms.newSymbolicConstant("a"), Terms.newSymbolicConstant("b")), true);
    LinkedHashSet<Atom> staleSet = new LinkedHashSet<>();
    DefaultLazyGroundingInstantiationStrategy strategy = new DefaultLazyGroundingInstantiationStrategy(workingMemory, new AtomStoreImpl(), Collections.emptyMap(), false);
    strategy.setStaleWorkingMemoryEntries(staleSet);
    strategy.setCurrentAssignment(null);
    List<ImmutablePair<Substitution, AssignmentStatus>> result = strategy.getAcceptedSubstitutions(Literals.fromAtom(nonGroundAtom, true), new BasicSubstitution());
    assertEquals(1, result.size());
    ImmutablePair<Substitution, AssignmentStatus> substitutionInfo = result.get(0);
    Substitution substitution = substitutionInfo.left;
    AssignmentStatus assignmentStatus = substitutionInfo.right;
    assertEquals(AssignmentStatus.TRUE, assignmentStatus);
    assertTrue(substitution.isVariableSet(Terms.newVariable("X")));
    assertEquals(Terms.newSymbolicConstant("b"), substitution.eval(Terms.newVariable("X")));
    assertTrue(staleSet.isEmpty());
}
Also used : LinkedHashSet(java.util.LinkedHashSet) WorkingMemory(at.ac.tuwien.kr.alpha.core.grounder.WorkingMemory) BasicSubstitution(at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution) AtomStoreImpl(at.ac.tuwien.kr.alpha.core.common.AtomStoreImpl) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) Substitution(at.ac.tuwien.kr.alpha.api.grounder.Substitution) BasicSubstitution(at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) Test(org.junit.jupiter.api.Test)

Example 9 with Substitution

use of at.ac.tuwien.kr.alpha.api.grounder.Substitution in project Alpha by alpha-asp.

the class SubstitutionTest method specializeTermsFunctionTermBinding.

@Test
public void specializeTermsFunctionTermBinding() {
    Substitution substitution = new BasicSubstitution();
    substitution.put(Y, A);
    FunctionTerm groundFunctionTerm = Terms.newFunctionTerm("f", B, C);
    Instance qfBC = new Instance(groundFunctionTerm);
    Term nongroundFunctionTerm = Terms.newFunctionTerm("f", B, X);
    BasicAtom qfBX = Atoms.newBasicAtom(Predicates.getPredicate("q", 1), nongroundFunctionTerm);
    Substitution substitution1 = BasicSubstitution.specializeSubstitution(qfBX, qfBC, substitution);
    assertEquals(C, substitution1.eval(X));
    assertEquals(A, substitution1.eval(Y));
}
Also used : FunctionTerm(at.ac.tuwien.kr.alpha.api.terms.FunctionTerm) Substitution(at.ac.tuwien.kr.alpha.api.grounder.Substitution) BasicSubstitution(at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution) Instance(at.ac.tuwien.kr.alpha.commons.substitutions.Instance) BasicSubstitution(at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution) ConstantTerm(at.ac.tuwien.kr.alpha.api.terms.ConstantTerm) Term(at.ac.tuwien.kr.alpha.api.terms.Term) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) FunctionTerm(at.ac.tuwien.kr.alpha.api.terms.FunctionTerm) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) Test(org.junit.jupiter.api.Test)

Example 10 with Substitution

use of at.ac.tuwien.kr.alpha.api.grounder.Substitution in project Alpha by alpha-asp.

the class SubstitutionTest method specializeBasicAtom.

@Test
public void specializeBasicAtom() {
    Predicate p = Predicates.getPredicate("p", 2);
    BasicAtom atom = Atoms.newBasicAtom(p, Arrays.asList(X, Y));
    Instance instance = new Instance(A, B);
    Substitution substitution = BasicSubstitution.specializeSubstitution(atom, instance, BasicSubstitution.EMPTY_SUBSTITUTION);
    BasicAtom substituted = atom.substitute(substitution);
    assertEquals(p, substituted.getPredicate());
    assertEquals(A, substituted.getTerms().get(0));
    assertEquals(B, substituted.getTerms().get(1));
}
Also used : Substitution(at.ac.tuwien.kr.alpha.api.grounder.Substitution) BasicSubstitution(at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution) Instance(at.ac.tuwien.kr.alpha.commons.substitutions.Instance) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate) Test(org.junit.jupiter.api.Test)

Aggregations

Substitution (at.ac.tuwien.kr.alpha.api.grounder.Substitution)34 BasicSubstitution (at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution)33 Test (org.junit.jupiter.api.Test)19 Literal (at.ac.tuwien.kr.alpha.api.programs.literals.Literal)13 Predicate (at.ac.tuwien.kr.alpha.api.programs.Predicate)10 BasicAtom (at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom)8 Instance (at.ac.tuwien.kr.alpha.commons.substitutions.Instance)8 CompiledRule (at.ac.tuwien.kr.alpha.core.rules.CompiledRule)8 VariableTerm (at.ac.tuwien.kr.alpha.api.terms.VariableTerm)7 AtomStoreImpl (at.ac.tuwien.kr.alpha.core.common.AtomStoreImpl)7 ImmutablePair (org.apache.commons.lang3.tuple.ImmutablePair)7 Atom (at.ac.tuwien.kr.alpha.api.programs.atoms.Atom)6 EnumerationLiteral (at.ac.tuwien.kr.alpha.core.atoms.EnumerationLiteral)6 AtomStore (at.ac.tuwien.kr.alpha.core.common.AtomStore)6 NoGood (at.ac.tuwien.kr.alpha.core.common.NoGood)6 WorkingMemory (at.ac.tuwien.kr.alpha.core.grounder.WorkingMemory)6 ArrayList (java.util.ArrayList)6 BindingResult (at.ac.tuwien.kr.alpha.core.grounder.instantiation.BindingResult)5 ASPCore2Program (at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program)4 NormalProgram (at.ac.tuwien.kr.alpha.api.programs.NormalProgram)4