How does SPARQL queries automatically read namespaces for abbreviations in virtuoso?

70 views Asked by At

I've set up some namespaces in virtuoso, but when I used jena to do sparql queries in virtuoso, some namespaces were not abbreviated:

What I want to do: SPARQL queries automatically read namespaces for abbreviations, rather than collecting all namespaces locally.

Do jena and virtuoso support this?

I know a way to do this, but it requires collecting all namespaces locally:

  1. SPARQL query
  2. Create map stores namespaces and abbreviations locally
  3. Replace uri's prefix with abbreviations from map

Information for reference

Virtuoso version 07.20.3235 (64e6ecd39) on Win64 (x86_64-generic-win-64) Single Server Edition

Data:

@prefix rdf:        <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix sio:     <http://semanticscience.org/resource/SIO_> .
@prefix cheminf:    <http://semanticscience.org/resource/CHEMINF_> .
@prefix snomedct:     <http://purl.bioontology.org/ontology/SNOMEDCT/> .
@prefix example:    <http://example.org/resource/> .

example:7732-18-5 rdf:type snomedct:101782006 .
example:7732-18-5 sio:SIO_000008 example:Descriptor_water_boiling_point_15039 .
example:Descriptor_water_boiling_point_15039 rdf:type sio:CHEMINF_000257 .

Jena code & SPARQL:

RDFConnection conn = RDFConnectionRemote.service("http://localhost:8890/sparql").build();
Model model = conn.queryConstruct("CONSTRUCT { ?s ?p ?o . } WHERE { ?s ?p ?o . FILTER ( ?s = <http://example.org/resource/7732-18-5> ) }");

// SPARQL Result
model.write(System.out, "ttl");

// model.listNameSpaces
Iterator iterator = model.listNameSpaces();
while (iterator.hasNext()) {
    System.out.println(iterator.next().toString());
}

SPARQL Result:

@prefix example: <http://example.org/resource/> .
@prefix ns1:     <http://semanticscience.org/resource/> .
@prefix rdf:     <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .

example:7732-18-5  rdf:type  <http://purl.bioontology.org/ontology/SNOMEDCT/101782006> ;
        ns1:SIO_SIO_000008  example:Descriptor_water_boiling_point_15039 .

(Jena)model.listNameSpaces:

http://www.w3.org/1999/02/22-rdf-syntax-ns#
http://purl.bioontology.org/ontology/SNOMEDCT/101782006
http://semanticscience.org/resource/

Best regards

2

There are 2 answers

1
TallTed On

Virtuoso allows you to "pre-set" persistent namespace declarations, which will be available for use in all SPARQL queries, and will typically be used in result documents that support namespaces, among other things. Adding such persistent declarations is typically and best done through the Conductor admin interface.

You can see the list of predeclared namespaces on any Virtuoso instance via the /sparql endpoint with a simple query argument of ?nsdecl. For example, see the following page for the Namespace Prefixes on the public DBpedia endpoint — https://dbpedia.org/sparql?nsdecl

To use these predefined namespaces in SPARQL through Jena, you may need to bypass the Jena/ARQ parser, as documented.

As may be obvious, you cannot be assured that any new Virtuoso instance will have the same namespaces predefined, so it is almost always best to handle these yourself, within your Turtle, SPARQL, and other documents that support inline namespaces.

1
chenkun On

Thanks everyone, this problem has been solved.

Not a bug, it's mandated by turtle (https://www.w3.org/TR/turtle/#sec-grammar). A prefixed name must begin with PN_CHARS_BASE after the colon.

Refer to: https://github.com/openlink/virtuoso-opensource/issues/1090.