Search in sources :

Example 1 with IntervalTerm

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

the class ParseTreeVisitor method visitTerm_interval.

@Override
public IntervalTerm visitTerm_interval(ASPCore2Parser.Term_intervalContext ctx) {
    // interval : lower = (NUMBER | VARIABLE) DOT DOT upper = (NUMBER | VARIABLE);
    ASPCore2Parser.IntervalContext ictx = ctx.interval();
    String lowerText = ictx.lower.getText();
    String upperText = ictx.upper.getText();
    Term lower = ictx.lower.getType() == ASPCore2Lexer.NUMBER ? Terms.newConstant(Integer.parseInt(lowerText)) : Terms.newVariable(lowerText);
    Term upper = ictx.upper.getType() == ASPCore2Lexer.NUMBER ? Terms.newConstant(Integer.parseInt(upperText)) : Terms.newVariable(upperText);
    return IntervalTerm.getInstance(lower, upper);
}
Also used : IntervalTerm(at.ac.tuwien.kr.alpha.commons.terms.IntervalTerm) ConstantTerm(at.ac.tuwien.kr.alpha.api.terms.ConstantTerm) Term(at.ac.tuwien.kr.alpha.api.terms.Term) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) FunctionTerm(at.ac.tuwien.kr.alpha.api.terms.FunctionTerm) ASPCore2Parser(at.ac.tuwien.kr.alpha.core.antlr.ASPCore2Parser)

Example 2 with IntervalTerm

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

the class IntervalLiteral method getIntervalSubstitutions.

private List<Substitution> getIntervalSubstitutions(Substitution partialSubstitution) {
    List<Substitution> substitutions = new ArrayList<>();
    List<Term> terms = getTerms();
    Term intervalRepresentingVariable = terms.get(1);
    IntervalTerm intervalTerm = (IntervalTerm) terms.get(0);
    // Check whether intervalRepresentingVariable is bound already.
    if (intervalRepresentingVariable instanceof VariableTerm) {
        // Still a variable, generate all elements in the interval.
        for (int i = intervalTerm.getLowerBound(); i <= intervalTerm.getUpperBound(); i++) {
            Substitution ith = new BasicSubstitution(partialSubstitution);
            ith.put((VariableTerm) intervalRepresentingVariable, Terms.newConstant(i));
            substitutions.add(ith);
        }
        return substitutions;
    } else {
        // The intervalRepresentingVariable is bound already, check if it is in the interval.
        if (!(intervalRepresentingVariable instanceof ConstantTerm) || !(((ConstantTerm<?>) intervalRepresentingVariable).getObject() instanceof Integer)) {
            // Term is not bound to an integer constant, not in the interval.
            return Collections.emptyList();
        }
        // TODO to avoid that type of unchecked cast, we may want interval terms to not extend AbstractTerm
        @SuppressWarnings("unchecked") Integer integer = ((ConstantTerm<Integer>) intervalRepresentingVariable).getObject();
        if (intervalTerm.getLowerBound() <= integer && integer <= intervalTerm.getUpperBound()) {
            return Collections.singletonList(partialSubstitution);
        }
        return Collections.emptyList();
    }
}
Also used : Substitution(at.ac.tuwien.kr.alpha.api.grounder.Substitution) BasicSubstitution(at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution) BasicSubstitution(at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution) ArrayList(java.util.ArrayList) IntervalTerm(at.ac.tuwien.kr.alpha.commons.terms.IntervalTerm) ConstantTerm(at.ac.tuwien.kr.alpha.api.terms.ConstantTerm) 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) ConstantTerm(at.ac.tuwien.kr.alpha.api.terms.ConstantTerm) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm)

Example 3 with IntervalTerm

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

the class IntervalTermToIntervalAtom method rewriteIntervalSpecifications.

/**
 * Rewrites intervals into a new variable and special IntervalAtom.
 *
 * @return true if some interval occurs in the rule.
 */
private static NormalRule rewriteIntervalSpecifications(NormalRule rule) {
    // Collect all intervals and replace them with variables.
    Map<VariableTerm, IntervalTerm> intervalReplacements = new LinkedHashMap<>();
    List<Literal> rewrittenBody = new ArrayList<>();
    for (Literal literal : rule.getBody()) {
        Literal rewrittenLiteral = rewriteLiteral(literal, intervalReplacements);
        if (rewrittenLiteral != null) {
            rewrittenBody.add(rewrittenLiteral);
        }
    }
    // Note that this cast is safe: NormalHead can only have a BasicAtom, so literalizing and getting back the Atom destroys type information,
    // but should never yield anything other than a BasicAtom
    NormalHead rewrittenHead = rule.isConstraint() ? null : Heads.newNormalHead((BasicAtom) rewriteLiteral(rule.getHead().getAtom().toLiteral(), intervalReplacements).getAtom());
    // If intervalReplacements is empty, no IntervalTerms have been found, keep rule as is.
    if (intervalReplacements.isEmpty()) {
        return rule;
    }
    // Add new IntervalAtoms representing the interval specifications.
    for (Map.Entry<VariableTerm, IntervalTerm> interval : intervalReplacements.entrySet()) {
        rewrittenBody.add(new IntervalAtom(interval.getValue(), interval.getKey()).toLiteral());
    }
    return new NormalRuleImpl(rewrittenHead, rewrittenBody);
}
Also used : IntervalAtom(at.ac.tuwien.kr.alpha.core.atoms.IntervalAtom) IntervalTerm(at.ac.tuwien.kr.alpha.commons.terms.IntervalTerm) ArrayList(java.util.ArrayList) NormalRuleImpl(at.ac.tuwien.kr.alpha.core.rules.NormalRuleImpl) LinkedHashMap(java.util.LinkedHashMap) NormalHead(at.ac.tuwien.kr.alpha.api.rules.heads.NormalHead) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) ComparisonLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.ComparisonLiteral) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 4 with IntervalTerm

use of at.ac.tuwien.kr.alpha.commons.terms.IntervalTerm 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;
}
Also used : FunctionTerm(at.ac.tuwien.kr.alpha.api.terms.FunctionTerm) ComparisonAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.ComparisonAtom) IntervalTerm(at.ac.tuwien.kr.alpha.commons.terms.IntervalTerm) ArrayList(java.util.ArrayList) 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) ComparisonLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.ComparisonLiteral) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) IntervalAtom(at.ac.tuwien.kr.alpha.core.atoms.IntervalAtom) ComparisonAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.ComparisonAtom)

Example 5 with IntervalTerm

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

the class ParserTest method parseInterval.

@Test
public void parseInterval() {
    ASPCore2Program parsedProgram = parser.parse("fact(2..5). p(X) :- q(a, 3 .. X).");
    IntervalTerm factInterval = (IntervalTerm) parsedProgram.getFacts().get(0).getTerms().get(0);
    assertTrue(factInterval.equals(IntervalTerm.getInstance(Terms.newConstant(2), Terms.newConstant(5))));
    IntervalTerm bodyInterval = (IntervalTerm) parsedProgram.getRules().get(0).getBody().stream().findFirst().get().getTerms().get(1);
    assertTrue(bodyInterval.equals(IntervalTerm.getInstance(Terms.newConstant(3), Terms.newVariable("X"))));
}
Also used : ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) IntervalTerm(at.ac.tuwien.kr.alpha.commons.terms.IntervalTerm) Test(org.junit.jupiter.api.Test)

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