Search in sources :

Example 6 with AggregateAtom

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));
    }
}
Also used : AggregateLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.AggregateLiteral) AggregateAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.AggregateAtom)

Example 7 with AggregateAtom

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();
    }
}
Also used : BasicRule(at.ac.tuwien.kr.alpha.core.rules.BasicRule) ST(org.stringtemplate.v4.ST) ComparisonOperator(at.ac.tuwien.kr.alpha.api.ComparisonOperator) ArrayList(java.util.ArrayList) NormalHead(at.ac.tuwien.kr.alpha.api.rules.heads.NormalHead) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) ComparisonLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.ComparisonLiteral) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) AggregateAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.AggregateAtom) ComparisonLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.ComparisonLiteral) InputProgram(at.ac.tuwien.kr.alpha.core.programs.InputProgram)

Example 8 with AggregateAtom

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);
}
Also used : ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) AggregateAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.AggregateAtom)

Aggregations

AggregateAtom (at.ac.tuwien.kr.alpha.api.programs.atoms.AggregateAtom)8 AggregateLiteral (at.ac.tuwien.kr.alpha.api.programs.literals.AggregateLiteral)4 Literal (at.ac.tuwien.kr.alpha.api.programs.literals.Literal)4 VariableTerm (at.ac.tuwien.kr.alpha.api.terms.VariableTerm)4 ComparisonOperator (at.ac.tuwien.kr.alpha.api.ComparisonOperator)3 NormalHead (at.ac.tuwien.kr.alpha.api.rules.heads.NormalHead)2 ArrayList (java.util.ArrayList)2 ImmutablePair (org.apache.commons.lang3.tuple.ImmutablePair)2 ASPCore2Program (at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program)1 InlineDirectives (at.ac.tuwien.kr.alpha.api.programs.InlineDirectives)1 Atom (at.ac.tuwien.kr.alpha.api.programs.atoms.Atom)1 ComparisonLiteral (at.ac.tuwien.kr.alpha.api.programs.literals.ComparisonLiteral)1 ChoiceHead (at.ac.tuwien.kr.alpha.api.rules.heads.ChoiceHead)1 FunctionTerm (at.ac.tuwien.kr.alpha.api.terms.FunctionTerm)1 Term (at.ac.tuwien.kr.alpha.api.terms.Term)1 Predicates (at.ac.tuwien.kr.alpha.commons.Predicates)1 Atoms (at.ac.tuwien.kr.alpha.commons.atoms.Atoms)1 ComparisonOperators (at.ac.tuwien.kr.alpha.commons.comparisons.ComparisonOperators)1 IntervalTerm (at.ac.tuwien.kr.alpha.commons.terms.IntervalTerm)1 Terms (at.ac.tuwien.kr.alpha.commons.terms.Terms)1