Search in sources :

Example 26 with VariableTerm

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

the class IntervalLiteral method getIntervalSubstitutions.

private List<Substitution> getIntervalSubstitutions(Substitution partialSubstitution) {
    List<Substitution> substitutions = new ArrayList<>();
    List<Term> terms = getTerms();
    Term intervalRepresentingVariable = terms.get(1);
    IntervalTerm intervalTerm = (IntervalTerm) terms.get(0);
    // Check whether intervalRepresentingVariable is bound already.
    if (intervalRepresentingVariable instanceof VariableTerm) {
        // Still a variable, generate all elements in the interval.
        for (int i = intervalTerm.getLowerBound(); i <= intervalTerm.getUpperBound(); i++) {
            Substitution ith = new BasicSubstitution(partialSubstitution);
            ith.put((VariableTerm) intervalRepresentingVariable, Terms.newConstant(i));
            substitutions.add(ith);
        }
        return substitutions;
    } else {
        // The intervalRepresentingVariable is bound already, check if it is in the interval.
        if (!(intervalRepresentingVariable instanceof ConstantTerm) || !(((ConstantTerm<?>) intervalRepresentingVariable).getObject() instanceof Integer)) {
            // Term is not bound to an integer constant, not in the interval.
            return Collections.emptyList();
        }
        // TODO to avoid that type of unchecked cast, we may want interval terms to not extend AbstractTerm
        @SuppressWarnings("unchecked") Integer integer = ((ConstantTerm<Integer>) intervalRepresentingVariable).getObject();
        if (intervalTerm.getLowerBound() <= integer && integer <= intervalTerm.getUpperBound()) {
            return Collections.singletonList(partialSubstitution);
        }
        return Collections.emptyList();
    }
}
Also used : Substitution(at.ac.tuwien.kr.alpha.api.grounder.Substitution) BasicSubstitution(at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution) BasicSubstitution(at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution) ArrayList(java.util.ArrayList) IntervalTerm(at.ac.tuwien.kr.alpha.commons.terms.IntervalTerm) ConstantTerm(at.ac.tuwien.kr.alpha.api.terms.ConstantTerm) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) Term(at.ac.tuwien.kr.alpha.api.terms.Term) IntervalTerm(at.ac.tuwien.kr.alpha.commons.terms.IntervalTerm) ConstantTerm(at.ac.tuwien.kr.alpha.api.terms.ConstantTerm) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm)

Example 27 with VariableTerm

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

the class TermTest method testTermVariableOccurrences.

@Test
public void testTermVariableOccurrences() {
    ConstantTerm<?> ta = Terms.newConstant("a");
    VariableTerm tx = VariableTermImpl.getInstance("X");
    FunctionTermImpl tf = FunctionTermImpl.getInstance("f", ta, tx);
    Set<VariableTerm> occurringVariables = tf.getOccurringVariables();
    assertEquals(tx, new ArrayList<>(occurringVariables).get(0), "Variable occurring as subterm must be reported as occurring variable.");
}
Also used : ArrayList(java.util.ArrayList) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) Test(org.junit.jupiter.api.Test)

Example 28 with VariableTerm

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

the class Terms method renameVariables.

/**
 * Renames variables in given set of terms.
 * @param varNamePrefix
 * @return
 */
public static Substitution renameVariables(Set<VariableTerm> terms, String varNamePrefix) {
    Unifier renamingSubstitution = new Unifier();
    int counter = 0;
    for (VariableTerm variable : terms) {
        renamingSubstitution.put(variable, Terms.newVariable(varNamePrefix + counter++));
    }
    return renamingSubstitution;
}
Also used : VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) Unifier(at.ac.tuwien.kr.alpha.commons.substitutions.Unifier)

Example 29 with VariableTerm

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

use of at.ac.tuwien.kr.alpha.api.terms.VariableTerm 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)

Aggregations

VariableTerm (at.ac.tuwien.kr.alpha.api.terms.VariableTerm)45 Term (at.ac.tuwien.kr.alpha.api.terms.Term)25 Literal (at.ac.tuwien.kr.alpha.api.programs.literals.Literal)18 Test (org.junit.jupiter.api.Test)13 ArrayList (java.util.ArrayList)12 HashSet (java.util.HashSet)10 BasicSubstitution (at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution)9 AggregateLiteral (at.ac.tuwien.kr.alpha.api.programs.literals.AggregateLiteral)8 Substitution (at.ac.tuwien.kr.alpha.api.grounder.Substitution)6 ArithmeticTerm (at.ac.tuwien.kr.alpha.api.terms.ArithmeticTerm)6 ConstantTerm (at.ac.tuwien.kr.alpha.api.terms.ConstantTerm)6 FunctionTerm (at.ac.tuwien.kr.alpha.api.terms.FunctionTerm)6 Unifier (at.ac.tuwien.kr.alpha.commons.substitutions.Unifier)6 IntervalTerm (at.ac.tuwien.kr.alpha.commons.terms.IntervalTerm)6 Map (java.util.Map)6 Atom (at.ac.tuwien.kr.alpha.api.programs.atoms.Atom)5 NormalHead (at.ac.tuwien.kr.alpha.api.rules.heads.NormalHead)5 Predicate (at.ac.tuwien.kr.alpha.api.programs.Predicate)4 AggregateAtom (at.ac.tuwien.kr.alpha.api.programs.atoms.AggregateAtom)4 BasicAtom (at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom)4