Search in sources :

Example 1 with BasicLiteral

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);
}
Also used : BasicRule(at.ac.tuwien.kr.alpha.core.rules.BasicRule) BasicLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.BasicLiteral) Head(at.ac.tuwien.kr.alpha.api.rules.heads.Head) NormalHead(at.ac.tuwien.kr.alpha.api.rules.heads.NormalHead) NormalHead(at.ac.tuwien.kr.alpha.api.rules.heads.NormalHead) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) BasicLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.BasicLiteral) ArrayList(java.util.ArrayList)

Example 2 with BasicLiteral

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;
}
Also used : BasicRule(at.ac.tuwien.kr.alpha.core.rules.BasicRule) Head(at.ac.tuwien.kr.alpha.api.rules.heads.Head) NormalHead(at.ac.tuwien.kr.alpha.api.rules.heads.NormalHead) ArrayList(java.util.ArrayList) Term(at.ac.tuwien.kr.alpha.api.terms.Term) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) LinkedList(java.util.LinkedList) BasicLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.BasicLiteral) NormalHead(at.ac.tuwien.kr.alpha.api.rules.heads.NormalHead) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) BasicLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.BasicLiteral) EnumerationAtom(at.ac.tuwien.kr.alpha.core.atoms.EnumerationAtom) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) Rule(at.ac.tuwien.kr.alpha.api.rules.Rule) BasicRule(at.ac.tuwien.kr.alpha.core.rules.BasicRule)

Aggregations

BasicLiteral (at.ac.tuwien.kr.alpha.api.programs.literals.BasicLiteral)2 Literal (at.ac.tuwien.kr.alpha.api.programs.literals.Literal)2 Head (at.ac.tuwien.kr.alpha.api.rules.heads.Head)2 NormalHead (at.ac.tuwien.kr.alpha.api.rules.heads.NormalHead)2 BasicRule (at.ac.tuwien.kr.alpha.core.rules.BasicRule)2 ArrayList (java.util.ArrayList)2 Rule (at.ac.tuwien.kr.alpha.api.rules.Rule)1 Term (at.ac.tuwien.kr.alpha.api.terms.Term)1 VariableTerm (at.ac.tuwien.kr.alpha.api.terms.VariableTerm)1 EnumerationAtom (at.ac.tuwien.kr.alpha.core.atoms.EnumerationAtom)1 LinkedList (java.util.LinkedList)1