Tags

, , , , , , , ,


If we want to read an ontology that also import another ontology ( have an OWL: IMPORT), both the ontology URI has to resolve.

But, most of the time we may need to load both the ontology from file system. The default JENA “read” method will process the ontology automatically and try to resolve the Import ontology when it read the base ontology.

Let, we have the following ontology import statement in our base ontology:


<Ontology rdf:about=”http://cli.altaf.org/ontology/ServiceMetaDataOntology”>
      <imports rdf:resource=”http://cli.stfx.ca/altaf/PatientOntology”/>
  </Ontology>


Now, If we read the base ontology from our file system, clearly the imported ontology URI will not get resolved. This will result exception reading the ontology.

We can solve the problem using JENA provided OntDocumentManager and FileManager classes. OntDocumentManager let us use a policy file which can be defined in rdf.  In this policy rdf we can define our import ontology file as OntologySpec and OntDocumentManger can resolve the relative url of the imported ontology automatically.

You could find an example ont-policy file in your JENA download directory at : …./etc/ont-policy.rdf

Now, we will follow the following steps:

1) Create a Ont-Policy file and add our import ontology reference there.

2) Read our Base ontology file

This should automatically load both the ontology without any error.

1) Adding the Ont-Policy File: In our source folder or any other location of convenience.

image

Now add an OntologySpec node containing the relative url of the import ontology/ontologies


<OntologySpec>
    <!– local version of the OWL language ontology (in OWL) –>
    <publicURI rdf:resource=”http://cli.stfx.ca/altaf/PatientOntology” />
    <!– uncomment the following line to re-direct attempts to http get the file–>
   <altURL    rdf:resource=”file:./src/PatientOntology.rdf”/>
    <language  rdf:resource=”http://www.w3.org/2002/07/owl” />
    <prefix    rdf:datatype=”&xsd;string”>PO</prefix>
</OntologySpec>

We may add more of OntologySpec Node as the number of imports in File.

2) Now we can read the file using following method:

public static void ReadOntologyWithImport() {
    //Set document manager policy file
    OntDocumentManager dm = new OntDocumentManager(“file:./src/ont-policy.rdf”);
    OntModelSpec modelSpec = PelletReasonerFactory.THE_SPEC;
    modelSpec.setDocumentManager(dm);
    OntoModel  ontModelWithImport = ModelFactory.createOntologyModel(modelSpec);

//Read the base Ontology File ; Here its SmdWithImport
    ontModelWithImport.read(FileManager.get().open(“./src/SmdWithRule.rdf”), NS);
}


NOW YOUR ONTOLOGY should be in the OntModel without any exception with imported ontology. Cheers.! Smile

Workarounds:

There are few other work around.

1) If you don’t want to use document manager, you must make sure the url resolve:

One way of doing that is to create a server (may be in your localhost) and store ontologies there and read.

2) Use setProcessImport(false) and remove import statement from ontology. Then you can read the import files and add the model to the base model.

3) We can use the document manager and add OntologySpec using Jena Programmatically.

Also, there is way to programmatically add  Imports programmatically.

 

You can see the JENA ontology API documentation for more details.

Also, the following method can be used if there is NO IMPORTS statement in the ontology:

public static void ReadOntologyWithoutImport() {
        InputStream ontInputStream = FileManager.get().open(“./src/SmdWithRule.rdf”);
        if (ontInputStream == null) {
            System.out.println(“No input file Found”);
        } else {
            ontInputStream = FileManager.get().open(“./src/PatientOntology.rdf”);
            ontModelWithoutImport = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC);
            ontModelWithoutImport.read(ontInputStream, NS);
        }
    }

Advertisements