Search in sources :

Example 1 with NormalRuleImpl

use of at.ac.tuwien.kr.alpha.core.rules.NormalRuleImpl 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 2 with NormalRuleImpl

use of at.ac.tuwien.kr.alpha.core.rules.NormalRuleImpl in project Alpha by alpha-asp.

the class ArithmeticTermsRewriting method rewriteRule.

/**
 * Takes a normal rule and rewrites it such that {@link ArithmeticTerm}s only appear inside {@link at.ac.tuwien.kr.alpha.common.atoms.ComparisonLiteral}s.
 *
 * @param inputProgramRule the rule to rewrite.
 * @return the rewritten rule. Note that a new {@link NormalRule} is returned for every call of this method.
 */
private NormalRule rewriteRule(NormalRule inputProgramRule) {
    // Reset number of introduced variables for each rule.
    numArithmeticVariables = 0;
    NormalHead rewrittenHead = null;
    List<Literal> rewrittenBodyLiterals = new ArrayList<>();
    // Rewrite head.
    if (!inputProgramRule.isConstraint()) {
        BasicAtom headAtom = inputProgramRule.getHeadAtom();
        if (containsArithmeticTermsToRewrite(headAtom)) {
            rewrittenHead = Heads.newNormalHead((BasicAtom) rewriteAtom(headAtom, rewrittenBodyLiterals));
        } else {
            rewrittenHead = inputProgramRule.getHead();
        }
    }
    // Rewrite body.
    for (Literal literal : inputProgramRule.getBody()) {
        if (!containsArithmeticTermsToRewrite(literal.getAtom())) {
            // Keep body literal as-is if no ArithmeticTerm occurs.
            rewrittenBodyLiterals.add(literal);
            continue;
        }
        rewrittenBodyLiterals.add(rewriteAtom(literal.getAtom(), rewrittenBodyLiterals).toLiteral(!literal.isNegated()));
    }
    return new NormalRuleImpl(rewrittenHead, rewrittenBodyLiterals);
}
Also used : NormalHead(at.ac.tuwien.kr.alpha.api.rules.heads.NormalHead) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) ArrayList(java.util.ArrayList) NormalRuleImpl(at.ac.tuwien.kr.alpha.core.rules.NormalRuleImpl) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom)

Aggregations

BasicAtom (at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom)2 Literal (at.ac.tuwien.kr.alpha.api.programs.literals.Literal)2 NormalHead (at.ac.tuwien.kr.alpha.api.rules.heads.NormalHead)2 NormalRuleImpl (at.ac.tuwien.kr.alpha.core.rules.NormalRuleImpl)2 ArrayList (java.util.ArrayList)2 ComparisonLiteral (at.ac.tuwien.kr.alpha.api.programs.literals.ComparisonLiteral)1 VariableTerm (at.ac.tuwien.kr.alpha.api.terms.VariableTerm)1 IntervalTerm (at.ac.tuwien.kr.alpha.commons.terms.IntervalTerm)1 IntervalAtom (at.ac.tuwien.kr.alpha.core.atoms.IntervalAtom)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1