Is it possible to find out whether two instances are of the same class, programmatically (Using api such as JENA)
Asked
Active
Viewed 1,104 times
7
-
Can you use pure Java methods? – Jivings Jan 29 '12 at 19:31
-
2Why Jena? You can easily get the `.class` of the two objects and compare them. – Hot Licks Jan 29 '12 at 19:36
-
2I don't think the question concerns java instances and classes, but RDF / OWL instances and classes. – user205512 Jan 29 '12 at 21:42
-
user205512: you are right, I would like to compare RDF/OWL instances. – PCoder Jan 30 '12 at 09:22
4 Answers
8
Easy in SPARQL:
ASK { <instance1> a ?class . <instance2> a ?class . }
In 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.

user205512
- 8,798
- 29
- 28
-
Now that SPARQL 1.1 has property paths, the SPARQL query simplifies to `ASK { ?class ^a
, – Joshua Taylor Jun 30 '14 at 23:36}`. It captures very well "is there a class of which both instance1 and instance2 are instances?"
4
Assuming you are using the Jena ontology API, it's pretty straightforward. Note that in RDF, a given instance can have many types, so your question is really "how can I test if two instances have one or more types in common?".
I would do it as follows. Assume the two instances you want to test are Individual
objects (note that you can do this with OntResource
, or even Resource
with a slight change in the code):
Individual i0 = ....;
Individual i1 = ....;
List the rdf:type
values for each, and convert them to sets
Set<Resource> types0 = i0.listRDFTypes( false ).toSet();
Set<Resource> types1 = i1.listRDFTypes( false ).toSet();
They have types in common if the intersection is non-empty:
types0.retainAll( types1 );
if (!types0.isEmpty()) {
// at least one type in common
// types0 contains the common type resources
}

Ian Dickinson
- 12,875
- 11
- 40
- 67
-
That is more pleasant. Guava has `Sets.intersection` which would be even more readable. Typo: `...types1 = i1.listRDFTypes...` – user205512 Jan 31 '12 at 13:43
-
Hi, I still have some confusion. Hope you can clear that out. As you suggested I get the types and compare their intersection. The problem is: for instances of two different classes the `listRDFTypes` still returns `[http://www.w3.org/2002/07/owl#Class]` as a type. As a result, any two instances I take are of same type. Am I missing something? – PCoder Feb 02 '12 at 17:41
-
1If a resource has `rdf:type` `owl:Class`, it *is* a class not an instance of a class. Or there's something unusual or broken in your modelling. If you are convinced that having `rdf:type owl:Class` is correct for your domain model, then you could filter out `owl:Class` from the set. If this isn't enough of an explanation, please prepare a *minimal* example of the problem (code and data) and email it to the jena-users support list at Apache. – Ian Dickinson Feb 02 '12 at 20:42
0
Compare their classes:
boolean same = obj1.getClass().equals(obj2.getClass());

Bohemian
- 412,405
- 93
- 575
- 722
-
1The OP was not 100% clear about this, but the question most likely concerns RDF/OWL classes and instances, not Java class comparison. – Jeen Broekstra Jan 29 '12 at 22:59
-1
I take it this is an extension to your earlier post so
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
}

William Greenly
- 3,914
- 20
- 18