Search in sources :

Example 11 with Literal

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

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

the class AggregateRewritingRuleAnalysis method findGlobalVariablesPerAggregate.

private void findGlobalVariablesPerAggregate() {
    // First, compute all global variables, that is all variables occurring in a rule except those occurring
    // inside aggregate elements.
    Set<VariableTerm> globalVariables = new HashSet<>();
    if (!rule.isConstraint()) {
        // Head must be normal at this point.
        NormalHead head = (NormalHead) rule.getHead();
        globalVariables.addAll(head.getAtom().getOccurringVariables());
    }
    for (Literal literal : rule.getBody()) {
        if (literal instanceof AggregateLiteral) {
            aggregatesInRule.add((AggregateLiteral) literal);
            AggregateAtom aggregateAtom = (AggregateAtom) literal.getAtom();
            // All variables in the bounds of an aggregate are also global variables.
            // Note that at this point, only lower bounds appear in aggregates.
            globalVariables.addAll(aggregateAtom.getLowerBoundTerm().getOccurringVariables());
        } else {
            globalVariables.addAll(literal.getOccurringVariables());
        }
    }
    // Second, compute for each aggregate those of its variables that are global.
    for (AggregateLiteral aggregateLiteral : aggregatesInRule) {
        Set<VariableTerm> globalVariablesInAggregate = new HashSet<>();
        for (VariableTerm aggregateVariable : aggregateLiteral.getAtom().getAggregateVariables()) {
            if (globalVariables.contains(aggregateVariable)) {
                globalVariablesInAggregate.add(aggregateVariable);
            }
        }
        globalVariablesPerAggregate.put(aggregateLiteral, globalVariablesInAggregate);
    }
}
Also used : NormalHead(at.ac.tuwien.kr.alpha.api.rules.heads.NormalHead) 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) AggregateAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.AggregateAtom) HashSet(java.util.HashSet)

Example 13 with Literal

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

the class AggregateRewritingRuleAnalysis method analyzeRuleDependencies.

private void analyzeRuleDependencies() {
    for (AggregateLiteral lit : globalVariablesPerAggregate.keySet()) {
        Set<VariableTerm> nonBindingVars = new HashSet<>(globalVariablesPerAggregate.get(lit));
        Term leftHandTerm = lit.getAtom().getLowerBoundTerm();
        if (lit.getBindingVariables().isEmpty() && leftHandTerm instanceof VariableTerm) {
            /*
				 * If the "left-hand" term LT of the literal is a variable and not binding, it has to be non-binding,
				 * i.e. the aggregate literal depends on the literals binding LT.
				 */
            nonBindingVars.add((VariableTerm) leftHandTerm);
        }
        Set<Literal> dependencies = new HashSet<>();
        Set<Literal> bodyWithoutLit = SetUtils.difference(rule.getBody(), Collections.singleton(lit));
        findBindingLiterals(nonBindingVars, new HashSet<>(), dependencies, bodyWithoutLit, globalVariablesPerAggregate);
        dependenciesPerAggregate.put(lit, dependencies);
    }
}
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) Term(at.ac.tuwien.kr.alpha.api.terms.Term) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) HashSet(java.util.HashSet)

Example 14 with Literal

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

the class AnalyzeUnjustifiedTest method justifyLargerRules.

@Test
public void justifyLargerRules() {
    String program = "p(X) :- q(X,Y), r(Y), not s(X,Y)." + "{ q(1,X)} :- dom(X)." + "dom(1..3)." + "{r(X)} :- p(X)." + "{r(2)}." + "{s(1,2)}." + ":- not p(1).";
    CompiledProgram internalProgram = parseAndPreprocess.apply(program);
    AtomStore atomStore = new AtomStoreImpl();
    NaiveGrounder grounder = new NaiveGrounder(internalProgram, atomStore, true);
    grounder.getNoGoods(null);
    TrailAssignment assignment = new TrailAssignment(atomStore);
    Atom p1 = parser.parse("p(1).").getFacts().get(0);
    Atom r2 = parser.parse("r(2).").getFacts().get(0);
    Atom s12 = parser.parse("s(1,2).").getFacts().get(0);
    Atom q11 = parser.parse("q(1,1).").getFacts().get(0);
    Atom q12 = parser.parse("q(1,2).").getFacts().get(0);
    Atom q13 = parser.parse("q(1,3).").getFacts().get(0);
    int p1Id = atomStore.get(p1);
    int r2Id = atomStore.get(r2);
    int s12Id = atomStore.get(s12);
    int q11Id = atomStore.get(q11);
    int q12Id = atomStore.get(q12);
    int q13Id = atomStore.get(q13);
    assignment.growForMaxAtomId();
    assignment.assign(p1Id, ThriceTruth.MBT);
    assignment.assign(r2Id, ThriceTruth.TRUE);
    assignment.assign(s12Id, ThriceTruth.TRUE);
    assignment.assign(q11Id, ThriceTruth.TRUE);
    assignment.assign(q12Id, ThriceTruth.TRUE);
    assignment.assign(q13Id, ThriceTruth.FALSE);
    Set<Literal> reasons = grounder.justifyAtom(p1Id, assignment);
    assertFalse(reasons.isEmpty());
}
Also used : NaiveGrounder(at.ac.tuwien.kr.alpha.core.grounder.NaiveGrounder) AtomStore(at.ac.tuwien.kr.alpha.core.common.AtomStore) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) AtomStoreImpl(at.ac.tuwien.kr.alpha.core.common.AtomStoreImpl) CompiledProgram(at.ac.tuwien.kr.alpha.core.programs.CompiledProgram) TrailAssignment(at.ac.tuwien.kr.alpha.core.solver.TrailAssignment) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) Test(org.junit.jupiter.api.Test)

Example 15 with Literal

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

the class AnalyzeUnjustifiedTest method justifyNegatedFactsRemovedFromReasons.

@Test
public void justifyNegatedFactsRemovedFromReasons() {
    String program = "forbidden(2,9). forbidden(1,9)." + "p(X) :- q(X)." + "q(X) :- p(X)." + "q(5) :- r." + "r :- not nr, not forbidden(2,9), not forbidden(1,9)." + "nr :- not r." + ":- not p(5).";
    CompiledProgram internalProgram = parseAndPreprocess.apply(program);
    AtomStore atomStore = new AtomStoreImpl();
    NaiveGrounder grounder = new NaiveGrounder(internalProgram, atomStore, true);
    grounder.getNoGoods(null);
    TrailAssignment assignment = new TrailAssignment(atomStore);
    int rId = atomStore.get(Atoms.newBasicAtom(Predicates.getPredicate("r", 0)));
    int nrId = atomStore.get(Atoms.newBasicAtom(Predicates.getPredicate("nr", 0)));
    assignment.growForMaxAtomId();
    assignment.assign(rId, ThriceTruth.FALSE);
    assignment.assign(nrId, ThriceTruth.TRUE);
    BasicAtom p5 = Atoms.newBasicAtom(Predicates.getPredicate("p", 1), Collections.singletonList(Terms.newConstant(5)));
    assignment.assign(atomStore.get(p5), ThriceTruth.MBT);
    Set<Literal> reasons = grounder.justifyAtom(atomStore.get(p5), assignment);
    assertFalse(reasons.isEmpty());
    for (Literal literal : reasons) {
        // Check that facts are not present in justification.
        assertNotEquals(literal.getPredicate(), Predicates.getPredicate("forbidden", 2));
    }
}
Also used : NaiveGrounder(at.ac.tuwien.kr.alpha.core.grounder.NaiveGrounder) AtomStore(at.ac.tuwien.kr.alpha.core.common.AtomStore) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) AtomStoreImpl(at.ac.tuwien.kr.alpha.core.common.AtomStoreImpl) CompiledProgram(at.ac.tuwien.kr.alpha.core.programs.CompiledProgram) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) TrailAssignment(at.ac.tuwien.kr.alpha.core.solver.TrailAssignment) 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