Search in sources :

Example 1 with ConstantTerm

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

the class ArithmeticTermsRewriting method rewriteArithmeticSubterms.

private Term rewriteArithmeticSubterms(Term term, List<Literal> bodyLiterals) {
    // Keep term as-is if it contains no ArithmeticTerm.
    if (!containsArithmeticTerm(term)) {
        return term;
    }
    // Switch on term type.
    if (term instanceof ArithmeticTerm) {
        VariableTerm replacementVariable = Terms.newVariable(ARITHMETIC_VARIABLES_PREFIX + numArithmeticVariables++);
        bodyLiterals.add(Atoms.newComparisonAtom(replacementVariable, term, ComparisonOperators.EQ).toLiteral());
        return replacementVariable;
    } else if (term instanceof VariableTerm || term instanceof ConstantTerm) {
        return term;
    } else if (term instanceof FunctionTerm) {
        List<Term> termList = ((FunctionTerm) term).getTerms();
        List<Term> rewrittenTermList = new ArrayList<>();
        for (Term subterm : termList) {
            rewrittenTermList.add(rewriteArithmeticSubterms(subterm, bodyLiterals));
        }
        return Terms.newFunctionTerm(((FunctionTerm) term).getSymbol(), rewrittenTermList);
    } else {
        throw Util.oops("Rewriting unknown Term type: " + term.getClass());
    }
}
Also used : FunctionTerm(at.ac.tuwien.kr.alpha.api.terms.FunctionTerm) ArithmeticTerm(at.ac.tuwien.kr.alpha.api.terms.ArithmeticTerm) ConstantTerm(at.ac.tuwien.kr.alpha.api.terms.ConstantTerm) 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) IntervalTerm(at.ac.tuwien.kr.alpha.commons.terms.IntervalTerm) ConstantTerm(at.ac.tuwien.kr.alpha.api.terms.ConstantTerm) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) FunctionTerm(at.ac.tuwien.kr.alpha.api.terms.FunctionTerm)

Example 2 with ConstantTerm

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

the class AlphaImplTest method programWithExternalStringStuff.

@Test
@SuppressWarnings("unchecked")
public void programWithExternalStringStuff() throws IOException {
    Alpha alpha = new AlphaImpl();
    ASPCore2Program prog = alpha.readProgram(InputConfig.forString(STRINGSTUFF_ASP));
    Set<AnswerSet> answerSets = alpha.solve(prog).collect(Collectors.toSet());
    // Verify every result string has length 6 and contains "foo"
    for (AnswerSet as : answerSets) {
        for (Atom atom : as.getPredicateInstances(Predicates.getPredicate("resultstring", 1))) {
            String resultstring = ((ConstantTerm<String>) atom.getTerms().get(0)).getObject();
            assertEquals(6, resultstring.length());
            assertTrue(resultstring.contains("foo"));
        }
    }
}
Also used : ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) AnswerSet(at.ac.tuwien.kr.alpha.api.AnswerSet) Alpha(at.ac.tuwien.kr.alpha.api.Alpha) 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 3 with ConstantTerm

use of at.ac.tuwien.kr.alpha.api.terms.ConstantTerm 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 4 with ConstantTerm

use of at.ac.tuwien.kr.alpha.api.terms.ConstantTerm 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 5 with ConstantTerm

use of at.ac.tuwien.kr.alpha.api.terms.ConstantTerm 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)

Aggregations

ConstantTerm (at.ac.tuwien.kr.alpha.api.terms.ConstantTerm)8 Term (at.ac.tuwien.kr.alpha.api.terms.Term)5 VariableTerm (at.ac.tuwien.kr.alpha.api.terms.VariableTerm)4 ArrayList (java.util.ArrayList)4 AnswerSet (at.ac.tuwien.kr.alpha.api.AnswerSet)3 Atom (at.ac.tuwien.kr.alpha.api.programs.atoms.Atom)3 Test (org.junit.jupiter.api.Test)3 Alpha (at.ac.tuwien.kr.alpha.api.Alpha)2 Substitution (at.ac.tuwien.kr.alpha.api.grounder.Substitution)2 ASPCore2Program (at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program)2 BasicSubstitution (at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution)2 IntervalTerm (at.ac.tuwien.kr.alpha.commons.terms.IntervalTerm)2 List (java.util.List)2 Predicate (at.ac.tuwien.kr.alpha.api.externals.Predicate)1 Predicate (at.ac.tuwien.kr.alpha.api.programs.Predicate)1 ArithmeticTerm (at.ac.tuwien.kr.alpha.api.terms.ArithmeticTerm)1 FunctionTerm (at.ac.tuwien.kr.alpha.api.terms.FunctionTerm)1 AnswerSetBuilder (at.ac.tuwien.kr.alpha.commons.AnswerSetBuilder)1 AnswerSets (at.ac.tuwien.kr.alpha.commons.AnswerSets)1 Predicates (at.ac.tuwien.kr.alpha.commons.Predicates)1