3
1

In the answer to this question here, using SPARQL 1.1, I wanted to bind a new variable based on the value of an existing one. Specifically, I had calculated an ID for the day of the week such that 0 = Sunday, 1 = Monday, 2 = Tuesday, etc. Based on the binding for ?dayID, I wanted to bind ?dayName.

Essentially, I want to code some traditional "cases" structure into SPARQL. Factoring out the day ID calculation for brevity (here hardcoded as Sunday), I tried:

SELECT ?dayName
WHERE { 
    BIND (0 AS ?dayID)

    FILTER (?dayIDCheck = ?dayID)
}
BINDINGS ?dayIDCheck ?dayName {
    (0 "Sunday"@en)
    (1 "Monday"@en)
    (2 "Tuesday"@en)
    (3 "Wednesday"@en)
    (4 "Thursday"@en)
    (5 "Friday"@en)
    (6 "Saturday"@en)
}

Expecting to get ?dayName bound to "Sunday"@en. However, over at sparql.org, I get no answers.

So, concrete questions:

  • Can anyone explain why the above method doesn't work?
  • Anyone got an alternative method to implementing "cases" in SPARQL 1.1?

asked 24 Mar '12, 23:40

Signified's gravatar image

Signified ♦
23.1k623
accept rate: 38%

edited 25 Mar '12, 11:57


BINDINGS defines a data table that is joined with the results of the query. It is not the substitution of the variables by the values.

So:

  SELECT * { ... FILTER(?x = 23) }
  BINDINGS ?x { (1) (2) (3) }

execute the { ... FILTER(?x = 23) }, where ?x is unbound, then joins it to the data table. Being unbound the graph pattern evaluates to the empty table, and joining it to the BINDINGS is still empty.

(NB: This was not my preferred design - I preferred the substitution semantics you expected.)

link

answered 25 Mar '12, 12:41

AndyS's gravatar image

AndyS ♦
12.4k27
accept rate: 32%

Thanks for the answer. That's a real pity.

(25 Mar '12, 12:48) Signified ♦ Signified's gravatar image
1

The WG is still open to comments. SPARQL is in the lucky position that there are several real and complete implementations so feedback from depoyed experience before formally completing the spec is possible.

(25 Mar '12, 12:54) AndyS ♦ AndyS's gravatar image

Here's what I know. That query wouldn't run at all against Virtuoso so I simplified the query slightly and set up a test case against a Jena in-memory model

@Test
public void testBinding() {
    Query q=QueryFactory.create(
        "SELECT ?dayName WHERE {"+ 
        "   FILTER (?dayIDCheck=0)"+
        "}"+
        "BINDINGS ?dayIDCheck ?dayName {"+
        "   (0 'Sunday'@en)"+
        "   (1 'Monday'@en)"+
        "   (2 'Tuesday'@en)"+
        "   (3 'Wednesday'@en)"+
        "   (4 'Thursday'@en)"+
        "   (5 'Friday'@en)"+
        "   (6 'Saturday'@en)" +
        "}"
    );
    Model m=ModelFactory.createDefaultModel();
    QueryExecution qe=QueryExecutionFactory.create(q,m);
    ResultSet r=qe.execSelect();
    assertEquals(1,r.getResultVars().size());
    assertEquals("dayName",r.getResultVars().get(0));
    assertTrue(r.hasNext());
    QuerySolution qs=r.nextSolution();
    assertEquals("Sunday",qs.get("dayName").asLiteral().getLexicalForm());  
}

This test case fails on the assertTrue() so that means no solutions are coming back.

I do get results if I (1) remove the FILTER or (2) write the following for the FILTER

 FILTER(!bound(?dayIDCheck))

so I guess that FILTER and BINDINGS don't work together in Jena the way you and I would expect them to. Perhaps this is a bug in Jena or perhaps we're not reading the SPARQL 1.1 spec correctly.

Note in a case like this, you can always write the old if-then-else ladder...

 IF(?x=0,"Sunday",IF(?x=1,"Monday",IF(?x=2,"Tuesday"...)))
link

answered 25 Mar '12, 12:16

database_animal's gravatar image

database_animal ♦
8.3k612
accept rate: 15%

Thanks for the details. It seems from @neumarcx's answer that the query works as we would expect in newer versions of Joseki, which tends to suggest that it's a bug with the implementations. I really can't see any reason why it wouldn't work wrt. the spec (unless maybe there was an issue with the FILTER scoping being mismatched with the BINDINGS scoping). Also, thanks for the if-then-else ladder. Hadn't seen that pattern before!

(25 Mar '12, 12:29) Signified ♦ Signified's gravatar image

Looks like your runtime sets ?dayIDCheck but doesn't bind it.

The query works just fine in my run-time environment and executes as expected.

link

answered 25 Mar '12, 11:33

neumarcx's gravatar image

neumarcx
192
accept rate: 0%

edited 25 Mar '12, 11:44

Signified's gravatar image

Signified ♦
23.1k623

You mean a bug in the endpoint?

(25 Mar '12, 11:34) Signified ♦ Signified's gravatar image
1

yep looks like you run fuseki

(25 Mar '12, 11:40) neumarcx neumarcx's gravatar image

Cool! What SPARQL engine is lotico running?

(25 Mar '12, 11:47) Signified ♦ Signified's gravatar image
1

we run a couple of different engines but for production we have use ARQ on joseki 3.4.4

For the new stuff we play with fuseki, and virtuoso is pretty cool as well. we will also take look at stardog soon.

(25 Mar '12, 11:52) neumarcx neumarcx's gravatar image
Your answer
toggle preview

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

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×543
×95
×46

Asked: 24 Mar '12, 23:40

Seen: 2,054 times

Last updated: 25 Mar '12, 12:54