public Result asResult() throws OpenRDFException { ObjectQuery qry = prepareObjectQuery(Object.class); return qry.evaluate(); }
public Result asResult() throws OpenRDFException { ObjectQuery qry = prepareObjectQuery(Object.class); return qry.evaluate(); }
/** * Matches objects that have the given concept rdf:type. This method will * include all objects that implement the given concept or a subclass of the * concept. The concept must be a named concept and cannot be mapped to * rdfs:Resource. The result of this method is not guaranteed to be unique * and may continue duplicates. Use the {@link Result#asSet()} method to * ensure uniqueness. * * @see #addDesignation(Object, Class) */ public synchronized <T> Result<T> getObjects(Class<T> concept) throws RepositoryException, QueryEvaluationException { try { return getObjectQuery(concept, 0).evaluate(concept); } catch (MalformedQueryException e) { throw new AssertionError(e); } }
/** * Matches objects that have the given concept rdf:type. This method will * include all objects that implement the given concept or a subclass of the * concept. The concept must be a named concept and cannot be mapped to * rdfs:Resource. The result of this method is not guaranteed to be unique * and may continue duplicates. Use the {@link Result#asSet()} method to * ensure uniqueness. * * @see #addDesignation(Object, Class) */ public synchronized <T> Result<T> getObjects(Class<T> concept) throws RepositoryException, QueryEvaluationException { try { return getObjectQuery(concept, 0).evaluate(concept); } catch (MalformedQueryException e) { throw new AssertionError(e); } }
public <T> Result<T> asResult(Class<T> of) throws OpenRDFException { if (of == null || Object.class.equals(of)) return asResult(); ObjectQuery qry = prepareObjectQuery(of); return qry.evaluate(of); }
public <T> Result<T> asResult(Class<T> of) throws OpenRDFException { if (of == null || Object.class.equals(of)) return asResult(); ObjectQuery qry = prepareObjectQuery(of); return qry.evaluate(of); }
/** * Finds a single object of given concept and uri. */ public synchronized <T> T findObject(Class<T> concept, Resource resource) throws RepositoryException, QueryEvaluationException { try { ObjectQuery query = getObjectQuery(concept, 0); query.setBinding("subj", resource); return query.evaluate(concept).next(); } catch (MalformedQueryException e) { throw new AssertionError(e); } }
/** * Returns how many values of the individuals <code>subjectURI</code> property <code>propertyURI</code> * are of a certain type. * @param subjectURI The URI of the subject for which the property values will be checked. * @param propertyURI The URI of the property which values will be checked. * @param valueTypeURI The URI of the class that will be counted across the values. * @return Returns the number of values of type <code>valueTypeURI</code> that <code>subjectURI</code> has * for the property <code>propertyURI</code>. * @throws RepositoryException Thrown if an error occurs regarding the connected triplestore. * @throws MalformedQueryException Thrown if the issued query to the triplestore is malformed, e.g. because any argument is not a valid IRI. * @throws QueryEvaluationException Thrown if the issued query could not be evaluated. */ private int getValuesOfTypeCount(String subjectURI, String propertyURI, String valueTypeURI) throws RepositoryException, MalformedQueryException, QueryEvaluationException { ObjectQuery query = getConnection().prepareObjectQuery(QUERY_PREFIX + "SELECT (COUNT(DISTINCT ?o) as ?count) " + "{" + " <" + subjectURI + "> <" + propertyURI + "> ?o . " + " { " + " ?o a ?t ." + " ?t rdfs:subClassOf+ <" + valueTypeURI + "> . " + " }" + " UNION " + " {" + " ?o a <" + valueTypeURI + "> . " + " }" + "}"); return ((BigInteger) query.evaluate().next()).intValue(); }
/** * Returns the superclasses of a class including all transitive superclasses and the class * itself. * @param clazz The class for which to find superclasses. * @return The transitive closure of superclasses of the given class including <code>clazz</code>. */ private static Set<BuildableRDFSClazz> getSuperclassClosure(BuildableRDFSClazz clazz) throws RepositoryException { Set<BuildableRDFSClazz> closure = Sets.newHashSet(clazz); ObjectConnection connection = clazz.getObjectConnection(); try { ObjectQuery query = connection.prepareObjectQuery( "SELECT ?super {" + " <" + clazz.getResourceAsString() + "> rdfs:subClassOf+ ?super . " + "}" ); closure.addAll(query.evaluate(BuildableRDFSClazz.class).asSet()); } catch (QueryEvaluationException | MalformedQueryException e) { throw new RepositoryException(e); } return closure; }
@Override public Set<RDFSProperty> getSubProperties() throws RepositoryException { try { ObjectQuery query = getObjectConnection().prepareObjectQuery( "SELECT ?sub {" + " ?sub rdfs:subPropertyOf <" + getResourceAsString() + "> ." + "}" ); return query.evaluate(RDFSProperty.class).asSet(); } catch (QueryEvaluationException | MalformedQueryException e) { throw new RepositoryException(e); } }
/** * Returns all resources that exist in a specific context. * @param context The context from which to get resources. * @return All resources (IRIs) that are present in {@code context}. * @throws RepositoryException Thrown if an error occurs while querying the repository. */ private Set<ResourceObject> getResourcesFromContext(URI context) throws RepositoryException { try { String q = QUERY_PREFIX + "SELECT ?o " + "FROM NAMED <" + context.toString() + "> " + "{" + " GRAPH <" + context.toString() + "> {" + " { ?o ?p ?x . } UNION { ?x ?p ?o . }" + " FILTER ( isIRI(?o) )" + " }" + "}"; Set<ResourceObject> resources = new HashSet<>(); for (Object current : getConnection().prepareObjectQuery(q).evaluate().asSet()) { if(current instanceof ResourceObject) { resources.add((ResourceObject) current); } } return resources; } catch (QueryEvaluationException | MalformedQueryException e) { throw new RepositoryException(e); } }
/** * Queries the restrictions about this property declared for the given class. * @param domainIri The IRI of the class in which context to query for restrictions. * @return Returns the restrictions for this property at the given class. * @throws RepositoryException Thrown if an error occurs while querying the repository. */ private Collection<Restriction> queryRestrictions(String domainIri) throws RepositoryException { try { return getObjectConnection().prepareObjectQuery( "SELECT ?r {" + " ?r a owl:Restriction ." + " <" + domainIri + "> rdfs:subClassOf+ ?r . " + " ?r owl:onProperty <" + getResourceAsString() + "> ." + "}" ).evaluate(Restriction.class).asSet(); } catch (QueryEvaluationException | MalformedQueryException e) { throw new RepositoryException(e); } }
/** * Returns the buildable named resource objects of RDFS classes that were found during * the last call to {@link #build()}. * * @return Returns the RDFS classes in the model built. */ @Override public Collection<BuildableRDFSClazz> getClazzes() throws RepositoryException { try { ObjectConnection connection = anno4j.getObjectRepository().getConnection(); ObjectQuery query = connection.prepareObjectQuery( "SELECT DISTINCT ?c {" + " ?c rdfs:subClassOf+ owl:Thing . " + " FILTER( isIRI(?c) )" + "}" ); return query.evaluate(BuildableRDFSClazz.class).asSet(); } catch (MalformedQueryException | QueryEvaluationException e) { throw new RepositoryException(e); } }
@Override public Set<RDFSClazz> getSubclazzes() throws RepositoryException { try { ObjectQuery query = getObjectConnection().prepareObjectQuery( "SELECT ?sub {" + " ?sub rdfs:subClassOf <" + getResourceAsString() + "> . " + " FILTER(?super != <" + getResourceAsString() + ">)" + "}" ); return query.evaluate(RDFSClazz.class).asSet(); } catch (MalformedQueryException | QueryEvaluationException e) { throw new RepositoryException(e); } }
/** * Returns buildable named resource objects of RDFS classes that were found during * the last call to {@link #build()} which are pairwise distinct, * i.e. that are not declared equivalent. * @return All pairwise distinct named classes in the repository. * @throws RepositoryException Thrown if an error occurs while querying the repository. */ private Collection<BuildableRDFSClazz> getDistinctClasses() throws RepositoryException { try { ObjectConnection connection = anno4j.getObjectRepository().getConnection(); ObjectQuery query = connection.prepareObjectQuery( "SELECT DISTINCT ?c {\n" + " ?c rdfs:subClassOf+ owl:Thing . \n" + " MINUS {\n" + " ?e owl:equivalentClass ?c . \n" + " FILTER(str(?e) < str(?c))\n" + // Impose order on equivalence. Pick only first lexicographical " }\n" + " FILTER( isIRI(?c) )\n" + "}" ); return query.evaluate(BuildableRDFSClazz.class).asSet(); } catch (MalformedQueryException | QueryEvaluationException e) { throw new RepositoryException(e); } }
@Override public Set<RDFSClazz> getDirectSuperclazzes() throws RepositoryException { ObjectConnection connection = getObjectConnection(); try { ObjectQuery query = connection.prepareObjectQuery( "SELECT ?c {" + " <" + getResourceAsString() + "> rdfs:subClassOf ?c . " + " MINUS {" + " <"+ getResourceAsString() + "> rdfs:subClassOf+ ?c2 . " + " ?c2 rdfs:subClassOf+ ?c . " + " FILTER(?c != ?c2 && <" + getResourceAsString() + "> != ?c2)" + " }" + "}" ); return query.evaluate(RDFSClazz.class).asSet(); } catch (MalformedQueryException | QueryEvaluationException e) { throw new RepositoryException(e); } }
@Override protected CloseableIteration<?, ?> getObjects() throws RepositoryException, QueryEvaluationException { if (creator == null || factory == null) { return super.getObjects(); } else if (binding == null) { ObjectQuery query = factory.createQuery(creator); if (query == null) return super.getObjects(); try { query.setBinding("self", getResource()); return query.evaluate(creator.getPropertyType()); } finally { factory.returnQuery(creator, query); } } else { CloseableIteratorIteration<BindingSet, QueryEvaluationException> result; result = new CloseableIteratorIteration<BindingSet, QueryEvaluationException>( bindings.iterator()); return new ObjectCursor(getObjectConnection(), result, binding); } }
@Override protected synchronized CloseableIteration<?, ?> getObjects() throws RepositoryException, QueryEvaluationException { if (creator == null || factory == null) { return super.getObjects(); } else if (binding == null) { ObjectQuery query = factory.createQuery(creator); if (query == null) return super.getObjects(); try { query.setBinding("self", getResource()); return query.evaluate(creator.getPropertyType()); } finally { factory.returnQuery(creator, query); } } else { CloseableIteratorIteration<BindingSet, QueryEvaluationException> result; result = new CloseableIteratorIteration<BindingSet, QueryEvaluationException>( bindings.iterator()); return new ObjectCursor(getObjectConnection(), result, binding); } }
@Override public Set<RDFSProperty> getIncomingProperties() throws RepositoryException { try { ObjectQuery query = getObjectConnection().prepareObjectQuery( "SELECT ?p {" + " { " + " ?p rdfs:range <" + getResourceAsString() + "> . " + " }" + " UNION" + " { " + " ?p rdfs:range ?c . " + " <" + getResourceAsString() + "> rdfs:subClassOf+ ?c . " + " } " + " MINUS {" + " ?p2 rdfs:range <" + getResourceAsString() + "> . " + " ?p owl:equivalentProperty+ ?p2 . " + " FILTER( str(?p2) < str(?p) )" + " }" + "}" ); return query.evaluate(RDFSProperty.class).asSet(); } catch (MalformedQueryException | QueryEvaluationException e) { throw new RepositoryException(e); } }
@Override public Set<RDFSProperty> getOutgoingProperties() throws RepositoryException { try { ObjectQuery query = getObjectConnection().prepareObjectQuery( "SELECT ?p {" + " { " + " ?p rdfs:domain <" + getResourceAsString() + "> . " + " }" + " UNION" + " { " + " ?p rdfs:domain ?c . " + " <" + getResourceAsString() + "> rdfs:subClassOf+ ?c . " + " } " + " MINUS {" + " ?p2 rdfs:domain <" + getResourceAsString() + "> . " + " ?p owl:equivalentProperty+ ?p2 . " + " FILTER( str(?p2) < str(?p) )" + " }" + "}" ); return query.evaluate(RDFSProperty.class).asSet(); } catch (MalformedQueryException | QueryEvaluationException e) { throw new RepositoryException(e); } }