use of at.ac.tuwien.kr.alpha.api.programs.atoms.Atom in project Alpha by alpha-asp.
the class LiteralInstantiationStrategyTest method defaultLazyGroundingSubstituteNonGroundLiteralWithTrueInstance.
/**
* 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 assignment where q(a, b) is
* assigned ThriceTruth.TRUE, so we expect the assignment status (i.e.
* assignment status of the found ground instance) passed back with the
* substitution to be TRUE. Furthermore, we expect the stale atom set to stay
* empty.
*/
@Test
public void defaultLazyGroundingSubstituteNonGroundLiteralWithTrueInstance() {
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);
BasicAtom groundAtom = Atoms.newBasicAtom(q, Terms.newSymbolicConstant("a"), Terms.newSymbolicConstant("b"));
atomStore.putIfAbsent(groundAtom);
assignment.growForMaxAtomId();
assignment.assign(atomStore.get(groundAtom), ThriceTruth.TRUE);
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.TRUE, assignmentStatus);
assertTrue(substitution.isVariableSet(Terms.newVariable("X")));
assertEquals(Terms.newSymbolicConstant("b"), substitution.eval(Terms.newVariable("X")));
assertTrue(staleSet.isEmpty());
}
use of at.ac.tuwien.kr.alpha.api.programs.atoms.Atom in project Alpha by alpha-asp.
the class AnalyzeUnjustifiedTest method justifyMultipleReasons.
@Test
public void justifyMultipleReasons() {
String program = "n(a). n(b). n(c). n(d). n(e)." + "s(a,b). s(b,c). s(c,d). s(d,e)." + "{ q(X) } :- n(X)." + "p(X) :- q(X)." + "p(X) :- p(Y), s(Y,X)." + ":- not p(c).";
CompiledProgram internalProgram = parseAndPreprocess.apply(program);
AtomStore atomStore = new AtomStoreImpl();
NaiveGrounder grounder = new NaiveGrounder(internalProgram, atomStore, true);
grounder.getNoGoods(null);
TrailAssignment assignment = new TrailAssignment(atomStore);
Atom qa = parser.parse("q(a).").getFacts().get(0);
Atom qb = parser.parse("q(b).").getFacts().get(0);
Atom qc = parser.parse("q(c).").getFacts().get(0);
Atom qd = parser.parse("q(d).").getFacts().get(0);
Atom qe = parser.parse("q(e).").getFacts().get(0);
int qaId = atomStore.get(qa);
int qbId = atomStore.get(qb);
int qcId = atomStore.get(qc);
int qdId = atomStore.get(qd);
int qeId = atomStore.get(qe);
assignment.growForMaxAtomId();
assignment.assign(qaId, ThriceTruth.FALSE);
assignment.assign(qbId, ThriceTruth.FALSE);
assignment.assign(qcId, ThriceTruth.FALSE);
assignment.assign(qdId, ThriceTruth.FALSE);
assignment.assign(qeId, ThriceTruth.FALSE);
Predicate nq = Predicates.getPredicate("_nq", 2, true);
Atom nqa = Atoms.newBasicAtom(nq, Arrays.asList(Terms.newConstant("1"), Terms.newSymbolicConstant("a")));
Atom nqb = Atoms.newBasicAtom(nq, Arrays.asList(Terms.newConstant("1"), Terms.newSymbolicConstant("b")));
Atom nqc = Atoms.newBasicAtom(nq, Arrays.asList(Terms.newConstant("1"), Terms.newSymbolicConstant("c")));
Atom nqd = Atoms.newBasicAtom(nq, Arrays.asList(Terms.newConstant("1"), Terms.newSymbolicConstant("d")));
Atom nqe = Atoms.newBasicAtom(nq, Arrays.asList(Terms.newConstant("1"), Terms.newSymbolicConstant("e")));
int nqaId = atomStore.get(nqa);
int nqbId = atomStore.get(nqb);
int nqcId = atomStore.get(nqc);
int nqdId = atomStore.get(nqd);
int nqeId = atomStore.get(nqe);
assignment.growForMaxAtomId();
assignment.assign(nqaId, ThriceTruth.TRUE);
assignment.assign(nqbId, ThriceTruth.TRUE);
assignment.assign(nqcId, ThriceTruth.TRUE);
assignment.assign(nqdId, ThriceTruth.TRUE);
assignment.assign(nqeId, ThriceTruth.TRUE);
Atom pc = parser.parse("p(c).").getFacts().get(0);
Set<Literal> reasons = grounder.justifyAtom(atomStore.get(pc), assignment);
assertFalse(reasons.isEmpty());
}
use of at.ac.tuwien.kr.alpha.api.programs.atoms.Atom in project Alpha by alpha-asp.
the class HanoiTowerTest method checkGoal.
/**
* Conducts a very simple, non-comprehensive goal check (i.e. it may classify answer sets as correct that are actually wrong) by checking if
* for every goal/3
* fact in the input there is a corresponding on/3 atom in the output.
*/
private void checkGoal(ASPCore2Program parsedProgram, AnswerSet answerSet) {
Predicate ongoal = Predicates.getPredicate("ongoal", 2);
Predicate on = Predicates.getPredicate("on", 3);
int steps = getSteps(parsedProgram);
SortedSet<Atom> onInstancesInAnswerSet = answerSet.getPredicateInstances(on);
for (Atom atom : parsedProgram.getFacts()) {
if (atom.getPredicate().getName().equals(ongoal.getName()) && atom.getPredicate().getArity() == ongoal.getArity()) {
Term expectedTop = atom.getTerms().get(0);
Term expectedBottom = atom.getTerms().get(1);
Term expectedSteps = Terms.newConstant(steps);
Atom expectedAtom = Atoms.newBasicAtom(on, expectedSteps, expectedBottom, expectedTop);
assertTrue(onInstancesInAnswerSet.contains(expectedAtom), "Answer set does not contain " + expectedAtom);
}
}
}
use of at.ac.tuwien.kr.alpha.api.programs.atoms.Atom 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);
}
use of at.ac.tuwien.kr.alpha.api.programs.atoms.Atom 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());
}
Aggregations