use of org.eclipse.rdf4j.rio.helpers.AbstractRDFHandler 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 AbstractRDFHandler() {
@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 IRI listHead = (IRI) 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);
}
}
use of org.eclipse.rdf4j.rio.helpers.AbstractRDFHandler in project incubator-rya by apache.
the class InferenceEngine method refreshAllValuesFromRestrictions.
private void refreshAllValuesFromRestrictions(final Map<Resource, IRI> restrictions) throws QueryEvaluationException {
allValuesFromByValueType.clear();
ryaDaoQueryWrapper.queryAll(null, OWL.ALLVALUESFROM, null, new AbstractRDFHandler() {
@Override
public void handleStatement(final Statement statement) throws RDFHandlerException {
final Resource directRestrictionClass = statement.getSubject();
if (restrictions.containsKey(directRestrictionClass) && statement.getObject() instanceof Resource) {
final IRI property = restrictions.get(directRestrictionClass);
final Resource valueClass = (Resource) statement.getObject();
// Should also be triggered by subclasses of the property restriction
final Set<Resource> restrictionClasses = new HashSet<>();
restrictionClasses.add(directRestrictionClass);
if (directRestrictionClass instanceof IRI) {
restrictionClasses.addAll(getSubClasses((IRI) directRestrictionClass));
}
for (final Resource restrictionClass : restrictionClasses) {
if (!allValuesFromByValueType.containsKey(valueClass)) {
allValuesFromByValueType.put(valueClass, new ConcurrentHashMap<>());
}
allValuesFromByValueType.get(valueClass).put(restrictionClass, property);
}
}
}
});
}
use of org.eclipse.rdf4j.rio.helpers.AbstractRDFHandler in project incubator-rya by apache.
the class InferenceEngine method fetchInstances.
/**
* Query for and collect all instances of a given type. Should only be called for types expected
* to have few members, such as ontology vocabulary terms, as instances will be collected in
* memory.
*/
private Set<IRI> fetchInstances(final IRI type) throws QueryEvaluationException {
final Set<IRI> instances = new HashSet<>();
ryaDaoQueryWrapper.queryAll(null, RDF.TYPE, type, new AbstractRDFHandler() {
@Override
public void handleStatement(final Statement st) throws RDFHandlerException {
if (st.getSubject() instanceof IRI) {
instances.add((IRI) st.getSubject());
}
}
});
return instances;
}
use of org.eclipse.rdf4j.rio.helpers.AbstractRDFHandler 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 IRI firstItem) throws QueryEvaluationException {
IRI 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 AbstractRDFHandler() {
@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 IRI object = (IRI) statement.getObject();
list.add(object);
}
});
final MutableObject<IRI> headHolder = new MutableObject<>();
// rdf.rest will point to the next bnode that's part of the list.
ryaDaoQueryWrapper.queryFirst(head, RDF.REST, null, new AbstractRDFHandler() {
@Override
public void handleStatement(final Statement statement) throws RDFHandlerException {
// This object is the next bnode head to look for.
final IRI object = (IRI) 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;
}
Aggregations