0
...
...
<owl:DatatypeProperty rdf:about="http://www.semanticweb.org/administrator/ontologies/2014/2/untitled-ontology-5#fileName">
    <rdfs:label>fileName</rdfs:label>
    <rdfs:comment>Name of File</rdfs:comment>
    <rdfs:domain rdf:resource="http://www.semanticweb.org/administrator/ontologies/2014/2/untitled-ontology-5#File"/>
    <rdfs:range rdf:resource="&xsd;string"/>
</owl:DatatypeProperty>


<owl:DatatypeProperty rdf:about="http://www.semanticweb.org/administrator/ontologies/2014/2/untitled-ontology-5#fileLastAccessed">
    <rdfs:label>fileLastAccessed</rdfs:label>
    <rdfs:comment>Time when the file was last accessed.</rdfs:comment>
    <rdfs:domain rdf:resource="http://www.semanticweb.org/administrator/ontologies/2014/2/untitled-ontology-5#File"/>
    <rdfs:range rdf:resource="&xsd;dateTime"/>
</owl:DatatypeProperty>
...
...

Above is a portion of the ontology that I have developed in protege.

You can see that datatype of the #fileName properties is &xsd;string and datatype of #fileLastAccessed is &xsd;dateTime

I am developing an application in which I would need to programmatically read the datatype of a property.

My question is that how would I know programmatically the datatype of a property.

Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
  • possible duplicate of [jena api get range of ObjectProperty](http://stackoverflow.com/questions/24250198/jena-api-get-range-of-objectproperty). That question specifically asks about object properties, but the same code works for data properties. – Joshua Taylor Aug 21 '14 at 17:26

1 Answers1

2

As you demonstrate in your example ontology, the range of a property is specified by the rdfs:range value on a rdf:Property/owl:DatatypeProperty instance.

Assuming that you had a Jena Model that contained the data in your example:

final Property fileName = model.getResource("http://www.semanticweb.org/administrator/ontologies/2014/2/untitled-ontology-5#fileName")
                               .as(Property.class);
final StmtIterator definedRanges = fileName.listProperties(RDFS.range)

In the previous code, definedRanges will be an iterator that is empty if no ranges were defined. If it is non-empty, it should indicate the range of your property. If your property is a owl:DatatypeProperty, then you should be safe assuming that it's specifying a literal datatype.

Rob Hall
  • 2,693
  • 16
  • 22
  • 2
    You can listProperties(RDFS.range) here, but you could also use OntProperty#listRange(), as illustrated in [jena api get range of ObjectProperty](http://stackoverflow.com/q/24250198/1281433) (the title says Object Property, but it should work with data properties too). – Joshua Taylor Aug 21 '14 at 17:29
  • 1
    That was exactly I needed, I further added below code to exactly extracct what I want `for (; definedRanges.hasNext(); ) { Statement stmt = definedRanges.next(); Property p2 = stmt.getPredicate(); RDFNode o2 = stmt.getObject(); ...` – Syed Rahman Mashwani Aug 21 '14 at 18:54