Search in sources :

Example 6 with RDFHandlerBase

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;
}
Also used : RDFHandlerException(org.openrdf.rio.RDFHandlerException) Statement(org.openrdf.model.Statement) Resource(org.openrdf.model.Resource) ArrayList(java.util.ArrayList) RDFHandlerBase(org.openrdf.rio.helpers.RDFHandlerBase) URI(org.openrdf.model.URI) MutableObject(org.apache.commons.lang3.mutable.MutableObject)

Example 7 with RDFHandlerBase

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);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) RDFHandlerException(org.openrdf.rio.RDFHandlerException) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Statement(org.openrdf.model.Statement) Resource(org.openrdf.model.Resource) RDFHandlerBase(org.openrdf.rio.helpers.RDFHandlerBase) URI(org.openrdf.model.URI)

Aggregations

Statement (org.openrdf.model.Statement)7 RDFHandlerException (org.openrdf.rio.RDFHandlerException)7 RDFHandlerBase (org.openrdf.rio.helpers.RDFHandlerBase)7 URI (org.openrdf.model.URI)6 HashSet (java.util.HashSet)5 LinkedHashSet (java.util.LinkedHashSet)5 Resource (org.openrdf.model.Resource)5 Set (java.util.Set)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 QueryEvaluationException (org.openrdf.query.QueryEvaluationException)2 IOException (java.io.IOException)1 List (java.util.List)1 MutableObject (org.apache.commons.lang3.mutable.MutableObject)1 ProducerRecord (org.apache.kafka.clients.producer.ProducerRecord)1 VisibilityStatement (org.apache.rya.api.model.VisibilityStatement)1 RyaStreamsException (org.apache.rya.streams.api.exception.RyaStreamsException)1 RDFFormat (org.openrdf.rio.RDFFormat)1 RDFParseException (org.openrdf.rio.RDFParseException)1