How can I move filtered triples from one graph to other? - sparql

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.

Related

Retrieve only one label from set of individuals with multiple labels

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

SHACL and RDF STAR (RDF*)

Is there a way in SHACL, to match only the triples of Claire?
<<ex:Bob ex:age 23>>
ex:date "2019-12-05"^^xsd:date ;
ex:author ex:Claire .
<<ex:Bob ex:age 123>>
ex:date "2019-12-06"^^xsd:date ;
ex:author ex:Bob .

SPARQL query: OR in FILTER?

I would like to search court cases based on their short title, but I've noticed in the RDF records that this information is sometimes stored under one property (cdm:expression_case-law_parties) and sometimes under another (cdm:expression_title_alternative). I would like to filter on both simultaneously. The below query, where I'm trying to use an OR || in the FILTER) does not work. What is the appropriate way?
PREFIX cdm: <http://publications.europa.eu/ontology/cdm#>
SELECT ?work ?expression ?ecli ?celex ?alttitle ?parties ?title
WHERE {
?work a ?class.
?expression cdm:expression_belongs_to_work ?work.
?expression cdm:expression_title ?title.
?expression cdm:expression_uses_language <http://publications.europa.eu/resource/authority/language/ENG>.
?work cdm:case-law_ecli ?ecli.
?work cdm:resource_legal_id_celex ?celex.
OPTIONAL{?expression cdm:expression_case-law_parties ?parties}
OPTIONAL{?expression cdm:expression_title_alternative ?alttitle}
FILTER(?class in (<http://publications.europa.eu/ontology/cdm#judgement>))
FILTER CONTAINS (?alttitle, "France v Commission") || (?parties, "France v Commission")}
LIMIT 15
From Stanislav Kralin's comment:
FILTER (CONTAINS (?alttitle, "France v Commission") || CONTAINS(?parties, "France v Commission"))

Query for age in sparql

I am writing a query that calculates the age of someone knowing their birthdate and deathdate. I want to ask for a certain artist (in this case Michael Jackson). The problem is that I can't reuse the birthdate and deathdate in the following query:
select ?artist ?age
where {
dbr:Michael_Jackson dbo:birthDate ?birthdate .
dbr:Michael_Jackson dbo:deathDate ?deathdate .
bind( year(?deathdate) - year(?birthdate) - if(month(?deathdate)<month(?birthdate) || (month(?deathdate)=month(?birthdate) && day(?deathdate<day(?birthdate)),1,0) as ?age)
}
Anyone knows how to solve this problem?
I edited the query a bit to get the values you're calculating against --
birthdate deathdate
"1958-8-29"^^<http://www.w3.org/2001/XMLSchema#date> "2009-6-25"^^<http://www.w3.org/2001/XMLSchema#date>
The error you're hitting is because these are not valid xsd:date literals -- which require 2 digits for the month.
This data issue has been fixed on Wikipedia and so on DBpedia-Live, and your query works there, as you can see. There are other issues with DBpedia-Live data (such as multiple versions of the abstract being shown, when you really only want to see the latest), but perhaps this solves your immediate need.

SPARQL group by a substring and average

I am querying a large data set (temperatures recorded hourly for nearly 20 years) and I'd rather get a summary, e.g. daily temperatures.
An example query is here:
http://www.boisvert.me.uk/opendata/sparql_aq+.html?pasteid=hu5rbc7W
PREFIX opensheff: <uri://opensheffield.org/properties#>
select ?time ?temp where {
?m opensheff:sensor <uri://opensheffield.org/datagrid/sensors/Weather_Mast/Weather_Mast.ic> ;
opensheff:rawValue ?temp ;
<http://purl.oclc.org/NET/ssnx/ssn#endTime> ?time .
FILTER (str(?time) > "2011-09-24")
}
ORDER BY ASC(?time)
And the results look like this:
time temp
"2011-09-24T00:00Z" 12.31
"2011-09-24T01:00Z" 11.68
"2011-09-24T02:00Z" 11.92
"2011-09-24T03:00Z" 11.59
Now I would like to group by a part of the date string, so as to get a daily average temperature:
time temp
"2011-09-24" 12.3 # or whatever
"2011-09-23" 11.7
"2011-09-22" 11.9
"2011-09-21" 11.6
So, how do I group by a substring of ?time ?
Eventually solved it. Running here:
http://www.boisvert.me.uk/opendata/sparql_aq+.html?pasteid=j8m0Qk6s
Code:
PREFIX opensheff:
select ?d AVG(?temp) as ?day_temp
where {
?m opensheff:sensor <uri://opensheffield.org/datagrid/sensors/Weather_Mast/Weather_Mast.ic> ;
opensheff:rawValue ?temp ;
<http://purl.oclc.org/NET/ssnx/ssn#endTime> ?time .
BIND( SUBSTR(?time, 1, 10) AS ?d ) .
}
GROUP BY ?d
ORDER BY ASC(?d)
We use BIND to set a new variable to the substring required, and then grouping and averaging by that variable is simple enough.