In ObjectLogic, there are two ways of organizing ontology elements: modules and namespaces. Both can be used concurrently and independently of each other. The difference between the two may seem subtle in some situations, yet it is important to understand and differentiate them as they are powerful constructs that are encountered frequently. This chapter will briefly clarify the differences and then explain how the two concepts are reflected in the OntoBroker API.
Output from a demo program (read further for explanation):
Printing concepts from all modules from namespace: http://example.org/animals#
Concept Module
http://example.org/animals#Cobra http://example.org/Europe
http://example.org/animals#Fox http://example.org/America
http://example.org/animals#Jaguar http://example.org/America
http://example.org/animals#Mustang http://example.org/America
http://example.org/animals#Viper http://example.org/Europe
Printing concepts from all modules from namespace: http://example.org/cars#
Concept Module
http://example.org/cars#Cobra http://example.org/America
http://example.org/cars#Fox http://example.org/Europe
http://example.org/cars#Jaguar http://example.org/Europe
http://example.org/cars#Mustang http://example.org/America
http://example.org/cars#Viper http://example.org/America
The Difference Between namespaces and modules
When working with a single ontology, it corresponds to exactly one module. The module name is normally near the top of each ontology file. Because of this one-to-one correspondence, the module name is sometimes also called the ontology name (e.g. in OntoStudio). If you recall how ontology files are loaded from the chapter on Ontology Management, you will also notice a one-to-one correspondence between ontology files and instances of the Ontology class. The terms module, ontology, and ontology file may thus be used interchangeably, as long as the notion of a physical container for ontology elements described above is meant.
On the other hand, namespaces represent a logical concept which serves the purpose of naming and referring to ontology elements unambiguously. This is independent of modules: one module may contain elements from multiple namespaces and one namespace may span several modules.
For illustration purposes, consider the above listing; the first half are concepts from an '...animals' namespace, looking at the individual concepts, you will notice they come from two different modules: '...Europe' and '...America'. In the second half of the listing, you will see concepts with the same local names as above with a different namespace assigned, which disambiguates them. In a nutshell, modules (Europe, America) represent a 'physical' organization, whereas namespaces (cars, animals) capture a logical organization.
Namespaces and modules in the OntoBroker API
In this part, we will demonstrate how namespaces and modules are handled in the OntoBroker API. Although, the examples only cover adding concepts to existing ontologies, this should result in an intuitive understanding that can also be applied to other tasks.
The examples assume the existence of two ontologies, America and Europe, whose contents are listed above. Both ontologies contain concepts from a cars namespace as well as from an animals namespace; note that the concepts above are listed according to the namespace, not the ontology (module).
NOTE: There are certainly better ways to model cars and animal species than as unrelated concepts.
We intend to add the taxi and police favorite Chevrolet Impala to the 'America' ontology. We already have the necessary ontology object (americaOnto). Thus, we only need to create the necessary axiom, this can be achieved using one of the axiom factory methods. We need to explicitly specify not only the 'cars' namespace to make it clear that we do not mean the antelope but also the module, otherwise an attempt to add the axiom to an ontology would fail.
// add Impala to cars in America
// module explicit as text
// namespace explicit as text
Axiom ax = KAON2Manager.factory().axiom(
"<http://example.org/cars#Impala>[]@<http://example.org/America>.",null);
americaOnto.addAxiom(ax);
As a next step, we extend our knowledge base by adding the classic among cars, the Volkswagen Beetle, to the 'Europe' ontology. This time, we make use of a convenience method of the Ontology class, that allows adding textually specified axioms directly to an ontology. By using the convenience method, it is no longer necessary to specify the module explicitly, as it is implied by the ontology (europeOnto).
// add Beetle to cars in Europe
// module implicit (one module per ontology!)
// namespace explicit as text
europeOnto.addAxioms("<http://example.org/cars#Beetle>[].");
For adding Mercury Cougar to the 'America' ontology, we follow the two-step approach again, first creating the axiom object using a factory method. Hence, the module needs to be specified explicitly. However, using a different overload of the factory method, we now pass the module and namespace as separate arguments. The module can be simply retrieved by calling the appropriate method of the Ontology instance. The namespace can then be fixed using a Namespaces object, which has the ability to hold multiple namespace declarations along with their respective prefixes and one default namespace.
// add Cougar to cars in America
// module explicit as a Term
// namespace explicit, default using a Namespaces object
Namespaces ns = new Namespaces();
ns.setDefaultNamespace(carsNS); // "http://example.org/cars#"
ax = KAON2Manager.factory().axiom("Cougar[].", ns, americaOnto.getModule());
americaOnto.addAxiom(ax);
Similar to the previous example, we add the rabbit to the 'animals' ontology. Only this time we make use of a non-default namespace. Therefore, the registered namespace prefix must also appear in the textual definition of the new axiom.
// add Rabbit to animals in America
// module explicit as a Term
// namespace explicit, non-default using a prefix
ns.registerPrefix("an", animalsNS);
ax = KAON2Manager.factory().axiom("an#Rabbit[].", ns, europeOnto.getModule());
europeOnto.addAxiom(ax);
The rabbit concludes our excursion to modules and namespaces in the OntoBroker API.
Querying modules and namespaces in ObjectLogic
Let us finish up the section with a tip for an ObjectLogic query that may find useful when experimenting with modules and namespaces; it lists concepts from all modules and all namespaces currently loaded.
?- ?C[]@?M AND ?C[_localName->?LN, _namespace->?NS].