use of org.openrdf.rio.helpers.RDFHandlerBase in project incubator-rya by apache.
the class InferenceEngine method getList.
/**
* Queries for all items that are in a list of the form:
* <pre>
* <:A> ?x _:bnode1 .
* _:bnode1 rdf:first <:B> .
* _:bnode1 rdf:rest _:bnode2 .
* _:bnode2 rdf:first <:C> .
* _:bnode2 rdf:rest rdf:nil .
* </pre>
* Where {@code :_bnode1} represents the first item in the list and
* {@code ?x} is some restriction on {@code <:A>}. This will return the
* list of resources, {@code [<:B>, <:C>]}.
* @param firstItem the first item in the list.
* @return the {@link List} of {@link Resource}s.
* @throws QueryEvaluationException
*/
private List<Resource> getList(final URI firstItem) throws QueryEvaluationException {
URI head = firstItem;
final List<Resource> list = new ArrayList<>();
// Go through and find all bnodes that are part of the defined list.
while (!RDF.NIL.equals(head)) {
// rdf.first will point to a type item that is in the list.
ryaDaoQueryWrapper.queryFirst(head, RDF.FIRST, null, new RDFHandlerBase() {
@Override
public void handleStatement(final Statement statement) throws RDFHandlerException {
// The object found in the query represents a type
// that should be included in the list.
final URI object = (URI) statement.getObject();
list.add(object);
}
});
final MutableObject<URI> headHolder = new MutableObject<>();
// rdf.rest will point to the next bnode that's part of the list.
ryaDaoQueryWrapper.queryFirst(head, RDF.REST, null, new RDFHandlerBase() {
@Override
public void handleStatement(final Statement statement) throws RDFHandlerException {
// This object is the next bnode head to look for.
final URI object = (URI) statement.getObject();
headHolder.setValue(object);
}
});
// of the list. Keep going until we reach rdf.nil.
if (headHolder.getValue() != null) {
head = headHolder.getValue();
} else {
head = RDF.NIL;
}
}
return list;
}
use of org.openrdf.rio.helpers.RDFHandlerBase in project incubator-rya by apache.
the class InferenceEngine method refreshOneOf.
private void refreshOneOf() throws QueryEvaluationException {
final Map<Resource, Set<Resource>> enumTypes = new HashMap<>();
// First query for all the owl:oneOf's.
// If we have the following oneOf:
// :A owl:oneOf (:B, :C)
// It will be represented by triples following a pattern similar to:
// <:A> owl:oneOf _:bnode1 .
// _:bnode1 rdf:first <:B> .
// _:bnode1 rdf:rest _:bnode2 .
// _:bnode2 rdf:first <:C> .
// _:bnode2 rdf:rest rdf:nil .
ryaDaoQueryWrapper.queryAll(null, OWL.ONEOF, null, new RDFHandlerBase() {
@Override
public void handleStatement(final Statement statement) throws RDFHandlerException {
final Resource enumType = statement.getSubject();
// listHead will point to a type class of the enumeration.
final URI listHead = (URI) statement.getObject();
if (!enumTypes.containsKey(enumType)) {
enumTypes.put(enumType, new LinkedHashSet<Resource>());
}
// enumeration.
try {
final Set<Resource> enumeration = new LinkedHashSet<>(getList(listHead));
if (!enumeration.isEmpty()) {
// Add this enumeration for this type.
enumTypes.get(enumType).addAll(enumeration);
}
} catch (final QueryEvaluationException e) {
throw new RDFHandlerException("Error getting enumeration list.", e);
}
}
});
synchronized (enumerations) {
enumerations.clear();
enumerations.putAll(enumTypes);
}
}
Aggregations