use of at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution in project Alpha by alpha-asp.
the class NaiveGrounder method continueBinding.
/**
* Helper method used by {@link NaiveGrounder#bindNextAtomInRule(RuleGroundingOrderImpl, int, int, int, BasicSubstitution)}.
*
* Takes an <code>ImmutablePair</code> of a {@link BasicSubstitution} and an accompanying {@link AssignmentStatus} and calls
* <code>bindNextAtomInRule</code> for the next literal in the grounding order.
* If the assignment status for the last bound literal was {@link AssignmentStatus#UNASSIGNED}, the <code>remainingTolerance</code>
* parameter is decreased by 1. If the remaining tolerance drops below zero, this method returns an empty {@link BindingResult}.
*
* @param groundingOrder
* @param orderPosition
* @param originalTolerance
* @param remainingTolerance
* @param lastLiteralBindingResult
* @return the result of calling bindNextAtomInRule on the next literal in the grounding order, or an empty binding result if remaining
* tolerance is less than zero.
*/
private BindingResult continueBinding(RuleGroundingOrder groundingOrder, int orderPosition, int originalTolerance, int remainingTolerance, ImmutablePair<Substitution, AssignmentStatus> lastLiteralBindingResult) {
Substitution substitution = lastLiteralBindingResult.left;
AssignmentStatus lastBoundLiteralAssignmentStatus = lastLiteralBindingResult.right;
switch(lastBoundLiteralAssignmentStatus) {
case TRUE:
return advanceAndBindNextAtomInRule(groundingOrder, orderPosition, originalTolerance, remainingTolerance, substitution);
case UNASSIGNED:
// The last literal bound to obtain the current substitution has not been assigned a truth value by the solver yet.
// If we still have enough tolerance, we can continue grounding nevertheless.
int toleranceForNextRun = remainingTolerance - 1;
if (toleranceForNextRun >= 0) {
return advanceAndBindNextAtomInRule(groundingOrder, orderPosition, originalTolerance, toleranceForNextRun, substitution);
} else {
return BindingResult.empty();
}
case FALSE:
throw Util.oops("Got an assignmentStatus FALSE for literal " + groundingOrder.getLiteralAtOrderPosition(orderPosition) + " and substitution " + substitution + " - should not happen!");
default:
throw Util.oops("Got unsupported assignmentStatus " + lastBoundLiteralAssignmentStatus);
}
}
use of at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution in project Alpha by alpha-asp.
the class EnumerationAtom method addEnumerationIndexToSubstitution.
/**
* Based on a given substitution, substitutes the first two terms of this {@link EnumerationAtom} with the values from the substitution,
* and returns a new substitution with all mappings from the input substitution plus a binding for the third term of the enum atom to the
* integer index that is mapped to the first two terms in the internal <code>ENUMERATIONS</code> map.
*
* @param substitution an input substitution which must provide ground terms for the first two terms of the enumeration atom.
* @return a new substitution where the third term of the enumeration atom is bound to an integer.
*/
public Substitution addEnumerationIndexToSubstitution(Substitution substitution) {
Term idTerm = this.getTerms().get(0).substitute(substitution);
Term enumerationTerm = this.getTerms().get(1).substitute(substitution);
if (!enumerationTerm.isGround()) {
throw new RuntimeException("Enumeration term is not ground after substitution. Should not happen.");
}
Integer enumerationIndex = getEnumerationIndex(idTerm, enumerationTerm);
BasicSubstitution retVal = new BasicSubstitution(substitution);
retVal.put((VariableTerm) getTerms().get(2), Terms.newConstant(enumerationIndex));
return retVal;
}
use of at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution 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.commons.substitutions.BasicSubstitution in project Alpha by alpha-asp.
the class LiteralInstantiationStrategyTest method defaultLazyGroundingSubstituteNonGroundLiteralWithUnassignedInstance.
/**
* 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 empty assignment, so we
* expect the assignment status (i.e. assignment status of the found ground
* instance) passed back with the substitution to be UNASSIGNED. Since
* UNASSIGNED and FALSE atoms are (potentially) stale in working memory, we
* expect the atom "q(a, b)" to be added to the stale set by the instantiation
* strategy.
*/
@Test
public void defaultLazyGroundingSubstituteNonGroundLiteralWithUnassignedInstance() {
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);
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.UNASSIGNED, assignmentStatus);
assertTrue(substitution.isVariableSet(Terms.newVariable("X")));
assertEquals(Terms.newSymbolicConstant("b"), substitution.eval(Terms.newVariable("X")));
assertEquals(1, staleSet.size());
assertTrue(staleSet.contains(Atoms.newBasicAtom(q, Terms.newSymbolicConstant("a"), Terms.newSymbolicConstant("b"))));
}
use of at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution in project Alpha by alpha-asp.
the class AtomCounterTests method createRuleAtom.
private void createRuleAtom() {
BasicAtom atomAA = Atoms.newBasicAtom(Predicates.getPredicate("aa", 0));
CompiledRule ruleAA = new InternalRule(Heads.newNormalHead(atomAA), Collections.singletonList(Atoms.newBasicAtom(Predicates.getPredicate("bb", 0)).toLiteral(false)));
atomStore.putIfAbsent(new RuleAtom(ruleAA, new BasicSubstitution()));
}
Aggregations