use of at.ac.tuwien.kr.alpha.commons.substitutions.Instance in project Alpha by alpha-asp.
the class FactIntervalEvaluator method unrollInstances.
private static List<Instance> unrollInstances(Term[] currentTerms, int currentPosition) {
if (currentPosition == currentTerms.length) {
return Collections.singletonList(new Instance(currentTerms));
}
Term currentTerm = currentTerms[currentPosition];
if (!(currentTerm instanceof IntervalTerm)) {
return unrollInstances(currentTerms, currentPosition + 1);
}
List<Instance> instances = new ArrayList<>();
int lower = ((IntervalTerm) currentTerm).getLowerBound();
int upper = ((IntervalTerm) currentTerm).getUpperBound();
for (int i = lower; i <= upper; i++) {
Term[] clonedTerms = currentTerms.clone();
clonedTerms[currentPosition] = Terms.newConstant(i);
instances.addAll(unrollInstances(clonedTerms, currentPosition + 1));
}
return instances;
}
use of at.ac.tuwien.kr.alpha.commons.substitutions.Instance in project Alpha by alpha-asp.
the class FactIntervalEvaluator method constructFactInstances.
/**
* Helper to construct Instances from a fact that may contain intervals.
*
* @param fact the fact potentially containing intervals.
* @return all instances stemming from unfolding the intervals.
*/
public static List<Instance> constructFactInstances(Atom fact) {
// Construct instance(s) from the fact.
int arity = fact.getPredicate().getArity();
Term[] currentTerms = new Term[arity];
boolean containsIntervals = false;
// Check if instance contains intervals at all.
for (int i = 0; i < arity; i++) {
Term term = fact.getTerms().get(i);
currentTerms[i] = term;
if (term instanceof IntervalTerm) {
containsIntervals = true;
} else if (term instanceof FunctionTerm && IntervalTerm.functionTermContainsIntervals((FunctionTerm) term)) {
containsIntervals = true;
throw new UnsupportedOperationException("Intervals inside function terms in facts are not supported yet. Try turning the fact into a rule.");
}
}
// If fact contains no intervals, simply return the single instance.
if (!containsIntervals) {
return Collections.singletonList(new Instance(currentTerms));
}
// Fact contains intervals, unroll them all.
return unrollInstances(currentTerms, 0);
}
use of at.ac.tuwien.kr.alpha.commons.substitutions.Instance 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.commons.substitutions.Instance in project Alpha by alpha-asp.
the class InternalProgram method recordFacts.
private void recordFacts(List<Atom> facts) {
for (Atom fact : facts) {
List<Instance> tmpInstances = FactIntervalEvaluator.constructFactInstances(fact);
Predicate tmpPredicate = fact.getPredicate();
factsByPredicate.putIfAbsent(tmpPredicate, new LinkedHashSet<>());
factsByPredicate.get(tmpPredicate).addAll(tmpInstances);
}
}
use of at.ac.tuwien.kr.alpha.commons.substitutions.Instance in project Alpha by alpha-asp.
the class AbstractLiteralInstantiationStrategy method getAcceptedSubstitutions.
/**
* See {@link LiteralInstantiationStrategy#getAcceptedSubstitutions(Literal, BasicSubstitution)}.
*
* A very general implementation of the basic steps needed to obtain ground substitutions for a positive literal.
* Potentially valid ground instances are obtained using {@link AbstractLiteralInstantiationStrategy#computeCandidateInstances(Atom)}, then
* checked, such that each candidate instance unifies with the given partial substitution and has a "valid" {@link AssignmentStatus},
* where "validity" of an {@link AssignmentStatus} is determined using the abstract method
* {@link AbstractLiteralInstantiationStrategy#assignmentStatusAccepted(AssignmentStatus)}.
*/
@Override
public final List<ImmutablePair<Substitution, AssignmentStatus>> getAcceptedSubstitutions(Literal lit, Substitution partialSubstitution) {
Atom atom = lit.getAtom();
Iterable<Instance> groundInstances = this.computeCandidateInstances(atom);
return this.buildSubstitutionsFromInstances(atom, groundInstances, partialSubstitution);
}
Aggregations