Is it possible to find out whether two instances are of the same class, programmatically (Using api such as JENA)
Is it possible to find out whether two instances are of the same RDF class, programmatically?
Asked Answered
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.
Now that SPARQL 1.1 has property paths, the SPARQL query simplifies to
ASK { ?class ^a <instance1>, <instance2> }
. It captures very well "is there a class of which both instance1 and instance2 are instances?" –
Bordelon 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
}
That is more pleasant. Guava has
Sets.intersection
which would be even more readable. Typo: ...types1 = i1.listRDFTypes...
–
Clareclarence 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? –
Quirites If 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. –
Dissogeny Compare their classes:
boolean same = obj1.getClass().equals(obj2.getClass());
The OP was not 100% clear about this, but the question most likely concerns RDF/OWL classes and instances, not Java class comparison. –
Metastasis
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
}
© 2022 - 2024 — McMap. All rights reserved.
.class
of the two objects and compare them. – Dayton