use of at.ac.tuwien.kr.alpha.api.programs.literals.BasicLiteral in project Alpha by alpha-asp.
the class PredicateInternalizer method makePrefixedPredicatesInternal.
public static Rule<Head> makePrefixedPredicatesInternal(Rule<Head> rule, String prefix) {
Head newHead = null;
if (rule.getHead() != null) {
if (!(rule.getHead() instanceof NormalHead)) {
throw new UnsupportedOperationException("Cannot make predicates in rules internal whose head is not normal.");
}
NormalHead head = (NormalHead) rule.getHead();
if (head.getAtom().getPredicate().getName().startsWith(prefix)) {
newHead = Heads.newNormalHead(makePredicateInternal(head.getAtom()));
} else {
newHead = head;
}
}
List<Literal> newBody = new ArrayList<>();
for (Literal bodyElement : rule.getBody()) {
// Only rewrite BasicAtoms.
if (bodyElement instanceof BasicLiteral) {
if (bodyElement.getAtom().getPredicate().getName().startsWith(prefix)) {
newBody.add(makePredicateInternal((BasicAtom) bodyElement.getAtom()).toLiteral(!bodyElement.isNegated()));
} else {
newBody.add(bodyElement);
}
} else {
// Keep other body element as is.
newBody.add(bodyElement);
}
}
return new BasicRule(newHead, newBody);
}
use of at.ac.tuwien.kr.alpha.api.programs.literals.BasicLiteral in project Alpha by alpha-asp.
the class EnumerationRewriting method rewriteRules.
private List<Rule<Head>> rewriteRules(List<Rule<Head>> srcRules, Predicate enumPredicate) {
List<Rule<Head>> rewrittenRules = new ArrayList<>();
for (Rule<Head> rule : srcRules) {
if (rule.getHead() != null && !(rule.getHead() instanceof NormalHead)) {
throw oops("Encountered rule whose head is not normal: " + rule);
}
if (rule.getHead() != null && ((NormalHead) rule.getHead()).getAtom().getPredicate().equals(enumPredicate)) {
throw oops("Atom declared as enumeration atom by directive occurs in head of the rule: " + rule);
}
List<Literal> modifiedBodyLiterals = new ArrayList<>(rule.getBody());
Iterator<Literal> rit = modifiedBodyLiterals.iterator();
LinkedList<Literal> rewrittenLiterals = new LinkedList<>();
while (rit.hasNext()) {
Literal literal = rit.next();
if (!(literal instanceof BasicLiteral)) {
continue;
}
BasicLiteral basicLiteral = (BasicLiteral) literal;
if (!basicLiteral.getPredicate().equals(enumPredicate)) {
continue;
}
// basicLiteral is an enumeration literal (i.e. predicate is marked as enum using directive)
rit.remove();
Term enumIdTerm = basicLiteral.getAtom().getTerms().get(0);
Term valueTerm = basicLiteral.getAtom().getTerms().get(1);
VariableTerm indexTerm = (VariableTerm) basicLiteral.getAtom().getTerms().get(2);
rewrittenLiterals.add(new EnumerationAtom(enumIdTerm, valueTerm, indexTerm).toLiteral());
}
modifiedBodyLiterals.addAll(rewrittenLiterals);
rewrittenRules.add(new BasicRule(rule.getHead(), modifiedBodyLiterals));
}
return rewrittenRules;
}
Aggregations