Search in sources :

Example 1 with AggregateElement

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

the class AggregateRewritingRuleAnalysisTest method bindingAggregateWithGlobals2.

@Test
public void bindingAggregateWithGlobals2() {
    AggregateRewritingRuleAnalysis analysis = analyze(BINDING_AGGREGATE_WITH_GLOBALS_2);
    assertEquals(2, analysis.globalVariablesPerAggregate.size());
    assertEquals(2, analysis.dependenciesPerAggregate.size());
    // Verify correct analysis of max aggregate
    List<Term> vertexDegreeTerms = Collections.singletonList(Terms.newVariable("DV"));
    Literal vertexDegreeLiteral = Literals.fromAtom(Atoms.newBasicAtom(Predicates.getPredicate("graph_vertex_degree", 3), Terms.newVariable("G"), Terms.newVariable("V"), Terms.newVariable("DV")), true);
    List<Literal> vertexDegreeLiterals = Collections.singletonList(vertexDegreeLiteral);
    AggregateElement vertexDegree = Atoms.newAggregateElement(vertexDegreeTerms, vertexDegreeLiterals);
    AggregateLiteral maxAggregate = Literals.fromAtom(Atoms.newAggregateAtom(ComparisonOperators.EQ, Terms.newVariable("DMAX"), AggregateFunctionSymbol.MAX, Collections.singletonList(vertexDegree)), true);
    assertTrue(analysis.globalVariablesPerAggregate.containsKey(maxAggregate));
    Set<VariableTerm> maxAggrGlobalVars = analysis.globalVariablesPerAggregate.get(maxAggregate);
    assertEquals(1, maxAggrGlobalVars.size());
    assertTrue(maxAggrGlobalVars.contains(Terms.newVariable("G")));
    assertTrue(analysis.dependenciesPerAggregate.containsKey(maxAggregate));
    Set<Literal> maxAggrDependencies = analysis.dependenciesPerAggregate.get(maxAggregate);
    assertEquals(1, maxAggrDependencies.size());
    Literal graph = Literals.fromAtom(Atoms.newBasicAtom(Predicates.getPredicate("graph", 1), Terms.newVariable("G")), true);
    assertTrue(maxAggrDependencies.contains(graph));
    // Verify correct analysis of count aggregate
    List<Term> maxVertexDegreeTerms = Collections.singletonList(Terms.newVariable("V"));
    Literal maxVertexDegreeLiteral = Literals.fromAtom(Atoms.newBasicAtom(Predicates.getPredicate("graph_vertex_degree", 3), Terms.newVariable("G"), Terms.newVariable("V"), Terms.newVariable("DMAX")), true);
    List<Literal> maxVertexDegreeLiterals = Collections.singletonList(maxVertexDegreeLiteral);
    AggregateElement maxVertexDegree = Atoms.newAggregateElement(maxVertexDegreeTerms, maxVertexDegreeLiterals);
    AggregateLiteral countAggregate = Literals.fromAtom(Atoms.newAggregateAtom(ComparisonOperators.EQ, Terms.newVariable("N"), AggregateFunctionSymbol.COUNT, Collections.singletonList(maxVertexDegree)), true);
    assertTrue(analysis.globalVariablesPerAggregate.containsKey(countAggregate));
    Set<VariableTerm> cntAggrGlobalVars = analysis.globalVariablesPerAggregate.get(countAggregate);
    assertEquals(2, cntAggrGlobalVars.size());
    assertTrue(cntAggrGlobalVars.contains(Terms.newVariable("G")));
    assertTrue(cntAggrGlobalVars.contains(Terms.newVariable("DMAX")));
    assertTrue(analysis.dependenciesPerAggregate.containsKey(countAggregate));
    Set<Literal> cntAggrDependencies = analysis.dependenciesPerAggregate.get(countAggregate);
    assertEquals(2, cntAggrDependencies.size());
    assertTrue(cntAggrDependencies.contains(graph));
    assertTrue(cntAggrDependencies.contains(maxAggregate));
}
Also used : AggregateElement(at.ac.tuwien.kr.alpha.api.programs.atoms.AggregateAtom.AggregateElement) 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) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) Term(at.ac.tuwien.kr.alpha.api.terms.Term) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) Test(org.junit.jupiter.api.Test)

Example 2 with AggregateElement

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

the class AbstractAggregateEncoder method encodeAggregateLiteral.

/**
 * Encodes the aggregate literal referenced by the given {@link AggregateInfo}.
 *
 * @param aggregateToEncode
 * @return
 */
public ASPCore2Program encodeAggregateLiteral(AggregateInfo aggregateToEncode) {
    AggregateLiteral literalToEncode = aggregateToEncode.getLiteral();
    if (literalToEncode.getAtom().getAggregateFunction() != this.aggregateFunctionToEncode) {
        throw new IllegalArgumentException("Encoder " + this.getClass().getSimpleName() + " cannot encode aggregate function " + literalToEncode.getAtom().getAggregateFunction());
    }
    if (!this.acceptedOperators.contains(literalToEncode.getAtom().getLowerBoundOperator())) {
        throw new IllegalArgumentException("Encoder " + this.getClass().getSimpleName() + " cannot encode aggregate function " + literalToEncode.getAtom().getAggregateFunction() + " with operator " + literalToEncode.getAtom().getLowerBoundOperator());
    }
    String aggregateId = aggregateToEncode.getId();
    ASPCore2Program literalEncoding = PredicateInternalizer.makePrefixedPredicatesInternal(encodeAggregateResult(aggregateToEncode), aggregateId);
    List<Rule<Head>> elementEncodingRules = new ArrayList<>();
    for (AggregateElement elementToEncode : literalToEncode.getAtom().getAggregateElements()) {
        Rule<Head> elementRule = encodeAggregateElement(aggregateToEncode, elementToEncode);
        elementEncodingRules.add(PredicateInternalizer.makePrefixedPredicatesInternal(elementRule, aggregateId));
    }
    return new InputProgram(ListUtils.union(literalEncoding.getRules(), elementEncodingRules), literalEncoding.getFacts(), new InlineDirectivesImpl());
}
Also used : ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) Head(at.ac.tuwien.kr.alpha.api.rules.heads.Head) AggregateLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.AggregateLiteral) AggregateElement(at.ac.tuwien.kr.alpha.api.programs.atoms.AggregateAtom.AggregateElement) ArrayList(java.util.ArrayList) Rule(at.ac.tuwien.kr.alpha.api.rules.Rule) BasicRule(at.ac.tuwien.kr.alpha.core.rules.BasicRule) InlineDirectivesImpl(at.ac.tuwien.kr.alpha.core.parser.InlineDirectivesImpl) InputProgram(at.ac.tuwien.kr.alpha.core.programs.InputProgram)

Aggregations

AggregateElement (at.ac.tuwien.kr.alpha.api.programs.atoms.AggregateAtom.AggregateElement)2 AggregateLiteral (at.ac.tuwien.kr.alpha.api.programs.literals.AggregateLiteral)2 ASPCore2Program (at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program)1 Literal (at.ac.tuwien.kr.alpha.api.programs.literals.Literal)1 Rule (at.ac.tuwien.kr.alpha.api.rules.Rule)1 Head (at.ac.tuwien.kr.alpha.api.rules.heads.Head)1 Term (at.ac.tuwien.kr.alpha.api.terms.Term)1 VariableTerm (at.ac.tuwien.kr.alpha.api.terms.VariableTerm)1 InlineDirectivesImpl (at.ac.tuwien.kr.alpha.core.parser.InlineDirectivesImpl)1 InputProgram (at.ac.tuwien.kr.alpha.core.programs.InputProgram)1 BasicRule (at.ac.tuwien.kr.alpha.core.rules.BasicRule)1 ArrayList (java.util.ArrayList)1 Test (org.junit.jupiter.api.Test)1