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.
}
Related
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 !
i'm running Ontology project on Netbeans using Jena for SPARQL (version 2.6.2 ) Queries, and now i'm trying to FILTERS for Temperature , this code doesn't work as expected and return no result
PREFIX ns: <http://www.semanticweb.org/pavilion/ontologies/2017/5/untitled-ontology-66#>
SELECT ?StarName ?Temperature
WHERE {
?star a ns:Star ;
ns:possessesSpectralType ?SpectralType ;
ns:possessesStarName ?StarName ;
ns:possessesTemperature ?Temperature .
FILTER (?Temperature > 10 ).
}
on the other hand i've tried this code and it works but only with Equal operator
PREFIX ns: <http://www.semanticweb.org/pavilion/ontologies/2017/5/untitled-ontology-66#>
SELECT ?star
WHERE {
?star a ns:Star ;
ns:possessesSpectralType ?SpectralType ;
ns:possessesStarName ?StarName ;
ns:possessesTemperature ?Temperature .
FILTER (?Temperature = ns:168 )
}
I have trouble solving the following problem:
"Construct a graph with the apoapsis of each planet together with a reference to the planet that comes next with regard to its distance from the sun."
Here is dump of the graph:
:Saturn
skos:exactMatch dbr:Saturn;
rdf:type dbo:Planet;
v:orbits :Sun;
v:apoapsis [rdf:value 9.0412; v:uom unit:AU] ;
v:orbitalPeriod [rdf:value 29.45; v:uom unit:YR ];
v:radius [rdf:value 60268; v:uom unit:KM] ;
v:temperature
[rdf:value -139;
v:uom unit:Deg_C ];
.
So in the graph is some data about the solarsystem. All planets (dbo:Planet) have the v:apoapsis property and a value telling the distance to the sun. I already figured out how to find all bigger values but i just want the next biggest one. The result looks like this:
:Mars v:apoapsis 1.666 ;
v:nextPlanet :Saturn , :Jupiter , :Uranus .
:Mercury v:apoapsis 0.467 ;
v:nextPlanet :Saturn , :Jupiter , :Uranus , :Mars , :Earth , :Venus .
:Earth v:apoapsis 1.017 ;
v:nextPlanet :Saturn , :Jupiter , :Uranus , :Mars .
:Venus v:apoapsis 0.728 ;
v:nextPlanet :Saturn , :Jupiter , :Uranus , :Mars , :Earth .
:Jupiter v:apoapsis 5.4588 ;
v:nextPlanet :Saturn , :Uranus .
:Saturn v:apoapsis 9.0412 ;
v:nextPlanet :Uranus .
The expected result should look like this:
:Mars v:apoapsis 1.666 ;
v:nextPlanet :Jupiter .
:Mercury v:apoapsis 0.467 ;
v:nextPlanet :Venus .
:Uranus v:apoapsis 20.11 ;
v:nextPlanet :Neptune .
I'm kind of new to SPARQL and suck with the idea of iterating over elements for this kind of task. A full solution is not necessary I just want to know how to solve this problem and I'm happy about some ideas. Thank you.
My most prominent query looks like this:
CONSTRUCT{?planet v:apoapsis ?AUdist;
v:nextPlanet ?nextPlanet .}
WHERE {
?planet a dbo:Planet.
?planet v:apoapsis ?dist.
?dist v:uom unit:AU;
rdf:value ?AUdist .
FILTER(?AUdist > ?AUdist2)
{
SELECT ?nextPlanet ?AUdist2
WHERE {
?nextPlanet a dbo:Planet.
?nextPlanet v:apoapsis ?dist2.
?dist2 v:uom unit:AU;
rdf:value ?AUdist2 .
}
ORDER BY ASC(?AUdist2)
}
{
}
}ORDER BY ASC(?AUdist)
The idea is to get the minimum distance value in a subquery and then get the corresponding planet in the outer query:
CONSTRUCT {
?planet v:apoapsis ?dist;
v:nextPlanet ?nextPlanet .
} WHERE {
?planet v:apoapsis ?dist ;
v:nextPlanet ?nextPlanet .
?nextPlanet v:apoapsis ?nextDist
BIND(abs(?dist - ?nextDist) as ?diff)
FILTER(?diff = ?minDiff)
# get planet and the minimum distance to its next planet
{
SELECT ?planet (min(?diff) as ?minDiff) {
?planet v:apoapsis ?dist ;
v:nextPlanet/v:apoapsis ?nextDist
BIND(abs(?dist - ?nextDist) as ?diff)
} GROUP BY ?planet
}
}
Note, the query here starts from your intermediate results. You didn't share the whole data, thus, I had to test on what I got from you.
The general approach to this sort of query is to:
Get all combinations of value1 and value2
Keep only those combinations where value1 is smaller than value2
Use GROUP BY and MIN to find the smallest value2 for a given value1
You've already done steps 1 and 2. To rewrite your query a bit:
SELECT * {
?planet v:apoapsis/rdf:value ?dist.
?otherPlanet v:apoapsis/rdf:value ?otherDist.
FILTER (?dist < ?otherDist)
}
Now in step 3, we want to group by ?planet, and find the smallest ?otherDist within each group:
SELECT ?planet (MIN(?otherDist) AS ?nextDist) {
?planet v:apoapsis/rdf:value ?dist.
?otherPlanet v:apoapsis/rdf:value ?otherDist.
FILTER (?dist < ?otherDist)
}
GROUP BY ?planet
That was the hard part. What remains is to turn the query above into a subquery inside a CONSTRUCT query that finds the ?nextPlanet corresponding to ?nextDist and constructs the target graph.
I have trouble with a SPARQL query. I have two graphs: Discipline and Professor. The graph Discipline contains different disciplines and is linked with the Professor thanks to his id. For example, I have:
<http://www.rdcproject.com/graph/disciplines> {
<http://www.rdfproject.com/77803>
a <http://www.rdfproject.com/Discipline> ;
<http://www.rdfproject.com/cfu>
"6" ;
<http://www.rdfproject.com/disciplineAbbreviation>
"SE" ;
<http://www.rdfproject.com/disciplinename>
"Software Engineering" ;
<http://www.rdfproject.com/hasCourseof>
<http://www.rdfproject.com/8028> ;
<http://www.rdfproject.com/idDiscipline>
"77803" ;
<http://www.rdfproject.com/isTaughtBy>
<http://www.rdfproject.com/0009004> ,
<http://www.rdfproject.com/0004003> ;
<http://www.rdfproject.com/obligatory>
"false" ;
<http://www.rdfproject.com/semester>
"1" ;
<http://www.rdfproject.com/totalhours>
"50" ;
<http://www.rdfproject.com/weekhours>
"5" ;
<http://www.rdfproject.com/year>
"1" .
}
This course is taught by two professors:
<http://www.rdfproject.com/0009004>
a <http://www.rdfproject.com/Teacher> ;
<http://www.rdfproject.com/firstName>
"Koby" ;
<http://www.rdfproject.com/idProfessor>
"0009004" ;
<http://www.rdfproject.com/lastName>
"Bryant" ;
<http://www.rdfproject.com/role>
"contratto" .
<http://www.rdfproject.com/0004003>
a <http://www.rdfproject.com/Teacher> ;
<http://www.rdfproject.com/firstName>
"Lebron" ;
<http://www.rdfproject.com/idProfessor>
"0004003" ;
<http://www.rdfproject.com/lastName>
"James" ;
<http://www.rdfproject.com/role>
"associato" .
Now I want all professors for all disciplines. I have created this query:
PREFIX uni: <http://www.rdfproject.com/>
PREFIX un: <http://www.w3.org/2007/ont/unit#>
SELECT ?idDiscipline ?disciplineName
(CONCAT('[',GROUP_CONCAT(?idProf;separator=","),', ',GROUP_CONCAT(?firstName;separator=","),', ',GROUP_CONCAT(?lastName;separator=","),', ',GROUP_CONCAT(?role;separator=","),']') as ?professors)
FROM <http://www.rdcproject.com/graph/disciplines>
FROM <http://www.rdcproject.com/graph/professor>
WHERE
{
{
?x a uni:Discipline;
uni:disciplinename ?disciplineName;
uni:idDiscipline ?idDiscipline;
uni:disciplineAbbreviation ?sigleDiscipline;
uni:cfu ?cfu;
uni:hasCourseof ?hasCourseof;
uni:obligatory ?obligatory;
uni:semester ?semester;
uni:totalhours ?totalhours;
uni:weekhours ?weekhours;
uni:year ?year;
uni:isTaughtBy ?isTaughtBy.
?isTaughtBy a uni:Teacher;
uni:idProfessor ?idProf;
uni:firstName ?firstName;
uni:role ?role;
uni:lastName ?lastName.
}
}
GROUP BY ?idDiscipline ?disciplineName
This query works fine if a discipline contains only one professor, but in this case the result is:
ID -> 77803"
NAME -> "Software Engineering"
PROFESSOR -> "[0009004,0004003, Kobe,Lebron, Bryant ,James,
contract,contract]"
How can I get this result?
ID -> 77803"
NAME -> "Software Engineering"
PROFESSOR -> "[0009004, Kobe, Bryant, contract]
[0004003, Lebron,James, contract]
Thanks
EDIT:
Thanks to #AKSW
My new Query is:
PREFIX uni: <http://www.rdfproject.com/>
PREFIX un: <http://www.w3.org/2007/ont/unit#>
SELECT ?idDiscipline ?sigleDiscipline ?disciplineName ?cfu ?hasCourseof ?obligatory ?semester ?totalhours ?weekhours ?year
(GROUP_CONCAT(DISTINCT ?prof_str;separator=",") AS ?Professor)
FROM <http://www.rdcproject.com/graph/disciplines>
FROM <http://www.rdcproject.com/graph/professor>
WHERE
{
{
?x a uni:Discipline;
uni:disciplinename ?disciplineName;
uni:idDiscipline ?idDiscipline;
uni:disciplineAbbreviation ?sigleDiscipline;
uni:cfu ?cfu;
uni:hasCourseof ?hasCourseof;
uni:obligatory ?obligatory;
uni:semester ?semester;
uni:totalhours ?totalhours;
uni:weekhours ?weekhours;
uni:year ?year;
uni:isTaughtBy ?isTaughtBy.
?isTaughtBy a uni:Teacher;
uni:idProfessor ?idProf;
uni:firstName ?firstName;
uni:lastName ?lastName;
uni:role ?role.
}
BIND(CONCAT('[',?idProf,',',?firstName,',',?lastName,',',?role,']') AS ?prof_str)
}
GROUP BY ?idDiscipline ?sigleDiscipline ?disciplineName ?cfu ?hasCourseof ?obligatory ?semester ?totalhours ?weekhours ?year
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