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);
}
}
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;
}
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);
}
}
Aggregations