Skip to content

Commit

Permalink
Improve grounder selectivity.
Browse files Browse the repository at this point in the history
- IndexedInstanceStorage joins instances based on most selective
  attribute.
  • Loading branch information
AntoniusW committed Mar 9, 2020
1 parent d966bf0 commit 13c36c6
Showing 1 changed file with 17 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -166,23 +166,34 @@ public List<Instance> getInstancesMatchingAtPosition(Term term, int position) {
throw new RuntimeException("IndexedInstanceStorage queried for position " + position + " which is not indexed.");
}
ArrayList<Instance> matchingInstances = indexForPosition.get(term);
return matchingInstances == null ? new ArrayList<>() : matchingInstances;
return matchingInstances == null ? Collections.emptyList() : matchingInstances;
}


private int getFirstGroundTermPosition(Atom atom) {
private int getMostSelectiveGroundTermPosition(Atom atom) {
int smallestNumberOfInstances = Integer.MAX_VALUE;
int mostSelectiveTermPosition = -1;
for (int i = 0; i < atom.getTerms().size(); i++) {
Term testTerm = atom.getTerms().get(i);
if (testTerm.isGround()) {
return i;
ArrayList<Instance> instancesMatchingTest = indices.get(i).get(testTerm);
if (instancesMatchingTest == null) {
// Ground term at i matches zero instances, it is most selective.
return i;
}
int numInstancesTestTerm = instancesMatchingTest.size();
if (numInstancesTestTerm < smallestNumberOfInstances) {
smallestNumberOfInstances = numInstancesTestTerm;
mostSelectiveTermPosition = i;
}
}
}
return -1;
return mostSelectiveTermPosition;
}

public List<Instance> getInstancesFromPartiallyGroundAtom(Atom substitute) {
List<Instance> getInstancesFromPartiallyGroundAtom(Atom substitute) {
// For selection of the instances, find ground term on which to select.
int firstGroundTermPosition = getFirstGroundTermPosition(substitute);
int firstGroundTermPosition = getMostSelectiveGroundTermPosition(substitute);
// Select matching instances, select all if no ground term was found.
if (firstGroundTermPosition != -1) {
Term firstGroundTerm = substitute.getTerms().get(firstGroundTermPosition);
Expand Down

0 comments on commit 13c36c6

Please sign in to comment.