use of at.ac.tuwien.kr.alpha.api.terms.FunctionTerm in project Alpha by alpha-asp.
the class IntervalTermToIntervalAtom method rewriteFunctionTerm.
private static FunctionTerm rewriteFunctionTerm(FunctionTerm functionTerm, Map<VariableTerm, IntervalTerm> intervalReplacement) {
List<Term> termList = new ArrayList<>(functionTerm.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" + intervalReplacement.size());
intervalReplacement.put(replacementVariable, (IntervalTerm) term);
termList.set(i, replacementVariable);
didChange = true;
}
if (term instanceof FunctionTerm) {
// Recursively rewrite function terms.
FunctionTerm rewrittenFunctionTerm = rewriteFunctionTerm((FunctionTerm) term, intervalReplacement);
if (rewrittenFunctionTerm != term) {
termList.set(i, rewrittenFunctionTerm);
didChange = true;
}
}
}
if (didChange) {
return Terms.newFunctionTerm(functionTerm.getSymbol(), termList);
}
return functionTerm;
}
use of at.ac.tuwien.kr.alpha.api.terms.FunctionTerm in project Alpha by alpha-asp.
the class AbstractAggregateEncoder method buildElementRuleHead.
/**
* Builds a the head atom for an aggregate element encoding rule of form
* <code>HEAD :- $element_literals$, $aggregate_dependencies$</code>, e.g.
* <code>count_1_element_tuple(count_1_args(Y), X) :- p(X, Y), q(Y)</code> for the rule body
* <code>N = #count{X : p(X, Y)}, q(Y)</code>.
*
* @param aggregateId
* @param element
* @param aggregateArguments
* @return
*/
protected BasicAtom buildElementRuleHead(String aggregateId, AggregateElement element, Term aggregateArguments) {
Predicate headPredicate = Predicates.getPredicate(this.getElementTuplePredicateSymbol(aggregateId), 2);
FunctionTerm elementTuple = Terms.newFunctionTerm(ELEMENT_TUPLE_FUNCTION_SYMBOL, element.getElementTerms());
return Atoms.newBasicAtom(headPredicate, aggregateArguments, elementTuple);
}
use of at.ac.tuwien.kr.alpha.api.terms.FunctionTerm in project Alpha by alpha-asp.
the class SumEncoder method buildElementRuleHead.
/**
* In contrast to encoders for other aggregate functions, the "element tuple" atom for sum encodings is ternary - apart
* from the aggregate arguments and the variable tuple identifying the aggregated element, it also holds the value to
* add to the result sum as its third argument.
*/
@Override
protected BasicAtom buildElementRuleHead(String aggregateId, AggregateElement element, Term aggregateArguments) {
Predicate headPredicate = Predicates.getPredicate(this.getElementTuplePredicateSymbol(aggregateId), 3);
FunctionTerm elementTuple = Terms.newFunctionTerm(AbstractAggregateEncoder.ELEMENT_TUPLE_FUNCTION_SYMBOL, element.getElementTerms());
return Atoms.newBasicAtom(headPredicate, aggregateArguments, elementTuple, element.getElementTerms().get(0));
}
Aggregations