Previous topic  Top  Next topic  Print this Topic
 

Ontology Management

 

Prior to working with a knowledge base, the necessary ontologies have to be loaded or created. After modifying a file-based ontology, it has to be saved in order to preserve the changes. This topic deals with ontology management.

Opening an Existing Ontology from a File Location

If we intend to access an existing ontology stored in a file, we first of all need to import it. OntologyManager contains multiple overloads of the importOntology() method for doing this.

Using a File parameter is convenient as this lets us easily specify both relative and absolute paths on the local host. Please note that local host means the computer where your Java application is running. This is not necessarily the same as the host on which OntoBroker is running.

// open an ontology from a file

File ontologyFile = new File("resources/ontology.obl");

Ontology ontology = OntologyManager.importOntology(ontoFile, null);

Another alternative is to use one of the physical URI overloads; this is especially suitable for importing ontologies located on a Web server, although the 'file://' pseudoprotocol can be used to specify paths on the local file system.

// open an ontology from a web url

String ontologyPhysicalUri = "http://example.org/ontology.obl";

Ontology ontology = OntologyManager.importOntology(ontologyPhysicalUri, null);

For importing multiple ontology files at once, overloads exist for both parameter types. There is also one additional parameter in all the overloads, the ProgressListener; this allows for progress notifications of the client code, see Progress Listener for more information.

Creating a New Ontology

Having initialized an OntologyManager instance, it can be used to create a new ontology. Only the ontologyURI must be passed to the method, null is allowed in place of the additional options parameter:

// create a new ontology

Ontology ontology = manager.createOntology("http://www.example.org/MyOnto", null);

Note that the ontology URI is a purely logical concept and although it may resemble a web URL, it does not in any way determine the file name or physical location upon saving. Also recall how the (logical) URI is different from the concept of a namespace. While a namespace can span multiple ontology files, there must be a one-to-one correspondence between ontology files and URIs. A (logical) URI corresponds to what is referred to as a module in F-Logic.

Saving an Ontology to a File

Whether you created a new ontology, or modified an existing one, it has to be saved to a file to preserve the changes. Saving an ontology to a file with the method saveOntology is straightforward; besides the file format, only the target must be provided. However, it is advisable to explicitly specify the text encoding to prevent problems with accented or national characters in a multi-platform setting.

Overloaded versions of the method also exist allowing writing an ontology to a an OutputStream or a Writer.

// save ontology to a file

File targetFile = new File("MyOntoFile.obl");

ontology.saveOntology(OntoBrokerOntologyFileFormat.OBJECTLOGIC,

                         targetFile, SerializationConstants.ENCODING_UTF);

Deleting an Ontology

When an ontology is no longer needed, it can be deleted. Deleting removes the ontology from the manager making it unavailable until it is re-imported again. This does not, however, modify the file system, should the ontology be originally imported from a file or not.

// delete an ontology

Ontology ontology = manager.getOntology("http://www.example.org/MyOnto");

manager.deleteOntology(ontology);