use of at.ac.tuwien.kr.alpha.api.terms.Term in project Alpha by alpha-asp.
the class ParserTest method cardinalityAggregate.
@Test
public void cardinalityAggregate() {
ASPCore2Program parsedProgram = parser.parse("num(K) :- K <= #count {X,Y,Z : p(X,Y,Z) }, dom(K).");
Optional<Literal> optionalBodyElement = parsedProgram.getRules().get(0).getBody().stream().filter((lit) -> lit instanceof AggregateLiteral).findFirst();
assertTrue(optionalBodyElement.isPresent());
Literal bodyElement = optionalBodyElement.get();
AggregateLiteral parsedAggregate = (AggregateLiteral) bodyElement;
VariableTerm x = Terms.newVariable("X");
VariableTerm y = Terms.newVariable("Y");
VariableTerm z = Terms.newVariable("Z");
List<Term> basicTerms = Arrays.asList(x, y, z);
AggregateAtom.AggregateElement aggregateElement = Atoms.newAggregateElement(basicTerms, Collections.singletonList(Atoms.newBasicAtom(Predicates.getPredicate("p", 3), x, y, z).toLiteral()));
AggregateAtom expectedAggregate = Atoms.newAggregateAtom(ComparisonOperators.LE, Terms.newVariable("K"), null, null, AggregateAtom.AggregateFunctionSymbol.COUNT, Collections.singletonList(aggregateElement));
assertEquals(expectedAggregate, parsedAggregate.getAtom());
}
use of at.ac.tuwien.kr.alpha.api.terms.Term in project Alpha by alpha-asp.
the class ArithmeticTermsRewriting method rewriteArithmeticSubterms.
private Term rewriteArithmeticSubterms(Term term, List<Literal> bodyLiterals) {
// Keep term as-is if it contains no ArithmeticTerm.
if (!containsArithmeticTerm(term)) {
return term;
}
// Switch on term type.
if (term instanceof ArithmeticTerm) {
VariableTerm replacementVariable = Terms.newVariable(ARITHMETIC_VARIABLES_PREFIX + numArithmeticVariables++);
bodyLiterals.add(Atoms.newComparisonAtom(replacementVariable, term, ComparisonOperators.EQ).toLiteral());
return replacementVariable;
} else if (term instanceof VariableTerm || term instanceof ConstantTerm) {
return term;
} else if (term instanceof FunctionTerm) {
List<Term> termList = ((FunctionTerm) term).getTerms();
List<Term> rewrittenTermList = new ArrayList<>();
for (Term subterm : termList) {
rewrittenTermList.add(rewriteArithmeticSubterms(subterm, bodyLiterals));
}
return Terms.newFunctionTerm(((FunctionTerm) term).getSymbol(), rewrittenTermList);
} else {
throw Util.oops("Rewriting unknown Term type: " + term.getClass());
}
}
use of at.ac.tuwien.kr.alpha.api.terms.Term 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.Term 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.Term 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);
}
}
Aggregations