semantic web - Adding an OPTIONAL clause to a SPARQL query using Jena ARQ -


is possible programmatically add optional clause sparql query using jena arq api? programmatically take query:

select ?concept ?p ?o {?s ?p ?o . } limit 10 

to this:

select  ?concept ?p ?o ?test {   ?s ?p ?o  optional { ?concept <http://www.test.com/test> ?test } } limit   10 

through arq it's simple add additional result variable ?test:

query q = queryfactory.create(query)     query.addresultvar(var); 

but i've found in api docs , trawling across net it's not possible add optional clause. need use different library?

yes can. see this introduction topic on apache jena site.

your starting point getting query pattern:

element pattern = q.getquerypattern(); 

that elementgroup if remember correctly. add optional in there:

((elementgroup) pattern).addelement(new elementoptional(...)); 

the ... bit elementtriplesblock, pretty straightforward.

inelegant, however. in general i'd recommend using visitors , algebra representation, direct route should work.


Comments