use of at.ac.tuwien.kr.alpha.api.terms.Term in project Alpha by alpha-asp.
the class ThreeColouringWheelTest method createColors.
private List<Atom> createColors(String... colours) {
List<Atom> facts = new ArrayList<>(colours.length);
Predicate predicate = Predicates.getPredicate("c", 1);
for (String colour : colours) {
List<Term> terms = new ArrayList<>(1);
terms.add(Terms.newConstant(colour));
facts.add(Atoms.newBasicAtom(predicate, terms));
}
return facts;
}
use of at.ac.tuwien.kr.alpha.api.terms.Term in project Alpha by alpha-asp.
the class ComparisonLiteralImpl method isLeftOrRightAssigning.
@Override
public boolean isLeftOrRightAssigning() {
final Term left = getTerms().get(0);
final Term right = getTerms().get(1);
return isNormalizedEquality && (assignable(left) && right.isGround() || assignable(right) && left.isGround());
}
use of at.ac.tuwien.kr.alpha.api.terms.Term in project Alpha by alpha-asp.
the class ComparisonLiteralImpl method getSatisfyingSubstitutions.
@Override
public List<Substitution> getSatisfyingSubstitutions(Substitution partialSubstitution) {
// Treat case where this is just comparison with all variables bound by partialSubstitution.
final Term left = getAtom().getTerms().get(0).substitute(partialSubstitution);
final Term right = getAtom().getTerms().get(1).substitute(partialSubstitution);
final boolean leftAssigning = assignable(left);
final boolean rightAssigning = assignable(right);
if (!leftAssigning && !rightAssigning) {
// No assignment (variables are bound by partialSubstitution), thus evaluate comparison only.
Term leftEvaluatedSubstitute = evaluateTerm(left);
if (leftEvaluatedSubstitute == null) {
return Collections.emptyList();
}
Term rightEvaluatedSubstitute = evaluateTerm(right);
if (rightEvaluatedSubstitute == null) {
return Collections.emptyList();
}
if (compare(leftEvaluatedSubstitute, rightEvaluatedSubstitute)) {
return Collections.singletonList(partialSubstitution);
} else {
return Collections.emptyList();
}
}
// Treat case that this is X = t or t = X.
VariableTerm variable = null;
Term expression = null;
if (leftAssigning) {
variable = (VariableTerm) left;
expression = right;
}
if (rightAssigning) {
variable = (VariableTerm) right;
expression = left;
}
Term groundTerm = expression.substitute(partialSubstitution);
Term resultTerm = null;
// Check if the groundTerm is an arithmetic expression and evaluate it if so.
if (groundTerm instanceof ArithmeticTerm) {
Integer result = Terms.evaluateGroundTerm(groundTerm);
if (result == null) {
return Collections.emptyList();
}
resultTerm = Terms.newConstant(result);
} else {
// Ground term is another term (constant, or function term).
resultTerm = groundTerm;
}
BasicSubstitution extendedSubstitution = new BasicSubstitution(partialSubstitution);
extendedSubstitution.put(variable, resultTerm);
return Collections.singletonList(extendedSubstitution);
}
use of at.ac.tuwien.kr.alpha.api.terms.Term in project Alpha by alpha-asp.
the class Terms method renameTerms.
public static List<Term> renameTerms(List<Term> terms, String prefix, int counterStartingValue) {
List<Term> renamedTerms = new ArrayList<>(terms.size());
AbstractTerm.RenameCounterImpl renameCounter = new AbstractTerm.RenameCounterImpl(counterStartingValue);
for (Term term : terms) {
renamedTerms.add(term.normalizeVariables(prefix, renameCounter));
}
return renamedTerms;
}
use of at.ac.tuwien.kr.alpha.api.terms.Term in project Alpha by alpha-asp.
the class AnswerSetQueryImpl method test.
/**
* Applies this query to an atom. Filters are worked off in
* order of ascending term index in a conjunctive fashion, i.e. for an atom
* to match the query, all of its terms must satisfy all filters on these
* terms
*
* @param atom the atom to which to apply the query
* @return true iff the atom satisfies the query
*/
@Override
public boolean test(Atom atom) {
if (!atom.getPredicate().equals(predicate)) {
return false;
}
for (int i = 0; i < atom.getTerms().size(); i++) {
Term ithTerm = atom.getTerms().get(i);
java.util.function.Predicate<Term> ithFilter = filters.get(i);
if (ithFilter != null && !ithFilter.test(ithTerm)) {
return false;
}
}
return true;
}
Aggregations