Search in sources :

Example 1 with EnumerationRewriting

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());
}
Also used : EnumerationRewriting(at.ac.tuwien.kr.alpha.core.programs.transformation.EnumerationRewriting) ST(org.stringtemplate.v4.ST) ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) Head(at.ac.tuwien.kr.alpha.api.rules.heads.Head) InlineDirectivesImpl(at.ac.tuwien.kr.alpha.core.parser.InlineDirectivesImpl) InputProgram(at.ac.tuwien.kr.alpha.core.programs.InputProgram)

Aggregations

ASPCore2Program (at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program)1 Head (at.ac.tuwien.kr.alpha.api.rules.heads.Head)1 InlineDirectivesImpl (at.ac.tuwien.kr.alpha.core.parser.InlineDirectivesImpl)1 InputProgram (at.ac.tuwien.kr.alpha.core.programs.InputProgram)1 EnumerationRewriting (at.ac.tuwien.kr.alpha.core.programs.transformation.EnumerationRewriting)1 ST (org.stringtemplate.v4.ST)1