Search in sources :

Example 6 with AbstractRDFHandler

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

Example 7 with AbstractRDFHandler

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);
                }
            }
        }
    });
}
Also used : IRI(org.eclipse.rdf4j.model.IRI) AbstractRDFHandler(org.eclipse.rdf4j.rio.helpers.AbstractRDFHandler) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) RDFHandlerException(org.eclipse.rdf4j.rio.RDFHandlerException) Statement(org.eclipse.rdf4j.model.Statement) Resource(org.eclipse.rdf4j.model.Resource) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 8 with AbstractRDFHandler

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;
}
Also used : IRI(org.eclipse.rdf4j.model.IRI) AbstractRDFHandler(org.eclipse.rdf4j.rio.helpers.AbstractRDFHandler) RDFHandlerException(org.eclipse.rdf4j.rio.RDFHandlerException) Statement(org.eclipse.rdf4j.model.Statement) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 9 with AbstractRDFHandler

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

Aggregations

Statement (org.eclipse.rdf4j.model.Statement)9 RDFHandlerException (org.eclipse.rdf4j.rio.RDFHandlerException)9 AbstractRDFHandler (org.eclipse.rdf4j.rio.helpers.AbstractRDFHandler)9 HashSet (java.util.HashSet)7 IRI (org.eclipse.rdf4j.model.IRI)6 LinkedHashSet (java.util.LinkedHashSet)5 Set (java.util.Set)5 Resource (org.eclipse.rdf4j.model.Resource)5 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 IOException (java.io.IOException)3 RDFParseException (org.eclipse.rdf4j.rio.RDFParseException)3 RDFParser (org.eclipse.rdf4j.rio.RDFParser)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 ProducerRecord (org.apache.kafka.clients.producer.ProducerRecord)2 QueryEvaluationException (org.eclipse.rdf4j.query.QueryEvaluationException)2 RDFFormat (org.eclipse.rdf4j.rio.RDFFormat)2 UnsupportedRDFormatException (org.eclipse.rdf4j.rio.UnsupportedRDFormatException)2 JCommander (com.beust.jcommander.JCommander)1 ParameterException (com.beust.jcommander.ParameterException)1