You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@jena.apache.org by Steve Vestal <st...@adventiumlabs.com> on 2020/03/01 17:06:45 UTC

Questions about blank nodes in SPARQL queries

I am using the results of one SPARQL query against an OntModel to
construct follow-on SPARQL queries.  The OntModel contains blank nodes
to denote some classes and individuals.  QueryResult gives me RDFNodes,
and I am using methods such as RDFNode#as(Class<T>) to obtain more
specific classifications for nodes including blank nodes (which seems to
work).  My understanding is that this gives me a Resource in the context
of the original, queried OntModel.

I am currently creating textual SPARQL queries.  My first question is,
what is the recommended way to represent a blank node in a textual
SPARQL query?  Is something like the following a reliable thing to do?

if (rdfNode.isAnon()) then {subjectName = "<" +
rdfNode.asResource().getId().getLabelString() + ">";}

My second question deals with the case where the initial SPARQL query is
a CONSTRUCT rather than a SELECT query, which returns a Model.  What is
the relationship between blank nodes in the constructed Model and the
OntModel that was initially queried?  If I visit triples using a
StmtIterator over the constructed Model, and create a textual name for a
blank node as above, can that name be used in a query applied to the
original OntModel?  How do I make sure that a blank node found in the
constructed Model will be mapped to the corresponding blank node in the
originating OntModel?




Re: Questions about blank nodes in SPARQL queries

Posted by Andy Seaborne <an...@apache.org>.
On 01/03/2020 17:06, Steve Vestal wrote:
> I am using the results of one SPARQL query against an OntModel to
> construct follow-on SPARQL queries.  The OntModel contains blank nodes
> to denote some classes and individuals.  QueryResult gives me RDFNodes,
> and I am using methods such as RDFNode#as(Class<T>) to obtain more
> specific classifications for nodes including blank nodes (which seems to
> work).  My understanding is that this gives me a Resource in the context
> of the original, queried OntModel.
 >
> I am currently creating textual SPARQL queries.  My first question is,
> what is the recommended way to represent a blank node in a textual
> SPARQL query?  Is something like the following a reliable thing to do?
> 
> if (rdfNode.isAnon()) then {subjectName = "<" +
> rdfNode.asResource().getId().getLabelString() + ">";}

It depends on how you are accessing the data.
If you have a same-JVM model (in-memory or TDB) as in your previous emails:

In a query string, or parameterized query string, use a URI starting "_:"

"<_:" +
   rdfNode.asResource().getId().getLabelString() +
  ">"

-------------------

If using Fuseki:

In the client, use RDFConnectionFactory.connectFuseki

OR
start the server with Context setting : arq:outputGraphBNodeLabels=true
causes blank nodes to come back with internal ids.

      --set arq:outputGraphBNodeLabels=true

and in the client:

      --set arq:inputGraphBNodeLabels=true

and use <_:...> in query strings.

> My second question deals with the case where the initial SPARQL query is
> a CONSTRUCT rather than a SELECT query, which returns a Model.  What is
> the relationship between blank nodes in the constructed Model and the
> OntModel that was initially queried?  If I visit triples using a
> StmtIterator over the constructed Model, and create a textual name for a
> blank node as above, can that name be used in a query applied to the
> original OntModel?  How do I make sure that a blank node found in the
> constructed Model will be mapped to the corresponding blank node in the
> originating OntModel?

When the data is same-JVM, it will be the same blank node as the data. 
The OntModel API works.
Use the Resource object from QuerySolution::geResource.

You don't need to create a string.

(Blank nodes have global system identifiers)

----------------------
Note about blank nodes:

If you find yourself using the blank nodes resources a lot and can 
change the data, then change the data to use a URI, even if one like 
<urn:uuid:...>

Blank nodes being blank are saying "you don't need the name - find it in 
the graph each time".

     Andy