Search in sources :

Example 6 with IntervalTerm

use of at.ac.tuwien.kr.alpha.commons.terms.IntervalTerm in project Alpha by alpha-asp.

the class FactIntervalEvaluator method unrollInstances.

private static List<Instance> unrollInstances(Term[] currentTerms, int currentPosition) {
    if (currentPosition == currentTerms.length) {
        return Collections.singletonList(new Instance(currentTerms));
    }
    Term currentTerm = currentTerms[currentPosition];
    if (!(currentTerm instanceof IntervalTerm)) {
        return unrollInstances(currentTerms, currentPosition + 1);
    }
    List<Instance> instances = new ArrayList<>();
    int lower = ((IntervalTerm) currentTerm).getLowerBound();
    int upper = ((IntervalTerm) currentTerm).getUpperBound();
    for (int i = lower; i <= upper; i++) {
        Term[] clonedTerms = currentTerms.clone();
        clonedTerms[currentPosition] = Terms.newConstant(i);
        instances.addAll(unrollInstances(clonedTerms, currentPosition + 1));
    }
    return instances;
}
Also used : Instance(at.ac.tuwien.kr.alpha.commons.substitutions.Instance) IntervalTerm(at.ac.tuwien.kr.alpha.commons.terms.IntervalTerm) ArrayList(java.util.ArrayList) Term(at.ac.tuwien.kr.alpha.api.terms.Term) IntervalTerm(at.ac.tuwien.kr.alpha.commons.terms.IntervalTerm) FunctionTerm(at.ac.tuwien.kr.alpha.api.terms.FunctionTerm)

Example 7 with IntervalTerm

use of at.ac.tuwien.kr.alpha.commons.terms.IntervalTerm in project Alpha by alpha-asp.

the class FactIntervalEvaluator method constructFactInstances.

/**
 * Helper to construct Instances from a fact that may contain intervals.
 *
 * @param fact the fact potentially containing intervals.
 * @return all instances stemming from unfolding the intervals.
 */
public static List<Instance> constructFactInstances(Atom fact) {
    // Construct instance(s) from the fact.
    int arity = fact.getPredicate().getArity();
    Term[] currentTerms = new Term[arity];
    boolean containsIntervals = false;
    // Check if instance contains intervals at all.
    for (int i = 0; i < arity; i++) {
        Term term = fact.getTerms().get(i);
        currentTerms[i] = term;
        if (term instanceof IntervalTerm) {
            containsIntervals = true;
        } else if (term instanceof FunctionTerm && IntervalTerm.functionTermContainsIntervals((FunctionTerm) term)) {
            containsIntervals = true;
            throw new UnsupportedOperationException("Intervals inside function terms in facts are not supported yet. Try turning the fact into a rule.");
        }
    }
    // If fact contains no intervals, simply return the single instance.
    if (!containsIntervals) {
        return Collections.singletonList(new Instance(currentTerms));
    }
    // Fact contains intervals, unroll them all.
    return unrollInstances(currentTerms, 0);
}
Also used : FunctionTerm(at.ac.tuwien.kr.alpha.api.terms.FunctionTerm) Instance(at.ac.tuwien.kr.alpha.commons.substitutions.Instance) IntervalTerm(at.ac.tuwien.kr.alpha.commons.terms.IntervalTerm) Term(at.ac.tuwien.kr.alpha.api.terms.Term) IntervalTerm(at.ac.tuwien.kr.alpha.commons.terms.IntervalTerm) FunctionTerm(at.ac.tuwien.kr.alpha.api.terms.FunctionTerm)

Example 8 with IntervalTerm

use of at.ac.tuwien.kr.alpha.commons.terms.IntervalTerm 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)

Aggregations

IntervalTerm (at.ac.tuwien.kr.alpha.commons.terms.IntervalTerm)8 Term (at.ac.tuwien.kr.alpha.api.terms.Term)6 FunctionTerm (at.ac.tuwien.kr.alpha.api.terms.FunctionTerm)5 VariableTerm (at.ac.tuwien.kr.alpha.api.terms.VariableTerm)5 ArrayList (java.util.ArrayList)5 BasicAtom (at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom)2 ComparisonLiteral (at.ac.tuwien.kr.alpha.api.programs.literals.ComparisonLiteral)2 ConstantTerm (at.ac.tuwien.kr.alpha.api.terms.ConstantTerm)2 Instance (at.ac.tuwien.kr.alpha.commons.substitutions.Instance)2 IntervalAtom (at.ac.tuwien.kr.alpha.core.atoms.IntervalAtom)2 Substitution (at.ac.tuwien.kr.alpha.api.grounder.Substitution)1 ASPCore2Program (at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program)1 Atom (at.ac.tuwien.kr.alpha.api.programs.atoms.Atom)1 ComparisonAtom (at.ac.tuwien.kr.alpha.api.programs.atoms.ComparisonAtom)1 Literal (at.ac.tuwien.kr.alpha.api.programs.literals.Literal)1 NormalHead (at.ac.tuwien.kr.alpha.api.rules.heads.NormalHead)1 BasicSubstitution (at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution)1 ASPCore2Parser (at.ac.tuwien.kr.alpha.core.antlr.ASPCore2Parser)1 NormalRuleImpl (at.ac.tuwien.kr.alpha.core.rules.NormalRuleImpl)1 LinkedHashMap (java.util.LinkedHashMap)1