/** * Checks whether there is any other resource than the given one which has an {@code rdfs:label} * which would possibly result in the same identifier name by {@link #toJavaName(String)}. * @param resource The resource that has the label. * @param label The label to check for. * @return Returns true iff there is no possibly conflicting label within the repository. * @throws RepositoryException Thrown if an error occurs while querying the repository. */ private boolean isRDFSLabelUnique(ResourceObject resource, String label) throws RepositoryException { // A conflicting label may be lead by some non-alphanumeric characters: StringBuilder regex = new StringBuilder("([^a-zA-Z0-9])*"); for(int i = 0; i < label.length(); i++) { char c = label.charAt(i); if(Character.isJavaIdentifierPart(c)) { regex.append(c); } // Between each char (and at the end) there may be non-alphanumeric chars (would be pruned out by name generation): regex.append("([^0-9a-zA-Z])*"); } try { return !connection.prepareBooleanQuery(QueryLanguage.SPARQL, "ASK {" + " ?s rdfs:label ?l . " + " FILTER( ?s != <" + resource.getResourceAsString() + "> && REGEX(LCASE(str(?l)), \"" + regex + "\") )" + "}" ).evaluate(); } catch (MalformedQueryException | QueryEvaluationException | RepositoryException e) { throw new RepositoryException(e); } }
return !connection.prepareBooleanQuery(QueryLanguage.SPARQL, "ASK {" + " ?s ?p ?o . " +
private BooleanQuery prepareBooleanQuery() throws MalformedQueryException, RepositoryException { String base = query.getBaseURI(); String sparql = bindMultiples(query.toString()); return bindSingles(con.prepareBooleanQuery(SPARQL, sparql, base)); }
private BooleanQuery prepareBooleanQuery() throws MalformedQueryException, RepositoryException { String base = query.getBaseURI(); String sparql = bindMultiples(query.toString()); return bindSingles(con.prepareBooleanQuery(SPARQL, sparql, base)); }
@Override public boolean hasParent(String resource) throws RepositoryException { try { BooleanQuery query = getObjectConnection().prepareBooleanQuery(QueryLanguage.SPARQL, "ASK {" + " <" + getResourceAsString() + "> rdfs:subClassOf+ <" + resource + "> . " + "}" ); return query.evaluate(); } catch (MalformedQueryException | QueryEvaluationException e) { throw new RepositoryException(e); } }
/** * Returns true if the resource object this support class belongs to has the given type, * i.e. a {@code rdf:type}-path exists from the resource to the given class. * @param type The type for which to check. This should be (but is not required to be) a RDFS/OWL class. * @return Returns true iff the resource has the specified {@code type}. * @throws RepositoryException Thrown if an error occurs while querying the repository. */ protected boolean isInstance(Resource type) throws RepositoryException { ObjectConnection connection = getObjectConnection(); try { // Do a SPARQL ASK query for a rdf:type edge or a subclass relationship of any assigned type: BooleanQuery query = connection.prepareBooleanQuery(QueryLanguage.SPARQL, "ASK {" + " {" + " <" + getResourceAsString() + "> rdf:type+ <" + type.toString() + "> . " + " } UNION {" + " <" + getResourceAsString() + "> a ?c ." + " ?c rdfs:subClassOf+ <" + type.toString() + "> . " + " }" + "}" ); return query.evaluate(); } catch (MalformedQueryException | RepositoryException | QueryEvaluationException e) { throw new RepositoryException(e); } }
/** * Returns whether the resource is of a certain type given by its IRI. * @param typeIri The IRI of the type to check for. * @return Returns true iff the resource is of the above type. * @throws RepositoryException Thrown if an error occurs while querying the repository. */ private boolean hasType(String typeIri) throws RepositoryException { try { ObjectConnection connection = getObjectConnection(); return connection.prepareBooleanQuery(QueryLanguage.SPARQL, "ASK {" + " <" + getResourceAsString() + "> a <" + typeIri + "> . " + "}" ).evaluate(); } catch (QueryEvaluationException | MalformedQueryException e) { throw new RepositoryException(e); } }
/** * Whether the mapping for the given property needs to be redefined in this class. * This is the case if the class has a (direct) {@code owl:Restriction} on the property * for this class. * @param property The property to check for. * @return Returns true if the property mapping needs a redefinition in the Java class generated * for this class. * @throws RepositoryException Thrown if an error occurs while querying the repository. */ boolean needsRedefinition(RDFSProperty property) throws RepositoryException { try { BooleanQuery query = getObjectConnection().prepareBooleanQuery(QueryLanguage.SPARQL, "ASK {" + " ?r a owl:Restriction . " + " <" + getResourceAsString() + "> rdfs:subClassOf ?r . " + " ?r owl:onProperty <" + property.getResourceAsString() + "> . " + "}" ); return query.evaluate(); } catch (MalformedQueryException | QueryEvaluationException e) { throw new RepositoryException(e); } }
@Override public boolean hasPropertyTransitive(RDFSProperty property) throws RepositoryException { try { BooleanQuery query = getObjectConnection().prepareBooleanQuery(QueryLanguage.SPARQL, "ASK {" + " {" + " <" + property.getResourceAsString() + "> rdfs:domain <" + getResourceAsString() + "> . " + " }" + " UNION" + " {" + " <" + getResourceAsString() + "> rdfs:subClassOf+ ?s ." + " <" + property.getResourceAsString() + "> rdfs:domain ?s ." + " }" + "}" ); return query.evaluate(); } catch (QueryEvaluationException | MalformedQueryException e) { throw new RepositoryException(e); } }