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);
}
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);
}
Aggregations