Search in sources :

Example 46 with Term

use of at.ac.tuwien.kr.alpha.api.terms.Term 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)

Example 47 with Term

use of at.ac.tuwien.kr.alpha.api.terms.Term in project Alpha by alpha-asp.

the class VariableEqualityRemoval method findAndReplaceVariableEquality.

private Rule<Head> findAndReplaceVariableEquality(Rule<Head> rule) {
    // Collect all equal variables.
    HashMap<VariableTerm, HashSet<VariableTerm>> variableToEqualVariables = new LinkedHashMap<>();
    HashSet<Literal> equalitiesToRemove = new HashSet<>();
    for (Literal bodyElement : rule.getBody()) {
        if (!(bodyElement instanceof ComparisonLiteral)) {
            continue;
        }
        ComparisonLiteral comparisonLiteral = (ComparisonLiteral) bodyElement;
        if (!comparisonLiteral.isNormalizedEquality()) {
            continue;
        }
        if (comparisonLiteral.getTerms().get(0) instanceof VariableTerm && comparisonLiteral.getTerms().get(1) instanceof VariableTerm) {
            VariableTerm leftVariable = (VariableTerm) comparisonLiteral.getTerms().get(0);
            VariableTerm rightVariable = (VariableTerm) comparisonLiteral.getTerms().get(1);
            HashSet<VariableTerm> leftEqualVariables = variableToEqualVariables.get(leftVariable);
            HashSet<VariableTerm> rightEqualVariables = variableToEqualVariables.get(rightVariable);
            if (leftEqualVariables == null && rightEqualVariables == null) {
                HashSet<VariableTerm> equalVariables = new LinkedHashSet<>(Arrays.asList(leftVariable, rightVariable));
                variableToEqualVariables.put(leftVariable, equalVariables);
                variableToEqualVariables.put(rightVariable, equalVariables);
            }
            if (leftEqualVariables == null && rightEqualVariables != null) {
                rightEqualVariables.add(leftVariable);
                variableToEqualVariables.put(leftVariable, rightEqualVariables);
            }
            if (leftEqualVariables != null && rightEqualVariables == null) {
                leftEqualVariables.add(rightVariable);
                variableToEqualVariables.put(rightVariable, leftEqualVariables);
            }
            if (leftEqualVariables != null && rightEqualVariables != null) {
                leftEqualVariables.addAll(rightEqualVariables);
                for (VariableTerm rightEqualVariable : rightEqualVariables) {
                    variableToEqualVariables.put(rightEqualVariable, leftEqualVariables);
                }
            }
            equalitiesToRemove.add(comparisonLiteral);
        }
    }
    if (variableToEqualVariables.isEmpty()) {
        // Skip rule if there is no equality between variables.
        return rule;
    }
    List<Literal> rewrittenBody = new ArrayList<>(rule.getBody());
    if (!rule.isConstraint() && rule.getHead() instanceof DisjunctiveHead) {
        throw new UnsupportedOperationException("VariableEqualityRemoval cannot be applied to rule with DisjunctiveHead, yet.");
    }
    NormalHead rewrittenHead = rule.isConstraint() ? null : Heads.newNormalHead(((NormalHead) rule.getHead()).getAtom());
    // Use substitution for actual replacement.
    Unifier replacementSubstitution = new Unifier();
    // For each set of equal variables, take the first variable and replace all others by it.
    for (Map.Entry<VariableTerm, HashSet<VariableTerm>> variableEqualityEntry : variableToEqualVariables.entrySet()) {
        VariableTerm variableToReplace = variableEqualityEntry.getKey();
        VariableTerm replacementVariable = variableEqualityEntry.getValue().iterator().next();
        if (variableToReplace == replacementVariable) {
            continue;
        }
        replacementSubstitution.put(variableToReplace, replacementVariable);
    }
    // Replace/Substitute in each literal every term where one of the common variables occurs.
    Iterator<Literal> bodyIterator = rewrittenBody.iterator();
    while (bodyIterator.hasNext()) {
        Literal literal = bodyIterator.next();
        if (equalitiesToRemove.contains(literal)) {
            bodyIterator.remove();
        }
        for (int i = 0; i < literal.getTerms().size(); i++) {
            Term replaced = literal.getTerms().get(i).substitute(replacementSubstitution);
            literal.getTerms().set(i, replaced);
        }
    }
    // Replace variables in head.
    if (rewrittenHead != null) {
        Atom headAtom = rewrittenHead.getAtom();
        for (int i = 0; i < headAtom.getTerms().size(); i++) {
            Term replaced = headAtom.getTerms().get(i).substitute(replacementSubstitution);
            headAtom.getTerms().set(i, replaced);
        }
    }
    return new BasicRule(rewrittenHead, rewrittenBody);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) BasicRule(at.ac.tuwien.kr.alpha.core.rules.BasicRule) ArrayList(java.util.ArrayList) DisjunctiveHead(at.ac.tuwien.kr.alpha.api.rules.heads.DisjunctiveHead) Term(at.ac.tuwien.kr.alpha.api.terms.Term) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) 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) ComparisonLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.ComparisonLiteral) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Unifier(at.ac.tuwien.kr.alpha.commons.substitutions.Unifier) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 48 with Term

use of at.ac.tuwien.kr.alpha.api.terms.Term in project Alpha by alpha-asp.

the class Unification method unifyAtoms.

private static Unifier unifyAtoms(Atom left, Atom right, boolean keepLeftAsIs) {
    Set<VariableTerm> leftOccurringVariables = left.getOccurringVariables();
    Set<VariableTerm> rightOccurringVaribles = right.getOccurringVariables();
    boolean leftSmaller = leftOccurringVariables.size() < rightOccurringVaribles.size();
    Set<VariableTerm> smallerSet = leftSmaller ? leftOccurringVariables : rightOccurringVaribles;
    Set<VariableTerm> largerSet = leftSmaller ? rightOccurringVaribles : leftOccurringVariables;
    for (VariableTerm variableTerm : smallerSet) {
        if (largerSet.contains(variableTerm)) {
            throw oops("Left and right atom share variables.");
        }
    }
    Unifier mgu = new Unifier();
    if (!left.getPredicate().equals(right.getPredicate())) {
        return null;
    }
    for (int i = 0; i < left.getPredicate().getArity(); i++) {
        final Term leftTerm = left.getTerms().get(i);
        final Term rightTerm = right.getTerms().get(i);
        if (!unifyTerms(leftTerm, rightTerm, mgu, keepLeftAsIs)) {
            return null;
        }
    }
    return mgu;
}
Also used : VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) Term(at.ac.tuwien.kr.alpha.api.terms.Term) ArithmeticTerm(at.ac.tuwien.kr.alpha.api.terms.ArithmeticTerm) FunctionTerm(at.ac.tuwien.kr.alpha.api.terms.FunctionTerm) Unifier(at.ac.tuwien.kr.alpha.commons.substitutions.Unifier)

Example 49 with Term

use of at.ac.tuwien.kr.alpha.api.terms.Term in project Alpha by alpha-asp.

the class AnswerSetToWorkbookMapperTest method rowMatchesAtom.

private static boolean rowMatchesAtom(Row row, Atom atom) {
    List<Term> terms = atom.getTerms();
    Cell cell;
    for (int i = 0; i < terms.size(); i++) {
        cell = row.getCell(i);
        if (cell == null) {
            return false;
        }
        if (!(cell.getStringCellValue().equals(terms.get(i).toString()))) {
            return false;
        }
    }
    return true;
}
Also used : Term(at.ac.tuwien.kr.alpha.api.terms.Term) Cell(org.apache.poi.ss.usermodel.Cell)

Example 50 with Term

use of at.ac.tuwien.kr.alpha.api.terms.Term in project Alpha by alpha-asp.

the class Substitutions method fromString.

public static Substitution fromString(String str) {
    String bare = str.substring(1, str.length() - 1);
    String[] assignments = bare.split(",");
    BasicSubstitution ret = new BasicSubstitution();
    for (String assignment : assignments) {
        String[] keyVal = assignment.split("->");
        VariableTerm variable = Terms.newVariable(keyVal[0]);
        Term assignedTerm = PROGRAM_PART_PARSER.parseTerm(keyVal[1]);
        ret.put(variable, assignedTerm);
    }
    return ret;
}
Also used : BasicSubstitution(at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) Term(at.ac.tuwien.kr.alpha.api.terms.Term)

Aggregations

Term (at.ac.tuwien.kr.alpha.api.terms.Term)52 VariableTerm (at.ac.tuwien.kr.alpha.api.terms.VariableTerm)36 ArrayList (java.util.ArrayList)16 ConstantTerm (at.ac.tuwien.kr.alpha.api.terms.ConstantTerm)15 FunctionTerm (at.ac.tuwien.kr.alpha.api.terms.FunctionTerm)15 IntervalTerm (at.ac.tuwien.kr.alpha.commons.terms.IntervalTerm)12 ArithmeticTerm (at.ac.tuwien.kr.alpha.api.terms.ArithmeticTerm)10 Predicate (at.ac.tuwien.kr.alpha.api.programs.Predicate)9 Test (org.junit.jupiter.api.Test)8 Literal (at.ac.tuwien.kr.alpha.api.programs.literals.Literal)7 Atom (at.ac.tuwien.kr.alpha.api.programs.atoms.Atom)6 BasicSubstitution (at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution)6 HashSet (java.util.HashSet)6 AggregateLiteral (at.ac.tuwien.kr.alpha.api.programs.literals.AggregateLiteral)4 Instance (at.ac.tuwien.kr.alpha.commons.substitutions.Instance)4 Substitution (at.ac.tuwien.kr.alpha.api.grounder.Substitution)3 BasicAtom (at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom)3 ComparisonLiteral (at.ac.tuwien.kr.alpha.api.programs.literals.ComparisonLiteral)3 Unifier (at.ac.tuwien.kr.alpha.commons.substitutions.Unifier)3 MinusTerm (at.ac.tuwien.kr.alpha.commons.terms.ArithmeticTermImpl.MinusTerm)3