use of at.ac.tuwien.kr.alpha.core.programs.transformation.aggregates.AggregateRewritingContext.AggregateInfo in project Alpha by alpha-asp.
the class AggregateRewriting method rewriteRulesWithAggregates.
/**
* Transforms (restricted) aggregate literals of format "VAR OP #AGG_FN{...}" into literals of format
* "<result_predicate>(ARGS, VAR)" where ARGS is a function term wrapping the aggregate's global variables.
*
* @param ctx the {@link AggregateRewritingContext} containing information about all aggregates.
* @return for each rule, its rewritten version where aggregates are replaced with output atoms of the encoding.
*/
private static List<Rule<Head>> rewriteRulesWithAggregates(AggregateRewritingContext ctx) {
List<Rule<Head>> rewrittenRules = new ArrayList<>();
for (Rule<Head> rule : ctx.getRulesWithAggregates()) {
List<Literal> rewrittenBody = new ArrayList<>();
for (Literal lit : rule.getBody()) {
if (lit instanceof AggregateLiteral) {
AggregateInfo aggregateInfo = ctx.getAggregateInfo((AggregateLiteral) lit);
rewrittenBody.add(Literals.fromAtom(aggregateInfo.getOutputAtom(), !lit.isNegated()));
} else {
rewrittenBody.add(lit);
}
}
rewrittenRules.add(new BasicRule(rule.getHead(), rewrittenBody));
}
return rewrittenRules;
}
use of at.ac.tuwien.kr.alpha.core.programs.transformation.aggregates.AggregateRewritingContext.AggregateInfo 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());
}
use of at.ac.tuwien.kr.alpha.core.programs.transformation.aggregates.AggregateRewritingContext.AggregateInfo in project Alpha by alpha-asp.
the class AggregateRewritingContextTest method countEqMaxEqGlobalVars.
@Test
public void countEqMaxEqGlobalVars() {
AggregateRewritingContext ctx = rewritingContextForAspString(CTX_TEST_GRAPH_ASP);
Map<ImmutablePair<AggregateFunctionSymbol, ComparisonOperator>, Set<AggregateInfo>> functionsToRewrite = ctx.getAggregateFunctionsToRewrite();
assertEquals(2, functionsToRewrite.size());
ImmutablePair<AggregateFunctionSymbol, ComparisonOperator> cntEq = new ImmutablePair<>(AggregateFunctionSymbol.COUNT, ComparisonOperators.EQ);
ImmutablePair<AggregateFunctionSymbol, ComparisonOperator> maxEq = new ImmutablePair<>(AggregateFunctionSymbol.MAX, ComparisonOperators.EQ);
assertTrue(functionsToRewrite.containsKey(cntEq));
assertTrue(functionsToRewrite.containsKey(maxEq));
Set<AggregateInfo> cntEqIds = functionsToRewrite.get(cntEq);
Set<AggregateInfo> maxEqIds = functionsToRewrite.get(maxEq);
assertEquals(2, cntEqIds.size());
assertEquals(1, maxEqIds.size());
Predicate<AggregateInfo> vertexDegreeCount = (info) -> {
if (info.getLiteral().getAtom().getAggregateElements().size() != 2) {
return false;
}
Set<VariableTerm> globalVars = info.getGlobalVariables();
if (globalVars.size() != 2) {
return false;
}
if (!globalVars.contains(Terms.newVariable("G"))) {
return false;
}
if (!globalVars.contains(Terms.newVariable("V"))) {
return false;
}
return true;
};
Predicate<AggregateInfo> maxDegreeVerticesCount = (info) -> {
if (info.getLiteral().getAtom().getAggregateElements().size() != 1) {
return false;
}
Set<VariableTerm> globalVars = info.getGlobalVariables();
if (globalVars.size() != 2) {
return false;
}
if (!globalVars.contains(Terms.newVariable("G"))) {
return false;
}
if (!globalVars.contains(Terms.newVariable("DMAX"))) {
return false;
}
return true;
};
boolean verifiedDegreeCount = false;
boolean verifiedMaxDegreeVerticesCount = false;
for (AggregateInfo id : cntEqIds) {
if (vertexDegreeCount.test(id)) {
verifiedDegreeCount = true;
} else if (maxDegreeVerticesCount.test(id)) {
verifiedMaxDegreeVerticesCount = true;
}
}
assertTrue(verifiedDegreeCount);
assertTrue(verifiedMaxDegreeVerticesCount);
}
use of at.ac.tuwien.kr.alpha.core.programs.transformation.aggregates.AggregateRewritingContext.AggregateInfo in project Alpha by alpha-asp.
the class AggregateRewritingContextTest method minEqAggregateNoGlobalVars.
@Test
public void minEqAggregateNoGlobalVars() {
AggregateRewritingContext ctx = rewritingContextForAspString(CTX_TEST_MIN_EQ_ASP);
Map<ImmutablePair<AggregateFunctionSymbol, ComparisonOperator>, Set<AggregateInfo>> functionsToRewrite = ctx.getAggregateFunctionsToRewrite();
assertEquals(1, functionsToRewrite.size());
ImmutablePair<AggregateFunctionSymbol, ComparisonOperator> minEq = new ImmutablePair<>(AggregateFunctionSymbol.MIN, ComparisonOperators.EQ);
assertTrue(functionsToRewrite.containsKey(minEq));
Set<AggregateInfo> minEqAggregateInfos = functionsToRewrite.get(minEq);
assertEquals(1, minEqAggregateInfos.size());
AggregateInfo info = minEqAggregateInfos.iterator().next();
assertTrue(info.getGlobalVariables().isEmpty());
}
Aggregations