use of at.ac.tuwien.kr.alpha.api.programs.atoms.AggregateAtom.AggregateElement in project Alpha by alpha-asp.
the class AggregateRewritingRuleAnalysisTest method bindingAggregateWithGlobals2.
@Test
public void bindingAggregateWithGlobals2() {
AggregateRewritingRuleAnalysis analysis = analyze(BINDING_AGGREGATE_WITH_GLOBALS_2);
assertEquals(2, analysis.globalVariablesPerAggregate.size());
assertEquals(2, analysis.dependenciesPerAggregate.size());
// Verify correct analysis of max aggregate
List<Term> vertexDegreeTerms = Collections.singletonList(Terms.newVariable("DV"));
Literal vertexDegreeLiteral = Literals.fromAtom(Atoms.newBasicAtom(Predicates.getPredicate("graph_vertex_degree", 3), Terms.newVariable("G"), Terms.newVariable("V"), Terms.newVariable("DV")), true);
List<Literal> vertexDegreeLiterals = Collections.singletonList(vertexDegreeLiteral);
AggregateElement vertexDegree = Atoms.newAggregateElement(vertexDegreeTerms, vertexDegreeLiterals);
AggregateLiteral maxAggregate = Literals.fromAtom(Atoms.newAggregateAtom(ComparisonOperators.EQ, Terms.newVariable("DMAX"), AggregateFunctionSymbol.MAX, Collections.singletonList(vertexDegree)), true);
assertTrue(analysis.globalVariablesPerAggregate.containsKey(maxAggregate));
Set<VariableTerm> maxAggrGlobalVars = analysis.globalVariablesPerAggregate.get(maxAggregate);
assertEquals(1, maxAggrGlobalVars.size());
assertTrue(maxAggrGlobalVars.contains(Terms.newVariable("G")));
assertTrue(analysis.dependenciesPerAggregate.containsKey(maxAggregate));
Set<Literal> maxAggrDependencies = analysis.dependenciesPerAggregate.get(maxAggregate);
assertEquals(1, maxAggrDependencies.size());
Literal graph = Literals.fromAtom(Atoms.newBasicAtom(Predicates.getPredicate("graph", 1), Terms.newVariable("G")), true);
assertTrue(maxAggrDependencies.contains(graph));
// Verify correct analysis of count aggregate
List<Term> maxVertexDegreeTerms = Collections.singletonList(Terms.newVariable("V"));
Literal maxVertexDegreeLiteral = Literals.fromAtom(Atoms.newBasicAtom(Predicates.getPredicate("graph_vertex_degree", 3), Terms.newVariable("G"), Terms.newVariable("V"), Terms.newVariable("DMAX")), true);
List<Literal> maxVertexDegreeLiterals = Collections.singletonList(maxVertexDegreeLiteral);
AggregateElement maxVertexDegree = Atoms.newAggregateElement(maxVertexDegreeTerms, maxVertexDegreeLiterals);
AggregateLiteral countAggregate = Literals.fromAtom(Atoms.newAggregateAtom(ComparisonOperators.EQ, Terms.newVariable("N"), AggregateFunctionSymbol.COUNT, Collections.singletonList(maxVertexDegree)), true);
assertTrue(analysis.globalVariablesPerAggregate.containsKey(countAggregate));
Set<VariableTerm> cntAggrGlobalVars = analysis.globalVariablesPerAggregate.get(countAggregate);
assertEquals(2, cntAggrGlobalVars.size());
assertTrue(cntAggrGlobalVars.contains(Terms.newVariable("G")));
assertTrue(cntAggrGlobalVars.contains(Terms.newVariable("DMAX")));
assertTrue(analysis.dependenciesPerAggregate.containsKey(countAggregate));
Set<Literal> cntAggrDependencies = analysis.dependenciesPerAggregate.get(countAggregate);
assertEquals(2, cntAggrDependencies.size());
assertTrue(cntAggrDependencies.contains(graph));
assertTrue(cntAggrDependencies.contains(maxAggregate));
}
use of at.ac.tuwien.kr.alpha.api.programs.atoms.AggregateAtom.AggregateElement in project Alpha by alpha-asp.
the class AbstractAggregateEncoder method encodeAggregateLiteral.
/**
* Encodes the aggregate literal referenced by the given {@link AggregateInfo}.
*
* @param aggregateToEncode
* @return
*/
public ASPCore2Program encodeAggregateLiteral(AggregateInfo aggregateToEncode) {
AggregateLiteral literalToEncode = aggregateToEncode.getLiteral();
if (literalToEncode.getAtom().getAggregateFunction() != this.aggregateFunctionToEncode) {
throw new IllegalArgumentException("Encoder " + this.getClass().getSimpleName() + " cannot encode aggregate function " + literalToEncode.getAtom().getAggregateFunction());
}
if (!this.acceptedOperators.contains(literalToEncode.getAtom().getLowerBoundOperator())) {
throw new IllegalArgumentException("Encoder " + this.getClass().getSimpleName() + " cannot encode aggregate function " + literalToEncode.getAtom().getAggregateFunction() + " with operator " + literalToEncode.getAtom().getLowerBoundOperator());
}
String aggregateId = aggregateToEncode.getId();
ASPCore2Program literalEncoding = PredicateInternalizer.makePrefixedPredicatesInternal(encodeAggregateResult(aggregateToEncode), aggregateId);
List<Rule<Head>> elementEncodingRules = new ArrayList<>();
for (AggregateElement elementToEncode : literalToEncode.getAtom().getAggregateElements()) {
Rule<Head> elementRule = encodeAggregateElement(aggregateToEncode, elementToEncode);
elementEncodingRules.add(PredicateInternalizer.makePrefixedPredicatesInternal(elementRule, aggregateId));
}
return new InputProgram(ListUtils.union(literalEncoding.getRules(), elementEncodingRules), literalEncoding.getFacts(), new InlineDirectivesImpl());
}
Aggregations