Search in sources :

Example 11 with Unifier

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

the class Terms method renameVariables.

/**
 * Renames variables in given set of terms.
 * @param varNamePrefix
 * @return
 */
public static Substitution renameVariables(Set<VariableTerm> terms, String varNamePrefix) {
    Unifier renamingSubstitution = new Unifier();
    int counter = 0;
    for (VariableTerm variable : terms) {
        renamingSubstitution.put(variable, Terms.newVariable(varNamePrefix + counter++));
    }
    return renamingSubstitution;
}
Also used : VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) Unifier(at.ac.tuwien.kr.alpha.commons.substitutions.Unifier)

Example 12 with Unifier

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

the class UnificationTest method nonunificationWithArithmeticTermsNested.

@Test
public void nonunificationWithArithmeticTermsNested() {
    Atom left = partsParser.parseLiteral("a(X + 7)").getAtom();
    Atom right = partsParser.parseLiteral("a(Y + (Z - 2))").getAtom();
    Unifier unifier = Unification.unifyAtoms(left, right);
    assertNull(unifier);
}
Also used : Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) Unifier(at.ac.tuwien.kr.alpha.commons.substitutions.Unifier) Test(org.junit.jupiter.api.Test)

Example 13 with Unifier

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

the class UnificationTest method unificationWithArithmeticTerms.

@Test
public void unificationWithArithmeticTerms() {
    Atom left = partsParser.parseLiteral("a(X - (3 * Y))").getAtom();
    Atom right = partsParser.parseLiteral("a(15 - Z)").getAtom();
    Unifier unifier = Unification.unifyAtoms(left, right);
    assertNotNull(unifier);
    assertEquals(3, unifier.getMappedVariables().size());
    assertEquals(left.substitute(unifier).toString(), right.substitute(unifier).toString());
}
Also used : Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) Unifier(at.ac.tuwien.kr.alpha.commons.substitutions.Unifier) Test(org.junit.jupiter.api.Test)

Example 14 with Unifier

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

the class UnificationTest method nonunificationWithArithmeticTerms.

@Test
public void nonunificationWithArithmeticTerms() {
    Atom left = partsParser.parseLiteral("a(X + 4)").getAtom();
    Atom right = partsParser.parseLiteral("a(Y - 4)").getAtom();
    Unifier unifier = Unification.unifyAtoms(left, right);
    assertNull(unifier);
}
Also used : Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) Unifier(at.ac.tuwien.kr.alpha.commons.substitutions.Unifier) Test(org.junit.jupiter.api.Test)

Example 15 with Unifier

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

the class UnificationTest method unificationNonGround.

@Test
public void unificationNonGround() {
    Atom left = partsParser.parseLiteral("p(X, 13)").getAtom();
    Atom right = partsParser.parseLiteral("p(Z, Y)").getAtom();
    Unifier unifier = Unification.unifyAtoms(left, right);
    assertNotNull(unifier);
    assertEquals(3, unifier.getMappedVariables().size());
    assertEquals("13", unifier.eval(Terms.newVariable("Y")).toString());
    // Check that the unifier sets X=Z by either mapping X -> Z or Z -> X.
    if (unifier.eval(Terms.newVariable("X")) != null) {
        // X is mapped, it must map to Z now.
        assertEquals("Z", unifier.eval(Terms.newVariable("X")).toString());
    } else {
        // X is not mapped, so Z must map to X.
        assertEquals("X", unifier.eval(Terms.newVariable("Z")).toString());
    }
}
Also used : Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) Unifier(at.ac.tuwien.kr.alpha.commons.substitutions.Unifier) Test(org.junit.jupiter.api.Test)

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