Search in sources :

Example 16 with Instance

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;
}
Also used : Instance(at.ac.tuwien.kr.alpha.commons.substitutions.Instance) IntervalTerm(at.ac.tuwien.kr.alpha.commons.terms.IntervalTerm) ArrayList(java.util.ArrayList) Term(at.ac.tuwien.kr.alpha.api.terms.Term) IntervalTerm(at.ac.tuwien.kr.alpha.commons.terms.IntervalTerm) FunctionTerm(at.ac.tuwien.kr.alpha.api.terms.FunctionTerm)

Example 17 with Instance

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);
}
Also used : FunctionTerm(at.ac.tuwien.kr.alpha.api.terms.FunctionTerm) Instance(at.ac.tuwien.kr.alpha.commons.substitutions.Instance) IntervalTerm(at.ac.tuwien.kr.alpha.commons.terms.IntervalTerm) Term(at.ac.tuwien.kr.alpha.api.terms.Term) IntervalTerm(at.ac.tuwien.kr.alpha.commons.terms.IntervalTerm) FunctionTerm(at.ac.tuwien.kr.alpha.api.terms.FunctionTerm)

Example 18 with Instance

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;
}
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 19 with Instance

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);
    }
}
Also used : Instance(at.ac.tuwien.kr.alpha.commons.substitutions.Instance) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate)

Example 20 with Instance

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);
}
Also used : Instance(at.ac.tuwien.kr.alpha.commons.substitutions.Instance) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom)

Aggregations

Instance (at.ac.tuwien.kr.alpha.commons.substitutions.Instance)25 Atom (at.ac.tuwien.kr.alpha.api.programs.atoms.Atom)10 Substitution (at.ac.tuwien.kr.alpha.api.grounder.Substitution)9 Literal (at.ac.tuwien.kr.alpha.api.programs.literals.Literal)9 BasicSubstitution (at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution)9 Predicate (at.ac.tuwien.kr.alpha.api.programs.Predicate)7 CompiledRule (at.ac.tuwien.kr.alpha.core.rules.CompiledRule)7 ArrayList (java.util.ArrayList)7 Map (java.util.Map)7 RuleAtom (at.ac.tuwien.kr.alpha.core.atoms.RuleAtom)5 Test (org.junit.jupiter.api.Test)5 Term (at.ac.tuwien.kr.alpha.api.terms.Term)4 AnalyzedProgram (at.ac.tuwien.kr.alpha.core.programs.AnalyzedProgram)4 CompiledProgram (at.ac.tuwien.kr.alpha.core.programs.CompiledProgram)4 InternalProgram (at.ac.tuwien.kr.alpha.core.programs.InternalProgram)4 Collections (java.util.Collections)4 List (java.util.List)4 GrounderHeuristicsConfiguration (at.ac.tuwien.kr.alpha.api.config.GrounderHeuristicsConfiguration)3 SystemConfig (at.ac.tuwien.kr.alpha.api.config.SystemConfig)3 ASPCore2Program (at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program)3