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