use of at.ac.tuwien.kr.alpha.core.grounder.WorkingMemory in project Alpha by alpha-asp.
the class LiteralInstantiatorTest method workingMemoryBasedVerifyPositiveGroundLiteralSatisfied.
@Test
public void workingMemoryBasedVerifyPositiveGroundLiteralSatisfied() {
Predicate p = Predicates.getPredicate("p", 2);
WorkingMemory workingMemory = new WorkingMemory();
workingMemory.initialize(p);
workingMemory.addInstance(Atoms.newBasicAtom(p, Terms.newSymbolicConstant("x"), Terms.newSymbolicConstant("y")), true);
VariableTerm x = Terms.newVariable("X");
VariableTerm y = Terms.newVariable("Y");
Literal lit = Literals.fromAtom(Atoms.newBasicAtom(p, x, y), true);
Substitution substitution = new BasicSubstitution();
substitution.put(x, Terms.newSymbolicConstant("x"));
substitution.put(y, Terms.newSymbolicConstant("y"));
LiteralInstantiator instantiator = new LiteralInstantiator(new WorkingMemoryBasedInstantiationStrategy(workingMemory));
LiteralInstantiationResult result = instantiator.instantiateLiteral(lit, substitution);
assertEquals(LiteralInstantiationResult.Type.CONTINUE, result.getType());
List<ImmutablePair<Substitution, AssignmentStatus>> substitutions = result.getSubstitutions();
assertEquals(1, substitutions.size());
assertEquals(AssignmentStatus.TRUE, substitutions.get(0).right);
Substitution resultSubstitution = substitutions.get(0).left;
// With the given input substitution, lit is ground and satisfied -
// we expect the instantiator to verify that.
assertEquals(substitution, resultSubstitution);
}
use of at.ac.tuwien.kr.alpha.core.grounder.WorkingMemory in project Alpha by alpha-asp.
the class LiteralInstantiatorTest method workingMemoryBasedVerifyPositiveGroundLiteralUnsatisfied.
@Test
public void workingMemoryBasedVerifyPositiveGroundLiteralUnsatisfied() {
Predicate p = Predicates.getPredicate("p", 2);
WorkingMemory workingMemory = new WorkingMemory();
workingMemory.initialize(p);
VariableTerm x = Terms.newVariable("X");
VariableTerm y = Terms.newVariable("Y");
Literal lit = Literals.fromAtom(Atoms.newBasicAtom(p, x, y), true);
Substitution substitution = new BasicSubstitution();
substitution.put(x, Terms.newSymbolicConstant("x"));
substitution.put(y, Terms.newSymbolicConstant("y"));
LiteralInstantiator instantiator = new LiteralInstantiator(new WorkingMemoryBasedInstantiationStrategy(workingMemory));
LiteralInstantiationResult result = instantiator.instantiateLiteral(lit, substitution);
// With the given input substitution, lit is ground, but not satisfied -
// we expect the instantiator to verify that and return an empty list of
// substitutions.
assertEquals(LiteralInstantiationResult.Type.STOP_BINDING, result.getType());
}
use of at.ac.tuwien.kr.alpha.core.grounder.WorkingMemory in project Alpha by alpha-asp.
the class StratifiedEvaluation method apply.
@Override
public // memories created here rather than re-initialize everything.
InternalProgram apply(AnalyzedProgram inputProgram) {
// Calculate a stratification and initialize the working memory.
ComponentGraph componentGraph = inputProgram.getComponentGraph();
List<ComponentGraph.SCComponent> strata = StratificationAlgorithm.calculateStratification(componentGraph);
predicateDefiningRules = inputProgram.getPredicateDefiningRules();
// Set up list of atoms which are known to be true - these will be expand by the evaluation.
Map<Predicate, Set<Instance>> knownFacts = new LinkedHashMap<>(inputProgram.getFactsByPredicate());
for (Map.Entry<Predicate, Set<Instance>> entry : knownFacts.entrySet()) {
workingMemory.initialize(entry.getKey());
workingMemory.addInstances(entry.getKey(), true, entry.getValue());
}
// Create working memories for all predicates occurring in each rule.
for (CompiledRule nonGroundRule : inputProgram.getRulesById().values()) {
for (Predicate predicate : nonGroundRule.getOccurringPredicates()) {
workingMemory.initialize(predicate);
}
}
workingMemory.reset();
// Set up literal instantiator.
literalInstantiator = new LiteralInstantiator(new WorkingMemoryBasedInstantiationStrategy(workingMemory));
// Evaluate the program part covered by the calculated stratification.
for (ComponentGraph.SCComponent currComponent : strata) {
evaluateComponent(currComponent);
}
// Build the program resulting from evaluating the stratified part.
// Add original input facts to newly derived ones.
additionalFacts.addAll(inputProgram.getFacts());
List<CompiledRule> outputRules = new ArrayList<>();
inputProgram.getRulesById().entrySet().stream().filter((entry) -> !solvedRuleIds.contains(entry.getKey())).forEach((entry) -> outputRules.add(entry.getValue()));
// NOTE: if InternalProgram requires solved rules, they should be added here.
return new InternalProgram(outputRules, additionalFacts);
}
use of at.ac.tuwien.kr.alpha.core.grounder.WorkingMemory 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.core.grounder.WorkingMemory 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());
}
Aggregations