Is it possible to find out whether two instances are of the same RDF class, programmatically?
Asked Answered
Q

4

7

Is it possible to find out whether two instances are of the same class, programmatically (Using api such as JENA)

Quirites answered 29/1, 2012 at 18:40 Comment(4)
Can you use pure Java methods?Sec
Why Jena? You can easily get the .class of the two objects and compare them.Dayton
I don't think the question concerns java instances and classes, but RDF / OWL instances and classes.Clareclarence
user205512: you are right, I would like to compare RDF/OWL instances.Quirites
C
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.

Clareclarence answered 29/1, 2012 at 21:3 Comment(1)
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
D
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
}
Dissogeny answered 30/1, 2012 at 14:0 Comment(3)
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
T
0

Compare their classes:

boolean same = obj1.getClass().equals(obj2.getClass());
Thorianite answered 29/1, 2012 at 19:42 Comment(1)
The OP was not 100% clear about this, but the question most likely concerns RDF/OWL classes and instances, not Java class comparison.Metastasis
E
-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
}
Enarthrosis answered 30/1, 2012 at 17:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.