Discussion:
Query VALUES Variable Names
Greg Albiston
2018-11-07 18:26:31 UTC
Permalink
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:
<http://purl.org/dc/elements/1.1/> \n"
                + "PREFIX :     <http://example.org/book/> \n"
                + "PREFIX ns:   <http://example.org/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);
    }
Andy Seaborne
2018-11-08 12:09:02 UTC
Permalink
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
Post by Greg Albiston
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");
<http://purl.org/dc/elements/1.1/> \n"
                + "PREFIX :     <http://example.org/book/> \n"
                + "PREFIX ns:   <http://example.org/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);
    }
Lorenz B.
2018-11-08 10:50:36 UTC
Permalink
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: <http://purl.org/dc/elements/1.1/> ||\n||"||||||+ ||"PREFIX : <http://example.org/book/> ||\n||"||||||+ ||"PREFIX ns: <http://example.org/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() {||||||||||@Override||||public void visit(ElementData el) {||||System.out.println(el||);||||}||||||||||});|


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

|List<Var> 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
Post by Greg Albiston
System.out.println("values_block");
<http://purl.org/dc/elements/1.1/> \n"
                + "PREFIX :     <http://example.org/book/> \n"
                + "PREFIX ns:   <http://example.org/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
Continue reading on narkive:
Loading...