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);
}
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());
}
}
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());
}
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"))));
}
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);
}
Aggregations