Search in sources :

Example 31 with Literal

use of at.ac.tuwien.kr.alpha.api.programs.literals.Literal in project Alpha by alpha-asp.

the class LiteralInstantiatorTest method workingMemoryBasedInstantiatePositiveBasicLiteral.

@Test
public void workingMemoryBasedInstantiatePositiveBasicLiteral() {
    Predicate p = Predicates.getPredicate("p", 2);
    WorkingMemory workingMemory = new WorkingMemory();
    workingMemory.initialize(p);
    workingMemory.addInstance(Atoms.newBasicAtom(p, Terms.newSymbolicConstant("x"), Terms.newSymbolicConstant("y")), true);
    workingMemory.addInstance(Atoms.newBasicAtom(p, Terms.newSymbolicConstant("x"), Terms.newSymbolicConstant("z")), true);
    VariableTerm x = Terms.newVariable("X");
    VariableTerm y = Terms.newVariable("Y");
    Literal lit = Literals.fromAtom(Atoms.newBasicAtom(p, x, y), true);
    Substitution substitution = new BasicSubstitution();
    substitution.put(x, Terms.newSymbolicConstant("x"));
    LiteralInstantiator instantiator = new LiteralInstantiator(new WorkingMemoryBasedInstantiationStrategy(workingMemory));
    LiteralInstantiationResult result = instantiator.instantiateLiteral(lit, substitution);
    assertEquals(LiteralInstantiationResult.Type.CONTINUE, result.getType());
    List<ImmutablePair<Substitution, AssignmentStatus>> substitutions = result.getSubstitutions();
    assertEquals(2, substitutions.size());
    boolean ySubstituted = false;
    boolean zSubstituted = false;
    for (ImmutablePair<Substitution, AssignmentStatus> resultSubstitution : substitutions) {
        assertTrue(resultSubstitution.left.isVariableSet(y));
        assertEquals(AssignmentStatus.TRUE, resultSubstitution.right);
        if (resultSubstitution.left.eval(y).equals(Terms.newSymbolicConstant("y"))) {
            ySubstituted = true;
        } else if (resultSubstitution.left.eval(y).equals(Terms.newSymbolicConstant("z"))) {
            zSubstituted = true;
        } else {
            fail("Invalid substitution for variable Y");
        }
    }
    assertTrue(ySubstituted && zSubstituted);
}
Also used : WorkingMemory(at.ac.tuwien.kr.alpha.core.grounder.WorkingMemory) BasicSubstitution(at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate) Substitution(at.ac.tuwien.kr.alpha.api.grounder.Substitution) BasicSubstitution(at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) EnumerationLiteral(at.ac.tuwien.kr.alpha.core.atoms.EnumerationLiteral) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) Test(org.junit.jupiter.api.Test)

Example 32 with Literal

use of at.ac.tuwien.kr.alpha.api.programs.literals.Literal in project Alpha by alpha-asp.

the class AggregateOperatorNormalizationTest method assertAggregateBoundIncremented.

private static void assertAggregateBoundIncremented(Rule<Head> sourceRule, Rule<Head> rewrittenRule) {
    AggregateLiteral sourceAggregate = null;
    for (Literal lit : sourceRule.getBody()) {
        if (lit instanceof AggregateLiteral) {
            sourceAggregate = (AggregateLiteral) lit;
        }
    }
    AggregateLiteral rewrittenAggregate = null;
    ComparisonLiteral addedComparisonLiteral = null;
    for (Literal lit : rewrittenRule.getBody()) {
        if (lit instanceof AggregateLiteral) {
            rewrittenAggregate = (AggregateLiteral) lit;
        } else if (lit instanceof ComparisonLiteral) {
            addedComparisonLiteral = (ComparisonLiteral) lit;
        }
    }
    assertNotNull(addedComparisonLiteral);
    assertEquals(addedComparisonLiteral.getAtom().getTerms().get(0), rewrittenAggregate.getAtom().getLowerBoundTerm());
    Term comparisonRightHandTerm = addedComparisonLiteral.getAtom().getTerms().get(1);
    assertTrue(comparisonRightHandTerm instanceof ArithmeticTerm);
    ArithmeticTerm incrementTerm = (ArithmeticTerm) comparisonRightHandTerm;
    assertEquals(ArithmeticOperator.PLUS, incrementTerm.getOperator());
    assertEquals(Terms.newConstant(1), incrementTerm.getRightOperand());
    Term sourceBound = sourceAggregate.getAtom().getLowerBoundTerm() != null ? sourceAggregate.getAtom().getLowerBoundTerm() : sourceAggregate.getAtom().getUpperBoundTerm();
    assertEquals(sourceBound, incrementTerm.getLeftOperand());
}
Also used : AggregateLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.AggregateLiteral) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) ComparisonLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.ComparisonLiteral) AggregateLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.AggregateLiteral) ArithmeticTerm(at.ac.tuwien.kr.alpha.api.terms.ArithmeticTerm) Term(at.ac.tuwien.kr.alpha.api.terms.Term) ArithmeticTerm(at.ac.tuwien.kr.alpha.api.terms.ArithmeticTerm) ComparisonLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.ComparisonLiteral)

Example 33 with Literal

use of at.ac.tuwien.kr.alpha.api.programs.literals.Literal in project Alpha by alpha-asp.

the class AggregateOperatorNormalizationTest method assertOperatorNormalized.

private static void assertOperatorNormalized(Rule<Head> rewrittenRule, ComparisonOperator expectedRewrittenOperator, boolean expectedRewrittenLiteralPositive) {
    AggregateLiteral rewrittenAggregate = null;
    for (Literal lit : rewrittenRule.getBody()) {
        if (lit instanceof AggregateLiteral) {
            rewrittenAggregate = (AggregateLiteral) lit;
        }
    }
    assertNotNull(rewrittenAggregate);
    assertEquals(expectedRewrittenOperator, rewrittenAggregate.getAtom().getLowerBoundOperator());
    assertTrue(expectedRewrittenLiteralPositive == !rewrittenAggregate.isNegated());
}
Also used : AggregateLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.AggregateLiteral) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) ComparisonLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.ComparisonLiteral) AggregateLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.AggregateLiteral)

Example 34 with Literal

use of at.ac.tuwien.kr.alpha.api.programs.literals.Literal 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 35 with Literal

use of at.ac.tuwien.kr.alpha.api.programs.literals.Literal in project Alpha by alpha-asp.

the class AggregateRewritingRuleAnalysisTest method bindingAggregateGlobalsNotIncluded.

@Test
public void bindingAggregateGlobalsNotIncluded() {
    AggregateRewritingRuleAnalysis analysis = analyze(BINDING_AGGREGATES_NO_GLOBALS_INCLUDED);
    assertEquals(2, analysis.globalVariablesPerAggregate.size());
    assertEquals(2, analysis.dependenciesPerAggregate.size());
    // Check that the #max aggregate does not include "not p(X)" as its dependency.
    for (Map.Entry<AggregateLiteral, Set<Literal>> aggregateDependencies : analysis.dependenciesPerAggregate.entrySet()) {
        if (aggregateDependencies.getKey().getAtom().getAggregateFunction() == AggregateFunctionSymbol.MAX) {
            for (Literal dependency : aggregateDependencies.getValue()) {
                assertFalse(dependency.isNegated());
            }
        }
    }
}
Also used : Set(java.util.Set) 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) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Aggregations

Literal (at.ac.tuwien.kr.alpha.api.programs.literals.Literal)82 Test (org.junit.jupiter.api.Test)42 VariableTerm (at.ac.tuwien.kr.alpha.api.terms.VariableTerm)20 ArrayList (java.util.ArrayList)20 AggregateLiteral (at.ac.tuwien.kr.alpha.api.programs.literals.AggregateLiteral)17 Atom (at.ac.tuwien.kr.alpha.api.programs.atoms.Atom)16 Substitution (at.ac.tuwien.kr.alpha.api.grounder.Substitution)14 Predicate (at.ac.tuwien.kr.alpha.api.programs.Predicate)14 BasicAtom (at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom)14 BasicSubstitution (at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution)13 Map (java.util.Map)12 Head (at.ac.tuwien.kr.alpha.api.rules.heads.Head)11 CompiledProgram (at.ac.tuwien.kr.alpha.core.programs.CompiledProgram)11 CompiledRule (at.ac.tuwien.kr.alpha.core.rules.CompiledRule)10 HashSet (java.util.HashSet)10 ComparisonLiteral (at.ac.tuwien.kr.alpha.api.programs.literals.ComparisonLiteral)9 Instance (at.ac.tuwien.kr.alpha.commons.substitutions.Instance)9 LinkedHashSet (java.util.LinkedHashSet)8 List (java.util.List)8 Term (at.ac.tuwien.kr.alpha.api.terms.Term)7