Search in sources :

Example 1 with AggregateFunctionSymbol

use of at.ac.tuwien.kr.alpha.api.programs.atoms.AggregateAtom.AggregateFunctionSymbol 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 2 with AggregateFunctionSymbol

use of at.ac.tuwien.kr.alpha.api.programs.atoms.AggregateAtom.AggregateFunctionSymbol 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 3 with AggregateFunctionSymbol

use of at.ac.tuwien.kr.alpha.api.programs.atoms.AggregateAtom.AggregateFunctionSymbol 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)

Example 4 with AggregateFunctionSymbol

use of at.ac.tuwien.kr.alpha.api.programs.atoms.AggregateAtom.AggregateFunctionSymbol in project Alpha by alpha-asp.

the class AggregateRewriting method apply.

/**
 * Rewrites all {@link AggregateLiteral}s in the given program.
 * The transformation workflow is split into a preprocessing- and an encoding phase.
 * During preprocessing, all aggregate literals with two comparison operators are split into two aggregate literals with
 * only a "lower bound" term and operator. After literal splitting, operators are normalized, i.e. all "count" and "sum"
 * literals are rewritten to use either "<=" or "=" as comparison operator.
 *
 * Rules containing {@link AggregateLiteral}s are registered in an {@link AggregateRewritingContext} during
 * preprocessing. After preprocessing, for each rule in the context, aggregate literals in the rule body are substituted
 * with normal literals of form "$id$_aggregate_result(...)". For each literal substituted this way, a set of rules
 * deriving the result literal is added that is semantically equivalent to the replaced aggregate literal.
 */
@Override
public ASPCore2Program apply(ASPCore2Program inputProgram) {
    AggregateRewritingContext ctx = new AggregateRewritingContext();
    List<Rule<Head>> outputRules = new ArrayList<>();
    for (Rule<Head> inputRule : inputProgram.getRules()) {
        // Split literals with two operators.
        for (Rule<Head> splitRule : AggregateLiteralSplitting.split(inputRule)) {
            // Normalize operators on aggregate literals after splitting.
            Rule<Head> operatorNormalizedRule = AggregateOperatorNormalization.normalize(splitRule);
            boolean hasAggregate = ctx.registerRule(operatorNormalizedRule);
            // Only keep rules without aggregates. The ones with aggregates are registered in the context and taken care of later.
            if (!hasAggregate) {
                outputRules.add(operatorNormalizedRule);
            }
        }
    }
    // Substitute AggregateLiterals with generated result literals.
    outputRules.addAll(rewriteRulesWithAggregates(ctx));
    InputProgram.Builder resultBuilder = InputProgram.builder().addRules(outputRules).addFacts(inputProgram.getFacts()).addInlineDirectives(inputProgram.getInlineDirectives());
    // Add sub-programs deriving respective aggregate literals.
    for (Map.Entry<ImmutablePair<AggregateFunctionSymbol, ComparisonOperator>, Set<AggregateInfo>> aggToRewrite : ctx.getAggregateFunctionsToRewrite().entrySet()) {
        ImmutablePair<AggregateFunctionSymbol, ComparisonOperator> func = aggToRewrite.getKey();
        AbstractAggregateEncoder encoder = getEncoderForAggregateFunction(func.left, func.right);
        resultBuilder.accumulate(encoder.encodeAggregateLiterals(aggToRewrite.getValue()));
    }
    return resultBuilder.build();
}
Also used : AbstractAggregateEncoder(at.ac.tuwien.kr.alpha.core.programs.transformation.aggregates.encoders.AbstractAggregateEncoder) Head(at.ac.tuwien.kr.alpha.api.rules.heads.Head) ComparisonOperator(at.ac.tuwien.kr.alpha.api.ComparisonOperator) Set(java.util.Set) ArrayList(java.util.ArrayList) AggregateFunctionSymbol(at.ac.tuwien.kr.alpha.api.programs.atoms.AggregateAtom.AggregateFunctionSymbol) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) Rule(at.ac.tuwien.kr.alpha.api.rules.Rule) BasicRule(at.ac.tuwien.kr.alpha.core.rules.BasicRule) Map(java.util.Map) InputProgram(at.ac.tuwien.kr.alpha.core.programs.InputProgram)

Aggregations

ComparisonOperator (at.ac.tuwien.kr.alpha.api.ComparisonOperator)4 AggregateFunctionSymbol (at.ac.tuwien.kr.alpha.api.programs.atoms.AggregateAtom.AggregateFunctionSymbol)4 Set (java.util.Set)4 ImmutablePair (org.apache.commons.lang3.tuple.ImmutablePair)4 AggregateInfo (at.ac.tuwien.kr.alpha.core.programs.transformation.aggregates.AggregateRewritingContext.AggregateInfo)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 Map (java.util.Map)2 ASPCore2Program (at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program)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 InputProgram (at.ac.tuwien.kr.alpha.core.programs.InputProgram)1 AbstractAggregateEncoder (at.ac.tuwien.kr.alpha.core.programs.transformation.aggregates.encoders.AbstractAggregateEncoder)1 BasicRule (at.ac.tuwien.kr.alpha.core.rules.BasicRule)1 ArrayList (java.util.ArrayList)1 Predicate (java.util.function.Predicate)1 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)1