Search in sources :

Example 1 with Term

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

the class ParserTest method cardinalityAggregate.

@Test
public void cardinalityAggregate() {
    ASPCore2Program parsedProgram = parser.parse("num(K) :-  K <= #count {X,Y,Z : p(X,Y,Z) }, dom(K).");
    Optional<Literal> optionalBodyElement = parsedProgram.getRules().get(0).getBody().stream().filter((lit) -> lit instanceof AggregateLiteral).findFirst();
    assertTrue(optionalBodyElement.isPresent());
    Literal bodyElement = optionalBodyElement.get();
    AggregateLiteral parsedAggregate = (AggregateLiteral) bodyElement;
    VariableTerm x = Terms.newVariable("X");
    VariableTerm y = Terms.newVariable("Y");
    VariableTerm z = Terms.newVariable("Z");
    List<Term> basicTerms = Arrays.asList(x, y, z);
    AggregateAtom.AggregateElement aggregateElement = Atoms.newAggregateElement(basicTerms, Collections.singletonList(Atoms.newBasicAtom(Predicates.getPredicate("p", 3), x, y, z).toLiteral()));
    AggregateAtom expectedAggregate = Atoms.newAggregateAtom(ComparisonOperators.LE, Terms.newVariable("K"), null, null, AggregateAtom.AggregateFunctionSymbol.COUNT, Collections.singletonList(aggregateElement));
    assertEquals(expectedAggregate, parsedAggregate.getAtom());
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Arrays(java.util.Arrays) AggregateAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.AggregateAtom) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) IntervalTerm(at.ac.tuwien.kr.alpha.commons.terms.IntervalTerm) InlineDirectives(at.ac.tuwien.kr.alpha.api.programs.InlineDirectives) Terms(at.ac.tuwien.kr.alpha.commons.terms.Terms) CharStreams(org.antlr.v4.runtime.CharStreams) CharStream(org.antlr.v4.runtime.CharStream) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) AggregateLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.AggregateLiteral) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Term(at.ac.tuwien.kr.alpha.api.terms.Term) ReadableByteChannel(java.nio.channels.ReadableByteChannel) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) ChoiceHead(at.ac.tuwien.kr.alpha.api.rules.heads.ChoiceHead) IOException(java.io.IOException) Atoms(at.ac.tuwien.kr.alpha.commons.atoms.Atoms) Test(org.junit.jupiter.api.Test) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) List(java.util.List) Predicates(at.ac.tuwien.kr.alpha.commons.Predicates) Stream(java.util.stream.Stream) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) Util(at.ac.tuwien.kr.alpha.commons.util.Util) Optional(java.util.Optional) Collections(java.util.Collections) FunctionTerm(at.ac.tuwien.kr.alpha.api.terms.FunctionTerm) ComparisonOperators(at.ac.tuwien.kr.alpha.commons.comparisons.ComparisonOperators) ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) AggregateLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.AggregateLiteral) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) AggregateLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.AggregateLiteral) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) IntervalTerm(at.ac.tuwien.kr.alpha.commons.terms.IntervalTerm) Term(at.ac.tuwien.kr.alpha.api.terms.Term) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) FunctionTerm(at.ac.tuwien.kr.alpha.api.terms.FunctionTerm) AggregateAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.AggregateAtom) Test(org.junit.jupiter.api.Test)

Example 2 with Term

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

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

the class AggregateOperatorNormalization method createPlusOneTerm.

/**
 * Creates a new {@link Literal} that assigns the given target variable to the given (integer) term plus one.
 *
 * @param term
 * @param targetVariable
 * @return
 */
private static Literal createPlusOneTerm(Term term, VariableTerm targetVariable) {
    Term increment = Terms.newArithmeticTerm(term, ArithmeticOperator.PLUS, Terms.newConstant(1));
    ComparisonAtom atom = Atoms.newComparisonAtom(targetVariable, increment, ComparisonOperators.EQ);
    return atom.toLiteral();
}
Also used : ComparisonAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.ComparisonAtom) Term(at.ac.tuwien.kr.alpha.api.terms.Term) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm)

Example 4 with Term

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

the class IntervalTermToIntervalAtom method rewriteLiteral.

/**
 * Replaces every IntervalTerm by a new variable and returns a mapping of the replaced VariableTerm -> IntervalTerm.
 *
 * @return the rewritten literal or null if the literal should be dropped from the final rule.
 */
private static Literal rewriteLiteral(Literal lit, Map<VariableTerm, IntervalTerm> intervalReplacement) {
    // final rule.
    if (lit instanceof ComparisonLiteral && ((ComparisonLiteral) lit).isNormalizedEquality()) {
        ComparisonAtom equalityLiteral = (ComparisonAtom) lit.getAtom();
        if (equalityLiteral.getTerms().get(0) instanceof VariableTerm && equalityLiteral.getTerms().get(1) instanceof IntervalTerm) {
            // Literal is of the form "X = A .. B".
            intervalReplacement.put((VariableTerm) equalityLiteral.getTerms().get(0), (IntervalTerm) equalityLiteral.getTerms().get(1));
            return null;
        }
        if (equalityLiteral.getTerms().get(1) instanceof VariableTerm && equalityLiteral.getTerms().get(0) instanceof IntervalTerm) {
            // Literal is of the form "A .. B = X".
            intervalReplacement.put((VariableTerm) equalityLiteral.getTerms().get(1), (IntervalTerm) equalityLiteral.getTerms().get(0));
            return null;
        }
    }
    Atom atom = lit.getAtom();
    List<Term> termList = new ArrayList<>(atom.getTerms());
    boolean didChange = false;
    for (int i = 0; i < termList.size(); i++) {
        Term term = termList.get(i);
        if (term instanceof IntervalTerm) {
            VariableTerm replacementVariable = Terms.newVariable(INTERVAL_VARIABLE_PREFIX + intervalReplacement.size());
            intervalReplacement.put(replacementVariable, (IntervalTerm) term);
            termList.set(i, replacementVariable);
            didChange = true;
        }
        if (term instanceof FunctionTerm) {
            // Rewrite function terms recursively.
            FunctionTerm rewrittenFunctionTerm = rewriteFunctionTerm((FunctionTerm) term, intervalReplacement);
            termList.set(i, rewrittenFunctionTerm);
            didChange = true;
        }
    }
    if (didChange) {
        Atom rewrittenAtom = atom.withTerms(termList);
        return lit.isNegated() ? rewrittenAtom.toLiteral().negate() : rewrittenAtom.toLiteral();
    }
    return lit;
}
Also used : FunctionTerm(at.ac.tuwien.kr.alpha.api.terms.FunctionTerm) ComparisonAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.ComparisonAtom) IntervalTerm(at.ac.tuwien.kr.alpha.commons.terms.IntervalTerm) ArrayList(java.util.ArrayList) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) Term(at.ac.tuwien.kr.alpha.api.terms.Term) IntervalTerm(at.ac.tuwien.kr.alpha.commons.terms.IntervalTerm) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) FunctionTerm(at.ac.tuwien.kr.alpha.api.terms.FunctionTerm) ComparisonLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.ComparisonLiteral) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) IntervalAtom(at.ac.tuwien.kr.alpha.core.atoms.IntervalAtom) ComparisonAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.ComparisonAtom)

Example 5 with Term

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

the class AggregateRewritingRuleAnalysis method analyzeRuleDependencies.

private void analyzeRuleDependencies() {
    for (AggregateLiteral lit : globalVariablesPerAggregate.keySet()) {
        Set<VariableTerm> nonBindingVars = new HashSet<>(globalVariablesPerAggregate.get(lit));
        Term leftHandTerm = lit.getAtom().getLowerBoundTerm();
        if (lit.getBindingVariables().isEmpty() && leftHandTerm instanceof VariableTerm) {
            /*
				 * If the "left-hand" term LT of the literal is a variable and not binding, it has to be non-binding,
				 * i.e. the aggregate literal depends on the literals binding LT.
				 */
            nonBindingVars.add((VariableTerm) leftHandTerm);
        }
        Set<Literal> dependencies = new HashSet<>();
        Set<Literal> bodyWithoutLit = SetUtils.difference(rule.getBody(), Collections.singleton(lit));
        findBindingLiterals(nonBindingVars, new HashSet<>(), dependencies, bodyWithoutLit, globalVariablesPerAggregate);
        dependenciesPerAggregate.put(lit, dependencies);
    }
}
Also used : AggregateLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.AggregateLiteral) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) AggregateLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.AggregateLiteral) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) Term(at.ac.tuwien.kr.alpha.api.terms.Term) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) HashSet(java.util.HashSet)

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