Sparql issue on BIND clause - sparql

I'm currently trying to sort some clients by priority (those that have a running subscription first then the others.
To that effect I use a BIND clause in this query :
SELECT DISTINCT ?context ?priority ?label {
?s a my:Client .
?s rdfs:label ?label .
BIND ( IF(EXISTS {?s rdf:type my:Subscriber}, 1, 0) AS ?priority )
} ORDER BY DESC(?priority) ASC(?label)
Seems to me that the ?priority var should always be bound to either 0 or 1 but whenever I execute the query I get (along with the expected output result) unbound ?priority "rows" for those items that should get 1 for priority so I need to add FILTER (bound(?priority)) in my query to get what i expect.
Is that normal or am I missing something ?
Thanks in advance,
Max.
Edit :
=> filed the sort issue in the tracker
I edited the query to check that the prority is both bound and unbound on the same node.
Here's a sample output of the resultset I get back
?context = urn:graph:1772#844 , ?priority = 1^^http://www.w3.org/2001/XMLSchema#integer , ?sort1 = client 2#fr
?context = urn:graph:1772#1690 , ?priority = 1^^http://www.w3.org/2001/XMLSchema#integer , ?sort1 = client 1#fr
?context = urn:graph:1772#742 , ?priority = 0^^http://www.w3.org/2001/XMLSchema#integer , ?sort1 = client 4#fr
?context = urn:graph:1772#1010 , ?priority = 0^^http://www.w3.org/2001/XMLSchema#integer , ?sort1 = client 3#fr
?context = urn:graph:1772#1690 , ?sort1 = client 1#fr
?context = urn:graph:1772#844 , ?sort1 = client 2#fr
In the last two rows, the variable ?priority is not bound at all.
By the way, you'll note that the second ORDER clause is also not honored if the first clause is unbound

There was a bug with the use of EXISTS as a child expression of another expression that could lead to incorrect results as in your example.
This is now fixed in the source code and will be included in the next release

Related

Anzograph SPARQL issue

I have the following SPARQL query :
SELECT * FROM {
?measurement a oboe-core:Measurement ;
oboe-core:ofCharacteristic oboe-core:Name ;
oboe-core:usesStandard :Anaee-franceExperimentalSiteNamingStandard ;
oboe-core:hasValue ?anaeeSiteNameStandard .
BIND ( IRI( CONCAT( "http://www.anaee-france.fr/ontology/anaee-france_ontology" ,
?anaeeSiteNameStandard ) ) AS ?site ) .
OPTIONAL { ?site rdfs:label ?_anaeeSiteName . }
BIND ( IF (BOUND (?_anaeeSiteName), ?_anaeeSiteName, "NULL_anaeeSiteName"#en ) AS
?anaeeSiteName) .
FILTER (lang( ?anaeeSiteName ) = "en") .
} limit 3
?_anaeeSiteName is empty knowing that my graph contains :
<http://www.anaee-france.fr/ontology/anaee-france_ontology#Guyaflux>
rdfs:label "Guyaflux"#en .
When I use directly
BIND ( IRI( "http://www.anaee-france.fr/ontology/anaee-france_ontology#Guyaflux" ) AS ?site ) .
Instead of BIND IRI CONCAT
BIND ( IRI( CONCAT( "http://www.anaee-france.fr/ontology/anaee-france_ontology" ,
?anaeeSiteNameStandard ) ) AS ?site ) .
I get some results.
Can anyone tell me what's wrong?
Thank's
We agree with Uninformed and Andy that we think there are syntax errors. You are missing the graph uri for the FROM and the WHERE key word, and the # in the concat is needed to match your example URI.
Disclaimer: I work for Cambridge Semantics Inc.
Sorry, I removed the "FROM " in order to simplify the query.
The complete Sparql query is below :
SELECT * FROM <foret> WHERE {
?measurement a oboe-core:Measurement ;
oboe-core:ofCharacteristic oboe-core:Name ;
oboe-core:usesStandard :Anaee-franceExperimentalSiteNamingStandard ;
oboe-core:hasValue ?anaeeSiteNameStandard .
BIND ( IRI( CONCAT( "http://www.anaee-france.fr/ontology/anaee-france_ontology" ,
?anaeeSiteNameStandard ) ) AS ?site ) .
OPTIONAL { ?site rdfs:label ?_anaeeSiteName . }
BIND ( IF (BOUND (?_anaeeSiteName), ?_anaeeSiteName, "NULL_anaeeSiteName"#en ) AS
?anaeeSiteName) .
FILTER (lang( ?anaeeSiteName ) = "en") .
} limit 3
The stange think is that when I add the :
BIND ( str(?anaeeSiteNameStandard) AS ?anaeeSiteNameStandard ) .
just before :
BIND ( IRI( CONCAT( "http://www.anaee-france.fr/ontology/anaee-france_ontology" ,
?anaeeSiteNameStandard ) ) AS ?site ) .
I get the expected result !

Filter by predicate attributes

How can I get all chairpersons, which are still holding this job (where the predicate has no end date?).
My current version returns all chairpersons:
SELECT ?chairperson ?x WHERE {
?university wdt:P488 ?chairperson.
}

Simple SPARQL query does not give result from turtle file

I have a ttl file which I am querying. This is a sample of two nodes that I am querying:
<http://natarchives.com.mt/archivalresource/R494Vol1>
a "http://data.archiveshub.ac.uk/def/ArchivalResource" ;
locah:accessProvidedBy "http://natarchives.com.mt/repository/MT01" ;
locah:dateCreatedAccumulatedEnd
"1497" ;
locah:dateCreatedAccumulatedStart
"1486" ;
locah:dateCreatedAccumulatedString
"1486-1497" ;
locah:level "http://data.archiveshub.ac.uk/page/level/file" ;
<http://purl.org/dc/terms/creator>
<http://natarchives.com.mt/author/R494Vol1_NotaryGiacomoZabbara> ;
<http://purl.org/dc/terms/identifier>
"R494Vol1" ;
<http://purl.org/dc/terms/language>
"la" ;
<http://purl.org/dc/terms/type>
"Register" ;
and
<http://natarchives.com.mt/deed/R494Vol1-D233>
locah:accessProvidedBy "http://natarchives.com.mt/repository/MT01" ;
locah:associatedWith "constituens" , "positum" , "annuam gabellam" , "juribus" , "melite" , "festo pasce ressureccionis dominice" , "moratorie" , "converso" , "bonam" , "ponderis" , "situm" , "nobilem" , "completis" , "procuratorem magnifici" , "precario" , "civitatis melite" , "jngabellacionem" , "territorium eiusdem magnifici" , "bona" , "pecunia" , "juravit" , "gabellam juxta usum melite" , "jngabellavit" , "personam" , "augustj" , "procurator magnifici" , "crastato" , "testibus testamur" ;
locah:associatedWith <http://natarchives.com.mt/person/person3617> , <http://natarchives.com.mt/place/place727> , <http://natarchives.com.mt/place/place191> , <http://natarchives.com.mt/person/person3612> , <http://natarchives.com.mt/person/person3616> , <http://natarchives.com.mt/place/place726> , <http://natarchives.com.mt/place/place190> , <http://natarchives.com.mt/person/person3619> , <http://natarchives.com.mt/person/person3611> , <http://natarchives.com.mt/person/person3615> , <http://natarchives.com.mt/person/person3614> , <http://natarchives.com.mt/person/person3618> , <http://natarchives.com.mt/place/place728> , <http://natarchives.com.mt/person/person3610> , <http://natarchives.com.mt/person/person3613> ;
locah:level "http://data.archiveshub.ac.uk/page/level/item" ;
<http://purl.org/dc/terms/date>
"8-8-1487" ;
<http://purl.org/dc/terms/identifier>
"R494Vol1-D233" ;
<http://purl.org/dc/terms/isPartOf>
"http://natarchives.com.mt/archivalresource/R494Vol1" ;
<http://purl.org/dc/terms/type>
"Cabella" .
This is the query I am trying:
SELECT ?x ?reg ?regId
WHERE {
?x dcterms:date "8-8-1487".
?x dcterms:isPartOf ?reg.
?reg dcterms:identifier ?regId.
}
As soon as I try to get the regId the query gives no results where it is obvious from the turtle file that there is a regId. Any idea why?
The problem is that in your data, the value of the isPartOf property is a string literal, "http://natarchives.com.mt/archivalresource/R494Vol1". A string literal is distinct from a URI. So what you're getting with your query is that ?x matches with <http://natarchives.com.mt/deed/R494Vol1-D233>, ?reg matches with "http://natarchives.com.mt/archivalresource/R494Vol1", but then ?regId doesn't match with anything, because there is no triple with the value for ?reg as the subject in your data (nor can there be, because subjects can't be literals in RDF).
To fix in your data, you would need to replace "http://natarchives.com.mt/archivalresource/R494Vol1" with a proper URI, surrounding with angle brackets instead of quotes: <http://natarchives.com.mt/archivalresource/R494Vol1>.
If it's not possible to fix your data, you can also query around it, by making SPARQL convert the literal to an IRI, for example:
SELECT ?x ?regIri ?regId
WHERE {
?x dcterms:date "8-8-1487".
?x dcterms:isPartOf ?reg.
BIND(IRI(?reg) as ?regIri)
?regIri dcterms:identifier ?regId.
}

Simple SPARQL query does not return any results

I am just getting up and running with Blazegraph in embedded mode. I load a few sample triples and am able to retrieve them with a "select all" query:
SELECT * WHERE { ?s ?p ?o }
This query returns all my sample triples:
[s=<<<http://github.com/jschmidt10#person_Thomas>, <http://github.com/jschmidt10#hasAge>, "30"^^<http://www.w3.org/2001/XMLSchema#int>>>;p=blaze:history:added;o="2017-01-15T16:11:15.909Z"^^<http://www.w3.org/2001/XMLSchema#dateTime>]
[s=<<<http://github.com/jschmidt10#person_Tommy>, <http://github.com/jschmidt10#hasLastName>, "Test">>;p=blaze:history:added;o="2017-01-15T16:11:15.909Z"^^<http://www.w3.org/2001/XMLSchema#dateTime>]
[s=<<<http://github.com/jschmidt10#person_Tommy>, <http://www.w3.org/2002/07/owl#sameAs>, <http://github.com/jschmidt10#person_Thomas>>>;p=blaze:history:added;o="2017-01-15T16:11:15.909Z"^^<http://www.w3.org/2001/XMLSchema#dateTime>]
[s=<http://github.com/jschmidt10#person_Thomas>;p=<http://github.com/jschmidt10#hasAge>;o="30"^^<http://www.w3.org/2001/XMLSchema#int>]
[s=<http://github.com/jschmidt10#person_Tommy>;p=<http://github.com/jschmidt10#hasLastName>;o="Test"]
[s=<http://github.com/jschmidt10#person_Tommy>;p=<http://www.w3.org/2002/07/owl#sameAs>;o=<http://github.com/jschmidt10#person_Thomas>]
Next I try a simple query for a particular subject:
SELECT * WHERE { <http://github.com/jschmidt10#person_Thomas> ?p ?o }
This query yields no results. It seems that none of my queries for a URI are working. I am able to get results when I query for a literal (e.g. ?s ?p "Test").
The API I am using to create my query is BigdataSailRepositoryConnection.prepareQuery().
Code snippet (Scala) that executes and generates the query:
val props = BasicRepositoryProvider.getProperties("./graph.jnl")
val sail = new BigdataSail(props)
val repo = new BigdataSailRepository(sail)
repo.initialize()
val query = "SELECT ?p ?o WHERE { <http://github.com/jschmidt10#person_Thomas> ?p ?o }"
val cxn = repo.getConnection
cxn.begin()
var res = cxn.
prepareTupleQuery(QueryLanguage.SPARQL, query).
evaluate()
while (res.hasNext) println(res.next)
cxn.close()
repo.shutDown()
Have you checked the way you filled the database? You might have characters that are getting encoded strangely, or it looks like you might have excess brackets in your objects.
From the print statement, your URI's are printing extra angled brackets. You are likely using:
val subject = valueFactory.createURI("<http://some.url/some/entity>")
when you should be doing this (without angled brackets):
val subject = valueFactory.createURI("http://some.url/some/entity")

Searching semantically tagged documents in MarkLogic

Can any one please point me to some simple examples of semantic tagging and querying semantically tagged documents in MarkLogic?
I am fairly new in this area,so some beginner level examples will do.
When you say "semantically tagged" do you mean regular XML documents that happen to have some triples in them? The discussion and examples at http://docs.marklogic.com/guide/semantics/embedded are pretty good for that.
Start by enabling the triple index in your database. Then insert a test doc. This is just XML, but the sem:triple element represents a semantic fact.
xdmp:document-insert(
'test.xml',
<test>
<source>AP Newswire</source>
<sem:triple date="1972-02-21" confidence="100">
<sem:subject>http://example.org/news/Nixon</sem:subject>
<sem:predicate>http://example.org/wentTo</sem:predicate>
<sem:object>China</sem:object>
</sem:triple>
</test>)
Then query it. The example query is pretty complicated. To understand what's going on I'd insert variations on that sample document, using different URIs instead of just test.xml, and see how the various query terms match up. Try using just the SPARQL component, without the extra cts query. Try cts:search with no SPARQL, just the cts:query.
xquery version "1.0-ml";
import module namespace sem = "http://marklogic.com/semantics"
at "/MarkLogic/semantics.xqy";
sem:sparql('
SELECT ?country
WHERE {
<http://example.org/news/Nixon> <http://example.org/wentTo> ?country
}
',
(),
(),
cts:and-query((
cts:path-range-query( "//sem:triple/#confidence", ">", 80) ,
cts:path-range-query( "//sem:triple/#date", "<", xs:date("1974-01-01")),
cts:or-query((
cts:element-value-query( xs:QName("source"), "AP Newswire"),
cts:element-value-query( xs:QName("source"), "BBC"))))))
In case you are talking about enriching your content using semantic technology, that is not directly provided by MarkLogic.
You can enrich your content externally, for instance by calling a public service like the one provided by OpenCalais, and then insert the enrichments to the content before insert.
You can also build lists of lookup values, and then using cts:highlight to mark such terms within your content. That could be as simple as:
let $labels := ("MarkLogic", "StackOverflow")
return
cts:highlight($doc, cts:word-query($labels), <b>{$cts:text}</b>)
Or with a more dynamic replacement using spraql:
let $labels := map:new()
let $_ :=
for $result in sem:sparql('
PREFIX demo: <http://www.marklogic.com/ontologies/demo#>
SELECT DISTINCT ?label
WHERE {
?s a demo:person.
{
?s demo:fullName ?label
} UNION {
?s demo:initialsName ?label
} UNION {
?s demo:email ?label
}
}
')
return
map:put($labels, map:get($result, 'label'), 'person')
return
cts:highlight($doc, cts:word-query(map:keys($labels)),
let $result := sem:sparql(concat('
PREFIX demo: <http://www.marklogic.com/ontologies/demo#>
SELECT DISTINCT ?s ?p
{
?s a demo:', map:get($labels, $cts:text), ' .
?s ?p "', $cts:text, '" .
}
'))
return
if (map:contains($labels, $cts:text))
then
element { xs:QName(fn:concat("demo:", map:get($labels, $cts:text))) } {
attribute subject { map:get($result, 's') },
attribute predicate { map:get($result, 'p') },
$cts:text
}
else ()
)
HTH!