cwm/sparql mystery

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

cwm/sparql mystery

Jon Crump-2

dear all,
[cwm 1.2.1 on Mac intel OS X 10.5.4]

Some things I don't understand about the implementation of sparql in cwm:

With a testGraph.rdf (appended), the following query returns the expected
results:

PREFIX ex: <http://temp.example.org/terms/>
PREFIX loc: <http://simile.mit.edu/2005/05/ontologies/location#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>

SELECT ?event ?place
WHERE {{?event ex:date ?date .
     FILTER (?date >= "2008-01-04"^^xsd:date && ?date <=
"2008-01-08"^^xsd:date)}
     UNION {?event ex:starts ?start; ex:finishes ?end.
         FILTER ((?start >= "2008-01-04"^^xsd:date && ?end <=
"2008-01-08"^^xsd:date)
         || (?start <= "2008-01-04"^^xsd:date && ?end >=
"2008-01-04"^^xsd:date)
         || (?start <= "2008-01-08"^^xsd:date && ?end >=
"2008-01-08"^^xsd:date)
         )
    }
OPTIONAL { ?event loc:place ?place .}
}

However, If I modify the SELECT clause thus:
SELECT *

I get the following traceback and runtime error:

Traceback (most recent call last):
   File "/Applications/RDFapps/cwm-1.2.1/cwm", line 750, in <module>
     doCommand()
   File "/Applications/RDFapps/cwm-1.2.1/cwm", line 741, in doCommand
     flags=option_flags[option_format])
   File "/Applications/RDFapps/cwm-1.2.1/swap/llyn.py", line 1872, in
dumpNested
     pp. dumpNested()
   File "/Applications/RDFapps/cwm-1.2.1/swap/pretty.py", line 524, in
dumpNested
     self.selectDefaultPrefix(Serializer.dumpNested)
   File "/Applications/RDFapps/cwm-1.2.1/swap/pretty.py", line 114, in
selectDefaultPrefix
     printFunction(dummySerializer)
   File "/Applications/RDFapps/cwm-1.2.1/swap/pretty.py", line 525, in
dumpNested
     self.dumpFormulaContents(context, self.sink, sorting=1, equals=1)
   File "/Applications/RDFapps/cwm-1.2.1/swap/pretty.py", line 600, in
dumpFormulaContents
     self._dumpSubject(currentSubject, context, sink, sorting, statements)
   File "/Applications/RDFapps/cwm-1.2.1/swap/pretty.py", line 632, in
_dumpSubject
     self.dumpFormulaContents(subj, sink, sorting)
   File "/Applications/RDFapps/cwm-1.2.1/swap/pretty.py", line 600, in
dumpFormulaContents
     self._dumpSubject(currentSubject, context, sink, sorting, statements)
   File "/Applications/RDFapps/cwm-1.2.1/swap/pretty.py", line 697, in
_dumpSubject
     self.dumpStatement(sink, s.quad, sorting)
   File "/Applications/RDFapps/cwm-1.2.1/swap/pretty.py", line 707, in
dumpStatement
     self._outputStatement(sink, triple) # Do 1-loops simply
   File "/Applications/RDFapps/cwm-1.2.1/swap/pretty.py", line 280, in
_outputStatement
     sink.makeStatement(auxPairs(quad), aIsPossible=aWorks)
   File "/Applications/RDFapps/cwm-1.2.1/swap/toXML.py", line 328, in
makeStatement
     raise RuntimeError("Unexpected subject", `subj`)
RuntimeError: ('Unexpected subject', "(21, ('2008-01-04',
u'http://www.w3.org/2001/XMLSchema#date'))")

I get the same error if the SELECT clause is anything like this:

SELECT ?event ?date
or
SELECT ?event ?start
or
SELECT ?event ?date ?start ?end ?place

Something not implemented yet? Or something I don't understand about how
sparql is implemented in cwm?

Grateful for any illumination,
Jon

PS.
Also, in a (not related?) issue:
following the sparql query, include --sparqlResults

Results in the following error:

Traceback (most recent call last):
   File "/Applications/RDFapps/cwm-1.2.1/cwm", line 750, in <module>
     doCommand()
   File "/Applications/RDFapps/cwm-1.2.1/cwm", line 698, in doCommand
     from cwm_sparql import outputString, SPARQL_NS
ImportError: No module named cwm_sparql

===============
testGraph.rdf follows:
===============

<rdf:RDF  xmlns:ex="http://temp.example.org/terms/"
     xmlns:loc="http://simile.mit.edu/2005/05/ontologies/location#"
     xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
     xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">

     <ex:Event rdf:about="http://temp.example.org/terms/Event#case0">
         <ex:date
rdf:datatype="http://www.w3.org/2001/XMLSchema#date">2008-01-03</ex:date>
         <loc:place
rdf:resource="http://temp.example.org/terms/Place#case0_place" />
     </ex:Event>
     <ex:Event rdf:about="http://temp.example.org/terms/Event#case1">
         <ex:starts
rdf:datatype="http://www.w3.org/2001/XMLSchema#date">2008-01-01</ex:starts>
         <ex:finishes
rdf:datatype="http://www.w3.org/2001/XMLSchema#date">2008-01-03</ex:finishes>
         <loc:place
rdf:resource="http://temp.example.org/terms/Place#case1_place" />
     </ex:Event>
     <ex:Event rdf:about="http://temp.example.org/terms/Event#case2">
         <ex:starts
rdf:datatype="http://www.w3.org/2001/XMLSchema#date">2008-01-02</ex:starts>
         <ex:finishes
rdf:datatype="http://www.w3.org/2001/XMLSchema#date">2008-01-04</ex:finishes>
         <loc:place
rdf:resource="http://temp.example.org/terms/Place#case2_place" />
     </ex:Event>
     <ex:Event rdf:about="http://temp.example.org/terms/Event#case3">
         <ex:starts
rdf:datatype="http://www.w3.org/2001/XMLSchema#date">2008-01-03</ex:starts>
         <ex:finishes
rdf:datatype="http://www.w3.org/2001/XMLSchema#date">2008-01-05</ex:finishes>
         <loc:place
rdf:resource="http://temp.example.org/terms/Place#case3_place" />
     </ex:Event>
     <ex:Event rdf:about="http://temp.example.org/terms/Event#case4">
         <ex:starts
rdf:datatype="http://www.w3.org/2001/XMLSchema#date">2008-01-05</ex:starts>
         <ex:finishes
rdf:datatype="http://www.w3.org/2001/XMLSchema#date">2008-01-07</ex:finishes>
         <loc:place
rdf:resource="http://temp.example.org/terms/Place#case4_place" />
     </ex:Event>
     <ex:Event rdf:about="http://temp.example.org/terms/Event#case5">
         <ex:starts
rdf:datatype="http://www.w3.org/2001/XMLSchema#date">2008-01-03</ex:starts>
         <ex:finishes
rdf:datatype="http://www.w3.org/2001/XMLSchema#date">2008-01-09</ex:finishes>
         <loc:place
rdf:resource="http://temp.example.org/terms/Place#case5_place" />
     </ex:Event>
     <ex:Event rdf:about="http://temp.example.org/terms/Event#case6">
         <ex:date
rdf:datatype="http://www.w3.org/2001/XMLSchema#date">2008-01-04</ex:date>
         <loc:place
rdf:resource="http://temp.example.org/terms/Place#case6_place" />
     </ex:Event>
     <ex:Event rdf:about="http://temp.example.org/terms/Event#case7">
         <ex:date
rdf:datatype="http://www.w3.org/2001/XMLSchema#date">2008-01-06</ex:date>
         <loc:place
rdf:resource="http://temp.example.org/terms/Place#case7_place" />
     </ex:Event>
     <ex:Event rdf:about="http://temp.example.org/terms/Event#case8">
         <ex:date
rdf:datatype="http://www.w3.org/2001/XMLSchema#date">2008-01-08</ex:date>
         <loc:place
rdf:resource="http://temp.example.org/terms/Place#case8_place" />
     </ex:Event>
     <ex:Event rdf:about="http://temp.example.org/terms/Event#case9">
         <ex:starts
rdf:datatype="http://www.w3.org/2001/XMLSchema#date">2008-01-08</ex:starts>
         <ex:finishes
rdf:datatype="http://www.w3.org/2001/XMLSchema#date">2008-01-10</ex:finishes>
         <loc:place
rdf:resource="http://temp.example.org/terms/Place#case9_place" />
     </ex:Event>
     <ex:Event rdf:about="http://temp.example.org/terms/Event#case10">
         <ex:starts
rdf:datatype="http://www.w3.org/2001/XMLSchema#date">2008-01-09</ex:starts>
         <ex:finishes
rdf:datatype="http://www.w3.org/2001/XMLSchema#date">2008-01-11</ex:finishes>
         <loc:place
rdf:resource="http://temp.example.org/terms/Place#case10_place" />
     </ex:Event>
     <ex:Event rdf:about="http://temp.example.org/terms/Event#case11">
         <ex:date
rdf:datatype="http://www.w3.org/2001/XMLSchema#date">2008-01-09</ex:date>
         <loc:place
rdf:resource="http://temp.example.org/terms/Place#case11_place" />
     </ex:Event>
</rdf:RDF>



Reply | Threaded
Open this post in threaded view
|

Re: cwm/sparql mystery

Jon Crump-2

OK. In the extremely unlikely event that there is anyone reading this list
who is as clueless and as at-sea as I am, I will answer my own question as
search bait:

The clue (after a ridiculous amount of trial and error) was when I finally
asked cwm to serialize the results of my query as n3 thus:

%cwm --rdf /path/to/rdf/model.rdf --sparql=/path/to/sparql/query.txt --n3

Doing this gave me sensible results, but they took the form of a "quoted"
graph. In such a graph, the query variables I was searching for are
expressed like this:

     {
         "Some Literal"     :bound "label" .
         "2008-01-02"^^xsd:date     :bound "start" .
         "2008-01-04"^^xsd:date     :bound "end" .
         Event:case2     :bound "event" .

         }     a :Result .

This means that if the values you're searching for contain a literal,
those literals are expressed as the SUBJECTS of the resulting triples and
if you ask for --rdf output, cwm will complain:

"ValueError: Bad subject of statement: Some Literal.
                 RDF/XML cannot serialize a graph in which a subject is a
literal."

This is apparently the problem also if what you're searching for is a
datatyped literal like "2008-01-04"^^xsd:date, in which case the somewhat
less explicit runtime error:

"RuntimeError: ('Unexpected subject', "(21, ('2008-01-04',
u'http://www.w3.org/2001/XMLSchema#date'))")"

Asking for --n3 output avoids this because, apparently, the "quoted graph"
style of representing a sparql query result doesn't care if the subjects
are literals.

The solution, for my purposes at any rate, was to use a CONSTRUCT query,
instead of a SELECT, thus:

CONSTRUCT {?event ex:hasdate ?date; ex:hasstart ?start; ex:hasend ?end;
ex:haslabel ?label.}

This way, the literals that are bound to the query variables are forced to
be objects of triples and can thus be serialized as either rdf/xml or n3:

<rdf:Description rdf:about="http://temp.example.org/terms/Event#case2">
    <hasend
rdf:datatype="http://www.w3.org/2001/XMLSchema#date">2008-01-04</hasend>
    <haslabel>Some Literal</haslabel>
    <hasstart
rdf:datatype="http://www.w3.org/2001/XMLSchema#date">2008-01-02</hasstart>
</rdf:Description>

Though it's not exactly to the point, and though much of it is over my
head, the discussion of design issues at:
http://www.w3.org/DesignIssues/N3QL.html was helpful in clarifying some of
the underlying issues for me.

Open question: I still haven't worked out why, with this graph and this
query, asking cwm to report with --sparqlResults gives me this Import
Error:

Traceback (most recent call last):
   File "/Applications/RDFapps/cwm-1.2.1/cwm", line 750, in <module>
     doCommand()
   File "/Applications/RDFapps/cwm-1.2.1/cwm", line 698, in doCommand
     from cwm_sparql import outputString, SPARQL_NS
ImportError: No module named cwm_sparql

Another Open question: I still haven't been able to coax the rdflib sparql
implementation to give me similarly sensible results with this test query;
but that's another story for another list I suppose.

Best,
Jon