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();
}
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;
}
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);
}
}
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);
}
}
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());
}
Aggregations