On 07/05/13 21:05, David Jordan wrote:
I ran into this same problem two weeks ago. Joshua Taylor provided
me
with an answer, which I have included below. I ended up writing code
similar to the following, where I passed a Resource object
(OntModel.getResource(classURI)) representing the class to the method.
private long listProperties(Resource resource){
StmtIterator iter = omodel.listStatements(resource, (Property)null,
(RDFNode)null);
while( iter.hasNext() ){
Statement stmt = iter.next();
Property property = stmt.getPredicate();
String pname = property.getLocalName();
}
}
This solves a completely different problem to listDeclaredProperties().
What this code does is to list the triples in the graph whose subject is
the class resource itself. This is not the same as finding the
properties that you would normally expect to be associated with an
*instance* of the class. An example to clarify:
@prefix ex: <http://example.org/example#>.
ex:Dog
rdf:type owl:Class ;
rdfs:comment "The class of all dogs".
ex:pedigree
rdf:type owl:DatatypeProperty ;
rdfs:domain ex:Dog ;
rdfs:range xsd:boolean ;
rdfs:comment "Denotes whether a particular dog is a pedigree or not" .
In this case, the method you provide will return two values, "type"
(short for rdf:type) and "comment" (short for rdfs:comment), because
those are the only properties *of the class resource itself*.
Conversely, listDeclaredProperties() will return ex:pedigree, because
that's a property that we would expect to find (note: not are guaranteed
to find, this is a rather informal contract) on instances of ex:Dog. For
example:
ex:fido
rdf:type ex:Dog ;
ex:pedigree "false"^^xsd:boolean ;
rdfs:comment "Fido is a lovable mongrel!".
By the way, the method you wrote is almost exactly the listProperties()
method of OntClass (as inherited from Resource), except that your
version reduces the URI's to local names. Which, incidentally, isn't
generally a very good idea. Different predicates have different
semantics, and the full URI is necessary to distinguish predicates that
might otherwise have the same name. If you need a short name to present
a property in a UI, it's much better to use the rdfs:label or
skos:prefLabel.
Ian