use of at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom in project Alpha by alpha-asp.
the class AnalyzeUnjustifiedTest method justifyNegatedFactsRemovedFromReasons.
@Test
public void justifyNegatedFactsRemovedFromReasons() {
String program = "forbidden(2,9). forbidden(1,9)." + "p(X) :- q(X)." + "q(X) :- p(X)." + "q(5) :- r." + "r :- not nr, not forbidden(2,9), not forbidden(1,9)." + "nr :- not r." + ":- not p(5).";
CompiledProgram internalProgram = parseAndPreprocess.apply(program);
AtomStore atomStore = new AtomStoreImpl();
NaiveGrounder grounder = new NaiveGrounder(internalProgram, atomStore, true);
grounder.getNoGoods(null);
TrailAssignment assignment = new TrailAssignment(atomStore);
int rId = atomStore.get(Atoms.newBasicAtom(Predicates.getPredicate("r", 0)));
int nrId = atomStore.get(Atoms.newBasicAtom(Predicates.getPredicate("nr", 0)));
assignment.growForMaxAtomId();
assignment.assign(rId, ThriceTruth.FALSE);
assignment.assign(nrId, ThriceTruth.TRUE);
BasicAtom p5 = Atoms.newBasicAtom(Predicates.getPredicate("p", 1), Collections.singletonList(Terms.newConstant(5)));
assignment.assign(atomStore.get(p5), ThriceTruth.MBT);
Set<Literal> reasons = grounder.justifyAtom(atomStore.get(p5), assignment);
assertFalse(reasons.isEmpty());
for (Literal literal : reasons) {
// Check that facts are not present in justification.
assertNotEquals(literal.getPredicate(), Predicates.getPredicate("forbidden", 2));
}
}
use of at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom in project Alpha by alpha-asp.
the class IntervalTermToIntervalAtom method rewriteIntervalSpecifications.
/**
* Rewrites intervals into a new variable and special IntervalAtom.
*
* @return true if some interval occurs in the rule.
*/
private static NormalRule rewriteIntervalSpecifications(NormalRule rule) {
// Collect all intervals and replace them with variables.
Map<VariableTerm, IntervalTerm> intervalReplacements = new LinkedHashMap<>();
List<Literal> rewrittenBody = new ArrayList<>();
for (Literal literal : rule.getBody()) {
Literal rewrittenLiteral = rewriteLiteral(literal, intervalReplacements);
if (rewrittenLiteral != null) {
rewrittenBody.add(rewrittenLiteral);
}
}
// Note that this cast is safe: NormalHead can only have a BasicAtom, so literalizing and getting back the Atom destroys type information,
// but should never yield anything other than a BasicAtom
NormalHead rewrittenHead = rule.isConstraint() ? null : Heads.newNormalHead((BasicAtom) rewriteLiteral(rule.getHead().getAtom().toLiteral(), intervalReplacements).getAtom());
// If intervalReplacements is empty, no IntervalTerms have been found, keep rule as is.
if (intervalReplacements.isEmpty()) {
return rule;
}
// Add new IntervalAtoms representing the interval specifications.
for (Map.Entry<VariableTerm, IntervalTerm> interval : intervalReplacements.entrySet()) {
rewrittenBody.add(new IntervalAtom(interval.getValue(), interval.getKey()).toLiteral());
}
return new NormalRuleImpl(rewrittenHead, rewrittenBody);
}
use of at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom in project Alpha by alpha-asp.
the class LiteralInstantiationStrategyTest method defaultLazyGroundingCheckFalseGroundLiteral.
/**
* 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.FALSE, so we expect the instantiation strategy
* to determine that p(a) is FALSE. 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 defaultLazyGroundingCheckFalseGroundLiteral() {
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.FALSE);
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.FALSE, assignmentStatus);
assertEquals(1, staleSet.size());
assertTrue(staleSet.contains(pOfA));
}
use of at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom in project Alpha by alpha-asp.
the class LiteralInstantiationStrategyTest method defaultLazyGroundingNoAssignmentSubstituteNonGroundLiteral.
/**
* Uses {@link DefaultLazyGroundingInstantiationStrategy} to find ground
* instances for the partially ground positive literal "q(a, X)".
*
* 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
* assignment status (i.e. assignment status of the found ground instance)
* passed back with the substitution to be 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 defaultLazyGroundingNoAssignmentSubstituteNonGroundLiteral() {
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);
LinkedHashSet<Atom> staleSet = new LinkedHashSet<>();
DefaultLazyGroundingInstantiationStrategy strategy = new DefaultLazyGroundingInstantiationStrategy(workingMemory, new AtomStoreImpl(), Collections.emptyMap(), false);
strategy.setStaleWorkingMemoryEntries(staleSet);
strategy.setCurrentAssignment(null);
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.BasicAtom in project Alpha by alpha-asp.
the class LiteralInstantiationStrategyTest method defaultLazyGroundingSubstituteNonGroundLiteralWithFalseInstance.
/**
* 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.FALSE, so we expect an empty list from
* {@link LiteralInstantiationStrategy#getAcceptedSubstitutions(Literal, Substitution)}.
* Furthermore, we expect the atom q(a, b) to be added to the stale atom set.
*/
@Test
public void defaultLazyGroundingSubstituteNonGroundLiteralWithFalseInstance() {
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.FALSE);
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());
assertTrue(result.isEmpty());
assertEquals(1, staleSet.size());
assertTrue(staleSet.contains(groundAtom));
}
Aggregations