/** method and result synchronised on this */ private <T> ObjectQuery getObjectQuery(Class<T> concept, int length) throws MalformedQueryException, RepositoryException { if (queries.containsKey(concept) && queries.get(concept).containsKey(length)) { return queries.get(concept).get(length); } else { String sparql = of.createObjectQuery(concept, length); ObjectQuery query = prepareObjectQuery(SPARQL, sparql); Map<Integer, ObjectQuery> map = queries.get(concept); if (map == null) { queries.put(concept, map = new HashMap<Integer, ObjectQuery>()); } map.put(length, query); return query; } }
/** method and result synchronised on this */ private <T> ObjectQuery getObjectQuery(Class<T> concept, int length) throws MalformedQueryException, RepositoryException { if (queries.containsKey(concept) && queries.get(concept).containsKey(length)) { return queries.get(concept).get(length); } else { String sparql = of.createObjectQuery(concept, length); ObjectQuery query = prepareObjectQuery(SPARQL, sparql); Map<Integer, ObjectQuery> map = queries.get(concept); if (map == null) { queries.put(concept, map = new HashMap<Integer, ObjectQuery>()); } map.put(length, query); return query; } }
/** * 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(); }
ObjectQuery query = getConnection().prepareObjectQuery(q); Set<?> result = query.evaluate().asSet();
private ObjectQuery prepareObjectQuery(Class<?> concept) throws MalformedQueryException, RepositoryException { String base = query.getBaseURI(); String sparql = bindMultiples(query.toObjectString(concept)); return bindSingles(con.prepareObjectQuery(SPARQL, sparql, base)); }
ObjectQuery query = getConnection().prepareObjectQuery(q); result = query.evaluate().asSet(); } catch (MalformedQueryException | RepositoryException | QueryEvaluationException e) {
private ObjectQuery prepareObjectQuery(Class<?> concept) throws MalformedQueryException, RepositoryException { String base = query.getBaseURI(); String sparql = bindMultiples(query.toObjectString(concept)); return bindSingles(con.prepareObjectQuery(SPARQL, sparql, base)); }
/** * 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; }
ObjectQuery query = getConnection().prepareObjectQuery(q); result = query.evaluate().asSet();
@Override public Integer getCardinality(RDFSClazz domainClazz) throws RepositoryException { try { ObjectQuery query = getObjectConnection().prepareObjectQuery( "SELECT ?card {" + " ?r a owl:Restriction . " +
@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); } }
@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 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); } }
/** * 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 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); } }
ObjectQuery query = getConnection().prepareObjectQuery(q); Result<?> result = query.evaluate();