Jazz Forum Welcome to the Jazz Community Forum Connect and collaborate with IBM Engineering experts and users

OPTIONAL in SPARQL's queries

Hello,

I tried to execute a Sparql query that has an OPTIONAL block, but I received an error from JFS output, saying "SparqlToTripleQuery.g: node from line 11:1 mismatched input 'OPTIONAL' expecting <UP>".

It seems like OPTIONAL is not supported. Is there a way to overcome this issue?

As you know, the intention of having OPTIONAL in the SPARQL standard is very significant. It allows us to retrieve resources that have missing properties. This is very important for the support of enhancement of the resource. Later versions of the resource can have more properties, but the old resource can be retrieved (the new properties can be specified as OPTIONAL in the query).

Thanks very much,

Vu

0 votes



2 answers

Permanent link
Hello,

I tried to execute a Sparql query that has an OPTIONAL block, but I received an error from JFS output, saying "SparqlToTripleQuery.g: node from line 11:1 mismatched input 'OPTIONAL' expecting <UP>".

It seems like OPTIONAL is not supported. Is there a way to overcome this issue?

As you know, the intention of having OPTIONAL in the SPARQL standard is very significant. It allows us to retrieve resources that have missing properties. This is very important for the support of enhancement of the resource. Later versions of the resource can have more properties, but the old resource can be retrieved (the new properties can be specified as OPTIONAL in the query).

Thanks very much,

Vu


SPARQL queries return solutions/results that match all graph patterns. If a graph pattern does not match, the solution is rejected. But, sometimes we want to retrieve the solutions where optional patterns do not match. For example, a user resource has first name, last name, middle name, and age. We want to query a list of all users with all of these properties, noting that some users do not have middle name. The query should return all user resources available regardless of whether users have middle name or not. Without the OPTIONAL option, we have to have two queries, one for retrieving a list of users who have middle name and the other for users who don't have middle name. This is cumbersome and inefficient. A much better way is to use the OPTIONAL option, which looks like


PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?firstname ?middlename ?lastname ?age
WHERE {
?x foaf:firstname ?firstname .
?x foaf:lastname ?lastname .
?x foaf:age ?age .
OPTIONAL { ?x foaf:middlename ?middlename . }
}


With this SPARQL query, a solution is returned if it matches all first three graph patterns (in the WHERE statement). The last graph pattern which is enclosed in the OPTIONAL option indicates that if a user has middle name, match and return a solution with middle name, firstname, lastname, and age. Otherwise, still match and return a solution with firstname, lastname, and age (rather than rejecting this solution).

0 votes


Permanent link
Hi, in initial development of the Foundation we were using a SPARQL engine that was translating native SPARQL to another query form and this was incomplete and had certain limitations. We have more recently moved to a complete and native RDF/SPARQL solution and OPTIONAL is certainly now supported.

Hello,

I tried to execute a Sparql query that has an OPTIONAL block, but I received an error from JFS output, saying "SparqlToTripleQuery.g: node from line 11:1 mismatched input 'OPTIONAL' expecting <UP>".

It seems like OPTIONAL is not supported. Is there a way to overcome this issue?

As you know, the intention of having OPTIONAL in the SPARQL standard is very significant. It allows us to retrieve resources that have missing properties. This is very important for the support of enhancement of the resource. Later versions of the resource can have more properties, but the old resource can be retrieved (the new properties can be specified as OPTIONAL in the query).

Thanks very much,

Vu

0 votes

Your answer

Register or log in to post your answer.

Dashboards and work items are no longer publicly available, so some links may be invalid. We now provide similar information through other means. Learn more here.

Search context
Follow this question

By Email: 

Once you sign in you will be able to subscribe for any updates here.

By RSS:

Answers
Answers and Comments
Question details

Question asked: Jun 30 '09, 11:07 p.m.

Question was seen: 9,904 times

Last updated: Jun 30 '09, 11:07 p.m.

Confirmation Cancel Confirm