Search in sources :

Example 16 with BasicSubstitution

use of at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution in project Alpha by alpha-asp.

the class NaiveGrounder method continueBinding.

/**
 * Helper method used by {@link NaiveGrounder#bindNextAtomInRule(RuleGroundingOrderImpl, int, int, int, BasicSubstitution)}.
 *
 * Takes an <code>ImmutablePair</code> of a {@link BasicSubstitution} and an accompanying {@link AssignmentStatus} and calls
 * <code>bindNextAtomInRule</code> for the next literal in the grounding order.
 * If the assignment status for the last bound literal was {@link AssignmentStatus#UNASSIGNED}, the <code>remainingTolerance</code>
 * parameter is decreased by 1. If the remaining tolerance drops below zero, this method returns an empty {@link BindingResult}.
 *
 * @param groundingOrder
 * @param orderPosition
 * @param originalTolerance
 * @param remainingTolerance
 * @param lastLiteralBindingResult
 * @return the result of calling bindNextAtomInRule on the next literal in the grounding order, or an empty binding result if remaining
 *         tolerance is less than zero.
 */
private BindingResult continueBinding(RuleGroundingOrder groundingOrder, int orderPosition, int originalTolerance, int remainingTolerance, ImmutablePair<Substitution, AssignmentStatus> lastLiteralBindingResult) {
    Substitution substitution = lastLiteralBindingResult.left;
    AssignmentStatus lastBoundLiteralAssignmentStatus = lastLiteralBindingResult.right;
    switch(lastBoundLiteralAssignmentStatus) {
        case TRUE:
            return advanceAndBindNextAtomInRule(groundingOrder, orderPosition, originalTolerance, remainingTolerance, substitution);
        case UNASSIGNED:
            // The last literal bound to obtain the current substitution has not been assigned a truth value by the solver yet.
            // If we still have enough tolerance, we can continue grounding nevertheless.
            int toleranceForNextRun = remainingTolerance - 1;
            if (toleranceForNextRun >= 0) {
                return advanceAndBindNextAtomInRule(groundingOrder, orderPosition, originalTolerance, toleranceForNextRun, substitution);
            } else {
                return BindingResult.empty();
            }
        case FALSE:
            throw Util.oops("Got an assignmentStatus FALSE for literal " + groundingOrder.getLiteralAtOrderPosition(orderPosition) + " and substitution " + substitution + " - should not happen!");
        default:
            throw Util.oops("Got unsupported assignmentStatus " + lastBoundLiteralAssignmentStatus);
    }
}
Also used : Substitution(at.ac.tuwien.kr.alpha.api.grounder.Substitution) BasicSubstitution(at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution) AssignmentStatus(at.ac.tuwien.kr.alpha.core.grounder.instantiation.AssignmentStatus)

Example 17 with BasicSubstitution

use of at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution in project Alpha by alpha-asp.

the class EnumerationAtom method addEnumerationIndexToSubstitution.

/**
 * Based on a given substitution, substitutes the first two terms of this {@link EnumerationAtom} with the values from the substitution,
 * and returns a new substitution with all mappings from the input substitution plus a binding for the third term of the enum atom to the
 * integer index that is mapped to the first two terms in the internal <code>ENUMERATIONS</code> map.
 *
 * @param substitution an input substitution which must provide ground terms for the first two terms of the enumeration atom.
 * @return a new substitution where the third term of the enumeration atom is bound to an integer.
 */
public Substitution addEnumerationIndexToSubstitution(Substitution substitution) {
    Term idTerm = this.getTerms().get(0).substitute(substitution);
    Term enumerationTerm = this.getTerms().get(1).substitute(substitution);
    if (!enumerationTerm.isGround()) {
        throw new RuntimeException("Enumeration term is not ground after substitution. Should not happen.");
    }
    Integer enumerationIndex = getEnumerationIndex(idTerm, enumerationTerm);
    BasicSubstitution retVal = new BasicSubstitution(substitution);
    retVal.put((VariableTerm) getTerms().get(2), Terms.newConstant(enumerationIndex));
    return retVal;
}
Also used : BasicSubstitution(at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution) Term(at.ac.tuwien.kr.alpha.api.terms.Term) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm)

Example 18 with BasicSubstitution

use of at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution in project Alpha by alpha-asp.

the class LiteralInstantiationStrategyTest method defaultLazyGroundingSubstituteNonGroundLiteralWithTrueInstance.

/**
 * Uses {@link DefaultLazyGroundingInstantiationStrategy} to find the ground
 * instance "q(a, b)" for the partially ground positive literal "q(a, X)".
 *
 * In this case, the instantiation strategy has an assignment where q(a, b) is
 * assigned ThriceTruth.TRUE, so we expect the assignment status (i.e.
 * assignment status of the found ground instance) passed back with the
 * substitution to be TRUE. Furthermore, we expect the stale atom set to stay
 * empty.
 */
@Test
public void defaultLazyGroundingSubstituteNonGroundLiteralWithTrueInstance() {
    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);
    AtomStore atomStore = new AtomStoreImpl();
    WritableAssignment assignment = new TrailAssignment(atomStore);
    BasicAtom groundAtom = Atoms.newBasicAtom(q, Terms.newSymbolicConstant("a"), Terms.newSymbolicConstant("b"));
    atomStore.putIfAbsent(groundAtom);
    assignment.growForMaxAtomId();
    assignment.assign(atomStore.get(groundAtom), ThriceTruth.TRUE);
    LinkedHashSet<Atom> staleSet = new LinkedHashSet<>();
    DefaultLazyGroundingInstantiationStrategy strategy = new DefaultLazyGroundingInstantiationStrategy(workingMemory, atomStore, Collections.emptyMap(), false);
    strategy.setStaleWorkingMemoryEntries(staleSet);
    strategy.setCurrentAssignment(assignment);
    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) TrailAssignment(at.ac.tuwien.kr.alpha.core.solver.TrailAssignment) 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) AtomStore(at.ac.tuwien.kr.alpha.core.common.AtomStore) 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) WritableAssignment(at.ac.tuwien.kr.alpha.core.solver.WritableAssignment) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) Test(org.junit.jupiter.api.Test)

Example 19 with BasicSubstitution

use of at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution in project Alpha by alpha-asp.

the class LiteralInstantiationStrategyTest method defaultLazyGroundingSubstituteNonGroundLiteralWithUnassignedInstance.

/**
 * Uses {@link DefaultLazyGroundingInstantiationStrategy} to find the ground
 * instance "q(a, b)" for the partially ground positive literal "q(a, X)".
 *
 * In this case, the instantiation strategy has an empty assignment, so we
 * expect the assignment status (i.e. assignment status of the found ground
 * instance) passed back with the substitution to be UNASSIGNED. Since
 * UNASSIGNED and FALSE atoms are (potentially) stale in working memory, we
 * expect the atom "q(a, b)" to be added to the stale set by the instantiation
 * strategy.
 */
@Test
public void defaultLazyGroundingSubstituteNonGroundLiteralWithUnassignedInstance() {
    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);
    AtomStore atomStore = new AtomStoreImpl();
    WritableAssignment assignment = new TrailAssignment(atomStore);
    LinkedHashSet<Atom> staleSet = new LinkedHashSet<>();
    DefaultLazyGroundingInstantiationStrategy strategy = new DefaultLazyGroundingInstantiationStrategy(workingMemory, atomStore, Collections.emptyMap(), false);
    strategy.setStaleWorkingMemoryEntries(staleSet);
    strategy.setCurrentAssignment(assignment);
    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.UNASSIGNED, assignmentStatus);
    assertTrue(substitution.isVariableSet(Terms.newVariable("X")));
    assertEquals(Terms.newSymbolicConstant("b"), substitution.eval(Terms.newVariable("X")));
    assertEquals(1, staleSet.size());
    assertTrue(staleSet.contains(Atoms.newBasicAtom(q, Terms.newSymbolicConstant("a"), Terms.newSymbolicConstant("b"))));
}
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) TrailAssignment(at.ac.tuwien.kr.alpha.core.solver.TrailAssignment) 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) AtomStore(at.ac.tuwien.kr.alpha.core.common.AtomStore) 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) WritableAssignment(at.ac.tuwien.kr.alpha.core.solver.WritableAssignment) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) Test(org.junit.jupiter.api.Test)

Example 20 with BasicSubstitution

use of at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution in project Alpha by alpha-asp.

the class AtomCounterTests method createRuleAtom.

private void createRuleAtom() {
    BasicAtom atomAA = Atoms.newBasicAtom(Predicates.getPredicate("aa", 0));
    CompiledRule ruleAA = new InternalRule(Heads.newNormalHead(atomAA), Collections.singletonList(Atoms.newBasicAtom(Predicates.getPredicate("bb", 0)).toLiteral(false)));
    atomStore.putIfAbsent(new RuleAtom(ruleAA, new BasicSubstitution()));
}
Also used : CompiledRule(at.ac.tuwien.kr.alpha.core.rules.CompiledRule) BasicSubstitution(at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) RuleAtom(at.ac.tuwien.kr.alpha.core.atoms.RuleAtom) InternalRule(at.ac.tuwien.kr.alpha.core.rules.InternalRule)

Aggregations

BasicSubstitution (at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution)24 Substitution (at.ac.tuwien.kr.alpha.api.grounder.Substitution)18 Test (org.junit.jupiter.api.Test)13 VariableTerm (at.ac.tuwien.kr.alpha.api.terms.VariableTerm)10 Predicate (at.ac.tuwien.kr.alpha.api.programs.Predicate)9 ImmutablePair (org.apache.commons.lang3.tuple.ImmutablePair)9 Literal (at.ac.tuwien.kr.alpha.api.programs.literals.Literal)8 BasicAtom (at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom)7 WorkingMemory (at.ac.tuwien.kr.alpha.core.grounder.WorkingMemory)7 Term (at.ac.tuwien.kr.alpha.api.terms.Term)6 EnumerationLiteral (at.ac.tuwien.kr.alpha.core.atoms.EnumerationLiteral)6 AtomStoreImpl (at.ac.tuwien.kr.alpha.core.common.AtomStoreImpl)5 Atom (at.ac.tuwien.kr.alpha.api.programs.atoms.Atom)4 AtomStore (at.ac.tuwien.kr.alpha.core.common.AtomStore)4 LinkedHashSet (java.util.LinkedHashSet)4 ConstantTerm (at.ac.tuwien.kr.alpha.api.terms.ConstantTerm)3 CompiledRule (at.ac.tuwien.kr.alpha.core.rules.CompiledRule)3 TrailAssignment (at.ac.tuwien.kr.alpha.core.solver.TrailAssignment)3 WritableAssignment (at.ac.tuwien.kr.alpha.core.solver.WritableAssignment)3 ArrayList (java.util.ArrayList)3