Search code examples
virtuososesame

How to get the prefix and namespace list from Virtuoso, using Sesame


How to get the prefix and namespace list for a vocabulary (or Graph) in Virtuoso, like we have it in Sesame:

In Sesame, we are able to retrieve the prefix and namespace list for a vocabulary (repository) using RepositoryConnection object,

RepositoryResult<Namespace> nameSpaces = connection.getNamespaces();

but how do we get the same list for the vocabulary, when we upload it to Virtuoso.

VirtGraph gives default prefix and namespace list, but not giving the prefix and namespace list from the uploaded vocabulary.


Solution

  • The wording of your initial question suggests that you may think that "vocabulary", "Graph", and "repository" are all synonyms. They are not! A "repository" (also known as a data store) may hold one or many "Graphs", one or more of which might contain a "vocabulary" (or, more commonly in RDF parlance, an "ontology"), which describe the terminology used to describe some class(es) of entity(ies), with or without "instance data" (sometimes known as "records", which are the actual descriptions of some actual instances of those classes).

    That said -- PREFIX (or @prefix) statements in RDF-Turtle, RDF-N3, and similar files are not actually part of the data; they are part of the serialization. Thus, they are not automatically persisted as prefixes or namespaces in the Virtuoso data store.

    The Virtuoso Conductor provides a section for defining namespaces (http://{virtuoso-host-fqdn}:{port}/conductor/Linked DataNamespaces). We generally recommend working through that interface, but experts can also work directly with the relevant SQL table, DB.DBA.SYS_XML_PERSISTENT_NS_DECL. Namespaces defined here are used when Virtuoso produces serialized output in formats which support CURIEs (a/k/a Compact URIs), and when Virtuoso interprets CURIEs in SPARQL queries and elsewhere.

    You can see the currently defined namespaces through the built-in page, http://{virtuoso-host-fqdn}:{port}/sparql?help=nsdecl, as may be seen on DBpedia, or through any SQL connection (iSQL, ODBC, JDBC, etc.) --

      SELECT  NS_PREFIX, 
              NS_URL 
        FROM  DB.DBA.SYS_XML_PERSISTENT_NS_DECL
    ORDER BY  LOWER(NS_PREFIX) ;
    

    You can also use Sesame (now RDF4J) methods to get these, as in this snippet from the documentation and sample code we provide --

    // test getNamespace
    Namespace testns = null;
    RepositoryResult<Namespace> namespaces = null;
    boolean hasNamespaces = false;
    
    try {
        namespaces = con.getNamespaces();
        hasNamespaces = namespaces.hasNext();
        while (namespaces.hasNext()) {
            Namespace ns = namespaces.next();
            // LOG("Namespace found: (" + ns.getName() + " " + ns.getPrefix() + ")");
            testns = ns;
        }
    }
    catch (Exception e) {
        log("Error[" + e + "]");
        e.printStackTrace();
        ok = false;
    }
    

    Our Providers also have methods for deleting and updating registered namespaces, which are implemented in the VirtuosoRepositoryConnection class, as discussed in the API docs for each Provider (RDF4J, Sesame 4, Sesame 2).

    (ObDisclaimer: OpenLink Software produces Virtuoso, and employs me.)