Search in sources :

Example 16 with Substitution

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

the class LiteralInstantiatorTest method workingMemoryBasedInstantiatePositiveBasicLiteral.

@Test
public void workingMemoryBasedInstantiatePositiveBasicLiteral() {
    Predicate p = Predicates.getPredicate("p", 2);
    WorkingMemory workingMemory = new WorkingMemory();
    workingMemory.initialize(p);
    workingMemory.addInstance(Atoms.newBasicAtom(p, Terms.newSymbolicConstant("x"), Terms.newSymbolicConstant("y")), true);
    workingMemory.addInstance(Atoms.newBasicAtom(p, Terms.newSymbolicConstant("x"), Terms.newSymbolicConstant("z")), true);
    VariableTerm x = Terms.newVariable("X");
    VariableTerm y = Terms.newVariable("Y");
    Literal lit = Literals.fromAtom(Atoms.newBasicAtom(p, x, y), true);
    Substitution substitution = new BasicSubstitution();
    substitution.put(x, Terms.newSymbolicConstant("x"));
    LiteralInstantiator instantiator = new LiteralInstantiator(new WorkingMemoryBasedInstantiationStrategy(workingMemory));
    LiteralInstantiationResult result = instantiator.instantiateLiteral(lit, substitution);
    assertEquals(LiteralInstantiationResult.Type.CONTINUE, result.getType());
    List<ImmutablePair<Substitution, AssignmentStatus>> substitutions = result.getSubstitutions();
    assertEquals(2, substitutions.size());
    boolean ySubstituted = false;
    boolean zSubstituted = false;
    for (ImmutablePair<Substitution, AssignmentStatus> resultSubstitution : substitutions) {
        assertTrue(resultSubstitution.left.isVariableSet(y));
        assertEquals(AssignmentStatus.TRUE, resultSubstitution.right);
        if (resultSubstitution.left.eval(y).equals(Terms.newSymbolicConstant("y"))) {
            ySubstituted = true;
        } else if (resultSubstitution.left.eval(y).equals(Terms.newSymbolicConstant("z"))) {
            zSubstituted = true;
        } else {
            fail("Invalid substitution for variable Y");
        }
    }
    assertTrue(ySubstituted && zSubstituted);
}
Also used : WorkingMemory(at.ac.tuwien.kr.alpha.core.grounder.WorkingMemory) BasicSubstitution(at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate) Substitution(at.ac.tuwien.kr.alpha.api.grounder.Substitution) BasicSubstitution(at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) EnumerationLiteral(at.ac.tuwien.kr.alpha.core.atoms.EnumerationLiteral) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) Test(org.junit.jupiter.api.Test)

Example 17 with Substitution

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

the class NaiveGrounder method bindNextAtomInRule.

// @formatter:off
/**
 * Computes ground substitutions for a literal based on a {@link RuleGroundingOrderImpl} and a {@link BasicSubstitution}.
 *
 * Computes ground substitutions for the literal at position <code>orderPosition</code> of <code>groundingOrder</code>
 * Actual substitutions are computed by this grounder's {@link LiteralInstantiator}.
 *
 * @param groundingOrder a {@link RuleGroundingOrderImpl} representing the body literals of a rule in the
 * 						 sequence in which the should be bound during grounding.
 * @param orderPosition the current position within <code>groundingOrder</code>, indicates which literal should be bound
 * @param originalTolerance the original tolerance of the used grounding heuristic
 * @param remainingTolerance the remaining tolerance, determining if binding continues in the presence of substitutions based on unassigned atoms
 * @param partialSubstitution a substitution
 * @return a {@link BindingResult} representing applicable ground substitutions for all literals after orderPosition in groundingOrder
 */
// @formatter:on
private BindingResult bindNextAtomInRule(RuleGroundingOrder groundingOrder, int orderPosition, int originalTolerance, int remainingTolerance, Substitution partialSubstitution) {
    Literal currentLiteral = groundingOrder.getLiteralAtOrderPosition(orderPosition);
    if (currentLiteral == null) {
        LOGGER.trace("No more literals found in grounding order, therefore stopping binding!");
        return BindingResult.singleton(partialSubstitution, originalTolerance - remainingTolerance);
    }
    LOGGER.trace("Binding current literal {} with remaining tolerance {} and partial substitution {}.", currentLiteral, remainingTolerance, partialSubstitution);
    LiteralInstantiationResult instantiationResult = ruleInstantiator.instantiateLiteral(currentLiteral, partialSubstitution);
    switch(instantiationResult.getType()) {
        case CONTINUE:
            /*
				 * Recursively call bindNextAtomInRule for each generated substitution
				 * and the next literal in the grounding order (i.e. advance), thereby reducing remaining
				 * tolerance by 1 iff a substitution uses an unassigned ground atom.
				 * If remainingTolerance falls below zero, an empty {@link BindingResult} is returned.
				 */
            List<ImmutablePair<Substitution, AssignmentStatus>> substitutionInfos = instantiationResult.getSubstitutions();
            LOGGER.trace("Literal instantiator yielded {} substitutions for literal {}.", substitutionInfos.size(), currentLiteral);
            BindingResult retVal = new BindingResult();
            for (ImmutablePair<Substitution, AssignmentStatus> substitutionInfo : substitutionInfos) {
                retVal.add(this.continueBinding(groundingOrder, orderPosition, originalTolerance, remainingTolerance, substitutionInfo));
            }
            return retVal;
        case PUSH_BACK:
            /*
				 * Delegate to pushBackAndBindNextAtomInRule(RuleGroundingOrder, int, int, int, Substitution, Assignment).
				 * Pushes the current literal to the end of the grounding order and calls bindNextAtomInRule with the modified grounding oder.
				 */
            LOGGER.trace("Pushing back literal {} in grounding order.", currentLiteral);
            return pushBackAndBindNextAtomInRule(groundingOrder, orderPosition, originalTolerance, remainingTolerance, partialSubstitution);
        case MAYBE_PUSH_BACK:
            /*
				 * Indicates that the rule instantiator could not find any substitutions for the current literal. If a permissive grounder heuristic is in
				 * use, push the current literal to the end of the grounding order and proceed with the next one, otherwise return an empty BindingResult.
				 */
            if (originalTolerance > 0) {
                LOGGER.trace("No substitutions yielded by literal instantiator for literal {}, but using permissive heuristic, therefore pushing the literal back.", currentLiteral);
                // i.e. it is deemed acceptable to have ground rules where a number of body atoms are not yet assigned a truth value by the solver.
                return pushBackAndBindNextAtomInRule(groundingOrder, orderPosition, originalTolerance, remainingTolerance, partialSubstitution);
            } else {
                LOGGER.trace("No substitutions found for literal {}", currentLiteral);
                return BindingResult.empty();
            }
        case STOP_BINDING:
            LOGGER.trace("No substitutions found for literal {}", currentLiteral);
            return BindingResult.empty();
        default:
            throw Util.oops("Unhandled literal instantiation result type: " + instantiationResult.getType());
    }
}
Also used : BindingResult(at.ac.tuwien.kr.alpha.core.grounder.instantiation.BindingResult) 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) LiteralInstantiationResult(at.ac.tuwien.kr.alpha.core.grounder.instantiation.LiteralInstantiationResult) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) AssignmentStatus(at.ac.tuwien.kr.alpha.core.grounder.instantiation.AssignmentStatus)

Example 18 with Substitution

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

the class NaiveGrounder method groundAndRegister.

/**
 * Grounds the given {@code nonGroundRule} by applying the given {@code substitutions} and registers the nogoods generated during that
 * process.
 *
 * @param nonGroundRule the rule to be grounded.
 * @param substitutions the substitutions to be applied.
 * @param newNoGoods    a set of nogoods to which newly generated nogoods will be added.
 */
private void groundAndRegister(final CompiledRule nonGroundRule, final List<Substitution> substitutions, final Map<Integer, NoGood> newNoGoods) {
    for (Substitution substitution : substitutions) {
        List<NoGood> generatedNoGoods = noGoodGenerator.generateNoGoodsFromGroundSubstitution(nonGroundRule, substitution);
        registry.register(generatedNoGoods, newNoGoods);
    }
}
Also used : Substitution(at.ac.tuwien.kr.alpha.api.grounder.Substitution) BasicSubstitution(at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution) NoGood(at.ac.tuwien.kr.alpha.core.common.NoGood)

Example 19 with Substitution

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

the class AbstractLiteralInstantiationStrategy method buildSubstitutionsFromInstances.

/**
 * Based on a list of candidate instances (see {@link AbstractLiteralInstantiationStrategy#computeCandidateInstances(Atom)}), create a list
 * of substitutions and assignment statuses such that each substitution represents a valid (according to the implementation-specific
 * definition of this instantiation strategy) ground instance of <code>atomToSubstitute</code>.
 *
 * @param atomToSubstitute
 * @param candidateInstances
 * @param partialSubstitution
 * @return
 */
protected final List<ImmutablePair<Substitution, AssignmentStatus>> buildSubstitutionsFromInstances(Atom atomToSubstitute, Iterable<Instance> candidateInstances, Substitution partialSubstitution) {
    List<ImmutablePair<Substitution, AssignmentStatus>> retVal = new ArrayList<>();
    // Filter for only instances unifying with partialSubsitution, i.e. "where all joins work out".
    Substitution currentInstanceSubstitution;
    Atom atomForCurrentInstance;
    for (Instance instance : candidateInstances) {
        currentInstanceSubstitution = BasicSubstitution.specializeSubstitution(atomToSubstitute, instance, partialSubstitution);
        if (currentInstanceSubstitution == null) {
            // Instance does not unify with partialSubstitution, move on to the next instance.
            continue;
        }
        // At this point, we know that the substitution works out.
        // Now check whether the resulting Atom has an acceptable AssignmentStatus.
        atomForCurrentInstance = Atoms.newBasicAtom(atomToSubstitute.getPredicate(), atomToSubstitute.getTerms()).substitute(currentInstanceSubstitution);
        AssignmentStatus assignmentStatus = this.getAssignmentStatusForAtom(atomForCurrentInstance);
        if (!this.assignmentStatusAccepted(assignmentStatus)) {
            // Atom has an assignment status deemed unacceptable by this instantiation strategy.
            continue;
        }
        retVal.add(new ImmutablePair<>(currentInstanceSubstitution, assignmentStatus));
    }
    return retVal;
}
Also used : 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) Instance(at.ac.tuwien.kr.alpha.commons.substitutions.Instance) ArrayList(java.util.ArrayList) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom)

Example 20 with Substitution

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

the class LiteralInstantiator method instantiateFixedInterpretationLiteral.

/**
 * Calculates satisfying substitutions for a given {@link FixedInterpretationLiteral} based on a partial substitution. This method assumes
 * that the partial substitution has <emph>not</emph> been applied to the passed literal.
 *
 * @param lit                 the (fixed interpretation) literal for which to calculate substitutions
 * @param partialSubstitution
 * @return a LiteralInstantiationResult representing the result of the search for substitutions
 */
private LiteralInstantiationResult instantiateFixedInterpretationLiteral(FixedInterpretationLiteral lit, Substitution partialSubstitution) {
    LOGGER.trace("Instantiating FixedInterpretationLiteral: {}", lit);
    List<Substitution> substitutions;
    FixedInterpretationLiteral substitutedLiteral = (FixedInterpretationLiteral) lit.substitute(partialSubstitution);
    if (this.shouldPushBackFixedInterpretationLiteral(substitutedLiteral)) {
        return LiteralInstantiationResult.pushBack();
    } else {
        substitutions = substitutedLiteral.getSatisfyingSubstitutions(partialSubstitution);
        return substitutions.isEmpty() ? LiteralInstantiationResult.stopBinding() : LiteralInstantiationResult.continueBindingWithTrueSubstitutions(substitutions);
    }
}
Also used : FixedInterpretationLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.FixedInterpretationLiteral) Substitution(at.ac.tuwien.kr.alpha.api.grounder.Substitution) BasicSubstitution(at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution)

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