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();
}
}
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.");
}
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;
}
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;
}
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;
}
Aggregations