I have a RDF Like this
#prefix ab: <http://www.example.org/assignment#>.
ab:Sun ab:name "Sun".
ab:Birds ab:name "Birds".
ab:Mountains" ab:name "Mountains".
ab:River ab:name "River".
ab:House ab:name "House".
ab:Land ab:name "Land".
ab:Boat ab:name "Boat".
ab:Tree ab:name "Tree".
ab:Sun ab:right-of ab:Birds;
ab:above ab:Mountains.
ab:Birds ab:above ab:Mountains.
ab:Mountains ab:above ab:River;
ab:above ab:House.
ab:House ab:above ab:Land;
ab:right-of ab:Boat.
ab:Land ab:right-of ab:River.
ab:Boat ab:above ab:River.
ab:Tree ab:above ab:House.
Its basically a relationship of directions between two objects
My Query is
What all are below “Sun” ?
I can understand its a form of inverse query processing and I need to interpret “below” as inverse of “above”. But how I do about that, should I use some form of carat operator^ here
Related
For a course I follow I have to write several SPARQL queries about Lego sets. I am interested in finding out if there is a Lego theme which has a single package type.
I have the following SPARQL query:
select distinct ?theme ?package_type (count(?theme) as ?amount_of_lego_sets)
where{
?Lego_set rdf:type wd:Q58780257 .
?Lego_set sch:audienceType ?audience .
?Lego_set ex:has_package_information ?package_info .
?audience ex:belongs_to_theme ?theme .
?package_info ex:has_packaging_type ?package_type .
} group by ?theme ?package_type
order by ?theme
Which produces the following output:
As you can see there is one set that has the theme “4 Juniors” and the package type “Polybag”. Now, I am interested in themes like Advanced models or Action Wheelers which only have a single package type. However, I found it challenging to filter for these themes.
What modification to my query could I implement to remove themes which have sets that have more than one package type?
This is an interesting question. I would use FILTER NOT EXISTS to add an atom to the query body, where we make sure that the theme doesn't have two types of packages, like this:
FILTER NOT EXISTS {
?Lego_set2 sch:audienceType ?audience2 .
?Lego_set2 ex:has_package_information ?package_info2 .
#Notice that the 'theme' variable must be the same as the outer query
?audience2 ex:belongs_to_theme ?theme .
?package_info2 ex:has_packaging_type ?package_type1, ?package_type2
FILTER(?pakage_type1 != ?package_type2)}
Thus your full query should be something like:
select distinct ?theme ?package_type (count(?theme) as ?amount_of_lego_sets)
where{
?Lego_set rdf:type wd:Q58780257 .
?Lego_set sch:audienceType ?audience .
?Lego_set ex:has_package_information ?package_info .
?audience ex:belongs_to_theme ?theme .
?package_info ex:has_packaging_type ?package_type .
FILTER NOT EXISTS {
?Lego_set2 sch:audienceType ?audience2 .
?Lego_set2 ex:has_package_information ?package_info2 .
#Notice that the 'theme' variable must be the same as the outer query
?audience2 ex:belongs_to_theme ?theme .
?package_info2 ex:has_packaging_type ?package_type1, ?package_type2
FILTER(?pakage_type1 != ?package_type2)}
} group by ?theme ?package_type
order by ?theme
You could also use the aggregation approach mentioned in the comments, but hopefully this one solves your problem.
Suppose I have the following set of individuals, where some of them have more than one rdfs:label:
Individual
Label
ind_01
"ind-001"
ind_02
"ind-002"
ind_02
"ind-2"
ind_03
"label3"
ind_04
"ind-4"
ind_04
"ind-04"
...
...
I would like to run a SPARQL query that retrieves only one label per individual, no matter which one (i.e., the choice can be totally arbitrary). Thus, a suitable output based on the above dataset would be:
Individual
Label
ind_01
"ind-001"
ind_02
"ind-002"
ind_03
"label3"
ind_04
"ind-4"
...
...
You could use SAMPLE, which "returns an arbitrary value from the multiset passed to it".
SELECT ?individual (SAMPLE(?label) AS ?label_sample)
WHERE {
?individual rdfs:label ?label .
}
GROUP BY ?individual
I am using jena-fuseki server. Suppose for example my default graph has the following triples.
#prefix bprefix: <http://raghav/Rathi#>
bprefix:title71 a bprefix:Title ;
bprefix:fromCountry bprefix:UnitedStates ;
bprefix:hasCast bprefix:BobbyMoynihan , bprefix:DemetriMartin , bprefix:EricEdelstein ;
bprefix:hasDateAdded "September 30, 2018" ;
bprefix:hasDescription "Grizzly, Panda and Ice Bear are three adopted bear brothers struggling against their animal instincts to fit into the civilized, modern human world." ;
bprefix:hasDuration "1 Season" ;
bprefix:hasID "80116921"^^xsd:int ;
bprefix:hasRating bprefix:TV-Y7 ;
bprefix:hasReleaseyear "2017"^^xsd:int ;
bprefix:hasTitle "We Bare Bears" ;
bprefix:hasType bprefix:TVShow ;
bprefix:isListedin bprefix:TVComedies , <http://raghav/Rathi#Kids'TV> .
bprefix:title84 a bprefix:Title ;
bprefix:fromCountry bprefix:UnitedKingdom ;
bprefix:hasCast bprefix:PaulHollywood ;
bprefix:hasDateAdded "September 29, 2017" ;
bprefix:hasDescription "Gear up for a fast-paced journey as celebrity chef and avid auto enthusiast Paul Hollywood takes in the cars and culture of France, Italy and Germany." ;
bprefix:hasDuration "1 Season" ;
bprefix:hasID "80199032"^^xsd:int ;
bprefix:hasRating bprefix:TV-14 ;
bprefix:hasReleaseyear "2014"^^xsd:int ;
bprefix:hasTitle "Paul Hollywood's Big Continental Road Trip" ;
bprefix:hasType bprefix:TVShow ;
bprefix:isListedin bprefix:BritishTVShows , bprefix:Docuseries , bprefix:InternationalTVShows .
bprefix:title28 a bprefix:Title ;
bprefix:fromCountry bprefix:UnitedStates ;
bprefix:hasDateAdded "September 7, 2018" ;
bprefix:hasDescription "Women whO ve been sexually brutalized in war-torn Congo begin to heal at City of Joy, a center that helps them regain a sense of self and empowerment." ;
bprefix:hasDirector bprefix:MadeleineGavin ;
bprefix:hasDuration "77 min" ;
bprefix:hasID "80203094"^^xsd:int ;
bprefix:hasRating bprefix:TV-MA ;
bprefix:hasReleaseyear "2018"^^xsd:int ;
bprefix:hasTitle "City of Joy" ;
bprefix:hasType bprefix:Movie ;
bprefix:isListedin bprefix:Documentaries .
Now I want to move the triples where title hasReleaseyear>2015 from this default graph to a named graph http://example.org/mynamedgraph.
I can construct the triples that I want to move using
CONSTRUCT {?title ?predicate ?object} WHERE{
?title ?predicate ?object.
?title bprefix:hasReleaseyear ?year FILTER(?year>2015)
}
What I could look online was that MOVE operation will move "all" the triples from default to this named graph. How can I filter the triples before moving them from default graph to named graph.
I would like to know if there is a possibility to retrieve UUID() urn: when using INSERT Statement in SPARQL query ?
My problem is simple but I don't know how to solve it using SPARQL :
I would like to store a lot of timestamp values. Same timestamp can appear multiple times, so I guess I can use UUID() to generate random URI.
I need urn: from UUID() function to relate my new triples.
I'm right ?
Or UUID() is not the solution ?
Thanks for your help.
EDIT :
Ok, so I have to say I would like to retrieve data in my python code.
I am using SPARQLWrapper to run my requests.
If I create one INSERT request like that :
INSERT {
?uuid1 rdf:type capt:ECHANTILLON .
?uuid1 capt:Nom ?uuid1 .
?uuid1 capt:Description '2019-08-07 16:07:34.027636' .
?uuid1 capt:Valeur '1565182189' .
} WHERE {
FILTER NOT EXISTS { ?uuid1 rdf:type capt:ECHANTILLON} .
BIND (UUID() AS ?uuid1) .
};
Then an other INSERT request using ?uuid1 from the first :
INSERT {
?uuid2 rdf:type capt:VALEUR .
?uuid2 capt:Nom ?uuid2 .
?uuid2 capt:Description '2019-08-07 16:07:34.027659' .
?uuid2 capt:Valeur '27.0' .
**?uuid1 capt:A_Pour_Valeur ?uuid2 .** <===
} WHERE {
FILTER NOT EXISTS { ?uuid2 rdf:type capt:VALEUR} .
BIND (UUID() AS ?uuid2) .
};
What I want is :
uuid1 = endpoint.setQuery(request_1)
request_2.addSubject(uuid1)
uuid2 = endpoint.setQuery(request_2)
Something like that.
How can I retrieve ?uuid1 from the first request if INSERT does not return this value ? I would like to make two requests if possible.
Have I to regroup two requests in one request, or have I to run a SELECT ?uuid1 request before running second ?
You can't get the value of the UUID directly from the SPARQL update - if you want to retrieve it via SPARQL somehow, you'll have to do a query after you've inserted it - or, of course, you could adapt your second SPARQL update to do the selection for you by querying for the 'correct' UUID in its WHERE clause.
However, in this case, that looks difficult to do, and I think the easiest solution for you is that you don't create the UUID in the SPARQL operation itself, but instead create it in code and then use that in your query string, e.g. by adding a VALUES clause, something like this:
import uuid
uuid1 = uuid.uuid1()
update = """INSERT { .... }
WHERE { VALUES ?uuid1 { <urn:uuid:""" + uuid1 + """> }
FILTER NOT EXISTS .... (etc) }"""
I'm trying to autocomplete what the user writes in an input, with terms in DBpedia, similar to this jsFiddle example. Try writing dog in the input of that jsFiddle, and you will see the 'Dog' term in the suggestions.
I have the following code, and the problem is that the 10-term list I got as a result does not contains the "Dog" alternative. So, if I could order the list by the length of the (string representation of) ?concept, then I could get that term. Is this possible?
SELECT DISTINCT ?concept
WHERE {
?concept a skos:Concept .
FILTER regex(str(?concept), "dog", "i")
}
ORDER BY ASC(?concept) LIMIT 10
So, if I could order the list by the lenght of the ?concept it is possible to get the term. But I can't find the right statement to do it. Is it possible?
It sounds like you're looking for strlen.
order by strlen(str(?concept))
E.g.,
select distinct ?concept where {
?concept a skos:Concept .
filter regex(str(?concept), "dog", "i")
}
order by strlen(str(?concept))
limit 10
SPARQL results
That said, if you're just checking string membership, you don't need all the power of regular expressions, and it might be more efficient to use contains and lcase to check whether the lowercased ?concept contains "dog" with a filter like:
filter contains(lcase(str(?concept)), "dog")
The table of contents in the SPARQL spec has a big list of functions that you can browse. In particular, you'd want to look at the subsections of 17.4 Function Definitions.