use of at.ac.tuwien.kr.alpha.api.programs.atoms.AggregateAtom.AggregateFunctionSymbol 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.api.programs.atoms.AggregateAtom.AggregateFunctionSymbol 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.api.programs.atoms.AggregateAtom.AggregateFunctionSymbol 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());
}
use of at.ac.tuwien.kr.alpha.api.programs.atoms.AggregateAtom.AggregateFunctionSymbol in project Alpha by alpha-asp.
the class AggregateRewriting method apply.
/**
* Rewrites all {@link AggregateLiteral}s in the given program.
* The transformation workflow is split into a preprocessing- and an encoding phase.
* During preprocessing, all aggregate literals with two comparison operators are split into two aggregate literals with
* only a "lower bound" term and operator. After literal splitting, operators are normalized, i.e. all "count" and "sum"
* literals are rewritten to use either "<=" or "=" as comparison operator.
*
* Rules containing {@link AggregateLiteral}s are registered in an {@link AggregateRewritingContext} during
* preprocessing. After preprocessing, for each rule in the context, aggregate literals in the rule body are substituted
* with normal literals of form "$id$_aggregate_result(...)". For each literal substituted this way, a set of rules
* deriving the result literal is added that is semantically equivalent to the replaced aggregate literal.
*/
@Override
public ASPCore2Program apply(ASPCore2Program inputProgram) {
AggregateRewritingContext ctx = new AggregateRewritingContext();
List<Rule<Head>> outputRules = new ArrayList<>();
for (Rule<Head> inputRule : inputProgram.getRules()) {
// Split literals with two operators.
for (Rule<Head> splitRule : AggregateLiteralSplitting.split(inputRule)) {
// Normalize operators on aggregate literals after splitting.
Rule<Head> operatorNormalizedRule = AggregateOperatorNormalization.normalize(splitRule);
boolean hasAggregate = ctx.registerRule(operatorNormalizedRule);
// Only keep rules without aggregates. The ones with aggregates are registered in the context and taken care of later.
if (!hasAggregate) {
outputRules.add(operatorNormalizedRule);
}
}
}
// Substitute AggregateLiterals with generated result literals.
outputRules.addAll(rewriteRulesWithAggregates(ctx));
InputProgram.Builder resultBuilder = InputProgram.builder().addRules(outputRules).addFacts(inputProgram.getFacts()).addInlineDirectives(inputProgram.getInlineDirectives());
// Add sub-programs deriving respective aggregate literals.
for (Map.Entry<ImmutablePair<AggregateFunctionSymbol, ComparisonOperator>, Set<AggregateInfo>> aggToRewrite : ctx.getAggregateFunctionsToRewrite().entrySet()) {
ImmutablePair<AggregateFunctionSymbol, ComparisonOperator> func = aggToRewrite.getKey();
AbstractAggregateEncoder encoder = getEncoderForAggregateFunction(func.left, func.right);
resultBuilder.accumulate(encoder.encodeAggregateLiterals(aggToRewrite.getValue()));
}
return resultBuilder.build();
}
Aggregations