Referencing results from a SPARQL query in a subsequent SPARQL query - sparql

We would like to use SPARQL in the following scenario:
Execute SPARQL query, e.g.
PREFIX brick: https://brickschema.org/schema/1.1/Brick#
SELECT ?ahu WHERE { ?ahu rdf:type brick:AHU }
Iterate over SPARQL query results and filter the results based on data from another source than RDF database, e.g. "filter out any ahu that do not have valid metadata a SQL database"
Execute another SPARQL query for each filtered result in the previous step, e.g.
PREFIX brick: https://brickschema.org/schema/1.1/Brick#
SELECT ?myZone WHERE {
?myZone rdf:type brick:HVAC_Zone .
"a particular ahu from previous steps" brick:feeds ?myZone}
Both SPARQL queries cannot be expressed as one SPARQL query since interaction with another data source is needed. How shall we design query in step 3 such that it "points" to the particular triplet (or SPARQL query result)?

Related

Alternative for BIND method in sparql

I am using Sansa stack to execute sparql queries on huge amount of data in HDFS .Sansa is a spark based framework that executes sparql queries on huge amount of data. My sparql query is having an assignment as below in it .
BIND(STRAFTER(str(?d), "#") as ?docid) .
But sansa is not able to do this conversion and the final output does not have the variable ?docid .
Is there any other function similar to BIND available in sparql ?

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).

Is there a way to sort SPARQL query results by relevance score in MarkLogic 8?

We're running SPARQL queries on some clinical ontology data in our MarkLogic server. Our queries look like the following:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX cts: <http://marklogic.com/cts#>
SELECT *
FROM <http://example/ontologies/snomedct>
WHERE {
?s rdfs:label ?o .
FILTER cts:contains(?o, cts:word-query("Smoke*", "wildcarded"))
}
LIMIT 10
We expected to get sorted results based off of relevance score, but instead they seemed to be in some random order. Tried many ways with the query but nothing worked. After some research we found this statement in the MarkLogic docs:
When understanding the order an expression returns in, there are two
main rules to consider:
cts:search expressions always return in relevance order (the most relevant to the least relevant).
XPath expressions always return in document order.
Does this mean that cts:contains is a XPath expression that always return in document order? If that's the case, how can we construct a SPARQL query that returns in relevance order?
Thanks,
Kevin
In the example you have, the language you are using is SPARQL - with a fragment filter of the cts:contains.
IN this case, the cts:contains is only useful in isolating fragment IDs that match - thus filtering the candidate documents used in the SPARQL query. Therefore, I do not believe that the the cts relevance is taken into account.
However, you could possibly get results you are looking for in a different way: Do an actual cts:search on the documents in question - then filter them using a cts:triple-range-query.
https://docs.marklogic.com/cts:triple-range-query

Jena TDB physical query plan with multiple FROM clauses

I am trying to figure out how Jena TDB handles SPARQL queries with multiple FROM clauses on the physical query plan level.
I would like to know how Jena TDB handles executing a query over different graphs.
I have made some small experiments and looked at the query algebra, however, it is not clear to me how the FROM clauses affect the algebra.
It looks like that the FROM clauses are discarded in the algebra. I expect that the algebra is evaluated over the union of the graphs, but I would like to be sure.
I have the following quads:
<http://example.com/book2/> <http://example.com/price> "5"^^<http://www.w3.org/2001/XMLSchema#integer> <http://example.com/A> .
<http://example.com/book2/> <http://example.com/title> "Lord of the Rings" <http://example.com/B> .
and the following query:
SELECT (AVG(?price) as ?total)
FROM <http://example.com/A>
FROM <http://example.com/B>
WHERE {
?book <http://example.com/price> ?price .
?book <http://example.com/title> ?title .
}
./tdbquery --loc test --query test.sparql --explain
The query algebra looks as follows:
INFO exec :: ALGEBRA
(project (?total)
(extend ((?total ?.0))
(group () ((?.0 (avg ?price)))
(bgp (triple ?book <http://example.com/price> ?price)))))
When I execute the query over the data I receive the expected result.
FROM (and FROM NAMED) aren't really part of the query, but indications of what the dataset to be queried ought to be. These clauses don't alter what the query will do, only what it operates on, so you don't see them in the algebra.
What a particular processor does with that information varies:
some processors will build the requested dataset (even downloading data)
but it is also common to provide a dataset explicitly in APIs (e.g. query(query_string, dataset)) in which case the processor will ignore it since a dataset has been provided.
a dataset might also be supplied with in a SPARQL protocol request, in which case, as with the API call, the processor will ignore the NAMED clause.
Now a TDB database is a dataset, but TDB has a special feature called 'dynamic datasets' which used FROM and FROM NAMED to form a sub-dataset in effect, limiting the graphs queried to those mentioned in the FROM clauses.

performing sparql query on the rdf data stored in relational database

I have stored large amount of RDF data into a relational database with the help of rdflib_sqlalchemy.SQLAlchemy.Now I want to execute Sparql query over the same.I can not find any thing to know how to implement sparql query here.
Can anyone help.
Including sample code would make it easier to know what you are after but see the documentation on querying an RDFLib graph with SPARQL.
Using the example from the rdflib-sqlalchemy README where graph is the name of the open graph.
rq = "select ?s where {?s ?p ?o} limit 10"
results = graph.query(rq)
for row in results:
print row.s