use of at.ac.tuwien.kr.alpha.core.programs.transformation.EnumerationRewriting in project Alpha by alpha-asp.
the class StringtemplateBasedAggregateEncoder method encodeAggregateResult.
@Override
protected ASPCore2Program encodeAggregateResult(AggregateInfo aggregateToEncode) {
String aggregateId = aggregateToEncode.getId();
/*
* Create a rule deriving a "bound" value for the core aggregate encoding.
* The bound is (in case of encodings for "<=" comparisons) the value that should be tested for being a lower bound, or
* else zero.
*/
Rule<Head> boundRule = null;
if (this.needsBoundRule) {
boundRule = this.buildBoundRule(aggregateToEncode);
} else {
/*
* Even if we don't have to create a bound rule because the aggregate encoding generates its own candidate values,
* we still generate a rule deriving zero as a bound, so that sums and counts over empty sets correctly return 0.
*/
boundRule = this.buildZeroBoundRule(aggregateToEncode);
}
// Generate encoding
ST coreEncodingTemplate = new ST(this.encodingTemplate);
coreEncodingTemplate.add("result_predicate", aggregateToEncode.getOutputAtom().getPredicate().getName());
coreEncodingTemplate.add("id", aggregateId);
coreEncodingTemplate.add("element_tuple", this.getElementTuplePredicateSymbol(aggregateId));
coreEncodingTemplate.add("bound", this.getBoundPredicateName(aggregateId));
String coreEncodingAsp = coreEncodingTemplate.render();
// Create the basic program
ASPCore2Program coreEncoding = new EnumerationRewriting().apply(parser.parse(coreEncodingAsp));
// Add the programatically created bound rule and return
return new InputProgram(ListUtils.union(coreEncoding.getRules(), Collections.singletonList(boundRule)), coreEncoding.getFacts(), new InlineDirectivesImpl());
}
Aggregations