Search code examples
sparqlrdfowlontologyrdfs

How to get, in a given ontology, the datatype properties without a defined rdfs:range


I need to check, in a given ontology, by means of an SPARQL query, the datatype properties that does not have rdfs:range defined. For example, in the following code, the result I'm looking for would be dataproperty2.

ont:Class1  a  owl:Class .
ont:Class2  a  owl:Class .

ont:dataProperty1  a  owl:DatatypeProperty ;
rdfs:domain  ont:Class1 ;
rdfs:label   "dataProperty1"@en ;
rdfs:range   xsd:string .

ont:dataProperty2  a  owl:DatatypeProperty ;
rdfs:domain  ont:Class2 ;
rdfs:label   "dataProperty2"@en .

I have defined this SPARQL query that retrieve the number of properties that match with this condition, but since is an aggregated function, i.e, COUNT, I'm having problems to get the datatype properties, not the number, that does not have rdfs:range defined.

SELECT ?return WHERE 
{
  {
  SELECT (COUNT(?p) as ?pCount)
    WHERE
    {
      ?p rdf:type owl:DatatypeProperty .
      ?p rdfs:range ?range .
    }
  }
  {
  SELECT DISTINCT (COUNT(?p) as ?prop)
    WHERE
    {
      ?p rdf:type owl:DatatypeProperty .
    }
  }
  BIND((?prop - ?pCount) as ?return) 
}

Solution

  • Just select the datatype properties and then filter out the ones that don't have range properties:

    select ?p where {
      ?p a owl:DatatypeProperty
      filter not exists {
        ?p rdfs:range ?range
      }
    }