Can I find out if two instances of the same RDF class are programmatic?

Is it possible to find out if two instances of the same class are programmatic (using api like JENA)

+7
source share
4 answers

Easy in SPARQL:

ASK { <instance1> a ?class . <instance2> a ?class . } 

In the Jena API:

 boolean shareClass = false; for (Statement s: instance1.listProperties(RDF.type)) { if (instance2.hasProperty(RDF.type, s.getObject()) { shareClass = true; break; } } 

Not very elegant.

+8
source

Assuming you are using the Jena ontology API, it's pretty simple. Note that in RDF this instance can have many types, so your question is really β€œhow can I check if two instances have one or more types?”.

I would do it as follows. Suppose the two instances you want to test are Individual objects (note that you can do this with OntResource or even Resource with a little code change):

 Individual i0 = ....; Individual i1 = ....; 

List the rdf:type values ​​for each and convert them to settings

 Set<Resource> types0 = i0.listRDFTypes( false ).toSet(); Set<Resource> types1 = i1.listRDFTypes( false ).toSet(); 

They have common types if the intersection is nonempty:

 types0.retainAll( types1 ); if (!types0.isEmpty()) { // at least one type in common // types0 contains the common type resources } 
+4
source

Compare their classes:

 boolean same = obj1.getClass().equals(obj2.getClass()); 
0
source

I believe this is an extension for your earlier post, therefore

 if (resource1.hasProperty(model.createProperty("http://www.w3.org/1999/02/22-rdf-syntax-ns#", "type"), model.createResource("http://typeUri")) && resource2.hasProperty(model.createProperty("http://www.w3.org/1999/02/22-rdf-syntax-ns#", "type"), model.createResource("http://typeUri"))) { // both resources are the same type } 
-one
source

All Articles