Search in sources :

Example 21 with Term

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

the class AnswerSetQueryTest method matchEvenIntegers.

@Test
public void matchEvenIntegers() {
    AnswerSetBuilder bld = new AnswerSetBuilder();
    bld.predicate("p").instance(1).instance(2).instance(3).instance(4).instance(5).instance("bla").symbolicInstance("blubb");
    AnswerSet as = bld.build();
    java.util.function.Predicate<Term> isInteger = (term) -> {
        if (!(term instanceof ConstantTerm<?>)) {
            return false;
        }
        String strValue = ((ConstantTerm<?>) term).getObject().toString();
        return strValue.matches("[0-9]+");
    };
    AnswerSetQueryImpl evenIntegers = AnswerSetQueryImpl.forPredicate(Predicates.getPredicate("p", 1)).withFilter(0, isInteger.and((term) -> Integer.valueOf(((ConstantTerm<?>) term).getObject().toString()) % 2 == 0));
    List<Atom> queryResult = as.query(evenIntegers);
    assertEquals(2, queryResult.size());
    for (Atom atom : queryResult) {
        ConstantTerm<?> term = (ConstantTerm<?>) atom.getTerms().get(0);
        assertTrue(Integer.valueOf(term.getObject().toString()) % 2 == 0);
    }
}
Also used : Term(at.ac.tuwien.kr.alpha.api.terms.Term) SortedSet(java.util.SortedSet) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) AnswerSets(at.ac.tuwien.kr.alpha.commons.AnswerSets) HashMap(java.util.HashMap) AnswerSet(at.ac.tuwien.kr.alpha.api.AnswerSet) Atoms(at.ac.tuwien.kr.alpha.commons.atoms.Atoms) TreeSet(java.util.TreeSet) AnswerSetBuilder(at.ac.tuwien.kr.alpha.commons.AnswerSetBuilder) ConstantTerm(at.ac.tuwien.kr.alpha.api.terms.ConstantTerm) Test(org.junit.jupiter.api.Test) Terms(at.ac.tuwien.kr.alpha.commons.terms.Terms) List(java.util.List) Predicates(at.ac.tuwien.kr.alpha.commons.Predicates) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Map(java.util.Map) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate) AnswerSet(at.ac.tuwien.kr.alpha.api.AnswerSet) ConstantTerm(at.ac.tuwien.kr.alpha.api.terms.ConstantTerm) AnswerSetBuilder(at.ac.tuwien.kr.alpha.commons.AnswerSetBuilder) Term(at.ac.tuwien.kr.alpha.api.terms.Term) ConstantTerm(at.ac.tuwien.kr.alpha.api.terms.ConstantTerm) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) Test(org.junit.jupiter.api.Test)

Example 22 with Term

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

the class ExternalLiteralImpl method getNonBindingVariables.

@Override
public Set<VariableTerm> getNonBindingVariables() {
    List<Term> input = getAtom().getInput();
    List<Term> output = getAtom().getOutput();
    // External atoms have their input always non-binding, since they cannot
    // be queried without some concrete input.
    Set<VariableTerm> nonbindingVariables = new HashSet<>();
    for (Term term : input) {
        nonbindingVariables.addAll(term.getOccurringVariables());
    }
    // non-binding.
    if (this.isNegated()) {
        for (Term out : output) {
            if (out instanceof VariableTerm) {
                nonbindingVariables.add((VariableTerm) out);
            }
        }
    }
    return nonbindingVariables;
}
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 23 with Term

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

the class ExternalLiteralImpl method getSatisfyingSubstitutions.

@Override
public List<Substitution> getSatisfyingSubstitutions(Substitution partialSubstitution) {
    List<Term> input = getAtom().getInput();
    List<Term> substitutes = new ArrayList<>(input.size());
    // to the partial substitution supplied by the grounder.
    for (Term t : input) {
        substitutes.add(t.substitute(partialSubstitution));
    }
    Set<List<ConstantTerm<?>>> results = getAtom().getInterpretation().evaluate(substitutes);
    if (results == null) {
        throw new NullPointerException("Predicate " + getPredicate().getName() + " returned null. It must return a Set.");
    }
    if (this.isNegated()) {
        return this.isNegatedLiteralSatisfied(results) ? Collections.singletonList(partialSubstitution) : Collections.emptyList();
    } else {
        return this.buildSubstitutionsForOutputs(partialSubstitution, results);
    }
}
Also used : ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) 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)

Example 24 with Term

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

the class ExternalLiteralImpl method buildSubstitutionsForOutputs.

private List<Substitution> buildSubstitutionsForOutputs(Substitution partialSubstitution, Set<List<ConstantTerm<?>>> outputs) {
    List<Substitution> retVal = new ArrayList<>();
    List<Term> externalAtomOutputTerms = this.getAtom().getOutput();
    for (List<ConstantTerm<?>> bindings : outputs) {
        if (bindings.size() < externalAtomOutputTerms.size()) {
            throw new RuntimeException("Predicate " + getPredicate().getName() + " returned " + bindings.size() + " terms when at least " + externalAtomOutputTerms.size() + " were expected.");
        }
        BasicSubstitution ith = new BasicSubstitution(partialSubstitution);
        boolean skip = false;
        for (int i = 0; i < externalAtomOutputTerms.size(); i++) {
            Term out = externalAtomOutputTerms.get(i);
            if (out instanceof VariableTerm) {
                ith.put((VariableTerm) out, bindings.get(i));
            } else {
                if (!bindings.get(i).equals(out)) {
                    skip = true;
                    break;
                }
            }
        }
        if (!skip) {
            retVal.add(ith);
        }
    }
    return retVal;
}
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) 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) ConstantTerm(at.ac.tuwien.kr.alpha.api.terms.ConstantTerm) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm)

Example 25 with Term

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

the class Unifier method mergeIntoLeft.

/**
 * Merge substitution right into left as used in the AnalyzeUnjustified.
 * Left mappings are seen as equalities, i.e.,
 * if left has {@literal A -> B} and right has {@literal A -> t} then the result will have {@literal A -> t} and {@literal B -> t}.
 * If both substitutions are inconsistent, i.e., {@literal A -> t1} in left and {@literal A -> t2} in right, then null is returned.
 * @param left
 * @param right
 * @return
 */
public static Unifier mergeIntoLeft(Unifier left, Unifier right) {
    // Note: we assume both substitutions are free of chains, i.e., no A->B, B->C but A->C, B->C.
    Unifier ret = new Unifier(left);
    for (Map.Entry<VariableTerm, Term> mapping : right.substitution.entrySet()) {
        VariableTerm variable = mapping.getKey();
        Term term = mapping.getValue();
        // If variable is unset, simply add.
        if (!ret.isVariableSet(variable)) {
            ret.put(variable, term);
            continue;
        }
        // Variable is already set.
        Term setTerm = ret.eval(variable);
        if (setTerm instanceof VariableTerm) {
            // Variable maps to another variable in left.
            // Add a new mapping of the setTerm variable into our right-assigned term.
            ret.put((VariableTerm) setTerm, term);
            // Note: Unifier.put takes care of resolving the chain variable->setTerm->term.
            continue;
        }
        // Check for inconsistency.
        if (setTerm != term) {
            return null;
        }
    // Now setTerm equals term, no action needed.
    }
    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) TreeMap(java.util.TreeMap) Map(java.util.Map)

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