Make a UNION return a single result in SPARQL - sparql

I am trying to query for the sh:description of a property shape, and if there is none, I want to follow the path to a property and get the rdfs:comment (Also for sh:name and rdfs:label). My data looks like the following:
ex:File
a owl:Class ;
a sh:NodeShape ;
sh:property ex:File-name ;
ex:File-name
a sh:PropertyShape ;
sh:path ex:filename ;
sh:datatype xsd:string ;
sh:description "The name of the file" ;
sh:maxCount 1 ;
ex:filename
a rdf:Property ;
rdfs:label "Filename" ;
rdfs:comment "The file name" ;
.
I have the query below, but it returns two results. How can I modify it to only return a single result (preferring sh:description over rdfs:comment)?
PREFIX : <http://www.example.org/#>
PREFIX sh: <http://www.w3.org/ns/shacl#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
select ?propertyShape ?property ?name ?description where {
:File sh:property ?propertyShape .
?propertyShape sh:path ?property .
{ ?propertyShape sh:name ?name } UNION { ?property rdfs:label ?name } .
{ ?propertyShape sh:description ?description } UNION { ?property rdfs:comment ?description } .
The above returns something like this:
propertyShape
property
name
description
:File-name
ex:filename
"Filename"
"The name of the file"
:File-name
ex:filename
"Filename"
"The file name"
I would like for it to return something like:
propertyShape
property
name
description
:File-name
ex:filename
"Filename"
"The name of the file"

You should be able to use a sequence of OPTIONAL blocks to do this:
SELECT ?propertyShape ?property ?name ?description WHERE {
:File sh:property ?propertyShape .
?propertyShape sh:path ?property .
OPTIONAL { ?propertyShape sh:name ?name }
OPTIONAL { ?property rdfs:label ?name }
OPTIONAL { ?propertyShape sh:description ?description }
OPTIONAL { ?property rdfs:comment ?description }
}
You'll still have to think about cases where there are multiple names/descriptions per property (this isn't guaranteed to return just a single result), but it should give you the "preferring sh:description over rdfs:comment" behavior.

Can't you just use path expressions such as
...
?propertyShape sh:name|(sh:path/rdfs:label) ?name .
?propertyShape sh:description|(sh:path/rdfs:comment) ?description .

Related

Why does a bound variable not work inside a FILTER in a UNION BLOCK?

The following query works as expected:
PREFIX nct: <http://n2t.net/ark:/39333/ncg/type#>
PREFIX ncv: <http://n2t.net/ark:/39333/ncg/vocab#>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
SELECT ?label ?url WHERE {
BIND("Chapel Hill" as ?placename)
{
?marker
a nct:HistoricalMarker ;
skos:prefLabel ?label ;
skos:note ?markerDescription ;
.
FILTER CONTAINS(?markerDescription, "Chapel Hill")
}
UNION
{
?musician skos:prefLabel ?label .
?place
skos:prefLabel ?placename ;
ncv:associatedWithAgent ?musician ;
.
}
UNION
{
?movie skos:prefLabel ?label .
?place
skos:prefLabel ?placename ;
ncv:filmingLocationFor ?movie ;
.
}
}
But if I change FILTER CONTAINS(?markerDescription, "Chapel Hill") in the first block to FILTER CONTAINS(?markerDescription, ?placename), it no longer works.
Try the working query.
Try the not working query.
Why?

Encountered " "<" "< "" at line 1, column 15. Was expecting: <IRIref>

Trying to use the query in the endpoint. The query was created in SPARQL. The error coming like
Encountered " "<" "< "" at line 1, column 15.
Was expecting:
<IRIref> ...
Query:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?Class ?Title ?Definition
WHERE {
?Value rdfs:label ?Class
FILTER regex(?Class, "Motion") .
?def rdfs:domain ?Value .
?def rdfs:label ?Title .
?def rdfs:comment ?Definition}
The url
http://localhost:3030/skosmos/query?query=
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?Class ?Title ?Definition
WHERE {
?Value rdfs:label ?Class
FILTER regex(?Class, "Motion") .
?def rdfs:domain ?Value .
?def rdfs:label ?Title .
?def rdfs:comment ?Definition
}
I took your original query, URL-encoded it with one of many services and tools you might use, randomly selected from a web search, and appended it to the start of what you had as "the URL", http://localhost:3030/skosmos/query?query= ... and you verified that this worked --
http://localhost:3030/skosmos/query?query=PREFIX+rdfs%3A+%3Chttp%3A%2F%2Fwww.w3.org%2F2000%2F01%2Frdf-schema%23%3E%0D%0A%0D%0ASELECT+%3FClass+%3FTitle+%3FDefinition%0D%0AWHERE+%7B%0D%0A%3FValue+rdfs%3Alabel+%3FClass%0D%0AFILTER+regex%28%3FClass%2C+%22Motion%22%29+.%0D%0A%3Fdef+rdfs%3Adomain+%3FValue+.%0D%0A%3Fdef+rdfs%3Alabel+%3FTitle+.%0D%0A%3Fdef+rdfs%3Acomment+%3FDefinition%7D

sparql query: get several values from a List

My query returns result dependent on which one of two sparql OPTIONS clauses come first for a List of Images. Although using Jena ARQ is not an option at this point, and I'd like to solve this with a pure SPARQL query, still I'd like to know how it could be solved with Jena as well.
My data presentation is attached below, the data may contain a list of images. My image representation is also below. I'm attaching my query as well.
The sprql query has two variables urlX, and urlY declared in the 2 OPTIONS blocks, if a List of images exists. Depending on which of the OPTIONS comes first, I get the value for that one variable, while the other one doesn't get reached. It seems the issue has to do with using OPTIONS clause. I'm not sure what else I can try instead, I'm far from being an expert on sparql queries. I want the query to do the following: if a collection of images is present, I want to see if both image sizes (dc:conformsTo) are present and get both urlX and urlY values, or get the ones that exist. Much appreciate your time.
My data representation:
#prefix dc: <http://purl.org/dc/terms/> .
#prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
#prefix lews: <http://lews.com/content/> .
#prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
lews:26331340 lews:name "the Human Good Luck Charm is Back"^^xsd:token ;
dc:created "2014-10-20T17:14:55.357-07:00"^^xsd:dateTime ;
dc:identifier "26331340"^^xsd:int ;
dc:modified "2016-08-04T13:43:00.897-07:00"^^xsd:dateTime ;
dc:title "the Human Good Luck Charm is Back" ;
dc:hasPart <http://lews.com/content/26331340#Images> ;
dc:abstract "As the World Series gets underway..." ;
dc:description "The super fan who rooted for the Royals is back to boost morale." ;
dc:subject "hoping for a World Series victory".
<http://lews.com/content/26331340#Images> dc:identifier "Images"^^xsd:token ;
rdf:first lews:26331375 ;
rdf:rest _:genid-15e530b0195547d9ac3f8e5e6785a747-x26331340Images26331376 ;
rdf:li <http://lews.com/content/26331340#Images> , _:genid-15e530b0195547d9ac3f8e5e6785a747-x26331340Images26331376 , _:genid-15e530b0195547d9ac3f8e5e6785a747-x26331340Images26331377 , _:genid-15e530b0195547d9ac3f8e5e6785a747-x26331340Images26331378 , _:genid-15e530b0195547d9ac3f8e5e6785a747-x26331340Images26331379 , _:genid-15e530b0195547d9ac3f8e5e6785a747-x26331340Images26331380 ;
a rdf:List .
_:genid-15e530b0195547d9ac3f8e5e6785a747-x26331340Images26331376 rdf:first lews:26331376 ;
rdf:rest _:genid-15e530b0195547d9ac3f8e5e6785a747-x26331340Images26331377 .
_:genid-15e530b0195547d9ac3f8e5e6785a747-x26331340Images26331377 rdf:first lews:26331377 ;
rdf:rest _:genid-15e530b0195547d9ac3f8e5e6785a747-x26331340Images26331378 .
_:genid-15e530b0195547d9ac3f8e5e6785a747-x26331340Images26331378 rdf:first lews:26331378 ;
rdf:rest _:genid-15e530b0195547d9ac3f8e5e6785a747-x26331340Images26331379 .
_:genid-15e530b0195547d9ac3f8e5e6785a747-x26331340Images26331379 rdf:first lews:26331379 ;
rdf:rest _:genid-15e530b0195547d9ac3f8e5e6785a747-x26331340Images26331380 .
_:genid-15e530b0195547d9ac3f8e5e6785a747-x26331340Images26331380 rdf:first lews:26331380 ;
rdf:rest rdf:nil .
My image representation:
#prefix dc: <http://purl.org/dc/terms/> .
#prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
#prefix lews: <http://abcnews.com/content/> .
#prefix mrss: <http://search.yahoo.com/mrss/> .
#prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
lews:26331376 lews:name "141020_wn_donvan0_704x396.jpg"^^xsd:token ;
lews:section "wnt"^^xsd:token ;
lews:type "Image"^^xsd:token ;
dc:conformsTo "704x396"^^xsd:token ;
dc:created "2014-10-20T17:15:09.637-07:00"^^xsd:dateTime ;
dc:hasFormat <http://lews.go.com/images/WNT/141020_wn_donvan0_704x396.jpg> ;
dc:identifier "26331376"^^xsd:int ;
dc:isPartOf <http://lews.go.com/WNT> ;
dc:modified "2014-10-20T17:15:09.947-07:00"^^xsd:dateTime ;
dc:type "StillImage"^^xsd:token ;
mrss:height "396"^^xsd:int ;
mrss:width "704"^^xsd:int ;
xsd:date "2014-10-20"^^xsd:date ;
xsd:gMonthDay "--10-20"^^xsd:gMonthDay ;
xsd:gYear "2014"^^xsd:gYear ;
xsd:gYearMonth "2014-10"^^xsd:gYearMonth .
My query:
PREFIX dc: <http://purl.org/dc/terms/>
PREFIX mrss: <http://search.yahoo.com/mrss/>
PREFIX search: <http://www.openrdf.org/contrib/lucenesail#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX lews: <http://abcnews.com/content/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT ?id ?title ?description ?urlX ?urlY ?section ?imgName
?subject dc:identifier ?id.
OPTIONAL {?subject dc:title ?title.}
OPTIONAL {?subject dc:description ?description.}
OPTIONAL {?subject dc:isPartOf ?section.}
OPTIONAL {
?subject dc:hasPart ?imageCol.
?imageCol dc:identifier "Images"^^xsd:token.
OPTIONAL{
?imageCol rdf:li ?bnode.
?bnode rdf:first ?image.
?image lews:name ?imgName;
dc:conformsTo "4x3";
dc:hasFormat ?urlX.
}
OPTIONAL{
?imageCol rdf:li ?bnode.
?bnode rdf:first ?image.
?image lews:name ?imgName;
dc:conformsTo "16x9";
dc:hasFormat ?urlY.
}
}
}
LIMIT ${limit}
If I understood correctly what it is you want, you just need to group the optionals differently:
PREFIX dc: <http://purl.org/dc/terms/>
PREFIX mrss: <http://search.yahoo.com/mrss/>
PREFIX search: <http://www.openrdf.org/contrib/lucenesail#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX lews: <http://abcnews.com/content/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT ?id ?title ?description ?urlX ?urlY ?section ?imgName {
?subject dc:identifier ?id.
OPTIONAL { ?subject dc:title ?title. }
OPTIONAL { ?subject dc:description ?description. }
OPTIONAL { ?subject dc:isPartOf ?section. }
OPTIONAL {
?subject dc:hasPart ?imageCol.
?imageCol dc:identifier "Images"^^xsd:token.
OPTIONAL {
?imageCol rdf:li ?bnode.
?bnode rdf:first ?image.
?image lews:name ?imgName;
# Here we optionally bind ?urlX and/or ?urlY
OPTIONAL {
?image dc:conformsTo "4x3";
dc:hasFormat ?urlX.
}
OPTIONAL {
?image dc:conformsTo "16x9";
dc:hasFormat ?urlY.
}
}
}
}

Sparql of DbPedia based upon name not subject

I am trying to query dbpedia to get some people data and I don't have subjects just names of the people I want to query and their birth/death dates.
I am trying to do a query along these lines. I want the name, birth date, death date and thumbnail of everyone with the surname Presley. What I then intend to do is loop through the results returned and find the best match for Elvis Presley 1935-1977 which is the data I have.
PREFIX dbo: <http://dbpedia.org/ontology/>
SELECT ?Name ?thumbnail ?birthDate ?deathDate WHERE {
{
dbo:name ?Name ;
dbo:birthDate ?birthDate ;
dbo:birthDate ?deathDate ;
dbo:thumbnail ?thumbnail ;
FILTER contains(?Name#en, "Presley")
}
What is the best way to construct my sparql query?
UPDATE:
I have put together this query which seems to work to some extent but I don't entirely understand it, and I can't figure out the contains, but it does at least run and return results.
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX dbo: <http://dbpedia.org/ontology/>
SELECT ?subject ?thumbnail ?birthdate ?deathdate WHERE {
{
?subject rdfs:label "Elvis Presley"#en ;
dbo:thumbnail ?thumbnail ;
dbo:birthDate ?birthdate ;
dbo:deathDate ?deathdate ;
a owl:Thing .
}
UNION
{
?altName rdfs:label "Elvis Presley"#en ;
dbo:thumbnail ?thumbnail ;
dbo:birthDate ?birthdate ;
dbo:deathDate ?deathdate ;
dbo:wikiPageRedirects ?s .
}
}
Some entities might not have all of that information, so it's better to use optional. You can use foaf:surname to check for surname directly.
select * where {
?s foaf:surname "Presley"#en
optional { ?s dbo:name ?name }
optional { ?s dbo:birthDate ?birth }
optional { ?s dbo:deathDate ?death }
optional { ?s dbo:thumbnail ?thumb }
}

How to SPARQL optional

I have to use this Spaql query to retrive information about a person, my problem is to break the optional construct up into multiple optional constructs.
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbpprop: <http://dbpedia.org/property/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT DISTINCT ?label ?abstract ?placeOfBirth
?birthPlace ?birthDate ?page ?thumbnail
WHERE {
<http://dbpedia.org/resource/Ismail_Kadare> rdfs:label ?label ;
dbo:abstract ?abstract ;
foaf:page ?page .
OPTIONAL {
<http://dbpedia.org/resource/Ismail_Kadare> dbpprop:placeOfBirth ?placeOfBirth ;
dbpprop:birthPlace ?birthPlace ;
dbo:birthDate ?birthDate ;
dbo:thumbnail ?thumbnail .
}
FILTER (LANG(?label) = 'en')
FILTER (LANG(?abstract) = 'en')
}
LIMIT 1
Splitting the OPTIONAL pattern into parts
The pattern
<http://dbpedia.org/resource/Ismail_Kadare> dbpprop:placeOfBirth ?placeOfBirth ;
dbpprop:birthPlace ?birthPlace ;
dbo:birthDate ?birthDate ;
dbo:thumbnail ?thumbnail .
is shorthand for four triple patterns:
<http://dbpedia.org/resource/Ismail_Kadare> dbpprop:placeOfBirth ?placeOfBirth .
<http://dbpedia.org/resource/Ismail_Kadare> dbpprop:birthPlace ?birthPlace .
<http://dbpedia.org/resource/Ismail_Kadare> dbo:birthDate ?birthDate .
<http://dbpedia.org/resource/Ismail_Kadare> dbo:thumbnail ?thumbnail .
Instead of OPTIONAL { …first pattern… }, you just need to use four optional blocks, one for each of the four triple patterns:
optional { <http://dbpedia.org/resource/Ismail_Kadare> dbpprop:placeOfBirth ?placeOfBirth }
optional { <http://dbpedia.org/resource/Ismail_Kadare> dbpprop:birthPlace ?birthPlace }
optional { <http://dbpedia.org/resource/Ismail_Kadare> dbo:birthDate ?birthDate }
optional { <http://dbpedia.org/resource/Ismail_Kadare> dbo:thumbnail ?thumbnail }
Other issues
It's worth nothing that language matching is a bit more complicated than string matching, so rather than
FILTER (LANG(?label) = 'en')
FILTER (LANG(?abstract) = 'en')
you should really be using
filter(langMatches(lang(?label),'en'))
filter(langMatches(lang(?abstract),'en'))
which allows you to retrieve results that use different language tags that are all English.
select distinct and limit 1 aren't both necessary
Notice that select distinct ensures that you don't have any duplicate rows in your results. However, limit 1 means that you'll only have one result at most anyhow, so there won't be any duplicates to remove.
Standard Namespaces
It looks like you're querying against DBpedia, so it might be worthwhile to use the same namespace prefixes that the public endpoint defines, so that you can copy and paste queries and experiment more easily. Doing that (and using a values ?x { dbpedia:Ismail_Kadare } to avoid some typing, we end up with this query:
select ?label ?abstract ?placeOfBirth ?birthPlace ?birthDate ?page ?thumbnail
where {
values ?x { dbpedia:Ismail_Kadare }
?x rdfs:label ?label ;
dbpedia-owl:abstract ?abstract ;
foaf:page ?page .
optional { ?x dbpprop:placeOfBirth ?placeOfBirth }
optional { ?x dbpprop:birthPlace ?birthPlace }
optional { ?x dbpedia-owl:birthDate ?birthDate }
optional { ?x dbpedia-owl:thumbnail ?thumbnail }
filter langMatches(lang(?label),'en')
filter langMatches(lang(?abstract),'en')
}
limit 1
The DBpedia endpoint won't return anything for that query, but that's because http://dbpedia.org/resource/Ismail_Kadare doesn't have a foaf:page property, not because the query is malformed. I don't know whether you're actually running this against DBpedia or not, so that may or not matter.