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