Search in sources :

Example 6 with Unifier

use of at.ac.tuwien.kr.alpha.commons.substitutions.Unifier in project Alpha by alpha-asp.

the class UnifierTest method mergeUnifierIntoLeft.

@Test
public void mergeUnifierIntoLeft() {
    VariableTerm varX = Terms.newVariable("X");
    VariableTerm varY = Terms.newVariable("Y");
    VariableTerm varZ = Terms.newVariable("Z");
    Term constA = Terms.newConstant("a");
    Unifier left = new Unifier();
    left.put(varX, varY);
    left.put(varZ, varY);
    Unifier right = new Unifier();
    right.put(varX, constA);
    Unifier merged = Unifier.mergeIntoLeft(left, right);
    assertEquals(constA, merged.eval(varY));
    assertEquals(constA, merged.eval(varZ));
}
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) Unifier(at.ac.tuwien.kr.alpha.commons.substitutions.Unifier) Test(org.junit.jupiter.api.Test)

Example 7 with Unifier

use of at.ac.tuwien.kr.alpha.commons.substitutions.Unifier in project Alpha by alpha-asp.

the class LitSet method toString.

@Override
public String toString() {
    StringBuilder sb = new StringBuilder("(" + atom + ",{");
    for (Unifier complementSubstitution : complementSubstitutions) {
        sb.append(atom.substitute(complementSubstitution));
        sb.append(", ");
    }
    sb.append("})");
    return sb.toString();
}
Also used : Unifier(at.ac.tuwien.kr.alpha.commons.substitutions.Unifier)

Example 8 with Unifier

use of at.ac.tuwien.kr.alpha.commons.substitutions.Unifier in project Alpha by alpha-asp.

the class LitSet method computeNormalizedSubstitutions.

private Set<Unifier> computeNormalizedSubstitutions() {
    Set<Unifier> ret = new LinkedHashSet<>();
    for (Unifier substitution : complementSubstitutions) {
        Unifier preNormalizedSubstitution = normalizeSubstitution(atom, substitution, normalizedLiteral);
        // Unifier may still contain variables in the right-hand side, those have to be normalized, too.
        Atom appliedSub = normalizedLiteral.substitute(preNormalizedSubstitution);
        // Apply substitution and normalize all remaining variables, i.e., those appearing at the right-hand side of the substitution.
        Atom normalized = computeNormalized(appliedSub, "_X");
        // Compute final substitution from normalized atom to the one where also variables are normalized.
        Unifier normalizedSubstitution = new Unifier(Unification.instantiate(normalizedLiteral, normalized));
        ret.add(normalizedSubstitution);
    }
    return ret;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Unifier(at.ac.tuwien.kr.alpha.commons.substitutions.Unifier) VariableNormalizableAtom(at.ac.tuwien.kr.alpha.api.programs.VariableNormalizableAtom) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom)

Example 9 with Unifier

use of at.ac.tuwien.kr.alpha.commons.substitutions.Unifier in project Alpha by alpha-asp.

the class InternalRule method renameVariables.

/**
 * Returns a new Rule that is equal to this one except that all variables are renamed to have the newVariablePostfix
 * appended.
 *
 * @param newVariablePostfix
 * @return
 */
@Override
public InternalRule renameVariables(String newVariablePostfix) {
    List<VariableTerm> occurringVariables = new ArrayList<>();
    BasicAtom headAtom = this.getHeadAtom();
    occurringVariables.addAll(headAtom.getOccurringVariables());
    for (Literal literal : this.getBody()) {
        occurringVariables.addAll(literal.getOccurringVariables());
    }
    Unifier variableReplacement = new Unifier();
    for (VariableTerm occurringVariable : occurringVariables) {
        final String newVariableName = occurringVariable.toString() + newVariablePostfix;
        variableReplacement.put(occurringVariable, Terms.newVariable(newVariableName));
    }
    BasicAtom renamedHeadAtom = headAtom.substitute(variableReplacement);
    ArrayList<Literal> renamedBody = new ArrayList<>(this.getBody().size());
    for (Literal literal : this.getBody()) {
        renamedBody.add(literal.substitute(variableReplacement));
    }
    return new InternalRule(Heads.newNormalHead(renamedHeadAtom), renamedBody);
}
Also used : Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) AggregateLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.AggregateLiteral) ArrayList(java.util.ArrayList) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) Unifier(at.ac.tuwien.kr.alpha.commons.substitutions.Unifier)

Example 10 with Unifier

use of at.ac.tuwien.kr.alpha.commons.substitutions.Unifier in project Alpha by alpha-asp.

the class AnalyzeUnjustified method unjustCover.

private Set<LitSet> unjustCover(List<Literal> vB, Set<Unifier> vY, Set<Unifier> vN, Assignment currentAssignment) {
    padDepth += 2;
    log("Begin UnjustCoverFixed()");
    log("Finding unjustified body literals in: {} / {} excluded {}", vB, vY, vN);
    Set<LitSet> ret = new LinkedHashSet<>();
    if (vB.isEmpty() || vY.isEmpty()) {
        log("End unjustCover().");
        padDepth -= 2;
        return Collections.emptySet();
    }
    int chosenLiteralPos = 0;
    // Find a body literal that is not a ComparisonLiteral, because these do not generate/have atoms assigned.
    for (int i = 0; i < vB.size(); i++) {
        if (!(vB.get(i) instanceof ComparisonLiteral)) {
            // TODO: Should this be FixedInterpretationLiteral instead??
            chosenLiteralPos = i;
            break;
        }
    }
    Atom b = vB.get(chosenLiteralPos).getAtom();
    log("Picked literal from body is: {}", b);
    for (Unifier sigmaY : vY) {
        Atom bSigmaY = b.substitute(sigmaY);
        log("Treating substitution for: {}", bSigmaY);
        Set<Unifier> vYp = new LinkedHashSet<>();
        log("Checking atoms over predicate: {}", b.getPredicate());
        AssignedAtomsIterator assignedAtomsOverPredicate = getAssignedAtomsOverPredicate(b.getPredicate());
        atomLoop: while (assignedAtomsOverPredicate.hasNext()) {
            Atom atom = assignedAtomsOverPredicate.next();
            // Check that atom is justified/true.
            log("Checking atom: {}", atom);
            if (atomStore.contains(atom)) {
                int atomId = atomStore.get(atom);
                if (currentAssignment.getTruth(atomId) != ThriceTruth.TRUE) {
                    log("Atom is not TRUE. Skipping.");
                    continue;
                }
            }
            // Note: in case the atom is not in the atomStore, it came from a fact and hence is true.
            Unifier sigma = Unification.instantiate(b, atom);
            if (sigma == null) {
                log("Atom does not unify with picked body literal.");
                continue;
            }
            Atom bSigma = b.substitute(sigma);
            if (!bSigma.isGround()) {
                throw oops("Resulting atom is not ground.");
            }
            Set<VariableTerm> variablesOccurringInSigma = sigma.getMappedVariables();
            if (Unification.instantiate(bSigmaY, bSigma) != null) {
                for (Unifier sigmaN : vN) {
                    ArrayList<Term> occurringVariables = new ArrayList<>(variablesOccurringInSigma);
                    occurringVariables.addAll(sigmaN.getMappedVariables());
                    BasicAtom genericAtom = Atoms.newBasicAtom(Predicates.getPredicate("_", occurringVariables.size(), true), occurringVariables);
                    Atom genericSubstituted = genericAtom.substitute(sigmaN).renameVariables("_analyzeTest");
                    if (Unification.instantiate(genericSubstituted, genericAtom.substitute(sigma)) != null) {
                        log("Atom {} is excluded by: {} via {}", genericSubstituted, sigmaN, sigma);
                        continue atomLoop;
                    }
                }
                log("Adding corresponding substitution to Y': {}", sigma);
                vYp.add(sigma);
            }
        }
        log("Unjustified body literals: {}", vYp);
        Set<Unifier> vYpUN = new LinkedHashSet<>();
        vYpUN.addAll(vYp);
        vYpUN.addAll(vN);
        LitSet toJustify = new LitSet(bSigmaY, vYpUN);
        if (!toJustify.coversNothing()) {
            log("New litset to do: {}", toJustify);
            ret.add(toJustify);
        } else {
            log("Generated LitSet covers nothing. Ignoring: {}", toJustify);
        }
        ArrayList<Literal> newB = new ArrayList<>(vB);
        newB.remove(chosenLiteralPos);
        ret.addAll(unjustCover(newB, vYp, vN, currentAssignment));
        log("Literal set(s) to treat: {}", ret);
    }
    log("End unjustCover().");
    padDepth -= 2;
    return ret;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) ArrayList(java.util.ArrayList) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) ComparisonLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.ComparisonLiteral) ComparisonLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.ComparisonLiteral) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) Unifier(at.ac.tuwien.kr.alpha.commons.substitutions.Unifier)

Aggregations

Unifier (at.ac.tuwien.kr.alpha.commons.substitutions.Unifier)20 Atom (at.ac.tuwien.kr.alpha.api.programs.atoms.Atom)15 Test (org.junit.jupiter.api.Test)11 VariableTerm (at.ac.tuwien.kr.alpha.api.terms.VariableTerm)6 BasicAtom (at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom)5 Literal (at.ac.tuwien.kr.alpha.api.programs.literals.Literal)5 ArrayList (java.util.ArrayList)5 ComparisonLiteral (at.ac.tuwien.kr.alpha.api.programs.literals.ComparisonLiteral)4 LinkedHashSet (java.util.LinkedHashSet)4 Term (at.ac.tuwien.kr.alpha.api.terms.Term)3 HashSet (java.util.HashSet)3 Set (java.util.Set)2 Predicate (at.ac.tuwien.kr.alpha.api.programs.Predicate)1 VariableNormalizableAtom (at.ac.tuwien.kr.alpha.api.programs.VariableNormalizableAtom)1 AggregateLiteral (at.ac.tuwien.kr.alpha.api.programs.literals.AggregateLiteral)1 DisjunctiveHead (at.ac.tuwien.kr.alpha.api.rules.heads.DisjunctiveHead)1 NormalHead (at.ac.tuwien.kr.alpha.api.rules.heads.NormalHead)1 ArithmeticTerm (at.ac.tuwien.kr.alpha.api.terms.ArithmeticTerm)1 FunctionTerm (at.ac.tuwien.kr.alpha.api.terms.FunctionTerm)1 Instance (at.ac.tuwien.kr.alpha.commons.substitutions.Instance)1