Skip to content

Instantly share code, notes, and snippets.

@Locke
Last active July 5, 2018 12:20
Show Gist options
  • Save Locke/0bc3a3c009e4ce39e7a681b5eb55f98f to your computer and use it in GitHub Desktop.
Save Locke/0bc3a3c009e4ce39e7a681b5eb55f98f to your computer and use it in GitHub Desktop.
representativeEntities
I noticed that Openllet implements `getInstances` in a way that respects the IndividualNodeSetPolicy (BY_SAME_AS / BY_NAME).
However, when I use Openllet with the Java Stream interface from `OWLReasoner.instances` the nested nodes get flatMapped and the stream contains all elements, especially those "duplicates" I'm not interested in.
It looks to me like the Java Stream API is quite new, but will be the preferred way in the future of this project. Therefore I would expect a Stream API to get instances without the "same as duplicates".
I currently see some possible ways:
1. change `OWLReasoner.instances` to respect IndividualNodeSetPolicy (would be a breaking change)
2. provide an additional method for such retrieval in the OWLReasoner interface (could be independent of the IndividualNodeSetPolicy setting, might be interesting for a mixed-mode usage)
3. leave this to the reasoner implementation, i.e. override OWLReasoner.instances in PelletReasoner
4. leave it to the user: build a stream based on the result of `getInstances` (that's what I'm doing for now)
PS: same applies to objectPropertyValues / getObjectPropertyValues
For 1. I could imagine to add a `representativeEntities` method to the DefaultNodeSet class (just a sketch):
```java
public Stream<E> representativeEntities() {
return nodes().map(Node::getRepresentativeElement);
}
```
And change `OWLReasoner.instances`:
```java
default Stream<OWLNamedIndividual> instances(OWLClassExpression ce, boolean direct) {
switch (this.getIndividualNodeSetPolicy()) {
case BY_SAME_AS:
return getInstances(ce, direct).representativeEntities();
case BY_NAME:
return getInstances(ce, direct).entities();
default:
throw new AssertionError("Unsupported IndividualNodeSetPolicy : " + this.getIndividualNodeSetPolicy());
}
}
```
def instances(direct: Boolean = false)(implicit reasoner: OWLReasoner): IndividualStream = {
val nodes: JStream[Node[OWLNamedIndividual]] = reasoner.getInstances(self, direct).nodes()
reasoner.getIndividualNodeSetPolicy match {
case IndividualNodeSetPolicy.BY_SAME_AS => nodes.map(_.getRepresentativeElement)
case IndividualNodeSetPolicy.BY_NAME => nodes.flatMap(_.entities()) // @see DefaultNodeSet.entities
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment