Re: Query VALUES Variable Names

2018-11-08 Thread Lorenz B.
Hello Greg,

I can understand that this is a bit misleading, but the methods you used
just work for VALUES clause "outside" of the query pattern:

|String ||queryString ||= ||"PREFIX dc:  
||\n||"||+ ||"PREFIX :  ||\n||"||+ ||"PREFIX 
ns:  ||\n||"||+ ||"||\n||"||+ ||"SELECT ?book 
?title ?price||\n||"||+ ||"{||\n||"||+ ||" ||\n||"||+ ||" ?book 
dc:title ?title ;||\n||"||+ ||" ns:price ?price .||\n||"||+ ||"}+ 
VALUES ?book { :book1 :book3 }"||; |

I don't know if there is any convenient method, but the VALUES clause in
query is inside an ElementGroup which contains a list of Element
objects, with the VALUES clause being of type ElementData: check this code

|ElementWalker.walk(query.getQueryPattern(), new ElementVisitorBase() 
{||@Overridepublic void visit(ElementData el) 
{System.out.println(el||);}||});|


If the VALUES clause is always in first position, it might work with

|List valueVars =
((ElementData)((ElementGroup)query.getQueryPattern()).get(||0||)).getVars();|


if not, you have to iterate over all elements and check which one is the
VALUES element.

Maybe one of the devs knows a better solution indeed ...


Kind regards,
Lorenz

> System.out.println("values_block");
>     String queryString = "PREFIX dc:
>  \n"
>     + "PREFIX :  \n"
>     + "PREFIX ns:    \n"
>     + "\n"
>     + "SELECT ?book ?title ?price\n"
>     + "{\n"
>     + "   VALUES ?book { :book1 :book3 }\n"
>     + "   ?book dc:title ?title ;\n"
>     + " ns:price ?price .\n"
>     + "}";
>     Query query = QueryFactory.create(queryString);
>
>     boolean result = query.hasValues();
>     boolean expResult = true;
>
>     System.out.println(query.toString());
>     System.out.println("Vars: " + query.getValuesVariables());
>     System.out.println("Exp: " + expResult);
>     System.out.println("Res: " + result); 

-- 
Lorenz Bühmann
AKSW group, University of Leipzig
Group: http://aksw.org - semantic web research center



Re: Query VALUES Variable Names

2018-11-08 Thread Andy Seaborne

Hi Greg,

query.getValuesVariables() applies specifically to the case of a 
trailing VALUES block.


SELECT *
WHERE {}
VALUES ?var { ... }

(I'll add some javadoc)

To get an inner VALUES block which can be anywhere in the graph pattern, 
the code has to walk the query or to walk into the WHERE pattern looking 
for the specific case of interest. Here, it is the fist syntax element 
in the ElementGroup which is the query pattern. (query.getQueryPattern().


Andy

On 07/11/2018 18:26, Greg Albiston wrote:

Hello,

I'm trying to retrieve the VALUES variable names of a Query parsed from 
a string.


When I run the below test the result is false when I was expecting true.

The query is from the SPARQL 1.1 standard.

Can the VALUES variable names be retrieved from another method?

Apologies if I've missed something,

Greg


@Test
     public void testValues_block() {
     System.out.println("values_block");
     String queryString = "PREFIX dc: 
 \n"

     + "PREFIX :  \n"
     + "PREFIX ns:    \n"
     + "\n"
     + "SELECT ?book ?title ?price\n"
     + "{\n"
     + "   VALUES ?book { :book1 :book3 }\n"
     + "   ?book dc:title ?title ;\n"
     + " ns:price ?price .\n"
     + "}";
     Query query = QueryFactory.create(queryString);

     boolean result = query.hasValues();
     boolean expResult = true;

     System.out.println(query.toString());
     System.out.println("Vars: " + query.getValuesVariables());
     System.out.println("Exp: " + expResult);
     System.out.println("Res: " + result);
     assertEquals(expResult, result);
     }