Search in sources :

Example 31 with Term

use of at.ac.tuwien.kr.alpha.api.terms.Term 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 32 with Term

use of at.ac.tuwien.kr.alpha.api.terms.Term 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 33 with Term

use of at.ac.tuwien.kr.alpha.api.terms.Term in project Alpha by alpha-asp.

the class ExternalLiteralImpl method getBindingVariables.

@Override
public Set<VariableTerm> getBindingVariables() {
    if (this.isNegated()) {
        return Collections.emptySet();
    }
    List<Term> output = getAtom().getOutput();
    Set<VariableTerm> binding = new HashSet<>(output.size());
    for (Term out : output) {
        if (out instanceof VariableTerm) {
            binding.add((VariableTerm) out);
        }
    }
    return binding;
}
Also used : VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) Term(at.ac.tuwien.kr.alpha.api.terms.Term) ConstantTerm(at.ac.tuwien.kr.alpha.api.terms.ConstantTerm) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) HashSet(java.util.HashSet)

Example 34 with Term

use of at.ac.tuwien.kr.alpha.api.terms.Term in project Alpha by alpha-asp.

the class Unifier method put.

@Override
public <T extends Comparable<T>> Term put(VariableTerm variableTerm, Term term) {
    // If term is not ground, store it for right-hand side reverse-lookup.
    if (!term.isGround()) {
        for (VariableTerm rightHandVariable : term.getOccurringVariables()) {
            rightHandVariableOccurrences.putIfAbsent(rightHandVariable, new ArrayList<>());
            rightHandVariableOccurrences.get(rightHandVariable).add(variableTerm);
        }
    }
    // Note: We're destroying type information here.
    Term ret = substitution.put(variableTerm, term);
    // Check if the just-assigned variable occurs somewhere in the right-hand side already.
    List<VariableTerm> rightHandOccurrences = rightHandVariableOccurrences.get(variableTerm);
    if (rightHandOccurrences != null) {
        // Replace all occurrences on the right-hand side with the just-assigned term.
        for (VariableTerm rightHandOccurrence : rightHandOccurrences) {
            // Substitute the right hand where this assigned variable occurs with the new value and store it.
            Term previousRightHand = substitution.get(rightHandOccurrence);
            if (previousRightHand == null) {
                // Variable does not occur on the lef-hand side, skip.
                continue;
            }
            substitution.put(rightHandOccurrence, previousRightHand.substitute(this));
        }
    }
    return ret;
}
Also used : VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) Term(at.ac.tuwien.kr.alpha.api.terms.Term)

Example 35 with Term

use of at.ac.tuwien.kr.alpha.api.terms.Term in project Alpha by alpha-asp.

the class TermTest method testTermOrdering.

@Test
public void testTermOrdering() {
    Term cts = Terms.newConstant("abc");
    Term cti = Terms.newConstant(2);
    Term cto = Terms.newConstant(new UUID(0, 0));
    Term ft = FunctionTermImpl.getInstance("f", Terms.newConstant("a"));
    assertTrue(cts.compareTo(cti) > 0);
    assertTrue(cti.compareTo(cts) < 0);
    assertTrue(cts.compareTo(cto) < 0);
    assertTrue(cto.compareTo(cts) > 0);
    assertTrue(cts.compareTo(ft) < 0);
    assertTrue(ft.compareTo(cts) > 0);
    assertTrue(cto.compareTo(ft) < 0);
    assertTrue(ft.compareTo(cto) > 0);
}
Also used : VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) Term(at.ac.tuwien.kr.alpha.api.terms.Term) ConstantTerm(at.ac.tuwien.kr.alpha.api.terms.ConstantTerm) UUID(java.util.UUID) Test(org.junit.jupiter.api.Test)

Aggregations

Term (at.ac.tuwien.kr.alpha.api.terms.Term)52 VariableTerm (at.ac.tuwien.kr.alpha.api.terms.VariableTerm)36 ArrayList (java.util.ArrayList)16 ConstantTerm (at.ac.tuwien.kr.alpha.api.terms.ConstantTerm)15 FunctionTerm (at.ac.tuwien.kr.alpha.api.terms.FunctionTerm)15 IntervalTerm (at.ac.tuwien.kr.alpha.commons.terms.IntervalTerm)12 ArithmeticTerm (at.ac.tuwien.kr.alpha.api.terms.ArithmeticTerm)10 Predicate (at.ac.tuwien.kr.alpha.api.programs.Predicate)9 Test (org.junit.jupiter.api.Test)8 Literal (at.ac.tuwien.kr.alpha.api.programs.literals.Literal)7 Atom (at.ac.tuwien.kr.alpha.api.programs.atoms.Atom)6 BasicSubstitution (at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution)6 HashSet (java.util.HashSet)6 AggregateLiteral (at.ac.tuwien.kr.alpha.api.programs.literals.AggregateLiteral)4 Instance (at.ac.tuwien.kr.alpha.commons.substitutions.Instance)4 Substitution (at.ac.tuwien.kr.alpha.api.grounder.Substitution)3 BasicAtom (at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom)3 ComparisonLiteral (at.ac.tuwien.kr.alpha.api.programs.literals.ComparisonLiteral)3 Unifier (at.ac.tuwien.kr.alpha.commons.substitutions.Unifier)3 MinusTerm (at.ac.tuwien.kr.alpha.commons.terms.ArithmeticTermImpl.MinusTerm)3