You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@jena.apache.org by Mikael Pesonen <mi...@lingsoft.fi> on 2016/05/27 13:23:26 UTC

Problem with graphs in SPARQL query in

Hi,

This query does what i want, that is, lists all graphs having the term 
"digitalisaatio"@fi:

PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
SELECT ?s ?graphTitle1  ?graphTitle2
WHERE {
GRAPH ?graph
{
         ?s skos:prefLabel "digitalisaatio"@fi

         OPTIONAL { ?graph dcterms:title ?graphTitle1 }
}}



But when I add new param
OPTIONAL { ?graph dc:title ?graphTitle2}

so that new query is this:
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
SELECT ?s ?graphTitle1  ?graphTitle2
WHERE {
GRAPH ?graph
{
         ?s skos:prefLabel "digitalisaatio"@fi

         OPTIONAL { ?graph dcterms:title ?graphTitle1 }
         OPTIONAL { ?graph dc:title ?graphTitle2}
}}

I dont get all the graphs anymore. OPTIONAL should never filter out 
data, just add? There must be a logical explanation, but I'm not getting 
it. Im using Fuseki.

Br,
Mikael

-- 
www.lingsoft.fi

Speech Applications - Language Management - Translation - Reader's and Writer's Tools - Text Tools - E-books and M-books

Mikael Pesonen
System Engineer

e-mail: mikael.pesonen@lingsoft.fi
Tel. +358 2 279 3300

Time zone: GMT+2

Helsinki Office
Etel�ranta 10
FI-00130 Helsinki
FINLAND

Turku Office
Linnankatu 10 A
FI-20100 Turku
FINLAND


Re: Problem with graphs in SPARQL query in

Posted by Mikael Pesonen <mi...@lingsoft.fi>.
Thanks, that works! Still have to learn the order of execution with 
these queries...

Br,
Mikael

On 30.5.2016 20:49, Andy Seaborne wrote:
> Mikael,
>
> So is the question why GRAPH ?g {} gets all the graphs but
> GRAPH ?g { OPTIONAL { ?g .... } }  doesn't?
>
> GRAPH ?g { PATTERN } is defined as:
>
> eval(D(G), Graph(var,PATTERN)) =
>      Let R be the empty multiset
>      foreach IRI i in D
>         R := Union(R, Join( eval(D(D[i]), PATTERN) , \u03a9(?var->i) )
>      the result is R
>
> That is, loop on each named graph, execute the inner part, then join 
> on the GRAPH variable.
>
> The important point here is that OPTIONAL { ?g .... } is executed, 
> then the GRAPH ?g is joined in, not the other way round.
>
> Try this:
>
> SELECT * {
>   GRAPH ?g {}
>   OPTIONAL { GRAPH ?g {  ?g dc:title ?title } } .
>   }
> }
>
> which is loosely - get all the graph names and if they have a 
> dc:title, include that.  OPTIONAL only ever adds - but at the point it 
> is executed, not through the query.
>
> Data (Trig):
> --------------------------------
> PREFIX :      <http://example/>
> PREFIX dc:    <http://purl.org/dc/elements/1.1/>
>
> <http://www.lingsoft.fi/tmp1>
> {
>      <http://www.lingsoft.fi/tmp1> dc:title    "title1"
> }
>
> <http://www.lingsoft.fi/tmp2>
> {
>      <urn:different> dc:title    "title1"
> }
>
> <http://www.lingsoft.fi/tmp3>
> {
>      <http://www.lingsoft.fi/tmp3> :no_title 123 .
> }
> --------------------------------
>
> --------------------------------------------
> | g                             | title    |
> ============================================
> | <http://www.lingsoft.fi/tmp2> |          |
> | <http://www.lingsoft.fi/tmp3> |          |
> | <http://www.lingsoft.fi/tmp1> | "title1" |
> --------------------------------------------
>
>     Andy
>
>
> On 30/05/16 13:55, Mikael Pesonen wrote:
>>
>> Hi,
>>
>> tested this some more. This
>>
>> PREFIX dcterms: <http://purl.org/dc/terms/>
>> PREFIX dc: <http://purl.org/dc/elements/1.1/>
>> SELECT DISTINCT ?graph ?graphTitle1 ?graphTitle2
>> WHERE {
>> GRAPH ?graph
>> {
>>          OPTIONAL { ?graph dcterms:title ?graphTitle1}
>>          OPTIONAL { ?graph dc:title ?graphTitle2}
>> }}
>>
>> returns same set as this
>>
>> PREFIX dcterms: <http://purl.org/dc/terms/>
>> PREFIX dc: <http://purl.org/dc/elements/1.1/>
>> SELECT DISTINCT ?graph ?graphTitle1
>> WHERE {
>> GRAPH ?graph
>> {
>>          OPTIONAL { ?graph dcterms:title ?graphTitle1}
>> }}
>>
>> and same set as this
>>
>> PREFIX dcterms: <http://purl.org/dc/terms/>
>> PREFIX dc: <http://purl.org/dc/elements/1.1/>
>> SELECT DISTINCT ?graph ?graphTitle1
>> WHERE {
>> GRAPH ?graph
>> {
>>          ?graph dcterms:title ?graphTitle1
>> }}
>>
>>
>>
>>
>>
>> This returns dc:title
>>
>> PREFIX dcterms: <http://purl.org/dc/terms/>
>> PREFIX dc: <http://purl.org/dc/elements/1.1/>
>> SELECT DISTINCT ?graph ?graphTitle1
>> WHERE {
>> GRAPH ?graph
>> {
>>         OPTIONAL  { ?graph dc:title ?graphTitle1 }
>> }}
>>
>> and this returns the same
>>
>>
>> PREFIX dcterms: <http://purl.org/dc/terms/>
>> PREFIX dc: <http://purl.org/dc/elements/1.1/>
>> SELECT DISTINCT ?graph ?graphTitle1
>> WHERE {
>> GRAPH ?graph
>> {
>>         ?graph dc:title ?graphTitle1
>> }}
>>
>>
>> To get all graphs, only this works:
>>
>> PREFIX dcterms: <http://purl.org/dc/terms/>
>> PREFIX dc: <http://purl.org/dc/elements/1.1/>
>> SELECT DISTINCT ?graph ?graphTitle1
>> WHERE {
>> GRAPH ?graph
>> {
>> }}
>>
>>
>> For testing I added a minimum graph
>>
>> PREFIX dc:        <http://purl.org/dc/elements/1.1/>
>>
>> INSERT DATA
>> {
>>    GRAPH <http://www.lingsoft.fi/tmp>
>>    {
>>         <http://www.lingsoft.fi/tmp> dc:title    "tmp title"
>>    }
>> }
>>
>> and this behaves like the rest of graphs.
>>
>> I'm using this db: apache-jena-fuseki-2.3.1
>>
>> Br,
>> Mikael
>>
>>
>> On 29.5.2016 12:36, Andy Seaborne wrote:
>>> On 27/05/16 14:23, Mikael Pesonen wrote:
>>>>
>>>> Hi,
>>>>
>>>> This query does what i want, that is, lists all graphs having the term
>>>> "digitalisaatio"@fi:
>>>>
>>>> PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
>>>> PREFIX dcterms: <http://purl.org/dc/terms/>
>>>> PREFIX dc: <http://purl.org/dc/elements/1.1/>
>>>> SELECT ?s ?graphTitle1  ?graphTitle2
>>>> WHERE {
>>>> GRAPH ?graph
>>>> {
>>>>          ?s skos:prefLabel "digitalisaatio"@fi
>>>>
>>>>          OPTIONAL { ?graph dcterms:title ?graphTitle1 }
>>>> }}
>>>>
>>>>
>>>>
>>>> But when I add new param
>>>> OPTIONAL { ?graph dc:title ?graphTitle2}
>>>>
>>>> so that new query is this:
>>>> PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
>>>> PREFIX dcterms: <http://purl.org/dc/terms/>
>>>> PREFIX dc: <http://purl.org/dc/elements/1.1/>
>>>> SELECT ?s ?graphTitle1  ?graphTitle2
>>>> WHERE {
>>>> GRAPH ?graph
>>>> {
>>>>          ?s skos:prefLabel "digitalisaatio"@fi
>>>>
>>>>          OPTIONAL { ?graph dcterms:title ?graphTitle1 }
>>>>          OPTIONAL { ?graph dc:title ?graphTitle2}
>>>> }}
>>>
>>> This will depend on your data model.
>>>
>>> Do you have a complete, minimal example? (some small amount of data
>>> and a query that you expect to work).
>>>
>>>> I dont get all the graphs anymore. OPTIONAL should never filter out
>>>> data, just add? There must be a logical explanation, but I'm not 
>>>> getting
>>>> it. Im using Fuseki.
>>>
>>> Which version?
>>>
>>>     Andy
>>>
>>> By the way:
>>>
>>> OPTIONAL { ?graph (dcterms:title|dc:title) ?graphTitle1 }
>>>
>>> asks for either dcterms:title or dc:title or both.
>>>
>>>>
>>>> Br,
>>>> Mikael
>>>>
>>>
>>
>

-- 
www.lingsoft.fi

Speech Applications - Language Management - Translation - Reader's and Writer's Tools - Text Tools - E-books and M-books

Mikael Pesonen
System Engineer

e-mail: mikael.pesonen@lingsoft.fi
Tel. +358 2 279 3300

Time zone: GMT+2

Helsinki Office
Etel�ranta 10
FI-00130 Helsinki
FINLAND

Turku Office
Linnankatu 10 A
FI-20100 Turku
FINLAND


Re: Problem with graphs in SPARQL query in

Posted by Andy Seaborne <an...@apache.org>.
Mikael,

So is the question why GRAPH ?g {} gets all the graphs but
GRAPH ?g { OPTIONAL { ?g .... } }  doesn't?

GRAPH ?g { PATTERN } is defined as:

eval(D(G), Graph(var,PATTERN)) =
      Let R be the empty multiset
      foreach IRI i in D
         R := Union(R, Join( eval(D(D[i]), PATTERN) , \u03a9(?var->i) )
      the result is R

That is, loop on each named graph, execute the inner part, then join on 
the GRAPH variable.

The important point here is that OPTIONAL { ?g .... } is executed, then 
the GRAPH ?g is joined in, not the other way round.

Try this:

SELECT * {
   GRAPH ?g {}
   OPTIONAL { GRAPH ?g {  ?g dc:title ?title } } .
   }
}

which is loosely - get all the graph names and if they have a dc:title, 
include that.  OPTIONAL only ever adds - but at the point it is 
executed, not through the query.

Data (Trig):
--------------------------------
PREFIX :      <http://example/>
PREFIX dc:    <http://purl.org/dc/elements/1.1/>

<http://www.lingsoft.fi/tmp1>
{
      <http://www.lingsoft.fi/tmp1> dc:title    "title1"
}

<http://www.lingsoft.fi/tmp2>
{
      <urn:different> dc:title    "title1"
}

<http://www.lingsoft.fi/tmp3>
{
      <http://www.lingsoft.fi/tmp3> :no_title 123 .
}
--------------------------------

--------------------------------------------
| g                             | title    |
============================================
| <http://www.lingsoft.fi/tmp2> |          |
| <http://www.lingsoft.fi/tmp3> |          |
| <http://www.lingsoft.fi/tmp1> | "title1" |
--------------------------------------------

     Andy


On 30/05/16 13:55, Mikael Pesonen wrote:
>
> Hi,
>
> tested this some more. This
>
> PREFIX dcterms: <http://purl.org/dc/terms/>
> PREFIX dc: <http://purl.org/dc/elements/1.1/>
> SELECT DISTINCT ?graph ?graphTitle1 ?graphTitle2
> WHERE {
> GRAPH ?graph
> {
>          OPTIONAL { ?graph dcterms:title ?graphTitle1}
>          OPTIONAL { ?graph dc:title ?graphTitle2}
> }}
>
> returns same set as this
>
> PREFIX dcterms: <http://purl.org/dc/terms/>
> PREFIX dc: <http://purl.org/dc/elements/1.1/>
> SELECT DISTINCT ?graph ?graphTitle1
> WHERE {
> GRAPH ?graph
> {
>          OPTIONAL { ?graph dcterms:title ?graphTitle1}
> }}
>
> and same set as this
>
> PREFIX dcterms: <http://purl.org/dc/terms/>
> PREFIX dc: <http://purl.org/dc/elements/1.1/>
> SELECT DISTINCT ?graph ?graphTitle1
> WHERE {
> GRAPH ?graph
> {
>          ?graph dcterms:title ?graphTitle1
> }}
>
>
>
>
>
> This returns dc:title
>
> PREFIX dcterms: <http://purl.org/dc/terms/>
> PREFIX dc: <http://purl.org/dc/elements/1.1/>
> SELECT DISTINCT ?graph ?graphTitle1
> WHERE {
> GRAPH ?graph
> {
>         OPTIONAL  { ?graph dc:title ?graphTitle1 }
> }}
>
> and this returns the same
>
>
> PREFIX dcterms: <http://purl.org/dc/terms/>
> PREFIX dc: <http://purl.org/dc/elements/1.1/>
> SELECT DISTINCT ?graph ?graphTitle1
> WHERE {
> GRAPH ?graph
> {
>         ?graph dc:title ?graphTitle1
> }}
>
>
> To get all graphs, only this works:
>
> PREFIX dcterms: <http://purl.org/dc/terms/>
> PREFIX dc: <http://purl.org/dc/elements/1.1/>
> SELECT DISTINCT ?graph ?graphTitle1
> WHERE {
> GRAPH ?graph
> {
> }}
>
>
> For testing I added a minimum graph
>
> PREFIX dc:        <http://purl.org/dc/elements/1.1/>
>
> INSERT DATA
> {
>    GRAPH <http://www.lingsoft.fi/tmp>
>    {
>         <http://www.lingsoft.fi/tmp> dc:title    "tmp title"
>    }
> }
>
> and this behaves like the rest of graphs.
>
> I'm using this db: apache-jena-fuseki-2.3.1
>
> Br,
> Mikael
>
>
> On 29.5.2016 12:36, Andy Seaborne wrote:
>> On 27/05/16 14:23, Mikael Pesonen wrote:
>>>
>>> Hi,
>>>
>>> This query does what i want, that is, lists all graphs having the term
>>> "digitalisaatio"@fi:
>>>
>>> PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
>>> PREFIX dcterms: <http://purl.org/dc/terms/>
>>> PREFIX dc: <http://purl.org/dc/elements/1.1/>
>>> SELECT ?s ?graphTitle1  ?graphTitle2
>>> WHERE {
>>> GRAPH ?graph
>>> {
>>>          ?s skos:prefLabel "digitalisaatio"@fi
>>>
>>>          OPTIONAL { ?graph dcterms:title ?graphTitle1 }
>>> }}
>>>
>>>
>>>
>>> But when I add new param
>>> OPTIONAL { ?graph dc:title ?graphTitle2}
>>>
>>> so that new query is this:
>>> PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
>>> PREFIX dcterms: <http://purl.org/dc/terms/>
>>> PREFIX dc: <http://purl.org/dc/elements/1.1/>
>>> SELECT ?s ?graphTitle1  ?graphTitle2
>>> WHERE {
>>> GRAPH ?graph
>>> {
>>>          ?s skos:prefLabel "digitalisaatio"@fi
>>>
>>>          OPTIONAL { ?graph dcterms:title ?graphTitle1 }
>>>          OPTIONAL { ?graph dc:title ?graphTitle2}
>>> }}
>>
>> This will depend on your data model.
>>
>> Do you have a complete, minimal example? (some small amount of data
>> and a query that you expect to work).
>>
>>> I dont get all the graphs anymore. OPTIONAL should never filter out
>>> data, just add? There must be a logical explanation, but I'm not getting
>>> it. Im using Fuseki.
>>
>> Which version?
>>
>>     Andy
>>
>> By the way:
>>
>> OPTIONAL { ?graph (dcterms:title|dc:title) ?graphTitle1 }
>>
>> asks for either dcterms:title or dc:title or both.
>>
>>>
>>> Br,
>>> Mikael
>>>
>>
>


Re: Problem with graphs in SPARQL query in

Posted by Mikael Pesonen <mi...@lingsoft.fi>.
Hi,

tested this some more. This

PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
SELECT DISTINCT ?graph ?graphTitle1 ?graphTitle2
WHERE {
GRAPH ?graph
{
         OPTIONAL { ?graph dcterms:title ?graphTitle1}
         OPTIONAL { ?graph dc:title ?graphTitle2}
}}

returns same set as this

PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
SELECT DISTINCT ?graph ?graphTitle1
WHERE {
GRAPH ?graph
{
         OPTIONAL { ?graph dcterms:title ?graphTitle1}
}}

and same set as this

PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
SELECT DISTINCT ?graph ?graphTitle1
WHERE {
GRAPH ?graph
{
         ?graph dcterms:title ?graphTitle1
}}





This returns dc:title

PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
SELECT DISTINCT ?graph ?graphTitle1
WHERE {
GRAPH ?graph
{
        OPTIONAL  { ?graph dc:title ?graphTitle1 }
}}

and this returns the same


PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
SELECT DISTINCT ?graph ?graphTitle1
WHERE {
GRAPH ?graph
{
        ?graph dc:title ?graphTitle1
}}


To get all graphs, only this works:

PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
SELECT DISTINCT ?graph ?graphTitle1
WHERE {
GRAPH ?graph
{
}}


For testing I added a minimum graph

PREFIX dc:        <http://purl.org/dc/elements/1.1/>

INSERT DATA
{
   GRAPH <http://www.lingsoft.fi/tmp>
   {
        <http://www.lingsoft.fi/tmp> dc:title    "tmp title"
   }
}

and this behaves like the rest of graphs.

I'm using this db: apache-jena-fuseki-2.3.1

Br,
Mikael


On 29.5.2016 12:36, Andy Seaborne wrote:
> On 27/05/16 14:23, Mikael Pesonen wrote:
>>
>> Hi,
>>
>> This query does what i want, that is, lists all graphs having the term
>> "digitalisaatio"@fi:
>>
>> PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
>> PREFIX dcterms: <http://purl.org/dc/terms/>
>> PREFIX dc: <http://purl.org/dc/elements/1.1/>
>> SELECT ?s ?graphTitle1  ?graphTitle2
>> WHERE {
>> GRAPH ?graph
>> {
>>          ?s skos:prefLabel "digitalisaatio"@fi
>>
>>          OPTIONAL { ?graph dcterms:title ?graphTitle1 }
>> }}
>>
>>
>>
>> But when I add new param
>> OPTIONAL { ?graph dc:title ?graphTitle2}
>>
>> so that new query is this:
>> PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
>> PREFIX dcterms: <http://purl.org/dc/terms/>
>> PREFIX dc: <http://purl.org/dc/elements/1.1/>
>> SELECT ?s ?graphTitle1  ?graphTitle2
>> WHERE {
>> GRAPH ?graph
>> {
>>          ?s skos:prefLabel "digitalisaatio"@fi
>>
>>          OPTIONAL { ?graph dcterms:title ?graphTitle1 }
>>          OPTIONAL { ?graph dc:title ?graphTitle2}
>> }}
>
> This will depend on your data model.
>
> Do you have a complete, minimal example? (some small amount of data 
> and a query that you expect to work).
>
>> I dont get all the graphs anymore. OPTIONAL should never filter out
>> data, just add? There must be a logical explanation, but I'm not getting
>> it. Im using Fuseki.
>
> Which version?
>
>     Andy
>
> By the way:
>
> OPTIONAL { ?graph (dcterms:title|dc:title) ?graphTitle1 }
>
> asks for either dcterms:title or dc:title or both.
>
>>
>> Br,
>> Mikael
>>
>

-- 
www.lingsoft.fi

Speech Applications - Language Management - Translation - Reader's and Writer's Tools - Text Tools - E-books and M-books

Mikael Pesonen
System Engineer

e-mail: mikael.pesonen@lingsoft.fi
Tel. +358 2 279 3300

Time zone: GMT+2

Helsinki Office
Etel�ranta 10
FI-00130 Helsinki
FINLAND

Turku Office
Linnankatu 10 A
FI-20100 Turku
FINLAND


Re: Problem with graphs in SPARQL query in

Posted by Andy Seaborne <an...@apache.org>.
On 27/05/16 14:23, Mikael Pesonen wrote:
>
> Hi,
>
> This query does what i want, that is, lists all graphs having the term
> "digitalisaatio"@fi:
>
> PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
> PREFIX dcterms: <http://purl.org/dc/terms/>
> PREFIX dc: <http://purl.org/dc/elements/1.1/>
> SELECT ?s ?graphTitle1  ?graphTitle2
> WHERE {
> GRAPH ?graph
> {
>          ?s skos:prefLabel "digitalisaatio"@fi
>
>          OPTIONAL { ?graph dcterms:title ?graphTitle1 }
> }}
>
>
>
> But when I add new param
> OPTIONAL { ?graph dc:title ?graphTitle2}
>
> so that new query is this:
> PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
> PREFIX dcterms: <http://purl.org/dc/terms/>
> PREFIX dc: <http://purl.org/dc/elements/1.1/>
> SELECT ?s ?graphTitle1  ?graphTitle2
> WHERE {
> GRAPH ?graph
> {
>          ?s skos:prefLabel "digitalisaatio"@fi
>
>          OPTIONAL { ?graph dcterms:title ?graphTitle1 }
>          OPTIONAL { ?graph dc:title ?graphTitle2}
> }}

This will depend on your data model.

Do you have a complete, minimal example? (some small amount of data and 
a query that you expect to work).

> I dont get all the graphs anymore. OPTIONAL should never filter out
> data, just add? There must be a logical explanation, but I'm not getting
> it. Im using Fuseki.

Which version?

	Andy

By the way:

OPTIONAL { ?graph (dcterms:title|dc:title) ?graphTitle1 }

asks for either dcterms:title or dc:title or both.

>
> Br,
> Mikael
>