Search in sources :

Example 1 with FixedInterpretationLiteral

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

Example 2 with FixedInterpretationLiteral

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

the class NoGoodGenerator method collectPosLiterals.

private List<Integer> collectPosLiterals(final CompiledRule nonGroundRule, final Substitution substitution) {
    final List<Integer> bodyLiteralsPositive = new ArrayList<>();
    for (Literal lit : nonGroundRule.getPositiveBody()) {
        if (lit instanceof FixedInterpretationLiteral) {
            // FixedInterpretationAtoms need not be shown to the solver, skip it.
            continue;
        }
        final Atom atom = lit.getAtom();
        // Skip the special enumeration atom.
        if (atom instanceof EnumerationAtom) {
            continue;
        }
        final Atom groundAtom = atom.substitute(substitution);
        // Consider facts to eliminate ground atoms from the generated nogoods that are always true
        // and eliminate nogoods that are always satisfied due to facts.
        Set<Instance> factInstances = factsFromProgram.get(groundAtom.getPredicate());
        if (factInstances != null && factInstances.contains(new Instance(groundAtom.getTerms()))) {
            // Skip positive atoms that are always true.
            continue;
        }
        if (!existsRuleWithPredicateInHead(groundAtom.getPredicate())) {
            // Atom is no fact and no rule defines it, it cannot be derived (i.e., is always false), skip whole rule as it will never fire.
            return null;
        }
        bodyLiteralsPositive.add(atomToLiteral(atomStore.putIfAbsent(groundAtom)));
    }
    return bodyLiteralsPositive;
}
Also used : FixedInterpretationLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.FixedInterpretationLiteral) Instance(at.ac.tuwien.kr.alpha.commons.substitutions.Instance) FixedInterpretationLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.FixedInterpretationLiteral) Literals.negateLiteral(at.ac.tuwien.kr.alpha.core.atoms.Literals.negateLiteral) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) Literals.atomToLiteral(at.ac.tuwien.kr.alpha.core.atoms.Literals.atomToLiteral) ArrayList(java.util.ArrayList) EnumerationAtom(at.ac.tuwien.kr.alpha.core.atoms.EnumerationAtom) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) EnumerationAtom(at.ac.tuwien.kr.alpha.core.atoms.EnumerationAtom) RuleAtom(at.ac.tuwien.kr.alpha.core.atoms.RuleAtom)

Example 3 with FixedInterpretationLiteral

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

the class LiteralInstantiator method instantiateBasicLiteral.

/**
 * Calculates substitutions for a given literal that is not a {@link FixedInterpretationLiteral} or {@link EnumerationLiteral}.
 * If applying the given partial substitution to the literal already grounds the literal, the resulting ground literal is verified based on
 * this instantiators {@link LiteralInstantiationStrategy}. If the literal is only partially ground after applying the partial substitution,
 * ground substitutions are looked up using the instantiators {@link LiteralInstantiationStrategy}. This method assumes that the partial
 * substitution has <emph>not</emph> been applied to the passed literal.
 *
 * @param lit
 * @param partialSubstitution
 */
private LiteralInstantiationResult instantiateBasicLiteral(Literal lit, Substitution partialSubstitution) {
    LOGGER.trace("Instantiating basic literal: {}", lit);
    List<ImmutablePair<Substitution, AssignmentStatus>> substitutions;
    Literal substitutedLiteral = lit.substitute(partialSubstitution);
    LOGGER.trace("Substituted literal is {}", substitutedLiteral);
    if (substitutedLiteral.isGround()) {
        LOGGER.trace("Literal {} is already ground, checking truth", substitutedLiteral);
        // Lit seems to be a basic literal, so its satisfiability w.r.t.
        // partialSubstitution is decided based on knownInstances by the
        // instantiationStrategy.
        AssignmentStatus truthForLiteral = this.instantiationStrategy.getTruthForGroundLiteral(substitutedLiteral);
        if (truthForLiteral == AssignmentStatus.FALSE) {
            return LiteralInstantiationResult.stopBinding();
        } else {
            return LiteralInstantiationResult.continueBinding(partialSubstitution, truthForLiteral);
        }
    } else {
        LOGGER.trace("Handling non-ground literal {}", substitutedLiteral);
        if (substitutedLiteral.isNegated()) {
            return LiteralInstantiationResult.maybePushBack();
        }
        // Query instantiationStrategy for acceptable substitutions.
        // Note: getAcceptedSubstitutions will only give substitutions where the
        // resulting ground atom is true or unassigned, false atoms are internally
        // discarded.
        substitutions = this.instantiationStrategy.getAcceptedSubstitutions(substitutedLiteral, partialSubstitution);
        LOGGER.trace("Got {} substitutions from instantiation strategy for {}", substitutions.size(), substitutedLiteral);
        return substitutions.isEmpty() ? LiteralInstantiationResult.maybePushBack() : LiteralInstantiationResult.continueBinding(substitutions);
    }
}
Also used : ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) IntervalLiteral(at.ac.tuwien.kr.alpha.core.atoms.IntervalLiteral) FixedInterpretationLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.FixedInterpretationLiteral) EnumerationLiteral(at.ac.tuwien.kr.alpha.core.atoms.EnumerationLiteral) ExternalLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.ExternalLiteral) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) ComparisonLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.ComparisonLiteral)

Aggregations

FixedInterpretationLiteral (at.ac.tuwien.kr.alpha.api.programs.literals.FixedInterpretationLiteral)3 Literal (at.ac.tuwien.kr.alpha.api.programs.literals.Literal)2 Substitution (at.ac.tuwien.kr.alpha.api.grounder.Substitution)1 Atom (at.ac.tuwien.kr.alpha.api.programs.atoms.Atom)1 ComparisonLiteral (at.ac.tuwien.kr.alpha.api.programs.literals.ComparisonLiteral)1 ExternalLiteral (at.ac.tuwien.kr.alpha.api.programs.literals.ExternalLiteral)1 BasicSubstitution (at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution)1 Instance (at.ac.tuwien.kr.alpha.commons.substitutions.Instance)1 EnumerationAtom (at.ac.tuwien.kr.alpha.core.atoms.EnumerationAtom)1 EnumerationLiteral (at.ac.tuwien.kr.alpha.core.atoms.EnumerationLiteral)1 IntervalLiteral (at.ac.tuwien.kr.alpha.core.atoms.IntervalLiteral)1 Literals.atomToLiteral (at.ac.tuwien.kr.alpha.core.atoms.Literals.atomToLiteral)1 Literals.negateLiteral (at.ac.tuwien.kr.alpha.core.atoms.Literals.negateLiteral)1 RuleAtom (at.ac.tuwien.kr.alpha.core.atoms.RuleAtom)1 ArrayList (java.util.ArrayList)1 ImmutablePair (org.apache.commons.lang3.tuple.ImmutablePair)1