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