Search in sources :

Example 1 with AggregateAtom

use of at.ac.tuwien.kr.alpha.api.programs.atoms.AggregateAtom 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 AggregateAtom

use of at.ac.tuwien.kr.alpha.api.programs.atoms.AggregateAtom in project Alpha by alpha-asp.

the class AggregateOperatorNormalization method rewriteAggregateOperator.

private static List<Literal> rewriteAggregateOperator(AggregateLiteral lit) {
    AggregateAtom atom = lit.getAtom();
    if (atom.getLowerBoundOperator() == null && atom.getUpperBoundOperator() != null) {
        return rewriteAggregateOperator(convertToLeftHandComparison(lit));
    }
    if (lit.getAtom().getAggregateFunction() == AggregateFunctionSymbol.MIN || lit.getAtom().getAggregateFunction() == AggregateFunctionSymbol.MAX) {
        // No operator normalization needed for #min/#max aggregates.
        return Collections.singletonList(lit);
    }
    if (atom.getLowerBoundOperator().equals(ComparisonOperators.EQ) || atom.getLowerBoundOperator().equals(ComparisonOperators.LE)) {
        // Nothing to do for operator "=" or "<=".
        return Collections.singletonList(lit);
    } else {
        List<Literal> retVal = new ArrayList<>();
        VariableTerm decrementedBound;
        ComparisonOperator lowerBoundOp = atom.getLowerBoundOperator();
        if (lowerBoundOp.equals(ComparisonOperators.LT)) {
            decrementedBound = Terms.newAnonymousVariable();
            retVal.add(createLowerBoundedAggregateLiteral(ComparisonOperators.LE, decrementedBound, atom, !lit.isNegated()));
            retVal.add(createPlusOneTerm(atom.getLowerBoundTerm(), decrementedBound));
        } else if (lowerBoundOp.equals(ComparisonOperators.NE)) {
            retVal.add(createLowerBoundedAggregateLiteral(ComparisonOperators.EQ, atom.getLowerBoundTerm(), atom, lit.isNegated()));
        } else if (lowerBoundOp.equals(ComparisonOperators.GT)) {
            retVal.add(createLowerBoundedAggregateLiteral(ComparisonOperators.LE, atom.getLowerBoundTerm(), atom, lit.isNegated()));
        } else if (lowerBoundOp.equals(ComparisonOperators.GE)) {
            decrementedBound = Terms.newAnonymousVariable();
            retVal.add(createLowerBoundedAggregateLiteral(ComparisonOperators.LE, decrementedBound, atom, lit.isNegated()));
            retVal.add(createPlusOneTerm(atom.getLowerBoundTerm(), decrementedBound));
        } else {
            throw new IllegalStateException("No operator rewriting logic available for literal: " + lit);
        }
        return retVal;
    }
}
Also used : ComparisonOperator(at.ac.tuwien.kr.alpha.api.ComparisonOperator) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) AggregateLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.AggregateLiteral) ArrayList(java.util.ArrayList) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) AggregateAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.AggregateAtom)

Example 3 with AggregateAtom

use of at.ac.tuwien.kr.alpha.api.programs.atoms.AggregateAtom in project Alpha by alpha-asp.

the class AggregateOperatorNormalization method convertToLeftHandComparison.

/**
 * Helper function to convert aggregate literals of form <code>#aggr{...} OP TERM</code> to literals of form
 * <code>TERM OP #aggr{...}</code>.
 *
 * @param lit an aggregate literal with only a right-hand term comparison
 * @return a semantically equivalent literal with only a left-hand comparison
 */
private static AggregateLiteral convertToLeftHandComparison(AggregateLiteral lit) {
    AggregateAtom atom = lit.getAtom();
    ComparisonOperator operator = atom.getUpperBoundOperator();
    ComparisonOperator flippedOperator;
    if (operator.equals(ComparisonOperators.EQ) || operator.equals(ComparisonOperators.NE)) {
        flippedOperator = operator;
    } else if (operator.equals(ComparisonOperators.LE)) {
        flippedOperator = ComparisonOperators.GE;
    } else if (operator.equals(ComparisonOperators.LT)) {
        flippedOperator = ComparisonOperators.GT;
    } else if (operator.equals(ComparisonOperators.GE)) {
        flippedOperator = ComparisonOperators.LE;
    } else if (operator.equals(ComparisonOperators.GT)) {
        flippedOperator = ComparisonOperators.LT;
    } else {
        throw new IllegalArgumentException("Unsupported comparison operator for aggregate atom: " + operator);
    }
    return Atoms.newAggregateAtom(flippedOperator, atom.getUpperBoundTerm(), atom.getAggregateFunction(), atom.getAggregateElements()).toLiteral(!lit.isNegated());
}
Also used : ComparisonOperator(at.ac.tuwien.kr.alpha.api.ComparisonOperator) AggregateAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.AggregateAtom)

Example 4 with AggregateAtom

use of at.ac.tuwien.kr.alpha.api.programs.atoms.AggregateAtom in project Alpha by alpha-asp.

the class AggregateLiteralSplitting method splitCombinedAggregateAtom.

private static ImmutablePair<AggregateAtom, AggregateAtom> splitCombinedAggregateAtom(AggregateAtom atom) {
    AggregateAtom leftHandAtom = Atoms.newAggregateAtom(atom.getLowerBoundOperator(), atom.getLowerBoundTerm(), atom.getAggregateFunction(), atom.getAggregateElements());
    AggregateAtom rightHandAtom = Atoms.newAggregateAtom(switchOperands(atom.getUpperBoundOperator()), atom.getUpperBoundTerm(), atom.getAggregateFunction(), atom.getAggregateElements());
    return new ImmutablePair<>(leftHandAtom, rightHandAtom);
}
Also used : ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) AggregateAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.AggregateAtom)

Example 5 with AggregateAtom

use of at.ac.tuwien.kr.alpha.api.programs.atoms.AggregateAtom in project Alpha by alpha-asp.

the class AggregateRewritingRuleAnalysis method findGlobalVariablesPerAggregate.

private void findGlobalVariablesPerAggregate() {
    // First, compute all global variables, that is all variables occurring in a rule except those occurring
    // inside aggregate elements.
    Set<VariableTerm> globalVariables = new HashSet<>();
    if (!rule.isConstraint()) {
        // Head must be normal at this point.
        NormalHead head = (NormalHead) rule.getHead();
        globalVariables.addAll(head.getAtom().getOccurringVariables());
    }
    for (Literal literal : rule.getBody()) {
        if (literal instanceof AggregateLiteral) {
            aggregatesInRule.add((AggregateLiteral) literal);
            AggregateAtom aggregateAtom = (AggregateAtom) literal.getAtom();
            // All variables in the bounds of an aggregate are also global variables.
            // Note that at this point, only lower bounds appear in aggregates.
            globalVariables.addAll(aggregateAtom.getLowerBoundTerm().getOccurringVariables());
        } else {
            globalVariables.addAll(literal.getOccurringVariables());
        }
    }
    // Second, compute for each aggregate those of its variables that are global.
    for (AggregateLiteral aggregateLiteral : aggregatesInRule) {
        Set<VariableTerm> globalVariablesInAggregate = new HashSet<>();
        for (VariableTerm aggregateVariable : aggregateLiteral.getAtom().getAggregateVariables()) {
            if (globalVariables.contains(aggregateVariable)) {
                globalVariablesInAggregate.add(aggregateVariable);
            }
        }
        globalVariablesPerAggregate.put(aggregateLiteral, globalVariablesInAggregate);
    }
}
Also used : NormalHead(at.ac.tuwien.kr.alpha.api.rules.heads.NormalHead) 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) AggregateAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.AggregateAtom) HashSet(java.util.HashSet)

Aggregations

AggregateAtom (at.ac.tuwien.kr.alpha.api.programs.atoms.AggregateAtom)8 AggregateLiteral (at.ac.tuwien.kr.alpha.api.programs.literals.AggregateLiteral)4 Literal (at.ac.tuwien.kr.alpha.api.programs.literals.Literal)4 VariableTerm (at.ac.tuwien.kr.alpha.api.terms.VariableTerm)4 ComparisonOperator (at.ac.tuwien.kr.alpha.api.ComparisonOperator)3 NormalHead (at.ac.tuwien.kr.alpha.api.rules.heads.NormalHead)2 ArrayList (java.util.ArrayList)2 ImmutablePair (org.apache.commons.lang3.tuple.ImmutablePair)2 ASPCore2Program (at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program)1 InlineDirectives (at.ac.tuwien.kr.alpha.api.programs.InlineDirectives)1 Atom (at.ac.tuwien.kr.alpha.api.programs.atoms.Atom)1 ComparisonLiteral (at.ac.tuwien.kr.alpha.api.programs.literals.ComparisonLiteral)1 ChoiceHead (at.ac.tuwien.kr.alpha.api.rules.heads.ChoiceHead)1 FunctionTerm (at.ac.tuwien.kr.alpha.api.terms.FunctionTerm)1 Term (at.ac.tuwien.kr.alpha.api.terms.Term)1 Predicates (at.ac.tuwien.kr.alpha.commons.Predicates)1 Atoms (at.ac.tuwien.kr.alpha.commons.atoms.Atoms)1 ComparisonOperators (at.ac.tuwien.kr.alpha.commons.comparisons.ComparisonOperators)1 IntervalTerm (at.ac.tuwien.kr.alpha.commons.terms.IntervalTerm)1 Terms (at.ac.tuwien.kr.alpha.commons.terms.Terms)1