code4good
code4good

Reputation: 1

SPARQL query does not find individual in imported ontology when run in jena

I have 3 ontology files where the first imports the second and the second imports the third:

The first ontology imports the second one:

<?xml version="1.0"?>
<rdf:RDF xmlns="http://www.example.com/user/rainer/ontologies/2016/1/usecase_individuals#"
     xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
     xmlns:owl="http://www.w3.org/2002/07/owl#"
     xmlns:xml="http://www.w3.org/XML/1998/namespace"
     xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
     xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
     xmlns:uc="http://www.example.com/user/rainer/ontologies/2016/1/usecase#">
    <owl:Ontology rdf:about="http://www.example.com/user/rainer/ontologies/2016/1/usecase_individuals">
        <owl:imports rdf:resource="http://www.example.com/user/rainer/ontologies/2016/1/usecase"/>
    </owl:Ontology>
....

The second ontology imports the thrid one:

<?xml version="1.0"?>
<rdf:RDF xmlns="http://www.example.com/user/rainer/ontologies/2016/1/usecase#"
     xml:base="http://www.example.com/user/rainer/ontologies/2016/1/usecase"
     xmlns:fgcm="http://www.example.com/user/rainer/ontologies/2016/1/fgcm#"
     xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
     xmlns:owl="http://www.w3.org/2002/07/owl#"
     xmlns:xml="http://www.w3.org/XML/1998/namespace"
     xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
     xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
     xmlns:uc="http://www.example.com/user/rainer/ontologies/2016/1/usecase#">
    <owl:Ontology rdf:about="http://www.example.com/user/rainer/ontologies/2016/1/usecase">
        <owl:imports rdf:resource="http://www.boeing.com/user/rainer/ontologies/2016/1/fgcm"/>
    </owl:Ontology>

....

And the third ontology (created in Protégé) asserts an individual:

<?xml version="1.0"?>
<rdf:RDF xmlns="http://www.boeing.com/user/rainer/ontologies/2016/1/fgcm#"

...

    <owl:NamedIndividual rdf:about="http://www.boeing.com/user/rainer/ontologies/2016/1/fgcm#admin">
        <rdf:type rdf:resource="http://www.boeing.com/user/rainer/ontologies/2016/1/fgcm#User"/>
        <userName>admin</userName>
    </owl:NamedIndividual>
...

When I open the first ontology in Protégé and execute the SPARQL query

PREFIX fgcm: <http://www.example.com/user/rainer/ontologies/2016/1/fgcm#>
SELECT ?subject ?name WHERE { ?subject fgcm:userName ?name}

it find the individual in the third ontology without a problem. When I run the same SPARQL query from code in Jena I don't get that individual. The query is run against an OntModel that was created with the default settings.

I know that Jena is able to load and import the ontologies because I can access classes and properties from the imported ontologies, both in SPARQL queries and directly using the Jena API. My problem appears to be limited to the individuals that are asserted in the imported ontology.

I have searched for settings (when loading the ontology such as the different OntModelSpecs or when creating/ running the query) that might change this behavior but haven't found any solutions.

Upvotes: 0

Views: 397

Answers (1)

code4good
code4good

Reputation: 1

It turned out that I was mistaken about Jena successfully loading the imported ontologies. (Not getting an error does not imply that the ontology that should be imported was actually found).

The SPARQL queries returned the expected result after using an OntDocumentManager and telling it where to find the ontology files that needed to be imported. This is the code snipped that worked for me:

OntDocumentManager mgr = new OntDocumentManager ();
mgr.addAltEntry("http://www.boeing.com/user/rainer/ontologies/2016/1/usecase", "file:C:\\Dev\\luna_workspace\\fgcm_translate\\usecase.owl");
mgr.addAltEntry("http://www.boeing.com/user/rainer/ontologies/2016/1/fgcm", "file:C:\\Dev\\luna_workspace\\fgcm_translate\\fgcm.owl");
OntModelSpec spec = new OntModelSpec ( OntModelSpec .OWL_DL_MEM_TRANS_INF);
spec.setDocumentManager(mgr);

OntModel model = ModelFactory.createOntologyModel(spec);

I hope this helps someone if they run into a similar problem.

Upvotes: 0

Related Questions