use of at.ac.tuwien.kr.alpha.api.programs.literals.Literal in project Alpha by alpha-asp.
the class RuleGroundingInfoImpl method selectNextGroundingLiteral.
private Literal selectNextGroundingLiteral(LinkedHashSet<Literal> remainingLiterals, Set<VariableTerm> boundVariables) {
Float bestSelectivity = Float.MAX_VALUE;
Literal bestLiteral = null;
boolean bestLiteralSharesVariables = false;
// To avoid cross products, select those first that have some of their variables already bound.
for (Literal literal : remainingLiterals) {
if (!boundVariables.containsAll(literal.getNonBindingVariables())) {
// Only consider literals whose nonbinding variables are already bound.
continue;
}
Float selectivity = literalSelectivity.get(literal);
boolean sharesVariables = sharesVariables(boundVariables, literal.getBindingVariables(), literal.getNonBindingVariables());
if (bestLiteral == null || sharesVariables && selectivity < bestSelectivity || sharesVariables && !bestLiteralSharesVariables) {
bestLiteral = literal;
bestSelectivity = selectivity;
bestLiteralSharesVariables = sharesVariables;
}
}
return bestLiteral;
}
use of at.ac.tuwien.kr.alpha.api.programs.literals.Literal in project Alpha by alpha-asp.
the class RuleGroundingInfoImpl method computeStartingLiterals.
/**
* Computes starting literals and indicates whether there is a fixed ground instantiation for this rule.
*
* @return true iff the rule has a fixed ground instantiation.
*/
private boolean computeStartingLiterals() {
LinkedHashSet<Literal> fixedStartingLiterals = new LinkedHashSet<>();
LinkedHashSet<Literal> ordinaryStartingLiterals = new LinkedHashSet<>();
// If the rule is ground, every body literal is a starting literal and the ground instantiation is fixed.
if (internalRule.isGround()) {
startingLiterals = new LinkedList<>(internalRule.getBody());
return true;
}
// Check each literal in the rule body whether it is eligible.
for (Literal literal : internalRule.getBody()) {
// Only literals that need no variables already bound can start grounding.
if (literal.getNonBindingVariables().size() != 0) {
continue;
}
if (literal.getAtom() instanceof BasicAtom && !literal.isNegated()) {
// Positive BasicAtom is the main/ordinary case.
ordinaryStartingLiterals.add(literal);
} else {
// If literal is no positive BasicAtom but requires no bound variables,
// it can be the starting literal for some (fixed) instantiation.
fixedStartingLiterals.add(literal);
}
}
// instantiation literals and those are starting for the one-time grounding.
if (!ordinaryStartingLiterals.isEmpty()) {
startingLiterals = new LinkedList<>(ordinaryStartingLiterals);
return false;
} else if (!fixedStartingLiterals.isEmpty()) {
startingLiterals = new LinkedList<>(fixedStartingLiterals);
return true;
} else {
throw new RuntimeException("Unsafe rule encountered: " + internalRule);
}
}
use of at.ac.tuwien.kr.alpha.api.programs.literals.Literal in project Alpha by alpha-asp.
the class AnalyzeUnjustified method unjustCover.
private Set<LitSet> unjustCover(List<Literal> vB, Set<Unifier> vY, Set<Unifier> vN, Assignment currentAssignment) {
padDepth += 2;
log("Begin UnjustCoverFixed()");
log("Finding unjustified body literals in: {} / {} excluded {}", vB, vY, vN);
Set<LitSet> ret = new LinkedHashSet<>();
if (vB.isEmpty() || vY.isEmpty()) {
log("End unjustCover().");
padDepth -= 2;
return Collections.emptySet();
}
int chosenLiteralPos = 0;
// Find a body literal that is not a ComparisonLiteral, because these do not generate/have atoms assigned.
for (int i = 0; i < vB.size(); i++) {
if (!(vB.get(i) instanceof ComparisonLiteral)) {
// TODO: Should this be FixedInterpretationLiteral instead??
chosenLiteralPos = i;
break;
}
}
Atom b = vB.get(chosenLiteralPos).getAtom();
log("Picked literal from body is: {}", b);
for (Unifier sigmaY : vY) {
Atom bSigmaY = b.substitute(sigmaY);
log("Treating substitution for: {}", bSigmaY);
Set<Unifier> vYp = new LinkedHashSet<>();
log("Checking atoms over predicate: {}", b.getPredicate());
AssignedAtomsIterator assignedAtomsOverPredicate = getAssignedAtomsOverPredicate(b.getPredicate());
atomLoop: while (assignedAtomsOverPredicate.hasNext()) {
Atom atom = assignedAtomsOverPredicate.next();
// Check that atom is justified/true.
log("Checking atom: {}", atom);
if (atomStore.contains(atom)) {
int atomId = atomStore.get(atom);
if (currentAssignment.getTruth(atomId) != ThriceTruth.TRUE) {
log("Atom is not TRUE. Skipping.");
continue;
}
}
// Note: in case the atom is not in the atomStore, it came from a fact and hence is true.
Unifier sigma = Unification.instantiate(b, atom);
if (sigma == null) {
log("Atom does not unify with picked body literal.");
continue;
}
Atom bSigma = b.substitute(sigma);
if (!bSigma.isGround()) {
throw oops("Resulting atom is not ground.");
}
Set<VariableTerm> variablesOccurringInSigma = sigma.getMappedVariables();
if (Unification.instantiate(bSigmaY, bSigma) != null) {
for (Unifier sigmaN : vN) {
ArrayList<Term> occurringVariables = new ArrayList<>(variablesOccurringInSigma);
occurringVariables.addAll(sigmaN.getMappedVariables());
BasicAtom genericAtom = Atoms.newBasicAtom(Predicates.getPredicate("_", occurringVariables.size(), true), occurringVariables);
Atom genericSubstituted = genericAtom.substitute(sigmaN).renameVariables("_analyzeTest");
if (Unification.instantiate(genericSubstituted, genericAtom.substitute(sigma)) != null) {
log("Atom {} is excluded by: {} via {}", genericSubstituted, sigmaN, sigma);
continue atomLoop;
}
}
log("Adding corresponding substitution to Y': {}", sigma);
vYp.add(sigma);
}
}
log("Unjustified body literals: {}", vYp);
Set<Unifier> vYpUN = new LinkedHashSet<>();
vYpUN.addAll(vYp);
vYpUN.addAll(vN);
LitSet toJustify = new LitSet(bSigmaY, vYpUN);
if (!toJustify.coversNothing()) {
log("New litset to do: {}", toJustify);
ret.add(toJustify);
} else {
log("Generated LitSet covers nothing. Ignoring: {}", toJustify);
}
ArrayList<Literal> newB = new ArrayList<>(vB);
newB.remove(chosenLiteralPos);
ret.addAll(unjustCover(newB, vYp, vN, currentAssignment));
log("Literal set(s) to treat: {}", ret);
}
log("End unjustCover().");
padDepth -= 2;
return ret;
}
use of at.ac.tuwien.kr.alpha.api.programs.literals.Literal in project Alpha by alpha-asp.
the class NoGoodGenerator method collectNegLiterals.
List<Integer> collectNegLiterals(final CompiledRule nonGroundRule, final Substitution substitution) {
final List<Integer> bodyLiteralsNegative = new ArrayList<>();
for (Literal lit : nonGroundRule.getNegativeBody()) {
Atom groundAtom = lit.getAtom().substitute(substitution);
final Set<Instance> factInstances = factsFromProgram.get(groundAtom.getPredicate());
if (factInstances != null && factInstances.contains(new Instance(groundAtom.getTerms()))) {
// Negative atom that is always true encountered, skip whole rule as it will never fire.
return null;
}
if (!existsRuleWithPredicateInHead(groundAtom.getPredicate())) {
// Negative atom is no fact and no rule defines it, it is always false, skip it.
continue;
}
bodyLiteralsNegative.add(atomToLiteral(atomStore.putIfAbsent(groundAtom)));
}
return bodyLiteralsNegative;
}
use of at.ac.tuwien.kr.alpha.api.programs.literals.Literal 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;
}
Aggregations