Search in sources :

Example 16 with Term

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;
}
Also used : ArrayList(java.util.ArrayList) Term(at.ac.tuwien.kr.alpha.api.terms.Term) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate)

Example 17 with Term

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());
}
Also used : Term(at.ac.tuwien.kr.alpha.api.terms.Term) ArithmeticTerm(at.ac.tuwien.kr.alpha.api.terms.ArithmeticTerm) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm)

Example 18 with Term

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);
}
Also used : BasicSubstitution(at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution) ArithmeticTerm(at.ac.tuwien.kr.alpha.api.terms.ArithmeticTerm) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) Term(at.ac.tuwien.kr.alpha.api.terms.Term) ArithmeticTerm(at.ac.tuwien.kr.alpha.api.terms.ArithmeticTerm) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm)

Example 19 with Term

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;
}
Also used : ArrayList(java.util.ArrayList) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) Term(at.ac.tuwien.kr.alpha.api.terms.Term) ArithmeticTerm(at.ac.tuwien.kr.alpha.api.terms.ArithmeticTerm) MinusTerm(at.ac.tuwien.kr.alpha.commons.terms.ArithmeticTermImpl.MinusTerm) FunctionTerm(at.ac.tuwien.kr.alpha.api.terms.FunctionTerm) ConstantTerm(at.ac.tuwien.kr.alpha.api.terms.ConstantTerm)

Example 20 with Term

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;
}
Also used : Term(at.ac.tuwien.kr.alpha.api.terms.Term) FunctionTerm(at.ac.tuwien.kr.alpha.api.terms.FunctionTerm) ConstantTerm(at.ac.tuwien.kr.alpha.api.terms.ConstantTerm)

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