use of at.ac.tuwien.kr.alpha.api.programs.atoms.AggregateAtom in project Alpha by alpha-asp.
the class AggregateLiteralSplitting method splitCombinedAggregateLiteral.
private static void splitCombinedAggregateLiteral(Literal literal, List<Literal> twoLiteralsSplitAggregates, List<ImmutablePair<Literal, Literal>> twoRulesSplitAggregates) {
AggregateLiteral aggLit = (AggregateLiteral) literal;
ImmutablePair<AggregateAtom, AggregateAtom> splitAggregate = splitCombinedAggregateAtom(aggLit.getAtom());
if (literal.isNegated()) {
// Negated aggregate require splitting in two rules.
twoRulesSplitAggregates.add(new ImmutablePair<>(splitAggregate.left.toLiteral(false), splitAggregate.right.toLiteral(false)));
} else {
// Positive aggregate requires two literals in the body.
twoLiteralsSplitAggregates.add(splitAggregate.left.toLiteral(true));
twoLiteralsSplitAggregates.add(splitAggregate.right.toLiteral(true));
}
}
use of at.ac.tuwien.kr.alpha.api.programs.atoms.AggregateAtom in project Alpha by alpha-asp.
the class MinMaxEncoder method encodeAggregateResult.
@Override
protected ASPCore2Program encodeAggregateResult(AggregateInfo aggregateToEncode) {
ST encodingTemplate = null;
if (this.getAggregateFunctionToEncode() == AggregateFunctionSymbol.MAX) {
encodingTemplate = new ST(MAX_LITERAL_ENCODING);
} else if (this.getAggregateFunctionToEncode() == AggregateFunctionSymbol.MIN) {
encodingTemplate = new ST(MIN_LITERAL_ENCODING);
} else {
// Note that this should definitely not happen due to the check in the constructor!
throw new UnsupportedOperationException("Cannot encode anything other than min/max aggregates!");
}
String id = aggregateToEncode.getId();
String resultName = aggregateToEncode.getOutputAtom().getPredicate().getName();
AggregateAtom atom = aggregateToEncode.getLiteral().getAtom();
ComparisonOperator cmpOp = atom.getLowerBoundOperator();
encodingTemplate.add("id", id);
encodingTemplate.add("aggregate_result", resultName);
if (cmpOp.equals(ComparisonOperators.EQ)) {
// Aggregate to encode binds a variable, use appropriate result rule.
ST resultRuleTemplate = new ST(BINDING_LITERAL_RESULT_RULE);
resultRuleTemplate.add("agg_func", atom.getAggregateFunction().toString().toLowerCase());
resultRuleTemplate.add("id", id);
resultRuleTemplate.add("aggregate_result", resultName);
return parser.parse(encodingTemplate.render() + resultRuleTemplate.render());
} else {
/*
* Aggregate encoding needs to compare aggregate value with another variable.
* Note that this should also use a string template for the result rule. However,
* since we need to compared to a (user-supplied) variable, we have to use a definitely
* non-conflicting variable name for the aggregate value, i.e. something prefixed with "_".
* Since the ProgramParser doesn't accept this, we need to build the result rule
* programmatically as a workaround.
*
* Result rule stringtemplate for reference:
* $aggregate_result$($args$, $cmp_term$) :-
* $cmp_term$ $cmp_op$ AGG_VAL,
* $id$_$agg_func$_element_tuple($args$, AGG_VAL),
* $dependencies;separator=\", \"$."
*/
NormalHead resultRuleHead = Heads.newNormalHead(Atoms.newBasicAtom(Predicates.getPredicate(resultName, 2), aggregateToEncode.getAggregateArguments(), atom.getLowerBoundTerm()));
List<Literal> resultRuleBody = new ArrayList<>();
VariableTerm aggregateValue = Terms.newVariable("_AGG_VAL");
ComparisonLiteral aggregateValueComparison = Literals.fromAtom(Atoms.newComparisonAtom(atom.getLowerBoundTerm(), aggregateValue, cmpOp), true);
Literal aggregateResult = Atoms.newBasicAtom(Predicates.getPredicate(id + "_" + atom.getAggregateFunction().toString().toLowerCase() + "_element_tuple", 2), aggregateToEncode.getAggregateArguments(), aggregateValue).toLiteral();
resultRuleBody.add(aggregateResult);
resultRuleBody.add(aggregateValueComparison);
resultRuleBody.addAll(aggregateToEncode.getDependencies());
InputProgram.Builder bld = InputProgram.builder(parser.parse(encodingTemplate.render()));
BasicRule resultRule = new BasicRule(resultRuleHead, resultRuleBody);
bld.addRule(resultRule);
return bld.build();
}
}
use of at.ac.tuwien.kr.alpha.api.programs.atoms.AggregateAtom in project Alpha by alpha-asp.
the class AggregateRewritingContext method registerAggregateLiteral.
private void registerAggregateLiteral(AggregateLiteral lit, Set<VariableTerm> globalVariables) {
AggregateAtom atom = lit.getAtom();
String id = atom.getAggregateFunction().toString().toLowerCase() + "_" + (++idCounter);
AggregateInfo info = new AggregateInfo(id, lit, globalVariables);
if (aggregateInfos.containsKey(lit)) {
throw oops("AggregateInfo for AggregateLiteral already existing.");
}
aggregateInfos.put(lit, info);
aggregateFunctionsToRewrite.putIfAbsent(new ImmutablePair<>(atom.getAggregateFunction(), atom.getLowerBoundOperator()), new LinkedHashSet<>());
aggregateFunctionsToRewrite.get(new ImmutablePair<>(atom.getAggregateFunction(), atom.getLowerBoundOperator())).add(info);
}
Aggregations