use of at.ac.tuwien.kr.alpha.api.grounder.Substitution in project Alpha by alpha-asp.
the class StratifiedEvaluation method calcSubstitutionsWithGroundingOrder.
private List<Substitution> calcSubstitutionsWithGroundingOrder(RuleGroundingOrder groundingOrder, List<Substitution> startingSubstitutions) {
// Iterate through the grounding order and whenever instantiation of a Literal with a given substitution
// causes a result with a type other than CONTINUE, discard that substitution.
// Note that this function uses a stack of partial substitutions to simulate a recursive function.
// For speed, we really want ArrayLists on the stack.
Stack<ArrayList<Substitution>> substitutionStack = new Stack<>();
if (startingSubstitutions instanceof ArrayList) {
substitutionStack.push((ArrayList<Substitution>) startingSubstitutions);
} else {
// Copy startingSubstitutions into ArrayList. Note: mostly happens for empty or
substitutionStack.push(new ArrayList<>(startingSubstitutions));
// singleton lists.
}
int currentOrderPosition = 0;
List<Substitution> fullSubstitutions = new ArrayList<>();
while (!substitutionStack.isEmpty()) {
List<Substitution> currentSubstitutions = substitutionStack.peek();
// If no more substitutions remain at current position, all have been processed, continue on next lower level.
if (currentSubstitutions.isEmpty()) {
substitutionStack.pop();
currentOrderPosition--;
continue;
}
// In case the full grounding order has been worked on, all current substitutions are full substitutions, add them to
// result.
Literal currentLiteral = groundingOrder.getLiteralAtOrderPosition(currentOrderPosition);
if (currentLiteral == null) {
fullSubstitutions.addAll(currentSubstitutions);
currentSubstitutions.clear();
// Continue on next lower level.
substitutionStack.pop();
currentOrderPosition--;
continue;
}
// Take one substitution from the top-list of the stack and try extending it.
// Work on last element (removing last element is
Substitution currentSubstitution = currentSubstitutions.remove(currentSubstitutions.size() - 1);
// O(1) for ArrayList).
LiteralInstantiationResult currentLiteralResult = literalInstantiator.instantiateLiteral(currentLiteral, currentSubstitution);
if (currentLiteralResult.getType() == LiteralInstantiationResult.Type.CONTINUE) {
// The currentSubstitution could be extended, push the extensions on the stack and continue working on them.
ArrayList<Substitution> furtheredSubstitutions = new ArrayList<>();
for (ImmutablePair<Substitution, AssignmentStatus> resultSubstitution : currentLiteralResult.getSubstitutions()) {
furtheredSubstitutions.add(resultSubstitution.left);
}
substitutionStack.push(furtheredSubstitutions);
// Continue work on the higher level.
currentOrderPosition++;
}
}
return fullSubstitutions;
}
use of at.ac.tuwien.kr.alpha.api.grounder.Substitution in project Alpha by alpha-asp.
the class NaiveGrounderTest method testPermissiveGrounderHeuristicTolerance.
/**
* Tests if {@link NaiveGrounder#getGroundInstantiations(InternalRule, RuleGroundingOrder, Substitution, Assignment)}
* produces ground instantiations for the rule with ID {@code ruleID} in {@code program} when {@code startingLiteral}
* unified with the numeric instance {@code startingInstance} is used as starting literal and the following
* additional conditions are established:
* <ul>
* <li>The atoms {@code b([startingInstance], 1), ..., b([startingInstance], n)} are added to the grounder's
* working memory without changing the assignment, where {@code arityOfB-1} occurences of {@code startingInstance}
* are used instead of {@code [startingInstance]} and {@code n} is the length of the {@code truthsOfB} array.
* For example, if the length of {@code truthsOfB} is 2 and {@code arityOfB} is also 2, these atoms are
* {@code b(1,1), b(1,2)}.
* </li>
* <li>The same atoms are assigned the truth values in the {@code truthsOfB} array.</li>
* </ul>
* It is asserted that ground instantiations are produced if and only if {@code expectNoGoods} is true.
* If ground instantiations are produced, it is also asserted that the numbers of unassigned positive body atoms
* determined by {@code getGroundInstantiations} match those given in {@code expectedNumbersOfUnassignedPositiveBodyAtoms}.
*/
private void testPermissiveGrounderHeuristicTolerance(ASPCore2Program program, int ruleID, Literal startingLiteral, int startingInstance, int tolerance, ThriceTruth[] truthsOfB, int arityOfB, boolean expectNoGoods, List<Integer> expectedNumbersOfUnassignedPositiveBodyAtoms) {
CompiledProgram internalPrg = InternalProgram.fromNormalProgram(NORMALIZE_TRANSFORM.apply(program));
AtomStore atomStore = new AtomStoreImpl();
TrailAssignment currentAssignment = new TrailAssignment(atomStore);
GrounderHeuristicsConfiguration heuristicConfiguration = GrounderHeuristicsConfiguration.getInstance(tolerance, tolerance);
NaiveGrounder grounder = (NaiveGrounder) GrounderFactory.getInstance("naive", internalPrg, atomStore, p -> true, heuristicConfiguration, true);
int[] bAtomIDs = new int[truthsOfB.length];
for (int i = 0; i < truthsOfB.length; i++) {
int[] bTerms = new int[arityOfB];
for (int n = 0; n < arityOfB; n++) {
bTerms[n] = (n == arityOfB - 1) ? i + 1 : startingInstance;
}
bAtomIDs[i] = atomStore.putIfAbsent(atom("b", bTerms));
}
addAtomsToWorkingMemoryWithoutChangingTheAssignment(atomStore, grounder, bAtomIDs);
assign(currentAssignment, bAtomIDs, truthsOfB);
grounder.bootstrap();
final CompiledRule nonGroundRule = grounder.getNonGroundRule(ruleID);
final Substitution substStartingLiteral = BasicSubstitution.specializeSubstitution(startingLiteral, new Instance(Terms.newConstant(startingInstance)), BasicSubstitution.EMPTY_SUBSTITUTION);
final BindingResult bindingResult = grounder.getGroundInstantiations(nonGroundRule, nonGroundRule.getGroundingInfo().orderStartingFrom(startingLiteral), substStartingLiteral, currentAssignment);
assertEquals(expectNoGoods, bindingResult.size() > 0);
if (bindingResult.size() > 0) {
assertEquals(expectedNumbersOfUnassignedPositiveBodyAtoms, bindingResult.getNumbersOfUnassignedPositiveBodyAtoms());
} else {
assertTrue(bindingResult.getNumbersOfUnassignedPositiveBodyAtoms().isEmpty());
}
}
use of at.ac.tuwien.kr.alpha.api.grounder.Substitution 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.grounder.Substitution in project Alpha by alpha-asp.
the class SubstitutionTest method specializeTermsFunctionTermBinding.
@Test
public void specializeTermsFunctionTermBinding() {
Substitution substitution = new BasicSubstitution();
substitution.put(Y, A);
FunctionTerm groundFunctionTerm = Terms.newFunctionTerm("f", B, C);
Instance qfBC = new Instance(groundFunctionTerm);
Term nongroundFunctionTerm = Terms.newFunctionTerm("f", B, X);
BasicAtom qfBX = Atoms.newBasicAtom(Predicates.getPredicate("q", 1), nongroundFunctionTerm);
Substitution substitution1 = BasicSubstitution.specializeSubstitution(qfBX, qfBC, substitution);
assertEquals(C, substitution1.eval(X));
assertEquals(A, substitution1.eval(Y));
}
use of at.ac.tuwien.kr.alpha.api.grounder.Substitution in project Alpha by alpha-asp.
the class SubstitutionTest method specializeBasicAtom.
@Test
public void specializeBasicAtom() {
Predicate p = Predicates.getPredicate("p", 2);
BasicAtom atom = Atoms.newBasicAtom(p, Arrays.asList(X, Y));
Instance instance = new Instance(A, B);
Substitution substitution = BasicSubstitution.specializeSubstitution(atom, instance, BasicSubstitution.EMPTY_SUBSTITUTION);
BasicAtom substituted = atom.substitute(substitution);
assertEquals(p, substituted.getPredicate());
assertEquals(A, substituted.getTerms().get(0));
assertEquals(B, substituted.getTerms().get(1));
}
Aggregations