Issue when updating triple with blank nodes using sqarql - sparql

This is my query, this query works fine for nodes with no blank node
but the issue with arrives while updating with blank node
here those nodes get repeated 100 times after updating
DELETE {
it:4D9AT21jAasfL9ah schema:description ?o_0 .
it:4D9AT21jAasfL9ah it:hasArea ?o_1 .
?o_1 rdf:type ?o_1_0 . ?o_1 schema:unitCode ?o_1_1 .
?o_1 schema:value ?o_1_2 . ?o_1 schema:unitText ?o_1_3 .
it:4D9AT21jAasfL9ah it:unresolvedWikidataType ?o_2 .
it:4D9AT21jAasfL9ah schema:longitude ?o_3 .
it:4D9AT21jAasfL9ah schema:addressCountry ?o_4 .
it:4D9AT21jAasfL9ah rdfs:label ?o_5 .
it:4D9AT21jAasfL9ah rdf:type ?o_6 .
it:4D9AT21jAasfL9ah schema:url ?o_7 .
it:4D9AT21jAasfL9ah it:wikiLink ?o_8 .
}
INSERT {
it:4D9AT21jAasfL9ah schema:description 'city and capital of Bali, Indonesia' .
it:4D9AT21jAasfL9ah it:hasArea _:bQRrRFzX1zv9qUGWy .
_:bQRrRFzX1zv9qUGWy rdf:type schema:QuantitativeValue;
schema:unitCode 'KMK';
schema:value '123.98';
schema:unitText 'km²' .
it:4D9AT21jAasfL9ah it:unresolvedWikidataType 'Q3199141', 'Q13220204' .
it:4D9AT21jAasfL9ah schema:longitude '115.21666666667' .
it:4D9AT21jAasfL9ah schema:addressCountry it:WtC49aYLe8s0lsFC .
it:4D9AT21jAasfL9ah rdfs:label 'Denpasar' .
it:4D9AT21jAasfL9ah rdf:type schema:City .
it:4D9AT21jAasfL9ah schema:url <http://www.denpasarkota.go.id/> .
it:4D9AT21jAasfL9ah it:wikiLink <https://en.wikipedia.org/wiki/Denpasar> .
}
WHERE {
it:4D9AT21jAasfL9ah ?p ?o .
OPTIONAL { it:4D9AT21jAasfL9ah schema:description ?o_0 }
OPTIONAL { it:4D9AT21jAasfL9ah it:hasArea ?o_1 }
OPTIONAL { ?o_1 rdf:type ?o_1_0 }
OPTIONAL { ?o_1 schema:unitCode ?o_1_1 }
OPTIONAL { ?o_1 schema:value ?o_1_2 }
OPTIONAL { ?o_1 schema:unitText ?o_1_3 }
OPTIONAL { it:4D9AT21jAasfL9ah it:unresolvedWikidataType ?o_2 }
OPTIONAL { it:4D9AT21jAasfL9ah schema:longitude ?o_3 }
OPTIONAL { it:4D9AT21jAasfL9ah schema:addressCountry ?o_4 }
OPTIONAL { it:4D9AT21jAasfL9ah rdfs:label ?o_5 }
OPTIONAL { it:4D9AT21jAasfL9ah rdf:type ?o_6 }
OPTIONAL { it:4D9AT21jAasfL9ah schema:url ?o_7 }
OPTIONAL { it:4D9AT21jAasfL9ah it:wikiLink ?o_8 }
}
This is the output in json format,
and only hasArea which has blank node values is being repeated many times
"schema:latitude": [
"-7.164444444444444"
],
"it:hasArea": [
{
"rdf:type": [
"schema:QuantitativeValue"
],
"schema:unitCode": [
"KMK"
],
"schema:value": [
"392.47"
],
"schema:unitText": [
"km²"
]
},
{
"rdf:type": [
"schema:QuantitativeValue"
],
"schema:unitCode": [
"KMK"
],
"schema:value": [
"392.47"
],
"schema:unitText": [
"km²"
]
},
{
"rdf:type": [
"schema:QuantitativeValue"
],
"schema:unitCode": [
"KMK"
],
"schema:value": [
"392.47"
],
"schema:unitText": [
"km²"
]
},...
],
"rdfs:label": [
"Denpasar"
],
...
Thanks in advance

Related

Unable to DELETE DATA in Jena Fuseki while INSERT DATA works perfectly

I am trying to delete rules written in turtle in Apache Jena Fuseki with the Jena API, the problem I am facing right now is that a INSERT DATA request works great as you can see :
INSERT DATA {
[ rdf:type swrl:Imp ;
rdfs:comment "" ;
rdfs:label "NewRule" ;
swrla:isRuleEnabled true ;
swrl:body [ rdf:type swrl:AtomList ;
rdf:first [] ;
rdf:rest [ rdf:type swrl:AtomList ;
rdf:first [ rdf:type swrl:ClassAtom ;
swrl:argument1 :c ;
swrl:classPredicate :Person
] ;
rdf:rest [ rdf:type swrl:AtomList ;
rdf:first [ rdf:type swrl:IndividualPropertyAtom ;
swrl:argument1 :d ;
swrl:argument2 :c ;
swrl:propertyPredicate :isAbove
] ;
rdf:rest ()
]
]
] ;
swrl:head [ rdf:type swrl:AtomList ;
rdf:first [ rdf:type swrl:ClassAtom ;
swrl:argument1 :d ;
swrl:classPredicate :ActionAssemble
] ;
rdf:rest ()
]
] .
}
but the DELETE DATA request which is the exact same syntax give me this error :
Line 12, column 9: Blank nodes not allowed in DELETE templates
And here is the request :
DELETE DATA {
[ rdf:type swrl:Imp ;
rdfs:comment "" ;
rdfs:label "NewRule" ;
swrla:isRuleEnabled true ;
swrl:body [ rdf:type swrl:AtomList ;
rdf:first [] ;
rdf:rest [ rdf:type swrl:AtomList ;
rdf:first [ rdf:type swrl:ClassAtom ;
swrl:argument1 :c ;
swrl:classPredicate :Person
] ;
rdf:rest [ rdf:type swrl:AtomList ;
rdf:first [ rdf:type swrl:IndividualPropertyAtom ;
swrl:argument1 :d ;
swrl:argument2 :c ;
swrl:propertyPredicate :isAbove
] ;
rdf:rest ()
]
]
] ;
swrl:head [ rdf:type swrl:AtomList ;
rdf:first [ rdf:type swrl:ClassAtom ;
swrl:argument1 :d ;
swrl:classPredicate :ActionAssemble
] ;
rdf:rest ()
]
] .
}
Thank you for your time !

SPARQL - CONSTRUCT Query Performance Problem

i've to query a set of statements before i create a new version of an object with the same identifier. With the result i analyse the changes and reuse referenced objects, if the haven't changed. But the following query is aweful and very slow. if there are ~ 1000 object versions, it runs about 120 seconds. and i've to import lot more !
Is there a way to query the statements in a more performant way? I know the "OPTIONAL" is bad, but the properties can be empty.
Thanks
PREFIX schema: <https://schema.org/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX as: <https://www.w3.org/ns/activitystreams#>
CONSTRUCT {
?publication rdf:type ?type ;
schema:about ?about ;
schema:about ?about;
schema:name ?name ;
as:name ?asname ;
as:published ?published ;
as:attributedTo ?attributedTo ;
schema:creativeWorkStatus ?creativeWorkStatus ;
schema:dateCreated ?dateCreated ;
schema:dateModified ?dateModified ;
schema:description ?description ;
schema:identifier ?identifier ;
schema:keywords ?keywords ;
schema:license ?license ;
schema:version ?version .
?about rdf:type ?aboutType ;
schema:name ?aboutName ;
schema:contactPoint ?aboutContactPoint ;
schema:location ?aboutLocation .
?aboutContactPoint rdf:type ?contactPointType ;
schema:email ?contactPointEmail ;
schema:name ?contactPointName ;
schema:telephone?contactPointTelephone .
?aboutLocation rdf:type ?locationType ;
schema:latitude ?locationLatitude ;
schema:longitude ?locationLongitude ;
schema:address ?locationAddress .
# Adress
?locationAddress rdf:type ?addressType ;
schema:addressCountry ?addressCountry ;
schema:addressLocality ?addressLocality ;
schema:streetAddress ?addressStreetAddress ;
schema:postalCode ?addressPostalCode .
}
FROM <http://localhost:8081/camel/fef6dc4b-e1e9-46f5-a34e-8ab5c1600ec4>
WHERE {
?publication rdf:type schema:CreativeWork ;
rdf:type ?type ;
schema:identifier "6472c8bbf6504b56b473a1e6a10fcc8d".
OPTIONAL {?publication schema:name ?name . }
OPTIONAL {?publication as:name ?asname . }
OPTIONAL {?publication as:published ?published . }
OPTIONAL {?publication as:attributedTo ?attributedTo . }
OPTIONAL {?publication schema:creativeWorkStatus ?creativeWorkStatus . }
OPTIONAL {?publication schema:dateCreated ?dateCreated . }
OPTIONAL {?publication schema:dateModified ?dateModified . }
OPTIONAL {?publication schema:description ?description . }
OPTIONAL {?publication schema:identifier ?identifier . }
OPTIONAL {?publication schema:keywords ?keywords . }
OPTIONAL {?publication schema:license ?license . }
OPTIONAL {?publication schema:version ?version . }
OPTIONAL {?publication schema:about ?about . }
# Organization
OPTIONAL {?publication schema:about/rdf:type ?aboutType . }
OPTIONAL {?publication schema:about/schema:name ?aboutName . }
OPTIONAL {?publication schema:about/schema:contactPoint ?aboutContactPoint . }
OPTIONAL {?publication schema:about/schema:location ?aboutLocation . }
# ContactPoint
OPTIONAL {?publication schema:about/schema:contactPoint/rdf:type ?contactPointType . }
OPTIONAL {?publication schema:about/schema:contactPoint/schema:email ?contactPointEmail . }
OPTIONAL {?publication schema:about/schema:contactPoint/schema:name ?contactPointName . }
OPTIONAL {?publication schema:about/schema:contactPoint/schema:telephone?contactPointTelephone . }
# Place
OPTIONAL {?publication schema:about/schema:location/rdf:type ?locationType . }
OPTIONAL {?publication schema:about/schema:location/schema:latitude ?locationLatitude . }
OPTIONAL {?publication schema:about/schema:location/schema:longitude ?locationLongitude . }
OPTIONAL {?publication schema:about/schema:location/schema:address ?locationAddress . }
# Adress
OPTIONAL {?publication schema:about/schema:location/schema:address/rdf:type ?addressType . }
OPTIONAL {?publication schema:about/schema:location/schema:address/schema:addressLocality ?addressLocality . }
OPTIONAL {?publication schema:about/schema:location/schema:address/schema:streetAddress ?addressStreetAddress . }
OPTIONAL {?publication schema:about/schema:location/schema:address/schema:postalCode ?addressPostalCode . }
OPTIONAL {?publication schema:about/schema:location/schema:address/schema:addressCountry ?addressCountry . }
}
Use VALUES to avoid OPTIONAL.
VALUES ?p {
schema:name
as:name
as:published
as:attributedTo
schema:creativeWorkStatus
schema:dateCreated
schema:dateModified
schema:description
schema:identifier
schema:keywords
schema:license
schema:version
schema:about
}
?publication ?p ?o .

Blank node skolemization in SPARQL without iteration

Is it possible to implement blank node skolemization in SPARQL without iteration? It seems to me that iteration is required to skolemize chains of blank nodes, such as:
#prefix : <http://example.com/> .
[ a :A ;
:p1 [
a :B
]
] .
A SPARQL Update operation for skolemization can start from the blank nodes that appear as subjects only in triples without blank node objects:
DELETE {
?b1 ?outP ?outO .
?inS ?inP ?b1 .
}
INSERT {
?iri ?outP ?outO .
?inS ?inP ?iri .
}
WHERE {
{
SELECT ?b1 (uuid() AS ?iri)
WHERE {
{
SELECT DISTINCT ?b1
WHERE {
?b1 ?p1 [] .
FILTER isBlank(?b1)
FILTER NOT EXISTS {
?b1 ?p2 ?b2 .
FILTER isBlank(?b2)
}
}
}
}
}
?b1 ?outP ?outO .
OPTIONAL {
?inS ?inP ?b1 .
}
}
This operation can be repeated until no blank nodes are found in the data:
ASK {
?bnode ?p [] .
FILTER isBlank(?bnode)
}
Is it possible to avoid the iteration and implement the blank node skolemization in a single SPARQL Update operation?
(Also, this approach assumes there are no "orphan" blank nodes (i.e. blank nodes that appear only as objects).)
I found a two-step solution skolemising subjects and objects separately and storing the blank node aliases (links between blank nodes and IRIs via owl:sameAs) as intermediate data:
PREFIX : <http://example.com/>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
####################
# Rewrite subjects #
####################
DELETE {
?bnode ?p ?o .
}
INSERT {
?iri ?p ?o .
GRAPH :aliases {
?bnode owl:sameAs ?iri .
}
}
WHERE {
{
SELECT ?bnode (uuid() AS ?iri)
WHERE {
{
SELECT DISTINCT ?bnode
WHERE {
?bnode ?p [] .
FILTER isBlank(?bnode)
}
}
}
}
?bnode ?p ?o .
}
;
###################
# Rewrite objects #
###################
DELETE {
?s ?p ?bnode .
}
INSERT {
?s ?p ?iri .
}
WHERE {
{
SELECT ?bnode ?iri
WHERE {
{
SELECT DISTINCT ?bnode
WHERE {
[] ?p ?bnode .
FILTER isBlank(?bnode)
}
}
OPTIONAL {
GRAPH :aliases {
?bnode owl:sameAs ?_iri .
}
}
BIND (coalesce(?_iri, uuid()) AS ?iri)
}
}
?s ?p ?bnode .
}
;
############################
# Clear blank node aliases #
############################
CLEAR GRAPH :aliases

Group_concat in SPARQL

I am a beginner with SPARQL, and I am trying to deal with the endpoint of the Spanish National Library.
I have a code that works, here it is:
prefix bne: <http://datos.bne.es/def/> # base URI for ontology documented at http://datos.bne.es/def/
prefix resource: <http://datos.bne.es/resource/>
select distinct
?book
?author
?title
?subtitle
?ISBN
?publisher
?date
?pags
?size
?series
?edition
?subjectLabel
where {
?book a bne:C1003 .
?book bne:P3001 "Errata Naturae" .
?book bne:P1011 ?author .
?book bne:P3002 ?title .
OPTIONAL { ?book bne:P3013 ?ISBN }
OPTIONAL { ?book bne:P3001 ?publisher }
OPTIONAL { ?book bne:P3006 ?date }
OPTIONAL { ?book bne:P3004 ?pags }
OPTIONAL { ?book bne:P3007 ?size }
OPTIONAL { ?book bne:P3016 ?series }
OPTIONAL { ?book bne:P1004 ?date }
OPTIONAL { ?book bne:P3017 ?edition }
OPTIONAL { ?book bne:P3014 ?subtitle }
OPTIONAL { ?book bne:OP3008 ?subject }
?subject rdfs:label ?subjectLabel
}
limit 50
But as some books has two or more subjects, the SPARQL repeats them in the results. I used group_concat, but for some reason it doesn't work:
prefix bne: <http://datos.bne.es/def/> # base URI for ontology documented at http://datos.bne.es/def/
prefix resource: <http://datos.bne.es/resource/>
select distinct
?book
?author
?title
?subtitle
?ISBN
?publisher
?date
?pags
?size
?series
?edition
(GROUP_CONCAT(DISTINCT(?subjectLabel); separator="//") as ?subjects)
where {
?book a bne:C1003 .
?book bne:P3001 "Errata Naturae" .
?book bne:P1011 ?author .
?book bne:P3002 ?title .
OPTIONAL { ?book bne:P3013 ?ISBN }
OPTIONAL { ?book bne:P3001 ?publisher }
OPTIONAL { ?book bne:P3006 ?date }
OPTIONAL { ?book bne:P3004 ?pags }
OPTIONAL { ?book bne:P3007 ?size }
OPTIONAL { ?book bne:P3016 ?series }
OPTIONAL { ?book bne:P1004 ?date }
OPTIONAL { ?book bne:P3017 ?edition }
OPTIONAL { ?book bne:P3014 ?subtitle }
OPTIONAL { ?book bne:OP3008 ?subject }
?subject rdfs:label ?subjectLabel
}
limit 50
group by ?book
order by ?date
Does someone know where I am making a mistake?
Thanks!
Edit:
I was doing one thing wrong: as #AKSW said, I have to group all the variables at the end of the code, or add variables on the select. I have a reduced version of the code for testing this:
PREFIX bne: <http://datos.bne.es/def/>
PREFIX resource: <http://datos.bne.es/resource/>
SELECT ?book ?author ?title (GROUP_CONCAT(DISTINCT ?subject ; separator='//') AS ?subjects)
WHERE
{ ?book a bne:C1003 ;
bne:P3001 "Errata Naturae" ;
bne:P1011 ?author ;
bne:P3002 ?title ;
bne:OP3008 ?subject
}
#group everything here
GROUP BY ?book ?author ?title
#JeenBroekstra, when I run it in a SPARQL Validator, it says it is OK, but when I try to run it in the SPARQL endpoint of the library, it gives me an error:
Virtuoso 37000 Error SP030: SPARQL compiler, line 6: syntax error at 'GROUP_CONCAT' before '('
As noted in comments, the problem is that the target endpoint is running on Virtuoso, Open Source Edition, v06.01.3127 as of 2011-11-16), which did not support GROUP_CONCAT in SPARQL, as SPARQL 1.1 had not yet been finalized.
Upgrading to a current version is strongly recommended!
There is a built-in function in this version of Virtuoso, available as sql:group_concat, as documented, which may serve you for now.

SPARQL distinct rowcount not returning correct number

I have the following SPARQL query , please pay attention to the rowcount in the selection predicate and the group by clause at the end of the query.
I want the query to return the correct row count in every record , I noticed that the row count that is being returned is not correct meaning if there is a single record I get 1 or sometimes 2 and if there are more than 2 records , I still get a 2, basically it seems to return random value.
I know its because of some issue with my query , can someone please let me know what I might be doing wrong ? I am using Apache Jena.
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX pf: <http://jena.hpl.hp.com/ARQ/property#>
PREFIX d: <http://data-vocabulary.org/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX s: <http://schema.org/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX gr: <http://purl.org/goodrelations/v1#>
SELECT DISTINCT (count(*) AS ?rowCount) ?productName ?offerImage ?offerName ?productCategory ?salePrice ?suggestedRetailPrice ?productImage ?productThumbNail ?productUrl (GROUP_CONCAT(DISTINCT ?productdescription) AS ?productdescriptions) ?productBrand ?productId ?productSku ?productModel ?productMPN ?productManufacturer ?productGtin13 ?productGtin14 ?productGtin8 ?productAvailable ?productUnAvailable ?productUsedCondition ?productNewCondition ?productColor ?productAggregateRatingValue ?productReviewCount
WHERE
{
?p2 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> d:Product .
?p2 <http://data-vocabulary.org/Product/offerDetails> ?schOffer .
?schOffer <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> d:Offer .
?schOffer <http://data-vocabulary.org/Offer/price> ?salePrice
OPTIONAL
{ ?schOffer <http://data-vocabulary.org/Offer/name> ?offerName }
OPTIONAL
{ ?p2 <http://data-vocabulary.org/Product/name> ?productName }
OPTIONAL
{ ?schOffer <http://data-vocabulary.org/Offer/image> ?offerImage }
OPTIONAL
{ ?p2 <http://data-vocabulary.org/Product/url> ?productUrl }
OPTIONAL
{ ?p2 <http://data-vocabulary.org/Product/image> ?productImage }
OPTIONAL
{ ?schOffer <http://data-vocabulary.org/Offer/category> ?productCategory }
OPTIONAL
{ ?p2 <http://data-vocabulary.org/Product/description> ?productdescription }
OPTIONAL
{ ?p2 <http://data-vocabulary.org/Product/brand> ?pBrandNode
OPTIONAL
{ ?pBrandNode <http://data-vocabulary.org/Brand/name> ?brandNodeName }
OPTIONAL
{ ?pBrandNode <http://data-vocabulary.org/Organization/name> ?brandNodeName }
BIND(str(coalesce(?brandNodeName, ?pBrandNode)) AS ?productBrand)
}
OPTIONAL
{ ?p2 <http://data-vocabulary.org/Product/manufacturer> ?pManufacturerNode
OPTIONAL
{ ?pManufacturerNode <http://data-vocabulary.org/manufacturer/name> ?manufacturerNodeName }
BIND(str(coalesce(?manufacturerNodeName, ?pManufacturerNode)) AS ?productManufacturer)
}
OPTIONAL
{ ?p2 <http://schema.org/Product/aggregateRating> ?pAggregateRating
OPTIONAL
{ ?pAggregateRating <http://schema.org/AggregateRating/ratingValue> ?productAggregateRatingValue }
OPTIONAL
{ ?pAggregateRating <http://schema.org/AggregateRating/reviewCount> ?productReviewCount }
}
OPTIONAL
{ ?p2 <http://data-vocabulary.org/Product/productID> ?productId }
OPTIONAL
{ ?p2 <http://data-vocabulary.org/Product/sku> ?productSku }
OPTIONAL
{ ?p2 <http://data-vocabulary.org/Product/itemCondition> ?productNewCondition .
?productNewCondition <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> s:NewCondition
}
OPTIONAL
{ ?p2 <http://data-vocabulary.org/Product/itemCondition> ?productUsedCondition .
?productUsedCondition <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> s:UsedCondition
}
OPTIONAL
{ ?p2 <http://data-vocabulary.org/Product/gtin13> ?productGtin13 }
OPTIONAL
{ ?p2 <http://data-vocabulary.org/Product/gtin14> ?productGtin14 }
OPTIONAL
{ ?p2 <http://data-vocabulary.org/Product/gtin8> ?productGtin8 }
OPTIONAL
{ ?schOffer <http://data-vocabulary.org/Offer/availability> ?productAvailable
FILTER ( ?productAvailable = s:InStock )
}
OPTIONAL
{ ?schOffer <http://data-vocabulary.org/Offer/availability> ?productUnAvailable
FILTER ( ?productUnAvailable = s:OutOfStock )
}
OPTIONAL
{ ?p2 <http://data-vocabulary.org/Product/model> ?productModel }
OPTIONAL
{ ?p2 <http://data-vocabulary.org/Product/mpn> ?productMPN }
OPTIONAL
{ ?p2 <http://data-vocabulary.org/Product/color> ?productColor }
}
GROUP BY ?productName ?offerImage ?offerName ?productCategory ?salePrice ?suggestedRetailPrice ?productImage ?productThumbNail ?productUrl ?productBrand ?productId ?productSku ?productModel ?productMPN ?productManufacturer ?productGtin13 ?productGtin14 ?productGtin8 ?productAvailable ?productUnAvailable ?productUsedCondition ?productNewCondition ?productColor ?productAggregateRatingValue ?productReviewCount
It is saying that the pattern you have more than one item in a group. Try running without the count and group by and look at the results where you get 2.