print multiple path in rdf graph using sparql query - sparql

My data which simply denotes two paths from :a to :v and k respectively is as follows:
#prefix : <http://m/questions/19587520/sparql-path-between-two-instance/> .
:a :z :d.
:d :p :e .
:e :g :f .
:f :l :g .
:g :m :h .
:h :p :k .
:k :p :c .
:c :p :v .
:a :p :x.
:x :q :y.
:y :z :c.
:a :l :g .
:g :m :h .
:h :p :k .
The SPARQL queries that I tried are
String querygraph=
"PREFIX : <http://m/questions/19587520/sparql-path-between-two-instance/>" +
"SELECT ?start ?end (count(?mid) as ?length)" +
"WHERE {" +
" values (?start ?end) { (:a :c) " +
" } " +
" ?start (: |!:)+ ?mid . " +
" ?mid (: | !:)* ?end . " +
" } " +
" group by ?start ?end " +
" ORDER BY ASC(?length)";
String querygraph1=
"PREFIX : <http://monika/questions/19587520/sparql-path-between-two-instance/>" +
"select (count(?m) as ?length) " +
"WHERE {" +
" values (?s ?d) { (:a :c) " +
" } " +
"?s (:|!:)+ ?m ."+
" ?m (: | !:)* ?d . " +
"}" ;
In this I need to print and calculate length of path but the problem is there are two paths from :a to :c. My code is not able to differentiate between them, it calculates it as one. Please help me to print both paths separately with their length.

This is not possible in the general case using SPARQL. But if you you know of some intermediate node that needs to be visited you can add this part as part of the triple pattern.
Another option that may work is if you put the two paths in different named graphs:
#prefix : <http://m/questions/19587520/sparql-path-between-two-instance/> .
GRAPH :path1 {
:a :z :d.
:d :p :e .
:e :g :f .
:f :l :g .
:g :m :h .
:h :p :k .
:k :p :c .
:c :p :v .
}
GRAPH :path2 {
:a :p :x.
:x :q :y.
:y :z :c.
:a :l :g .
:g :m :h .
:h :p :k .
}
Then you could express a query against the first graph as:
PREFIX : <http://m/questions/19587520/sparql-path-between-two-instance/>
SELECT DISTINCT ?midI ?p ?midJ
FROM :path1
where {
VALUES (?begin ?end) { (:a :k) }
?begin !:* ?midI .
?midI ?p ?midJ .
?midJ !:* ?end .
}
This way you can avoid having the two paths cross each other. However, property paths in SPARQL cannot guarantee a shortest path (or all paths for that matter), only that there is one. You can count the length as well. There is a useful question here on stackoverflow that is related to this problem that I would recommend having a look at: Finding all steps in property path.

Related

Replace multiple value in SPARQL

I'm trying to use the BIND and REPLACE functions to create a new column in which there is the real title of an object code.
For example, I have a column with ENV_TOB and I would like to create a column that changes "ENV_TOB" to "SLIDE". I have several codes like "ENV_XXX" that I would also like to include in my query. So far, I've only managed to replace one value.
select ?code_type
{
GRAPH xxx:optimized {
OPTIONAL {?o xxx:code_type ?code_type}
FILTER (?code_type IN
("ENV_BAL"^^xsd:string, "ENV_BBF"^^xsd:string, "ENV_CAB"^^xsd:string, "ENV_JRG"^^xsd:string, "ENV_STO"^^xsd:string, "ENV_TJE"^^xsd:string, "ENV_TOB"^^xsd:string, "ENV_TPP"^^xsd:string ))
}
BIND (REPLACE(?code_type, "ENV_TOB", "Slide") AS ?TYPE_JEU)
}
If you just want to get a different value occurs in the data, and the mapping a direct mapping, that is, if you want "Slide" when the value is exactly "ENV_TOB", you can use a VALUES block. For instance:
#prefix : <urn:ex:>
:a :label "ENV_TOB" .
:b :label "ENV_TOB" .
:c :label "ENV_BBF" .
:d :label "ENV_CAB" .
:e :label "Other Label" .
:f :label "I went down the ENV_TOB" .
:g :label "I live in a ENV_CAB" .
prefix : <urn:ex:>
select ?subject ?label ?better_label where {
?subject :label ?label .
optional {
values (?label ?better_label) {
("ENV_TOB" "Slide")
("ENV_CAB" "House")
}
}
}
subject
label
better_label
:a
"ENV_TOB"
"Slide"
:b
"ENV_TOB"
"Slide"
:c
"ENV_BBF"
:d
"ENV_CAB"
"House"
:e
"Other Label"
:f
"I went down the ENV_TOB"
:g
"I live in a ENV_CAB"

SPARQL subquery with limit

I'm using Apache Jena Fuseki to query my graph using SPARQL.
I need to do subquery with limit, and I found one example similar to my requirements here.
The example looks like this.
Data
#prefix : <http://people.example/> .
:alice :name "Alice", "Alice Foo", "A. Foo" .
:alice :knows :bob, :carol .
:bob :name "Bob", "Bob Bar", "B. Bar" .
:carol :name "Carol", "Carol Baz", "C. Baz" .
Query
PREFIX : <http://people.example/>
SELECT ?y ?name WHERE {
:alice :knows ?y .
{
SELECT ?y ?name WHERE {
?y :name ?name
}
ORDER BY ?name
LIMIT 1
}
}
Result
?y ?name
:bob "B. Bar"
:carol "C. Baz"
I did the same query with the same data on Fuseki, but the result is empty.
Is it problem of SPARQL implementation in Fuseki?
Which SPARQL engine can I use to get the same result as showed in the example?

SPARQL Query with quantifier

I need to query using quantifier such as ANY(∀).
For example:
Data
:a :to :b . (b is a non_target_type)
:b :to :c . (c is a target_type)
:c :to :d . (d is a non_target_type)
:d :to :e . (e is a target_type)
:e :to :f . (f is a target_type)
A query is like
:a -> ?y -> ?z .
?z a :target_type .
∀?y, ?y is not a :target_type . (Here's the problem)
Then I expect to get ?z only contains :c
How can I get the result by sparql?
It sounds like you're asking for
SELECT ?z WHERE {
:a :to ?y .
?y :to ?z .
?z a :targetType .
FILTER NOT EXISTS {
?y a :targetType
}
}
This finds any ?z such that:
?z a :target_type; and
:a is connected to something that is connected to ?z; and
it's not the case that ?y a :targetType
That would be mean that :a is connected through some ?y to :z such that ?y isn't a :targetType. But if you want to make sure that there's no such ?y that is a :targetType, you'd use:
SELECT ?z WHERE {
:a :to/:to ?z .
?z a :targetType .
FILTER NOT EXISTS {
:a :to ?y .
?y :to ?z .
?y a :targetType
}
}
Here is the graph and query result. What I expect is result ":d", other than ":d and :g".
#prefix ns: <http://example/> .
#prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
ns:a ns:to ns:b .
ns:b ns:to ns:c .
ns:c ns:to ns:d .
ns:d ns:to ns:e .
ns:e ns:to ns:f .
ns:f ns:to ns:g .
ns:g ns:to ns:h .
ns:a rdf:type ns:target .
ns:d rdf:type ns:target .
ns:g rdf:type ns:target .

SPARQL When do we use the "a"

I'm new on SPARQL. I read the tutorial on Jena (https://jena.apache.org/tutorials/sparql.html) and I understand most of the example. But I don't really understand when we have to use the a
For example with this triples :
#prefix vCard: <http://www.w3.org/2001/vcard-rdf/3.0#> .
#prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
<http://somewhere/MattJones/> vCard:FN "Matt Jones" .
<http://somewhere/MattJones/> vCard:N _:b0 .
_:b0 vCard:Family "Jones" .
_:b0 vCard:Given "Matthew" .
<http://somewhere/RebeccaSmith/> vCard:FN "Becky Smith" .
<http://somewhere/RebeccaSmith/> vCard:N _:b1 .
_:b1 vCard:Family "Smith" .
_:b1 vCard:Given "Rebecca" .
<http://somewhere/JohnSmith/> vCard:FN "John Smith" .
<http://somewhere/JohnSmith/> vCard:N _:b2 .
_:b2 vCard:Family "Smith" .
_:b2 vCard:Given "John" .
<http://somewhere/SarahJones/> vCard:FN "Sarah Jones" .
<http://somewhere/SarahJones/> vCard:N _:b3 .
_:b3 vCard:Family "Jones" .
_:b3 vCard:Given "Sarah" .
We can write something like this :
PREFIX vcard: <http://www.w3.org/2001/vcard-rdf/3.0#>
SELECT ?y ?givenName
WHERE
{ ?y vcard:Family "Smith" .
?y vcard:Given ?givenName .
}
But I the slideshow of my course I found something like this :
SELECT ?label_du_président ?âge_du_président ? label_du_pays
WHERE {
?une_élection a ex:electionPrésidentielle .
?une_élection ex:gagnéePar ?un_president .
?une_élection ex:alieuDans ?un_pays .
?un_president rdfs:label ?label_du_president .
?un_president ex:âge ?âge_du_president .
?un_pays rdfs:label ?label_du_pays
}
Why and when use the a ?
It's a shorthand notation for the rdf:type property. From the SPARQL Query Language specs:
The keyword "a" can be used as a predicate in a triple pattern and is an alternative for the IRI http://www.w3.org/1999/02/22-rdf-syntax-ns#type. This keyword is case-sensitive.
Think of it as meaning "is a (kind of)".

sparql how to group correctly this data

First of all, I didn't make a minimum example because i think that my problem can be understood without it.
Second, I didn't give you the data because i think that my problem can be solved without it. However, I'm open to give it to you if you ask.
This is my query:
select distinct (?x as ?likedItem) (?item as ?suggestedItem) ?similarity ?becauseOf ((?similarity * ?importance * ?levelImportance) as ?finalSimilarity)
{
values ?user {bo:ania}
#the variable ?x is bound to the items the user :ania has liked.
?user rs:hasRated ?ratings.
?ratings a rs:Likes.
?ratings rs:aboutItem ?x.
?ratings rs:ratesBy ?ratingValue.
#level 0 class similarities
{
#extract all the items that are from the same class (type) as the liked items.
#I assumed the being from the same class accounts for 50% of the similarities.
#This value can be changed according to the test or the application domain.
values ?classImportance {0.5} #class level
bind (?classImportance as ?importance)
bind( 4/7 as ?levelImportance)
?x a ?class.
?class rdfs:subClassOf ?mainClass .
?mainClass rdfs:subClassOf rs:RecommendableClass .
?mainClass rs:hasSimilarityConfiguration ?similarityConfiguration .
?similarityConfiguration rs:hasClassSimilarity ?classSimilarity .
?classSimilarity rs:appliedOnClass ?class .
?classSimilarity rs:hasClassSimilarityValue ?similarity .
?item a ?class.
bind (concat("it shares the same class, which is ", strafter(str(?class), "#"), ", with ", strafter(str(?x), "#")) as ?becauseOf)
}
union
#level 0 instance similarities
{
#extract the items that share the same value for important predicates with the already liked items..
#I assumed that having the same instance for important predicates account for 100% of the similarities.
#This value can be changed according to the test or the application domain.
values ?instanceImportance {1} #instance level
bind (?instanceImportance as ?importance)
bind( 4/7 as ?levelImportance)
?x a ?class.
?class rdfs:subClassOf ?mainClass .
?mainClass rdfs:subClassOf rs:RecommendableClass .
?mainClass rs:hasSimilarityConfiguration ?similarityConfiguration .
?similarityConfiguration rs:hasPropertySimilarity ?propertySimilarity .
?propertySimilarity rs:appliedOnProperty ?property .
?propertySimilarity rs:hasPropertySimilarityValue ?similarity .
?x ?property ?value .
?item ?property ?value .
bind (concat("it shares ", strafter(str(?value), "#"), " for predicate ", strafter(str(?property), "#"), " with ", strafter(str(?x), "#")) as ?becauseOf)
}
filter (?x != ?item)
}
This is the result:
As you see, the result contains many values for the same suggestedItem, I want to make group according to the suggestedItem and sum the values of finalSimilarity
I tried this:
select ?item (SUM(?similarity * ?importance * ?levelImportance ) as ?finalSimilarity) (group_concat(distinct ?x) as ?likedItem) (group_concat(?becauseOf ; separator = " ,and ") as ?reason) where
{
values ?user {bo:ania}
#the variable ?x is bound to the items the user :ania has liked.
?user rs:hasRated ?ratings.
?ratings a rs:Likes.
?ratings rs:aboutItem ?x.
?ratings rs:ratesBy ?ratingValue.
#level 0 class similarities
{
#extract all the items that are from the same class (type) as the liked items.
#I assumed the being from the same class accounts for 50% of the similarities.
#This value can be changed according to the test or the application domain.
values ?classImportance {0.5} #class level
bind (?classImportance as ?importance)
bind( 4/7 as ?levelImportance)
?x a ?class.
?class rdfs:subClassOf ?mainClass .
?mainClass rdfs:subClassOf rs:RecommendableClass .
?mainClass rs:hasSimilarityConfiguration ?similarityConfiguration .
?similarityConfiguration rs:hasClassSimilarity ?classSimilarity .
?classSimilarity rs:appliedOnClass ?class .
?classSimilarity rs:hasClassSimilarityValue ?similarity .
?item a ?class.
bind (concat("it shares the same class, which is ", strafter(str(?class), "#"), ", with ", strafter(str(?x), "#")) as ?becauseOf)
}
union
#level 0 instance similarities
{
#extract the items that share the same value for important predicates with the already liked items..
#I assumed that having the same instance for important predicates account for 100% of the similarities.
#This value can be changed according to the test or the application domain.
values ?instanceImportance {1} #instance level
bind (?instanceImportance as ?importance)
bind( 4/7 as ?levelImportance)
?x a ?class.
?class rdfs:subClassOf ?mainClass .
?mainClass rdfs:subClassOf rs:RecommendableClass .
?mainClass rs:hasSimilarityConfiguration ?similarityConfiguration .
?similarityConfiguration rs:hasPropertySimilarity ?propertySimilarity .
?propertySimilarity rs:appliedOnProperty ?property .
?propertySimilarity rs:hasPropertySimilarityValue ?similarity .
?x ?property ?value .
?item ?property ?value .
bind (concat("it shares ", strafter(str(?value), "#"), " for predicate ", strafter(str(?property), "#"), " with ", strafter(str(?x), "#")) as ?becauseOf)
}
filter (?x != ?item)
}
group by ?item
order by desc(?finalSimilarity)
but the result is:
this is something wrong in my way because if you look at the finalSimilarity in, the value is 1.7. However, if you sum that manually from the first query, you get 0.62 so I did something wrong,
could you help me discover it?
Please note that the two queries are the same, it is just the select statment are different
Hint
I am already able to solve it using two selects like this:
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rs: <http://www.SemanticRecommender.com/rs#>
PREFIX bo: <http://www.BookOntology.com/bo#>
PREFIX :<http://www.SemanticBookOntology.com/sbo#>
select ?suggestedItem ( SUM (?finalSimilarity) as ?summedFinalSimilarity) (group_concat(distinct strafter(str(?likedItem), "#")) as ?becauseYouHaveLikedThisItem) (group_concat(?becauseOf ; separator = " ,and ") as ?reason)
where {
select distinct (?x as ?likedItem) (?item as ?suggestedItem) ?similarity ?becauseOf ((?similarity * ?importance * ?levelImportance) as ?finalSimilarity)
where
{
values ?user {bo:ania}
#the variable ?x is bound to the items the user :ania has liked.
?user rs:hasRated ?ratings.
?ratings a rs:Likes.
?ratings rs:aboutItem ?x.
?ratings rs:ratesBy ?ratingValue.
#level 0 class similarities
{
#extract all the items that are from the same class (type) as the liked items.
#I assumed the being from the same class accounts for 50% of the similarities.
#This value can be changed according to the test or the application domain.
values ?classImportance {0.5} #class level
bind (?classImportance as ?importance)
bind( 4/7 as ?levelImportance)
?x a ?class.
?class rdfs:subClassOf ?mainClass .
?mainClass rdfs:subClassOf rs:RecommendableClass .
?mainClass rs:hasSimilarityConfiguration ?similarityConfiguration .
?similarityConfiguration rs:hasClassSimilarity ?classSimilarity .
?classSimilarity rs:appliedOnClass ?class .
?classSimilarity rs:hasClassSimilarityValue ?similarity .
?item a ?class.
bind (concat("it shares the same class, which is ", strafter(str(?class), "#"), ", with ", strafter(str(?x), "#")) as ?becauseOf)
}
union
#level 0 instance similarities
{
#extract the items that share the same value for important predicates with the already liked items..
#I assumed that having the same instance for important predicates account for 100% of the similarities.
#This value can be changed according to the test or the application domain.
values ?instanceImportance {1} #instance level
bind (?instanceImportance as ?importance)
bind( 4/7 as ?levelImportance)
?x a ?class.
?class rdfs:subClassOf ?mainClass .
?mainClass rdfs:subClassOf rs:RecommendableClass .
?mainClass rs:hasSimilarityConfiguration ?similarityConfiguration .
?similarityConfiguration rs:hasPropertySimilarity ?propertySimilarity .
?propertySimilarity rs:appliedOnProperty ?property .
?propertySimilarity rs:hasPropertySimilarityValue ?similarity .
?x ?property ?value .
?item ?property ?value .
bind (concat("it shares ", strafter(str(?value), "#"), " for predicate ", strafter(str(?property), "#"), " with ", strafter(str(?x), "#")) as ?becauseOf)
}
filter (?x != ?item)
}
}
group by ?suggestedItem
order by desc(?summedFinalSimilarity)
but to me that is a stupid solution and there must be a more clever one where i can take the aggregated data using one select
Without seeing your data, it's impossible to say, and with a query this big, it's probably not worth trying to debug the exact problem, but it's easy for this to happen if you can have duplicates (which would be easy to get, especially if you're using unions where some condition could match both parts). For instance, suppose you have data like this:
#prefix : <urn:ex:>
:x :similar [ :sim 0.10 ; :mult 2 ] ,
[ :sim 0.12 ; :mult 1 ] ,
[ :sim 0.12 ; :mult 1 ] , # yup, a duplicate
[ :sim 0.15 ; :mult 4 ] .
Then if you run this query, you'll get four result rows:
prefix : <urn:ex:>
select ?sim ((?sim * ?mult) as ?final) {
:x :similar [ :sim ?sim ; :mult ?mult ] .
}
----------------
| sim | final |
================
| 0.15 | 0.60 |
| 0.12 | 0.12 |
| 0.12 | 0.12 |
| 0.10 | 0.20 |
----------------
However, if you select distinct, you'll only see three:
select distinct ?sim ((?sim * ?mult) as ?final) {
:x :similar [ :sim ?sim ; :mult ?mult ] .
}
----------------
| sim | final |
================
| 0.15 | 0.60 |
| 0.12 | 0.12 |
| 0.10 | 0.20 |
----------------
Once you start to group by and sum, those non-distinct values will both get included:
select (sum(?sim * ?mult) as ?final) {
:x :similar [ :sim ?sim ; :mult ?mult ] .
}
---------
| final |
=========
| 1.04 |
---------
That sum is the sum of all four terms, not the three distinct ones. Even if the data doesn't have the duplicate values, the union can introduce the duplicate results:
#prefix : <urn:ex:>
:x :similar [ :sim 0.10 ; :mult 2 ] ,
[ :sim 0.12 ; :mult 1 ] ,
[ :sim 0.15 ; :mult 4 ] .
prefix : <urn:ex:>
select (sum(?sim * ?mult) as ?final) {
{ :x :similar [ :sim ?sim ; :mult ?mult ] }
union
{ :x :similar [ :sim ?sim ; :mult ?mult ] }
}
---------
| final |
=========
| 1.84 |
---------
Since you found the need to use group_concat(distinct …), I wouldn't be surprised if there are duplicates of that nature.