use of at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom in project Alpha by alpha-asp.
the class StratifiedEvaluationTest method testRecursiveRanking.
@Test
public void testRecursiveRanking() {
// @formatter:off
String asp = "thing(a).\n" + "thing(b).\n" + "thing(c).\n" + "thing_before(a, b).\n" + "thing_before(b, c).\n" + "has_prev_thing(X) :- thing(X), thing_succ(_, X).\n" + "first_thing(X) :- thing(X), not has_prev_thing(X).\n" + "thing_not_succ(X, Y) :-\n" + " thing(X),\n" + " thing(Y),\n" + " thing(INTM),\n" + " thing_before(X, Y),\n" + " thing_before(X, INTM),\n" + " thing_before(INTM, X).\n" + "thing_succ(X, Y) :-\n" + " thing(X),\n" + " thing(Y),\n" + " thing_before(X, Y),\n" + " not thing_not_succ(X, Y).\n" + "thing_rank(X, 1) :- first_thing(X).\n" + "thing_rank(X, R) :-\n" + " thing(X),\n" + " thing_succ(Y, X),\n" + " thing_rank(Y, K),\n" + " R = K + 1.";
// @formatter:on
CompiledProgram evaluated = parseAndEvaluate.apply(asp);
Predicate rank = Predicates.getPredicate("thing_rank", 2);
BasicAtom rank1 = Atoms.newBasicAtom(rank, Terms.newSymbolicConstant("a"), Terms.newConstant(1));
BasicAtom rank2 = Atoms.newBasicAtom(rank, Terms.newSymbolicConstant("b"), Terms.newConstant(2));
BasicAtom rank3 = Atoms.newBasicAtom(rank, Terms.newSymbolicConstant("c"), Terms.newConstant(3));
List<Atom> evaluatedFacts = evaluated.getFacts();
assertTrue(evaluatedFacts.contains(rank1));
assertTrue(evaluatedFacts.contains(rank2));
assertTrue(evaluatedFacts.contains(rank3));
}
use of at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom in project Alpha by alpha-asp.
the class PredicateInternalizer method makePrefixedPredicatesInternal.
public static ASPCore2Program makePrefixedPredicatesInternal(ASPCore2Program program, String prefix) {
InputProgram.Builder prgBuilder = InputProgram.builder();
for (Atom atom : program.getFacts()) {
if (atom.getPredicate().getName().startsWith(prefix)) {
prgBuilder.addFact(PredicateInternalizer.makePredicateInternal((BasicAtom) atom));
} else {
prgBuilder.addFact(atom);
}
}
for (Rule<Head> rule : program.getRules()) {
prgBuilder.addRule(PredicateInternalizer.makePrefixedPredicatesInternal(rule, prefix));
}
prgBuilder.addInlineDirectives(program.getInlineDirectives());
return prgBuilder.build();
}
use of at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom in project Alpha by alpha-asp.
the class LiteralInstantiationStrategyTest method defaultLazyGroundingNoAssignmentGroundLiteral.
/**
* Uses {@link DefaultLazyGroundingInstantiationStrategy} to check the truth
* (i.e. {@link AssignmentStatus}) of the positive ground literal "p(a)".
*
* In this case, the instantiation strategy does not have an assignment set (as
* is the case when {@link NaiveGrounder} is in bootstrap),
* 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 defaultLazyGroundingNoAssignmentGroundLiteral() {
Predicate p = Predicates.getPredicate("p", 1);
BasicAtom pOfA = Atoms.newBasicAtom(p, Terms.newSymbolicConstant("a"));
WorkingMemory workingMemory = new WorkingMemory();
LinkedHashSet<Atom> staleSet = new LinkedHashSet<>();
DefaultLazyGroundingInstantiationStrategy strategy = new DefaultLazyGroundingInstantiationStrategy(workingMemory, new AtomStoreImpl(), Collections.emptyMap(), false);
strategy.setStaleWorkingMemoryEntries(staleSet);
strategy.setCurrentAssignment(null);
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.BasicAtom in project Alpha by alpha-asp.
the class LiteralInstantiationStrategyTest method defaultLazyGroundingCheckTrueGroundLiteral.
/**
* 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.TRUE, 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 defaultLazyGroundingCheckTrueGroundLiteral() {
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.TRUE);
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.BasicAtom in project Alpha by alpha-asp.
the class LiteralInstantiationStrategyTest method defaultLazyGroundingCheckUnassignedGroundLiteral.
/**
* 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 empty assignment,
* so we expect the instantiation strategy to determine that p(a) is UNASSIGNED.
* Since UNASSIGNED and FALSE atoms are (potentially) stale in working memory,
* we expect the atom "p(a)" to be added to the stale set by the instantiation
* strategy.
*/
@Test
public void defaultLazyGroundingCheckUnassignedGroundLiteral() {
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);
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.UNASSIGNED, assignmentStatus);
assertEquals(1, staleSet.size());
assertTrue(staleSet.contains(pOfA));
}
Aggregations