Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check multiple resources exist or not via SPARQL query

Tags:

sparql

I have multiple resources like A, B, C.

I want to know these resources exist in my database or not.

Here is a sample of query for one of them:

ASK { <http://fkg.iust.ac.ir/resource/A> ?p  ?o }

This query returns true or false.

It's better to use one query, and I need to get 2 columns, resource & existing.

Here is my sample response:

---------------------------------------------------
|        resource        |        existing        |
|========================|========================|
|           :A           |          true          |
|------------------------|------------------------|
|           :B           |          false         |
|------------------------|------------------------|
|           :C           |          true          |
---------------------------------------------------

I know ASK and UNION, but how can I put them together for this sample?

like image 228
Aref Avatar asked May 14 '19 20:05

Aref


1 Answers

Query:

PREFIX : <http://fkg.iust.ac.ir/resource/>

SELECT ?resource (EXISTS { ?resource ?p ?o } AS ?existing) {
    VALUES ?resource { :A :B :C }
}

Test data:

<http://fkg.iust.ac.ir/resource/A> a _:dummy.
<http://fkg.iust.ac.ir/resource/C> a _:dummy.

Result:

-----------------------
| resource | existing |
=======================
| :A       | true     |
| :B       | false    |
| :C       | true     |
-----------------------
  1. To get a tabular result, use SELECT instead of ASK
  2. To get multiple solution for a known list of resources, use VALUES
  3. EXISTS is like a mini-ASK query that can be embedded into expressions
  4. Use the SELECT (expression AS ?variable) form to bind the result of the mini-ASK to the variable ?existing
  5. Caveat: The answer depends on what you mean by “a resource exists”. In an RDF graph, nodes are not created and deleted explicitly, but they exist simply by virtue of being used in a triple. The original ASK query as written checks whether there is any triple that has the node as its subject. For completeness, one may want to check for the object position as well:
    (EXISTS { ?resource ?p ?o } || EXISTS { ?s ?p ?resource })
    
like image 190
cygri Avatar answered Nov 19 '22 16:11

cygri