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);
}
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);
}
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;
}
}
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());
}
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());
}
Aggregations