Search in sources :

Example 56 with Atom

use of at.ac.tuwien.kr.alpha.api.programs.atoms.Atom 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 57 with Atom

use of at.ac.tuwien.kr.alpha.api.programs.atoms.Atom 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)

Example 58 with Atom

use of at.ac.tuwien.kr.alpha.api.programs.atoms.Atom in project Alpha by alpha-asp.

the class LiteralInstantiationStrategyTest method defaultLazyGroundingCheckMustBeTrueGroundLiteral.

/**
 * Uses {@link DefaultLazyGroundingInstantiationStrategy} to check the truth
 * (i.e. {@link AssignmentStatus}) of the positive ground literal "p(a)".
 *
 * In this case, the instantiation strategy has an assignment where the atom
 * "p(a)" is assigned ThriceTruth.MBT, so we expect the instantiation strategy
 * to determine that p(a) is TRUE. Furthermore, the stale atom set
 * (used by {@link NaiveGrounder} to clean up atoms that should be deleted from
 * working memory) must stay empty.
 */
@Test
public void defaultLazyGroundingCheckMustBeTrueGroundLiteral() {
    Predicate p = Predicates.getPredicate("p", 1);
    BasicAtom pOfA = Atoms.newBasicAtom(p, Terms.newSymbolicConstant("a"));
    WorkingMemory workingMemory = new WorkingMemory();
    AtomStore atomStore = new AtomStoreImpl();
    WritableAssignment assignment = new TrailAssignment(atomStore);
    atomStore.putIfAbsent(pOfA);
    assignment.growForMaxAtomId();
    assignment.assign(atomStore.get(pOfA), ThriceTruth.MBT);
    LinkedHashSet<Atom> staleSet = new LinkedHashSet<>();
    DefaultLazyGroundingInstantiationStrategy strategy = new DefaultLazyGroundingInstantiationStrategy(workingMemory, atomStore, Collections.emptyMap(), false);
    strategy.setStaleWorkingMemoryEntries(staleSet);
    strategy.setCurrentAssignment(assignment);
    AssignmentStatus assignmentStatus = strategy.getTruthForGroundLiteral(Literals.fromAtom(pOfA, true));
    assertEquals(AssignmentStatus.TRUE, assignmentStatus);
    assertTrue(staleSet.isEmpty());
}
Also used : LinkedHashSet(java.util.LinkedHashSet) AtomStore(at.ac.tuwien.kr.alpha.core.common.AtomStore) WorkingMemory(at.ac.tuwien.kr.alpha.core.grounder.WorkingMemory) AtomStoreImpl(at.ac.tuwien.kr.alpha.core.common.AtomStoreImpl) WritableAssignment(at.ac.tuwien.kr.alpha.core.solver.WritableAssignment) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) TrailAssignment(at.ac.tuwien.kr.alpha.core.solver.TrailAssignment) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate) Test(org.junit.jupiter.api.Test)

Example 59 with Atom

use of at.ac.tuwien.kr.alpha.api.programs.atoms.Atom in project Alpha by alpha-asp.

the class LiteralInstantiationStrategyTest method defaultLazyGroundingSubstituteNonGroundLiteralWithUnassignedInstance.

/**
 * Uses {@link DefaultLazyGroundingInstantiationStrategy} to find the ground
 * instance "q(a, b)" for the partially ground positive literal "q(a, X)".
 *
 * In this case, the instantiation strategy has an empty assignment, so we
 * expect the assignment status (i.e. assignment status of the found ground
 * instance) passed back with the substitution to be UNASSIGNED. Since
 * UNASSIGNED and FALSE atoms are (potentially) stale in working memory, we
 * expect the atom "q(a, b)" to be added to the stale set by the instantiation
 * strategy.
 */
@Test
public void defaultLazyGroundingSubstituteNonGroundLiteralWithUnassignedInstance() {
    Predicate q = Predicates.getPredicate("q", 2);
    BasicAtom nonGroundAtom = Atoms.newBasicAtom(q, Terms.newSymbolicConstant("a"), Terms.newVariable("X"));
    WorkingMemory workingMemory = new WorkingMemory();
    workingMemory.initialize(q);
    workingMemory.addInstance(Atoms.newBasicAtom(q, Terms.newSymbolicConstant("a"), Terms.newSymbolicConstant("b")), true);
    AtomStore atomStore = new AtomStoreImpl();
    WritableAssignment assignment = new TrailAssignment(atomStore);
    LinkedHashSet<Atom> staleSet = new LinkedHashSet<>();
    DefaultLazyGroundingInstantiationStrategy strategy = new DefaultLazyGroundingInstantiationStrategy(workingMemory, atomStore, Collections.emptyMap(), false);
    strategy.setStaleWorkingMemoryEntries(staleSet);
    strategy.setCurrentAssignment(assignment);
    List<ImmutablePair<Substitution, AssignmentStatus>> result = strategy.getAcceptedSubstitutions(Literals.fromAtom(nonGroundAtom, true), new BasicSubstitution());
    assertEquals(1, result.size());
    ImmutablePair<Substitution, AssignmentStatus> substitutionInfo = result.get(0);
    Substitution substitution = substitutionInfo.left;
    AssignmentStatus assignmentStatus = substitutionInfo.right;
    assertEquals(AssignmentStatus.UNASSIGNED, assignmentStatus);
    assertTrue(substitution.isVariableSet(Terms.newVariable("X")));
    assertEquals(Terms.newSymbolicConstant("b"), substitution.eval(Terms.newVariable("X")));
    assertEquals(1, staleSet.size());
    assertTrue(staleSet.contains(Atoms.newBasicAtom(q, Terms.newSymbolicConstant("a"), Terms.newSymbolicConstant("b"))));
}
Also used : LinkedHashSet(java.util.LinkedHashSet) WorkingMemory(at.ac.tuwien.kr.alpha.core.grounder.WorkingMemory) BasicSubstitution(at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution) AtomStoreImpl(at.ac.tuwien.kr.alpha.core.common.AtomStoreImpl) TrailAssignment(at.ac.tuwien.kr.alpha.core.solver.TrailAssignment) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate) AtomStore(at.ac.tuwien.kr.alpha.core.common.AtomStore) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) Substitution(at.ac.tuwien.kr.alpha.api.grounder.Substitution) BasicSubstitution(at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution) WritableAssignment(at.ac.tuwien.kr.alpha.core.solver.WritableAssignment) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) Test(org.junit.jupiter.api.Test)

Example 60 with Atom

use of at.ac.tuwien.kr.alpha.api.programs.atoms.Atom in project Alpha by alpha-asp.

the class SolverTests method instanceEnumerationAtom.

@RegressionTest
public void instanceEnumerationAtom(RegressionTestConfig cfg) {
    Set<AnswerSet> answerSets = buildSolverForRegressionTest("# enumeration_predicate_is enum." + "dom(1). dom(2). dom(3)." + "p(X) :- dom(X)." + "q(Y) :- p(Y)." + "unique_position(Term,Pos) :- q(Term), enum(id0,Term,Pos)." + "wrong_double_occurrence :- unique_position(T1,P), unique_position(T2,P), T1 != T2.", cfg).collectSet();
    // Since enumeration depends on evaluation, we do not know which unique_position is actually assigned.
    // Check manually that there is one answer set, wrong_double_occurrence has not been derived, and enum yielded a unique position for each term.
    assertEquals(1, answerSets.size());
    AnswerSet answerSet = answerSets.iterator().next();
    assertEquals(null, answerSet.getPredicateInstances(Predicates.getPredicate("wrong_double_occurrence", 0)));
    SortedSet<Atom> positions = answerSet.getPredicateInstances(Predicates.getPredicate("unique_position", 2));
    assertEnumerationPositions(positions, 3);
}
Also used : TestUtils.assertRegressionTestAnswerSet(at.ac.tuwien.kr.alpha.core.test.util.TestUtils.assertRegressionTestAnswerSet) AnswerSet(at.ac.tuwien.kr.alpha.api.AnswerSet) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) TestUtils.buildSolverForRegressionTest(at.ac.tuwien.kr.alpha.core.test.util.TestUtils.buildSolverForRegressionTest)

Aggregations

Atom (at.ac.tuwien.kr.alpha.api.programs.atoms.Atom)73 Test (org.junit.jupiter.api.Test)38 Predicate (at.ac.tuwien.kr.alpha.api.programs.Predicate)27 BasicAtom (at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom)27 LinkedHashSet (java.util.LinkedHashSet)16 Unifier (at.ac.tuwien.kr.alpha.commons.substitutions.Unifier)15 ArrayList (java.util.ArrayList)14 AnswerSet (at.ac.tuwien.kr.alpha.api.AnswerSet)13 Literal (at.ac.tuwien.kr.alpha.api.programs.literals.Literal)12 AtomStoreImpl (at.ac.tuwien.kr.alpha.core.common.AtomStoreImpl)11 Instance (at.ac.tuwien.kr.alpha.commons.substitutions.Instance)9 AtomStore (at.ac.tuwien.kr.alpha.core.common.AtomStore)9 WorkingMemory (at.ac.tuwien.kr.alpha.core.grounder.WorkingMemory)9 TrailAssignment (at.ac.tuwien.kr.alpha.core.solver.TrailAssignment)9 ASPCore2Program (at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program)8 RuleAtom (at.ac.tuwien.kr.alpha.core.atoms.RuleAtom)8 WritableAssignment (at.ac.tuwien.kr.alpha.core.solver.WritableAssignment)7 Substitution (at.ac.tuwien.kr.alpha.api.grounder.Substitution)6 ExternalAtom (at.ac.tuwien.kr.alpha.api.programs.atoms.ExternalAtom)6 AnswerSetBuilder (at.ac.tuwien.kr.alpha.commons.AnswerSetBuilder)6