Retrieve smallest subgraph for given subjects from RDF graph via SPARQL - sparql

As you might see from the formulation of my question title, I am new to SPARQL and RDF in general, so please excuse me for not being able to formulate the question more precisely.
I am looking for a SPARQL query which returns a list of triples that describe a connected subgraph (assuming that the queried graph is completly connected). I found a solution at SPARQL query to construct sub-graph with select paths (paths have different lengths) but it only works if the passed nodes are directly connected. If for example I have a scenario where I pass nodes that are not connected directly I don't get a result. I would like to get a subgraph (as triples) that is allowed to include additional nodes but has to be connected.
Example: Given Graph G=(V,E), with V={A,B,C,D,E,F,G,H} and E as described in the following picture (predicates are not displayed as they are not important). I would like to find a query which when given {C,E,H} (see picture 1) returns the triples that describe the connected subgraph given in picture 2, that is {(C,E),(E,G),(G,H)}.
Obviously there might be multiple ways in which this can be achieved in an arbitrary graph, so in this case there are two possibilities: 1. A structure should be returned that is the minimum spanning tree of those vertices ({C,E,G,H}) or 2. If this is not possible, a structure should be returned from which a minimum spanning tree can be calculated afterwards
Thanks for any help.

The primary misunderstanding, wrt SPARQL, seems to be in the second paragraph. The predicates ARE important. They are the driving force behind RDF and SPARQL - i.e. naming the "relationships", which are the names of edges. As you'll see below, it's not possible to answer the question without knowing something about the predicate names.
But let me try anyway. Assuming all predicates are named :p1, I think what you may want is something like the following:
CONSTRUCT {
:C :p1 :E .
:E :p1 ?G .
?G :p1 :H .
}
WHERE {
:C :p1 :E .
:E :p1 ?G .
?G :p1 :H .
}
The WHERE clause will find the graph pattern, in your specific example finding a solution for ?G. The CONSTRUCT statement then creates your minimal spanning tree.
Another tip for going from graph theory to RDF and SPARQL is the use of URI's to uniquely name nodes and edges. The above takes a specific XML-based shortcut that assumes a default prefix has been defined. For example, if the default prefix were <http://example.com/graphTheory/>, then replace the : in the above with the prefix, e.g. <http://example.com/graphTheory/p1>.

Related

How to get the path details between two nodes in DBpedia in SPARQL

I want to check if two DBpedia nodes have a path using;
dct:subject and skos:broader properties
without specifying properties
For instance consider the two DBpedia nodes; http://dbpedia.org/resource/Cat and http://dbpedia.org/resource/Dog.
Following previous questions related to this in SO, I tried to use the following wildcard query to do it (without specifying the dct:subject and skos:broader properties).
ASK {
<http://dbpedia.org/resource/Cat> ((<>|!<>)|^(<>|!<>))* <http://dbpedia.org/resource/Dog>
}
However, I get a memory error. I am wondering if there is a more suitable way of doing this in sparql.
I am happy to provide more details if needed.

How to determine whether two SPARQL queries are identical using Python?

When using SPARQL to query RDF dataset, the same query can be written in many different ways. For example, sparql queries are always permutation-invariant with respect to some clauses inside it. Also, we can rename the variables inside a sparql query. But how can we identify those identical SPARQL queries? Ideally, there should be a python package that can parse a sparql query (i.e., a string object) into a query object, and different strings share the same underlying query are parsed into the same object, then we can simply compare the parsed query objects to determine whether two sparql queries are identical. Is there any tool like this (seems prepareQuery() in rdflib doesn't work in this way)? If not, then what should I do?
Semantically identical queries example:
SELECT ?x WHERE { ?x foaf:haha ?k .\n ?person foaf:knows ?x .}
SELECT ?s WHERE { ?person foaf:knows ?s .\n ?s foaf:haha ?k .}
The paper "Generating SPARQL Query Containment Benchmarks
using the SQCFramework" by Muhammad Seleem et al., mentions "SPARQL query containment solvers" where
Query containment is the problem of deciding if the result set of a query Q1 is included
in the result set of another query Q2
If you use such a solver to test whether the result set of Q1 is a subset of Q2 and vice versa, you have established that they are semantically identical.
As for your "off-the-shelf tool": the former paper mentions that those are tested in another paper "Evaluating and benchmarking sparql query containment solvers." by M.W. Chekol et al..
As for the complexity and computability, the latter paper mentions:
The query containment problem for full SPARQL is undecidable [15, 1].
Hence, it is necessary to reduce SPARQL in order to consider it. A
double exponential upper bound has been proven for the containment and
equivalence problems of SPARQL queries without OPTIONAL , FILTER and
under set semantics [7].
However, query containment in both directions is only one way to determine identity of queries. I am unaware whether there is a proof of a better complexity/computability for query identity than for query containment (or a proof on the contrary).

Display all fields in Wikidata Query Service

Wikidata provides query browser at https://query.wikidata.org
I want to display films all fields. I tried with using * but its not working. Does anybody know how to display all fields of the data for Films?
To work with SPARQL is necessary to understand some concepts, as #AKSW said in the comments of the question. If you don't understand the meaning of ?film ?p ?o. This is called triple¹ and is composed by subject-predicate-object. E. g., in the case of the films, it could be: x is a film. This is what you are querying in the Wikidata Query Service (WDQS) when you use ?film wdt:P31 wd:Q11424.
I think it isn't possible to display all the property-values of an item. In addition it probably could cause a timeout because there is many statements of many items.
If you want to check the property-values of all the films in Wikidata I think an option might be you write or find a script to extract the items with P31-Q11424 (instance of films). For that, the accessing data section could be useful (e. g. with pywikibot you could query and extract what you want).
If you are interested in SPARQL and WDQS I recommend you to read some help resources:
Wikidata Query Service Help, specifically the SPARQL tutorial.
Query examples (read another queries is how I began to learn).
SPARQL 1.1 Query Language specification.
RDF Dump Format (because read about the ontology of Wikidata could help to understand the concepts).
Edit
When I answer it I wrote triplestore and linked it to its respective page in the Wikipedia in English, but after the comment of #AKSW I consider I was wrong because the triplestore is the concept which is used to refer to the storage and retrieval of triple or semantic triple, "a set of three entities that codifies a statement about semantic data in the form of subject–predicate–object expressions" (from Semantic triple page in Wikipedia in English).

SPARQL Query of FOAF doesn't return any result

I want to get some information from the FOAF ontology. I tried the following SPARQL query, but it returns no results. I tried this query to get familiar with FOAF, but what I really want to do is to find all the people that a particular person ?x knows (using the property foaf:knows). How do I do this?
PREFIX foaf:<http://xmlns.com/foaf/0.1/>
SELECT ?name ?mbox
WHERE { ?x foaf:name ?name .
?x foaf:mbox ?mbox .
}
Semantic web is made of different components.
Knowledge is represented as RDF triples. These triples describes Resources based on a Subject - Predicate - Object syntax. For example, "John is a Male" may be represented as a RDF triple.
On top of RDF, we may use RDFS and OWL to specify restrictions and other information on these data. Thanks to RDFS, I can specify that "Male is a subclass of Person" and it is therefore possible to infer that "John is a Person". RDFS and OWL helps to define ontologies. An ontology is a vocabulary (that can be general or specific to a domain) to represents data. For example, I may want to create an ontology CAT to represent data on cats.
In that case, I would create my CAT vocabulary defining that "Cat is a subclass of Animal" and "hasOwner is a property that links a cat to a Person" and some other properties. Then, I am able to instantiate some individuals to create data on cats. For example by saying that "Baccara is a Cat" and "Baccara hasOwner John".
FOAF is basically a vocabulary to represent data on people and especially links between these people. FOAF vocabulary gives some properties and classes to handle easily information on people. But it doesn't provide any piece of information, only the "structure"/"model"/"schema" to organize information.
There are no individuals in the FOAF dataset. That is why your query returns no result. Since there's no people in the FOAF dataset, it is normal that the query returns nothing.
You may want to build your own RDF dataset based on FOAF vocabulary. To do so, you can try a tool like Protégé, or more easily with a text editor if you're familiar with RDF/XML or Turtle.
Otherwise, if you only need to get familiar with FOAF, you can query the model. For example, you may want to get all the subclasses of Agent :
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT distinct ?c
WHERE { ?c rdfs:subClassOf foaf:Agent }
I recommend you to read a bit on the semantic web components (especially RDF and RDFS, and differences between them) before going any further in FOAF. Plus, a nice exercise to learn SPARQL consists in querying DBpedia: http://dbpedia.org/sparql.

The limit of Sparql

I would like to know how to express the following question in sparql:
"Give me the parents whose every child goes to MIT"
More generally, I would like to know what are the limits of query sparql please? What kinds of questions with answers in database cannot be formulated as sparql, please?
Thank you for your help
You can express this using a negated existential quantification. Like this:
SELECT ?parent
WHERE { ?parent a :Parent .
FILTER NOT EXISTS {
?c :childOf ?parent .
?c :enrolledIn ?school .
FILTER (str(?school) != "MIT")
}
}
This query asks for all parents who do not havy any child that is enrolled in a school different from MIT.
Your question involves quantification, and is one example of things that cannot be expressed as one query in regular SPARQL 1.0. (It may be expressed in SPARQL 1.1 as shown in Jeen Broekstra's answer, or as an OWL class.)
Many SPARQL 1.0 implementations, though, have developped extensions to handle quantification. A commercial example is Intellidimension Semantics Platform, which would give you something like:
SELECT ?parent
WHERE { ?child :hasParent ?parent FORALL(?child){ ?child :hasSchool "MIT" } }
An academic example is SPARQLog from Oxford University Computing Lab. I am not aware that this system is available as an easy download, but the paper is freely available and provides insight into the difficulties of implementing quantification for SPARQL.
As for your question about the limits of SPARQL, it is too general to answer in a few words, but here is a link to a relevant paper, again as far as SPARQL 1.0 is concerned: Semantics and Complexity of SPARQL
For the specific question you can read up on relational division. Alternatively, you can find all the children who do not go to MIT, find their parents, and remove those parents from the list of all parents.
Sorry, can't help with SPARQL's limits.