0

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

chenkun
  • 45
  • 6
  • as you already know, you have to register your own namespaces to the Jena model before writing it to System.out` - loading Turtle data into Virtuoso doesn't mean namespces will be registered. Those are just a serialization feature, but not a feature of the RDF data model itself. – UninformedUser Jan 04 '23 at 08:08
  • you can also try to just keep a local Turtle file with only the prefix declarations and load them into a Jena model first, then add the triples of the SPARQL query model to the model with the prefixes. – UninformedUser Jan 04 '23 at 08:11
  • I can set namespaces in virtuoso and some can be returned by jena, such as `SPARQL Result:`, `(Jena)model.listNameSpaces:` above, but some can't. There will be lot's of newly released datasets, so there is no guarantee whose namespaces is the most comprehensive. Locally maintaining namespaces may not seem like a perfect solution, would it be more appropriate to just get namespaces in SPARQL Endpoint(if return TURTLE)? Personal opinion, may have shortcomings, but also hope a lot of guidance :) – chenkun Jan 04 '23 at 10:31

2 Answers2

0

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.

TallTed
  • 9,069
  • 2
  • 22
  • 37
  • I have added namespaces to virtuoso and can see them on the page `http://localhost:8890/sparql?nsdecl`. But I don't know how to get these namespaces using jena without knowing the dba password. In the `Jena's SPARQL` query returned from TURTLE without `@prefix`, some are effective(rdf, rdfs, example, ...) but some are not(sio, cheminf, snomedct). – chenkun Jan 05 '23 at 05:11
0

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.

chenkun
  • 45
  • 6
  • I should also have noticed before — your prefixes don't do what you think, given the data as presented here. In your data block, the last object is `sio:CHEMINF_000257` which would expand to `http://semanticscience.org/resource/SIO_CHEMINF_000257` given your declaration of `@prefix sio: .`. Similar issues are seen with your `@prefix cheminf: .` and `sio:SIO_000008`. – TallTed Jan 05 '23 at 13:31