Search in sources :

Example 51 with Literal

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

the class AggregateRewritingRuleAnalysisTest method nonBindingAggregateNoGlobals2.

@Test
public void nonBindingAggregateNoGlobals2() {
    AggregateRewritingRuleAnalysis analysis = analyze(NONBINDING_AGGREGATE_NO_GLOBALS_2);
    assertEquals(1, analysis.globalVariablesPerAggregate.size());
    assertEquals(1, analysis.dependenciesPerAggregate.size());
    AggregateLiteral aggregate = new ArrayList<>(analysis.globalVariablesPerAggregate.keySet()).get(0);
    assertTrue(analysis.globalVariablesPerAggregate.get(aggregate).isEmpty());
    assertFalse(analysis.dependenciesPerAggregate.get(aggregate).isEmpty());
    Set<Literal> dependencies = analysis.dependenciesPerAggregate.get(aggregate);
    assertEquals(3, dependencies.size());
    Literal threePlusY = Literals.fromAtom(Atoms.newComparisonAtom(Terms.newVariable("X"), Terms.newArithmeticTerm(Terms.newConstant(3), ArithmeticOperator.PLUS, Terms.newVariable("Y")), ComparisonOperators.EQ), true);
    assertTrue(dependencies.contains(threePlusY));
    Literal zPlusFour = Literals.fromAtom(Atoms.newComparisonAtom(Terms.newVariable("Y"), Terms.newArithmeticTerm(Terms.newVariable("Z"), ArithmeticOperator.PLUS, Terms.newConstant(4)), ComparisonOperators.EQ), true);
    assertTrue(dependencies.contains(zPlusFour));
    Literal rSZ = Literals.fromAtom(Atoms.newBasicAtom(Predicates.getPredicate("r", 2), Terms.newVariable("S"), Terms.newVariable("Z")), true);
    assertTrue(dependencies.contains(rSZ));
}
Also used : 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) Test(org.junit.jupiter.api.Test)

Example 52 with Literal

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

the class AggregateRewritingRuleAnalysisTest method bindingAggregateWithGlobals1.

@Test
public void bindingAggregateWithGlobals1() {
    AggregateRewritingRuleAnalysis analysis = analyze(BINDING_AGGREGATE_WITH_GLOBALS_1);
    assertEquals(1, analysis.globalVariablesPerAggregate.size());
    assertEquals(1, analysis.dependenciesPerAggregate.size());
    AggregateLiteral aggregate = new ArrayList<>(analysis.globalVariablesPerAggregate.keySet()).get(0);
    assertFalse(analysis.globalVariablesPerAggregate.get(aggregate).isEmpty());
    assertFalse(analysis.dependenciesPerAggregate.get(aggregate).isEmpty());
    Set<VariableTerm> globalVars = analysis.globalVariablesPerAggregate.get(aggregate);
    assertTrue(globalVars.contains(Terms.newVariable("G")));
    assertTrue(globalVars.contains(Terms.newVariable("V")));
    Set<Literal> dependencies = analysis.dependenciesPerAggregate.get(aggregate);
    assertEquals(2, dependencies.size());
    Literal graph = Literals.fromAtom(Atoms.newBasicAtom(Predicates.getPredicate("graph", 1), Terms.newVariable("G")), true);
    assertTrue(dependencies.contains(graph));
    Literal graphVertex = Literals.fromAtom(Atoms.newBasicAtom(Predicates.getPredicate("graph_vertex", 2), Terms.newVariable("G"), Terms.newVariable("V")), true);
    assertTrue(dependencies.contains(graphVertex));
}
Also used : 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) Test(org.junit.jupiter.api.Test)

Example 53 with Literal

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

the class PredicateInternalizer method makePrefixedPredicatesInternal.

public static Rule<Head> makePrefixedPredicatesInternal(Rule<Head> rule, String prefix) {
    Head newHead = null;
    if (rule.getHead() != null) {
        if (!(rule.getHead() instanceof NormalHead)) {
            throw new UnsupportedOperationException("Cannot make predicates in rules internal whose head is not normal.");
        }
        NormalHead head = (NormalHead) rule.getHead();
        if (head.getAtom().getPredicate().getName().startsWith(prefix)) {
            newHead = Heads.newNormalHead(makePredicateInternal(head.getAtom()));
        } else {
            newHead = head;
        }
    }
    List<Literal> newBody = new ArrayList<>();
    for (Literal bodyElement : rule.getBody()) {
        // Only rewrite BasicAtoms.
        if (bodyElement instanceof BasicLiteral) {
            if (bodyElement.getAtom().getPredicate().getName().startsWith(prefix)) {
                newBody.add(makePredicateInternal((BasicAtom) bodyElement.getAtom()).toLiteral(!bodyElement.isNegated()));
            } else {
                newBody.add(bodyElement);
            }
        } else {
            // Keep other body element as is.
            newBody.add(bodyElement);
        }
    }
    return new BasicRule(newHead, newBody);
}
Also used : BasicRule(at.ac.tuwien.kr.alpha.core.rules.BasicRule) BasicLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.BasicLiteral) Head(at.ac.tuwien.kr.alpha.api.rules.heads.Head) NormalHead(at.ac.tuwien.kr.alpha.api.rules.heads.NormalHead) NormalHead(at.ac.tuwien.kr.alpha.api.rules.heads.NormalHead) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) BasicLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.BasicLiteral) ArrayList(java.util.ArrayList)

Example 54 with Literal

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

the class StratifiedEvaluation method calculateSatisfyingSubstitutionsForRule.

private List<Substitution> calculateSatisfyingSubstitutionsForRule(CompiledRule rule, boolean checkAllStartingLiterals) {
    LOGGER.debug("Grounding rule {}", rule);
    RuleGroundingInfo groundingOrders = rule.getGroundingInfo();
    // Treat rules with fixed instantiation first.
    LOGGER.debug("Is fixed rule? {}", rule.getGroundingInfo().hasFixedInstantiation());
    if (groundingOrders.hasFixedInstantiation()) {
        RuleGroundingOrder fixedGroundingOrder = groundingOrders.getFixedGroundingOrder();
        return calcSubstitutionsWithGroundingOrder(fixedGroundingOrder, Collections.singletonList(new BasicSubstitution()));
    }
    List<Literal> startingLiterals = groundingOrders.getStartingLiterals();
    // Check only one starting literal if indicated by the parameter.
    if (!checkAllStartingLiterals) {
        // If this is the first evaluation run, it suffices to start from the first starting literal only.
        Literal lit = startingLiterals.get(0);
        return calcSubstitutionsWithGroundingOrder(groundingOrders.orderStartingFrom(lit), substituteFromRecentlyAddedInstances(lit));
    }
    // Ground from all starting literals.
    // Collection of full ground substitutions for the given rule.
    List<Substitution> groundSubstitutions = new ArrayList<>();
    for (Literal lit : startingLiterals) {
        List<Substitution> substitutionsForStartingLiteral = calcSubstitutionsWithGroundingOrder(groundingOrders.orderStartingFrom(lit), substituteFromRecentlyAddedInstances(lit));
        groundSubstitutions.addAll(substitutionsForStartingLiteral);
    }
    return groundSubstitutions;
}
Also used : Substitution(at.ac.tuwien.kr.alpha.api.grounder.Substitution) BasicSubstitution(at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution) BasicSubstitution(at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) ArrayList(java.util.ArrayList) RuleGroundingOrder(at.ac.tuwien.kr.alpha.core.grounder.RuleGroundingOrder) RuleGroundingInfo(at.ac.tuwien.kr.alpha.core.grounder.RuleGroundingInfo)

Example 55 with Literal

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

the class StratifiedEvaluation method prepareInitialEvaluation.

/**
 * To be called at the start of evaluateComponent. Adds all known instances of the predicates occurring in the given set
 * of rules to the "modifiedInLastEvaluationRun" map in order to "bootstrap" incremental grounding, i.e. making sure
 * that those instances are taken into account for ground substitutions by evaluateRule.
 */
private void prepareInitialEvaluation(Set<CompiledRule> rulesToEvaluate) {
    modifiedInLastEvaluationRun = new HashMap<>();
    for (CompiledRule rule : rulesToEvaluate) {
        // Register rule head instances.
        Predicate headPredicate = rule.getHeadAtom().getPredicate();
        IndexedInstanceStorage headInstances = workingMemory.get(headPredicate, true);
        modifiedInLastEvaluationRun.putIfAbsent(headPredicate, new LinkedHashSet<>());
        if (headInstances != null) {
            modifiedInLastEvaluationRun.get(headPredicate).addAll(headInstances.getAllInstances());
        }
        // Register positive body literal instances.
        for (Literal lit : rule.getPositiveBody()) {
            Predicate bodyPredicate = lit.getPredicate();
            IndexedInstanceStorage bodyInstances = workingMemory.get(bodyPredicate, true);
            modifiedInLastEvaluationRun.putIfAbsent(bodyPredicate, new LinkedHashSet<>());
            if (bodyInstances != null) {
                modifiedInLastEvaluationRun.get(bodyPredicate).addAll(bodyInstances.getAllInstances());
            }
        }
    }
}
Also used : CompiledRule(at.ac.tuwien.kr.alpha.core.rules.CompiledRule) IndexedInstanceStorage(at.ac.tuwien.kr.alpha.core.grounder.IndexedInstanceStorage) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate)

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