Search in sources :

Example 6 with VariableTerm

use of at.ac.tuwien.kr.alpha.api.terms.VariableTerm in project Alpha by alpha-asp.

the class AggregateOperatorNormalization method createPlusOneTerm.

/**
 * Creates a new {@link Literal} that assigns the given target variable to the given (integer) term plus one.
 *
 * @param term
 * @param targetVariable
 * @return
 */
private static Literal createPlusOneTerm(Term term, VariableTerm targetVariable) {
    Term increment = Terms.newArithmeticTerm(term, ArithmeticOperator.PLUS, Terms.newConstant(1));
    ComparisonAtom atom = Atoms.newComparisonAtom(targetVariable, increment, ComparisonOperators.EQ);
    return atom.toLiteral();
}
Also used : ComparisonAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.ComparisonAtom) Term(at.ac.tuwien.kr.alpha.api.terms.Term) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm)

Example 7 with VariableTerm

use of at.ac.tuwien.kr.alpha.api.terms.VariableTerm in project Alpha by alpha-asp.

the class IntervalTermToIntervalAtom method rewriteLiteral.

/**
 * Replaces every IntervalTerm by a new variable and returns a mapping of the replaced VariableTerm -> IntervalTerm.
 *
 * @return the rewritten literal or null if the literal should be dropped from the final rule.
 */
private static Literal rewriteLiteral(Literal lit, Map<VariableTerm, IntervalTerm> intervalReplacement) {
    // final rule.
    if (lit instanceof ComparisonLiteral && ((ComparisonLiteral) lit).isNormalizedEquality()) {
        ComparisonAtom equalityLiteral = (ComparisonAtom) lit.getAtom();
        if (equalityLiteral.getTerms().get(0) instanceof VariableTerm && equalityLiteral.getTerms().get(1) instanceof IntervalTerm) {
            // Literal is of the form "X = A .. B".
            intervalReplacement.put((VariableTerm) equalityLiteral.getTerms().get(0), (IntervalTerm) equalityLiteral.getTerms().get(1));
            return null;
        }
        if (equalityLiteral.getTerms().get(1) instanceof VariableTerm && equalityLiteral.getTerms().get(0) instanceof IntervalTerm) {
            // Literal is of the form "A .. B = X".
            intervalReplacement.put((VariableTerm) equalityLiteral.getTerms().get(1), (IntervalTerm) equalityLiteral.getTerms().get(0));
            return null;
        }
    }
    Atom atom = lit.getAtom();
    List<Term> termList = new ArrayList<>(atom.getTerms());
    boolean didChange = false;
    for (int i = 0; i < termList.size(); i++) {
        Term term = termList.get(i);
        if (term instanceof IntervalTerm) {
            VariableTerm replacementVariable = Terms.newVariable(INTERVAL_VARIABLE_PREFIX + intervalReplacement.size());
            intervalReplacement.put(replacementVariable, (IntervalTerm) term);
            termList.set(i, replacementVariable);
            didChange = true;
        }
        if (term instanceof FunctionTerm) {
            // Rewrite function terms recursively.
            FunctionTerm rewrittenFunctionTerm = rewriteFunctionTerm((FunctionTerm) term, intervalReplacement);
            termList.set(i, rewrittenFunctionTerm);
            didChange = true;
        }
    }
    if (didChange) {
        Atom rewrittenAtom = atom.withTerms(termList);
        return lit.isNegated() ? rewrittenAtom.toLiteral().negate() : rewrittenAtom.toLiteral();
    }
    return lit;
}
Also used : FunctionTerm(at.ac.tuwien.kr.alpha.api.terms.FunctionTerm) ComparisonAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.ComparisonAtom) IntervalTerm(at.ac.tuwien.kr.alpha.commons.terms.IntervalTerm) ArrayList(java.util.ArrayList) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) Term(at.ac.tuwien.kr.alpha.api.terms.Term) IntervalTerm(at.ac.tuwien.kr.alpha.commons.terms.IntervalTerm) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) FunctionTerm(at.ac.tuwien.kr.alpha.api.terms.FunctionTerm) ComparisonLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.ComparisonLiteral) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) IntervalAtom(at.ac.tuwien.kr.alpha.core.atoms.IntervalAtom) ComparisonAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.ComparisonAtom)

Example 8 with VariableTerm

use of at.ac.tuwien.kr.alpha.api.terms.VariableTerm 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 9 with VariableTerm

use of at.ac.tuwien.kr.alpha.api.terms.VariableTerm 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 10 with VariableTerm

use of at.ac.tuwien.kr.alpha.api.terms.VariableTerm in project Alpha by alpha-asp.

the class ArithmeticTermsRewritingTest method rewriteRule.

@Test
public void rewriteRule() {
    NormalProgram inputProgram = NormalProgramImpl.fromInputProgram(parser.parse("p(X+1) :- q(Y/2), r(f(X*2),Y), X-2 = Y*3, X = 0..9."));
    assertEquals(1, inputProgram.getRules().size());
    ArithmeticTermsRewriting arithmeticTermsRewriting = new ArithmeticTermsRewriting();
    NormalProgram rewrittenProgram = arithmeticTermsRewriting.apply(inputProgram);
    // Expect the rewritten program to be one rule with: p(_A0) :- _A0 = X+1,  _A1 = Y/2, q(_A1), _A2 = X*2, r(f(_A2),Y), X-2 = Y*3, X = 0..9.
    assertEquals(1, rewrittenProgram.getRules().size());
    NormalRule rewrittenRule = rewrittenProgram.getRules().get(0);
    assertTrue(rewrittenRule.getHeadAtom().getTerms().get(0) instanceof VariableTerm);
    assertEquals(7, rewrittenRule.getBody().size());
}
Also used : NormalProgram(at.ac.tuwien.kr.alpha.api.programs.NormalProgram) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) NormalRule(at.ac.tuwien.kr.alpha.api.rules.NormalRule) Test(org.junit.jupiter.api.Test)

Aggregations

VariableTerm (at.ac.tuwien.kr.alpha.api.terms.VariableTerm)45 Term (at.ac.tuwien.kr.alpha.api.terms.Term)25 Literal (at.ac.tuwien.kr.alpha.api.programs.literals.Literal)18 Test (org.junit.jupiter.api.Test)13 ArrayList (java.util.ArrayList)12 HashSet (java.util.HashSet)10 BasicSubstitution (at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution)9 AggregateLiteral (at.ac.tuwien.kr.alpha.api.programs.literals.AggregateLiteral)8 Substitution (at.ac.tuwien.kr.alpha.api.grounder.Substitution)6 ArithmeticTerm (at.ac.tuwien.kr.alpha.api.terms.ArithmeticTerm)6 ConstantTerm (at.ac.tuwien.kr.alpha.api.terms.ConstantTerm)6 FunctionTerm (at.ac.tuwien.kr.alpha.api.terms.FunctionTerm)6 Unifier (at.ac.tuwien.kr.alpha.commons.substitutions.Unifier)6 IntervalTerm (at.ac.tuwien.kr.alpha.commons.terms.IntervalTerm)6 Map (java.util.Map)6 Atom (at.ac.tuwien.kr.alpha.api.programs.atoms.Atom)5 NormalHead (at.ac.tuwien.kr.alpha.api.rules.heads.NormalHead)5 Predicate (at.ac.tuwien.kr.alpha.api.programs.Predicate)4 AggregateAtom (at.ac.tuwien.kr.alpha.api.programs.atoms.AggregateAtom)4 BasicAtom (at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom)4