Search in sources :

Example 1 with ComparisonOperator

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

the class ParseTreeVisitor method visitAggregate.

@Override
public AggregateLiteral visitAggregate(ASPCore2Parser.AggregateContext ctx) {
    // aggregate : NAF? (lt=term lop=binop)? aggregate_function CURLY_OPEN aggregate_elements CURLY_CLOSE (uop=binop ut=term)?;
    boolean isPositive = ctx.NAF() == null;
    Term lt = null;
    ComparisonOperator lop = null;
    Term ut = null;
    ComparisonOperator uop = null;
    if (ctx.lt != null) {
        lt = (Term) visit(ctx.lt);
        lop = visitBinop(ctx.lop);
    }
    if (ctx.ut != null) {
        ut = (Term) visit(ctx.ut);
        uop = visitBinop(ctx.uop);
    }
    AggregateAtom.AggregateFunctionSymbol aggregateFunction = visitAggregate_function(ctx.aggregate_function());
    List<AggregateAtom.AggregateElement> aggregateElements = visitAggregate_elements(ctx.aggregate_elements());
    return Atoms.newAggregateAtom(lop, lt, uop, ut, aggregateFunction, aggregateElements).toLiteral(isPositive);
}
Also used : ComparisonOperator(at.ac.tuwien.kr.alpha.api.ComparisonOperator) IntervalTerm(at.ac.tuwien.kr.alpha.commons.terms.IntervalTerm) ConstantTerm(at.ac.tuwien.kr.alpha.api.terms.ConstantTerm) 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)

Example 2 with ComparisonOperator

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

the class ParseTreeVisitor method visitChoice.

@Override
public Head visitChoice(ASPCore2Parser.ChoiceContext ctx) {
    // choice : (lt=term lop=binop)? CURLY_OPEN choice_elements? CURLY_CLOSE (uop=binop ut=term)?;
    Term lt = null;
    ComparisonOperator lop = null;
    Term ut = null;
    ComparisonOperator uop = null;
    if (ctx.lt != null) {
        lt = (Term) visit(ctx.lt);
        lop = visitBinop(ctx.lop);
    }
    if (ctx.ut != null) {
        ut = (Term) visit(ctx.ut);
        uop = visitBinop(ctx.uop);
    }
    return Heads.newChoiceHead(visitChoice_elements(ctx.choice_elements()), lt, lop, ut, uop);
}
Also used : ComparisonOperator(at.ac.tuwien.kr.alpha.api.ComparisonOperator) IntervalTerm(at.ac.tuwien.kr.alpha.commons.terms.IntervalTerm) ConstantTerm(at.ac.tuwien.kr.alpha.api.terms.ConstantTerm) 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)

Example 3 with ComparisonOperator

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

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

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

the class AggregateRewritingContextTest method countEqAggregateNoGlobalVars.

@Test
public void countEqAggregateNoGlobalVars() {
    AggregateRewritingContext ctx = rewritingContextForAspString(CTX_TEST_CNT_EQ_ASP);
    Map<ImmutablePair<AggregateFunctionSymbol, ComparisonOperator>, Set<AggregateInfo>> functionsToRewrite = ctx.getAggregateFunctionsToRewrite();
    assertEquals(1, functionsToRewrite.size());
    ImmutablePair<AggregateFunctionSymbol, ComparisonOperator> cntEq = new ImmutablePair<>(AggregateFunctionSymbol.COUNT, ComparisonOperators.EQ);
    assertTrue(functionsToRewrite.containsKey(cntEq));
    Set<AggregateInfo> cntEqAggregateInfos = functionsToRewrite.get(cntEq);
    assertEquals(1, cntEqAggregateInfos.size());
    AggregateInfo info = cntEqAggregateInfos.iterator().next();
    assertTrue(info.getGlobalVariables().isEmpty());
}
Also used : ComparisonOperator(at.ac.tuwien.kr.alpha.api.ComparisonOperator) Set(java.util.Set) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) AggregateFunctionSymbol(at.ac.tuwien.kr.alpha.api.programs.atoms.AggregateAtom.AggregateFunctionSymbol) AggregateInfo(at.ac.tuwien.kr.alpha.core.programs.transformation.aggregates.AggregateRewritingContext.AggregateInfo) Test(org.junit.jupiter.api.Test)

Aggregations

ComparisonOperator (at.ac.tuwien.kr.alpha.api.ComparisonOperator)9 VariableTerm (at.ac.tuwien.kr.alpha.api.terms.VariableTerm)5 AggregateAtom (at.ac.tuwien.kr.alpha.api.programs.atoms.AggregateAtom)4 AggregateFunctionSymbol (at.ac.tuwien.kr.alpha.api.programs.atoms.AggregateAtom.AggregateFunctionSymbol)4 Set (java.util.Set)4 ImmutablePair (org.apache.commons.lang3.tuple.ImmutablePair)4 AggregateInfo (at.ac.tuwien.kr.alpha.core.programs.transformation.aggregates.AggregateRewritingContext.AggregateInfo)3 ArrayList (java.util.ArrayList)3 Test (org.junit.jupiter.api.Test)3 Literal (at.ac.tuwien.kr.alpha.api.programs.literals.Literal)2 Rule (at.ac.tuwien.kr.alpha.api.rules.Rule)2 Head (at.ac.tuwien.kr.alpha.api.rules.heads.Head)2 ConstantTerm (at.ac.tuwien.kr.alpha.api.terms.ConstantTerm)2 FunctionTerm (at.ac.tuwien.kr.alpha.api.terms.FunctionTerm)2 Term (at.ac.tuwien.kr.alpha.api.terms.Term)2 IntervalTerm (at.ac.tuwien.kr.alpha.commons.terms.IntervalTerm)2 InputProgram (at.ac.tuwien.kr.alpha.core.programs.InputProgram)2 BasicRule (at.ac.tuwien.kr.alpha.core.rules.BasicRule)2 Map (java.util.Map)2 ASPCore2Program (at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program)1