Tutorial, C++


All components of owlcpp reside in owlcpp namespace. In this tutorial we assume that the following alias is in effect:

namespace oc = owlcpp;

owlcpp compiles into three libraries: rdf, io, and logic. To use owlcpp in your code, please make sure your toolchain "knows" the location of owlcpp headers and libraries. One way to do this is to use Boost.Build for building your project, as shown in Getting started section.

Loading ontology document

To load an RDF/XML ontology document into the owlcpp triple store one has to create the triple store and invoke load_file() method:

oc::Triple_store triple_store;
load_file("ontology.owl", triple_store);

Triple_store class is defined in owlcpp/rdf/triple_store.hpp header.

load_file() is defined in owlcpp/io/input.hpp.

Ontology documents often have other ontologies as dependencies that also need to be loaded. To load the imported ontologies along with the main one, one has to create a catalog, specify the locations of the imports (e.g., by providing the name of the directory in which they are located), and pass the catalog to the loading method:

oc::Catalog catalog;
add(catalog, "path"); //path can be file or directory
load_file("ontology.owl", triple_store, catalog);

Searching RDF triples

The triples in the triple store can be searched by matching the IDs of the subject, predicate, object, or document. For example, to find every class declaration, one would search for triples containing owl:Class as the object. The IDs of the standard OWL nodes are predefined by the library and can be accessed, as in this example, as oc::terms::owl_Class::id(). The find_triple() method returns an iterator range over the triples matching the query. Iterating over the range can be done with a regular for loop, but the BOOST_FOREACH macro provides a more concise expression. The C++ code below illustrates defining this query, iterating over the matching triples, and printing their subjects.

   oc::Triple const& triple,
   triple_store.find_triple(oc::any(), oc::any(), oc::terms::owl_Class::id(), oc::any())
) {
   std::cout << to_string(triple.subj_, ts) << '\n';

To search for triples referring to a user-defined IRI node, one first has to look up the node's ID in the triple store and pass the ID to the find_triple() method.

oc::Node_id const* id = triple_store.find_node_iri("http://example.com#XYZ");

   oc::Triple const& triple,
   triple_store.find_triple(*id, oc::any(), oc::any(), oc::any())
) {
   std::cout << to_string(triple.obj_, triple_store) << '\n';

Converting RDF triples to DL axioms

Translation of RDF triples to axioms is implemented according to the W3C Recommendation.

If the triples do not meet some of the stated requirements, the process produces an exception carrying the relevant information. Axioms can be generated from the entire store, or from a subset of the triples.

Converting all triples in the triple store to axioms, passing them to the FaCT++ reasoner, and checking whether the ontology is consistent is performed as follows:

ReasoningKernel kernel;
submit(triple_store, kernel);
if(kernel.isKBConsistent()) cout << "consistent";

© 2015 Mikhail K. Levin | contact