Search in sources :

Example 6 with FunctionTerm

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;
}
Also used : FunctionTerm(at.ac.tuwien.kr.alpha.api.terms.FunctionTerm) ArrayList(java.util.ArrayList) IntervalTerm(at.ac.tuwien.kr.alpha.commons.terms.IntervalTerm) 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)

Example 7 with 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);
}
Also used : FunctionTerm(at.ac.tuwien.kr.alpha.api.terms.FunctionTerm) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate)

Example 8 with FunctionTerm

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));
}
Also used : FunctionTerm(at.ac.tuwien.kr.alpha.api.terms.FunctionTerm) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate)

Aggregations

FunctionTerm (at.ac.tuwien.kr.alpha.api.terms.FunctionTerm)8 Term (at.ac.tuwien.kr.alpha.api.terms.Term)6 VariableTerm (at.ac.tuwien.kr.alpha.api.terms.VariableTerm)5 IntervalTerm (at.ac.tuwien.kr.alpha.commons.terms.IntervalTerm)4 ArrayList (java.util.ArrayList)3 Predicate (at.ac.tuwien.kr.alpha.api.programs.Predicate)2 BasicAtom (at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom)2 ArithmeticTerm (at.ac.tuwien.kr.alpha.api.terms.ArithmeticTerm)2 ConstantTerm (at.ac.tuwien.kr.alpha.api.terms.ConstantTerm)2 Instance (at.ac.tuwien.kr.alpha.commons.substitutions.Instance)2 Substitution (at.ac.tuwien.kr.alpha.api.grounder.Substitution)1 Atom (at.ac.tuwien.kr.alpha.api.programs.atoms.Atom)1 ComparisonAtom (at.ac.tuwien.kr.alpha.api.programs.atoms.ComparisonAtom)1 ComparisonLiteral (at.ac.tuwien.kr.alpha.api.programs.literals.ComparisonLiteral)1 BasicSubstitution (at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution)1 IntervalAtom (at.ac.tuwien.kr.alpha.core.atoms.IntervalAtom)1 Test (org.junit.jupiter.api.Test)1