Search in sources :

Example 1 with AggregateInfo

use of at.ac.tuwien.kr.alpha.core.programs.transformation.aggregates.AggregateRewritingContext.AggregateInfo in project Alpha by alpha-asp.

the class AggregateRewriting method rewriteRulesWithAggregates.

/**
 * Transforms (restricted) aggregate literals of format "VAR OP #AGG_FN{...}" into literals of format
 * "<result_predicate>(ARGS, VAR)" where ARGS is a function term wrapping the aggregate's global variables.
 *
 * @param ctx the {@link AggregateRewritingContext} containing information about all aggregates.
 * @return for each rule, its rewritten version where aggregates are replaced with output atoms of the encoding.
 */
private static List<Rule<Head>> rewriteRulesWithAggregates(AggregateRewritingContext ctx) {
    List<Rule<Head>> rewrittenRules = new ArrayList<>();
    for (Rule<Head> rule : ctx.getRulesWithAggregates()) {
        List<Literal> rewrittenBody = new ArrayList<>();
        for (Literal lit : rule.getBody()) {
            if (lit instanceof AggregateLiteral) {
                AggregateInfo aggregateInfo = ctx.getAggregateInfo((AggregateLiteral) lit);
                rewrittenBody.add(Literals.fromAtom(aggregateInfo.getOutputAtom(), !lit.isNegated()));
            } else {
                rewrittenBody.add(lit);
            }
        }
        rewrittenRules.add(new BasicRule(rule.getHead(), rewrittenBody));
    }
    return rewrittenRules;
}
Also used : BasicRule(at.ac.tuwien.kr.alpha.core.rules.BasicRule) Head(at.ac.tuwien.kr.alpha.api.rules.heads.Head) AggregateLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.AggregateLiteral) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) AggregateLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.AggregateLiteral) ArrayList(java.util.ArrayList) Rule(at.ac.tuwien.kr.alpha.api.rules.Rule) BasicRule(at.ac.tuwien.kr.alpha.core.rules.BasicRule) AggregateInfo(at.ac.tuwien.kr.alpha.core.programs.transformation.aggregates.AggregateRewritingContext.AggregateInfo)

Example 2 with AggregateInfo

use of at.ac.tuwien.kr.alpha.core.programs.transformation.aggregates.AggregateRewritingContext.AggregateInfo in project Alpha by alpha-asp.

the class AggregateRewritingContextTest method countEqAggregateNoGlobalVars.

@Test
public void countEqAggregateNoGlobalVars() {
    AggregateRewritingContext ctx = rewritingContextForAspString(CTX_TEST_CNT_EQ_ASP);
    Map<ImmutablePair<AggregateFunctionSymbol, ComparisonOperator>, Set<AggregateInfo>> functionsToRewrite = ctx.getAggregateFunctionsToRewrite();
    assertEquals(1, functionsToRewrite.size());
    ImmutablePair<AggregateFunctionSymbol, ComparisonOperator> cntEq = new ImmutablePair<>(AggregateFunctionSymbol.COUNT, ComparisonOperators.EQ);
    assertTrue(functionsToRewrite.containsKey(cntEq));
    Set<AggregateInfo> cntEqAggregateInfos = functionsToRewrite.get(cntEq);
    assertEquals(1, cntEqAggregateInfos.size());
    AggregateInfo info = cntEqAggregateInfos.iterator().next();
    assertTrue(info.getGlobalVariables().isEmpty());
}
Also used : ComparisonOperator(at.ac.tuwien.kr.alpha.api.ComparisonOperator) Set(java.util.Set) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) AggregateFunctionSymbol(at.ac.tuwien.kr.alpha.api.programs.atoms.AggregateAtom.AggregateFunctionSymbol) AggregateInfo(at.ac.tuwien.kr.alpha.core.programs.transformation.aggregates.AggregateRewritingContext.AggregateInfo) Test(org.junit.jupiter.api.Test)

Example 3 with AggregateInfo

use of at.ac.tuwien.kr.alpha.core.programs.transformation.aggregates.AggregateRewritingContext.AggregateInfo in project Alpha by alpha-asp.

the class AggregateRewritingContextTest method countEqMaxEqGlobalVars.

@Test
public void countEqMaxEqGlobalVars() {
    AggregateRewritingContext ctx = rewritingContextForAspString(CTX_TEST_GRAPH_ASP);
    Map<ImmutablePair<AggregateFunctionSymbol, ComparisonOperator>, Set<AggregateInfo>> functionsToRewrite = ctx.getAggregateFunctionsToRewrite();
    assertEquals(2, functionsToRewrite.size());
    ImmutablePair<AggregateFunctionSymbol, ComparisonOperator> cntEq = new ImmutablePair<>(AggregateFunctionSymbol.COUNT, ComparisonOperators.EQ);
    ImmutablePair<AggregateFunctionSymbol, ComparisonOperator> maxEq = new ImmutablePair<>(AggregateFunctionSymbol.MAX, ComparisonOperators.EQ);
    assertTrue(functionsToRewrite.containsKey(cntEq));
    assertTrue(functionsToRewrite.containsKey(maxEq));
    Set<AggregateInfo> cntEqIds = functionsToRewrite.get(cntEq);
    Set<AggregateInfo> maxEqIds = functionsToRewrite.get(maxEq);
    assertEquals(2, cntEqIds.size());
    assertEquals(1, maxEqIds.size());
    Predicate<AggregateInfo> vertexDegreeCount = (info) -> {
        if (info.getLiteral().getAtom().getAggregateElements().size() != 2) {
            return false;
        }
        Set<VariableTerm> globalVars = info.getGlobalVariables();
        if (globalVars.size() != 2) {
            return false;
        }
        if (!globalVars.contains(Terms.newVariable("G"))) {
            return false;
        }
        if (!globalVars.contains(Terms.newVariable("V"))) {
            return false;
        }
        return true;
    };
    Predicate<AggregateInfo> maxDegreeVerticesCount = (info) -> {
        if (info.getLiteral().getAtom().getAggregateElements().size() != 1) {
            return false;
        }
        Set<VariableTerm> globalVars = info.getGlobalVariables();
        if (globalVars.size() != 2) {
            return false;
        }
        if (!globalVars.contains(Terms.newVariable("G"))) {
            return false;
        }
        if (!globalVars.contains(Terms.newVariable("DMAX"))) {
            return false;
        }
        return true;
    };
    boolean verifiedDegreeCount = false;
    boolean verifiedMaxDegreeVerticesCount = false;
    for (AggregateInfo id : cntEqIds) {
        if (vertexDegreeCount.test(id)) {
            verifiedDegreeCount = true;
        } else if (maxDegreeVerticesCount.test(id)) {
            verifiedMaxDegreeVerticesCount = true;
        }
    }
    assertTrue(verifiedDegreeCount);
    assertTrue(verifiedMaxDegreeVerticesCount);
}
Also used : AggregateFunctionSymbol(at.ac.tuwien.kr.alpha.api.programs.atoms.AggregateAtom.AggregateFunctionSymbol) Rule(at.ac.tuwien.kr.alpha.api.rules.Rule) Predicate(java.util.function.Predicate) Set(java.util.Set) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) ComparisonOperator(at.ac.tuwien.kr.alpha.api.ComparisonOperator) Test(org.junit.jupiter.api.Test) Terms(at.ac.tuwien.kr.alpha.commons.terms.Terms) Head(at.ac.tuwien.kr.alpha.api.rules.heads.Head) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) AggregateInfo(at.ac.tuwien.kr.alpha.core.programs.transformation.aggregates.AggregateRewritingContext.AggregateInfo) ProgramParserImpl(at.ac.tuwien.kr.alpha.core.parser.ProgramParserImpl) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Map(java.util.Map) ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ComparisonOperators(at.ac.tuwien.kr.alpha.commons.comparisons.ComparisonOperators) ComparisonOperator(at.ac.tuwien.kr.alpha.api.ComparisonOperator) Set(java.util.Set) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) AggregateFunctionSymbol(at.ac.tuwien.kr.alpha.api.programs.atoms.AggregateAtom.AggregateFunctionSymbol) AggregateInfo(at.ac.tuwien.kr.alpha.core.programs.transformation.aggregates.AggregateRewritingContext.AggregateInfo) Test(org.junit.jupiter.api.Test)

Example 4 with AggregateInfo

use of at.ac.tuwien.kr.alpha.core.programs.transformation.aggregates.AggregateRewritingContext.AggregateInfo in project Alpha by alpha-asp.

the class AggregateRewritingContextTest method minEqAggregateNoGlobalVars.

@Test
public void minEqAggregateNoGlobalVars() {
    AggregateRewritingContext ctx = rewritingContextForAspString(CTX_TEST_MIN_EQ_ASP);
    Map<ImmutablePair<AggregateFunctionSymbol, ComparisonOperator>, Set<AggregateInfo>> functionsToRewrite = ctx.getAggregateFunctionsToRewrite();
    assertEquals(1, functionsToRewrite.size());
    ImmutablePair<AggregateFunctionSymbol, ComparisonOperator> minEq = new ImmutablePair<>(AggregateFunctionSymbol.MIN, ComparisonOperators.EQ);
    assertTrue(functionsToRewrite.containsKey(minEq));
    Set<AggregateInfo> minEqAggregateInfos = functionsToRewrite.get(minEq);
    assertEquals(1, minEqAggregateInfos.size());
    AggregateInfo info = minEqAggregateInfos.iterator().next();
    assertTrue(info.getGlobalVariables().isEmpty());
}
Also used : ComparisonOperator(at.ac.tuwien.kr.alpha.api.ComparisonOperator) Set(java.util.Set) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) AggregateFunctionSymbol(at.ac.tuwien.kr.alpha.api.programs.atoms.AggregateAtom.AggregateFunctionSymbol) AggregateInfo(at.ac.tuwien.kr.alpha.core.programs.transformation.aggregates.AggregateRewritingContext.AggregateInfo) Test(org.junit.jupiter.api.Test)

Aggregations

AggregateInfo (at.ac.tuwien.kr.alpha.core.programs.transformation.aggregates.AggregateRewritingContext.AggregateInfo)4 ComparisonOperator (at.ac.tuwien.kr.alpha.api.ComparisonOperator)3 AggregateFunctionSymbol (at.ac.tuwien.kr.alpha.api.programs.atoms.AggregateAtom.AggregateFunctionSymbol)3 Set (java.util.Set)3 ImmutablePair (org.apache.commons.lang3.tuple.ImmutablePair)3 Test (org.junit.jupiter.api.Test)3 Rule (at.ac.tuwien.kr.alpha.api.rules.Rule)2 Head (at.ac.tuwien.kr.alpha.api.rules.heads.Head)2 ASPCore2Program (at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program)1 AggregateLiteral (at.ac.tuwien.kr.alpha.api.programs.literals.AggregateLiteral)1 Literal (at.ac.tuwien.kr.alpha.api.programs.literals.Literal)1 VariableTerm (at.ac.tuwien.kr.alpha.api.terms.VariableTerm)1 ComparisonOperators (at.ac.tuwien.kr.alpha.commons.comparisons.ComparisonOperators)1 Terms (at.ac.tuwien.kr.alpha.commons.terms.Terms)1 ProgramParserImpl (at.ac.tuwien.kr.alpha.core.parser.ProgramParserImpl)1 BasicRule (at.ac.tuwien.kr.alpha.core.rules.BasicRule)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 Predicate (java.util.function.Predicate)1 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)1