sparql-query and sparql-update both are not the same? - sparql

I find that these are two different query languages:
SPARQL-QUERY and SPARQL-UPDATE.
What other types I could see in SPRARQL?
And I am looking for a syntax where I can replace a particular element property with a new value.
But, using insert query, I can only see that the new value is being added as additional value of the property instead of replacing the whole values of the property.
So, is there any other language for this purpose, like sparql-update something?
Also, I can see that delete option is there. But I don't want to specify a particular value, but to delete the whole pattern. Of course, we can specify the pattern I guess. But I just wonder, if there is a specific language for this purpose.
EDIT:
And in the following query, I don't find the purpose of using where clause at all. It always inserts specified value as a new value, but is not replacing it. We need to use the delete clause specifically. Then what's the purpose of where clause here?
PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX indexing: <http://fedora.info/definitions/v4/indexing#>
PREFIX custom: <http://namespaces.info/custom#>
DELETE {
}
INSERT {
<> indexing:hasIndexingTransformation "default";
rdf:type indexing:Indexable;
dc:title "title3";
custom:ownerId "owner6";
dc:identifier "test:10";
}
WHERE {
<>
custom:ownerId "owner1";
}

The SPARQL recommendation is separated into separate documents, see SPARQL 1.1 Overview from W3C.
The WHERE clause can be empty, but also look into INSERT DATA, which takes a set of triple specifications (not patterns - no variables) and inserts them. No WHERE clause id needed int that case. Same for deleting triple specifications with DELETE DATA.

Related

Negation does not work as expected for SPARQL property paths

According to SPARQL Property Paths, negation is expressed with the operator !, i.e., !(a|b|c|d) means any relations that do not fall into {a, b, c, d}.
Based one this definition, I find the following example very counterintuitive.
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX : <http://rdf.freebase.com/ns/>
ASK {
:m.0262dl9
!(:type.object.type|<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>)
:common.topic
}
The above query returns false because the only relations from :m.0262dl9 to :common.topic are :type.object.type and <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>, which are negated. However, if we add ^:type.object.type, which means the inverse of :type.object.type to the negated set, the answer becomes true. In other words, we modify
!(:type.object.type|<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>)
to
!(:type.object.type|^:type.object.type|<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>)
This is quite weird, since the first query is false entails that the second query must also be false. I am really not sure why the second query returns true. Am I misunderstanding the definition of ! or ^?

Functions in SPARQL to Manipulate IRIs?

I want to write some reusable SPARQL queries to do things like take an IRI, get the name part (typically after the # sign), modify it (e.g., replace underscores with blank spaces) and put it in the rdfs:label property. This would be useful for Protege which doesn't fill in the rdfs:label if you use user defined IRIs. Or take an IRI with a user defined name, do the same and then replace the user defined IRI with a UUID. I looked in the SPARQL spec for functions to manipulate IRIs and either they don't exist or I'm missing something obvious. I'm posting this to make sure it isn't the latter. I know it is easy to do the equivalent with things like SUBSTR but I'm surprised that there aren't predefined operators to do things like getting the name part of an IRI and getting the base and want to double check before I roll my own.
In case anyone else wants to do this, I figured it out. There are some answers on this site but they are all for SQL or other languages than SPARQL. The following is for classes and it should be obvious how to adapt it for other entities. Note: this works in the Snap SPARQL Plugin for Protege (that's why I use CONSTRUCT rather than INSERT), however, there is a bug in their implementation of SUBSTR so that it uses 0 based indexing rather than 1 based as the spec says. So if you use this in Snap SPARQL change the 1 to a 2.
CONSTRUCT {?c rdfs:label ?lblname.}
WHERE {?c rdfs:subClassOf owl:Thing.
BIND(STRAFTER(STR(?c), '#') as ?name)
BIND(REPLACE(?name,"([A-Z])", " $1" ) as ?namewbs)
BIND (IF (STRSTARTS(?namewbs," "),SUBSTR(?namewbs,1),?namewbs) AS ?lblname)
FILTER(?c != owl:Thing || ?c != owl:Nothing)}

Find synonym for a specific word with Wikidata SPARQL

I'm trying to construct a query for https://query.wikidata.org/ that finds synonyms for a given string. I know it will involve finding lemmas of the particular string (the string is "kind" in this example) and the P5973 synonym property. This returns zero results:
select ?lexeme ?lemma ?synonym WHERE {
?lexeme wikibase:lemma ?lemma.
FILTER (?lemma ='kind')
?lexeme wdt:P5973 ?synonym .
}
I've used the string kind in this example. I would like to return synonyms for all meanings of the word kind, so both empathetic and type would be correct responses.

Is it possible to use variables as integers in SPARQL property paths?

I am currently trying to create pointers to datatype values as they cannot be linked directly. However, I would like to be able to evaluate the pointers from within the SPARQL environment, which raised specifically in the case that the desired value is part of an ordered rdf:List some questions for me. My approach is to use property paths within a SPARQL query in which I can use the defined individual, property and index of the ordered list that the pointer has attached to it.
Given the following example data with the shortened syntax for ordered lists by ttl:
ex:myObject ex:somePropery ("1" "2" "3") .
ex:myPointer ex:lookAtIndividual ex:myObject;
ex:lookAtProperty ex:someProperty ;
ex:lookAtIndex "3"^^xsd:integer .
Now I would like to create a SPARQL query that -- based on the pointer -- returns the value at the given index. To my understanding the query could/should look something like this:
SELECT ?value
WHERE {
ex:myPointer ex:lookAtIndividual ?individual ;
ex:lookAtProperty ?prop ;
ex:lookAtIndex ?index .
?individual ?prop/rdf:rest{?index-1}/rdf:first ?value .
}
But if I try to execute this query with TopBraid, it shows an error message that ?index has been found when <INTEGER> was expected. I also tried binding the index in the SPARQL query via BIND(?index-1 AS ?i), again without success. If the pointed value is not stored in a list, the query without property path works fine.
Is it in general possible to use a value that is connected via datatype property within a SPARQL query as path length for property paths?
This syntax: rdf:rest{<number>} is not standard SPARQL. So the short answer is, regrettably: no, you can't use variables as integers in SPARQL property paths, for the simple reason that you can't use integers in SPARQL property paths at all.
In an earlier draft of the SPARQL standard, there was a proposal to use this kind of syntax to allow specifying the min and max length of a property path, e.g. rdf:rest{1, 3} would match any paths using rdf:rest properties between length 1 and 3. But this was never fully standardized and most SPARQL engines don't implement it.
If you happen to use a SPARQL engine that does implement it, you will have to get in touch with the developers directly to ask if they can extend the mechanism to allow use of variables in this position (the error message suggests to me that it's currently just not possible).
As an aside: there's a SPARQL 1.2 community initiative going on. It only just got started but one of the proposals on the table is re-introducing this particular piece of functionality to the standard.

generate an urn/iri/uri dynamically

I need to generate dynamically the name of a graph depending on the time.
I've tough that some think like
select ?g where {
bind(concat("<urn:myNewGraph_",str(now()),">") as ?g)
}
would have done the trick, but with Stardog I get a null result.
If instead I run this
select ?g where {
bind(concat("urn:myNewGraph_",str(now())) as ?g)
}
i get urn:myNewGraph_2015-05-28T09:37:11.823Z
Any Ideas?
moreover I'm not sure that even if i can get somehow a string like <urn:myNewGraph_2015-05-28T09:37:11.823Z> would have worked as a valid argument for a graph name as can be seen from this not-working test:
INSERT {graph ?g {<urn:s> <urn:p> <urn:o>}
where {
?g="<rn:myNewGraph_2015-05-28T09:37:11.823Z>"
}
is there a proper way to generate an urn/iri/uri dynamically?
Your original query looks correct, and produces a valid result when I execute it using a different SPARQL engine (Sesame), so I guess that you might want to report this to the Stardog developers as a possible bug.
However, to be able to use the value thus obtained it needs to be an actual URI (or IRI) - whereas what you're producing is a literal string.
You need to change two things: first of all, get rid of the enclosing < and > (these brackets are not actually part of the IRI) - so actually your second query is better. Second, use the IRI function to convert your string value to an IRI:
INSERT {GRAPH ?g {<urn:s> <urn:p> <urn:o>} }
WHERE {
BIND( IRI(CONCAT("urn:myNewGraph_",STR(NOW()))) as ?g)
}
Not sure it's necessary in your case, but in general you may need to use the ENCODE_FOR_URI function in there somewhere, to make sure that any special characters in your string are properly encoded/escaped before turning it into an IRI.