Replace multiple value in SPARQL - 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"

Related

How to create a visual graph config in workbench mode

This is my example.ttl:
#prefix : <http://example.com#> .
:NewYork a :City .
:London a :City .
:bob a :Person, :Man;
:name "Bob";
:bornIn :London.
:susan a :Person, :Woman;
:name "Susan";
:bornIn :NewYork.
:jane a :Person, :Woman;
:name "Jane".
<<:bob :meet :susan>> :venue :NewYork;
:date "2020-10-12" .
<<:jane :told :susan>> :about <<:bob :love :susan>>;
:date "2021-06-15" .
I import it into GraphDB, and Create visual graph config : I choose "Start with graph query results", I want get all nodes and edges in the graph, but I still don't know how to set the Graph expansion, Node basics and Edge basics.
Would anyone show me how to do it?
Thanks a lot!
The Visual Graph doesn't support RDF*, but you could expand all nodes by creating a construct query, using Standard Reification.
Go to Create graph config -> Start with graph query results and write following construct query:
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix : <http://example.com#>
CONSTRUCT {
?s a ?o;
a ?o1;
:name ?name;
:bornIn ?cityBorn.
:meeting rdf:subject ?pers1;
rdf:predicate :meet;
rdf:object ?pers2;
:venue ?city.
:gossip rdf:subject ?pers5;
rdf:predicate :love;
rdf:object ?pers6.
:story rdf:subject ?pers3;
rdf:predicate :told;
rdf:object ?pers4.
:story :about :gossip;
:date ?date .
} WHERE {
?s a ?o;
a ?o1;
:name ?name.
<<?pers1 :meet ?pers2>> :venue ?city.
<<?pers3 :told ?pers4>> :about <<?pers5 :love ?pers6>>;
:date ?date .
OPTIONAL {
?s :bornIn ?cityBorn.
}
}

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?

How do I write a SPARQL query for this

How do i write a SPARQL query to retrieve all the movies and co-directors of a movie having director with name "George".
triples are
#prefix : <http://org.example/> .
:movie1 :hasDirectors :d1, :d2, :d3 .
:movie2 :hasDirectors :d3, :d4 .
:movie3 :hasDirectors :d1, :d3
:d1 :name "George" .
:d2 :name "Jack" .
:d6 :name "Raj" .
:d3 :name "Henry" .
:d4 :name "Williams" .
As posted in the comments the query should be as follows:
SELECT ?movie ?name ?coDirectors_name WHERE { ?movie :hasDirectors ?d. ?d :name ?
name. FILTER(?name="George") ?movie :hasDirectors ?coDirectors . ?coDirectors :name
?coDirectors_name FILTER(?coDirectors_name != "George") }

querying child data-properties on RDF-store

I have, in my ontology, an entity named "person" that has a class child named "member". Some like this (indenting for indicate the hierarchy),
person
member
Also, I have some attributes (dataProperty), associated with "person", and another attributes associated with "member". Specifically, that attributes could be phone numbers. I propose a hierarchy of attributes in this way,
phone (domain: person)
office-phone (domain: member)
office-phone-1 (domain: member)
office-phone-2 (domain: member)
personal-phone (domain: person)
I'm doing the following,
SELECT ?s ?attr ?data
WHERE
{
value ?attr {:phone} .
?s rdf:type :member .
?s ?attr ?data .
}
to get all phone-numbers,
instance-member :phone "value-of-personal-phone"
instance-member :phone "value-of-office-phone-1"
instance-member :phone "value-of-office-phone-2"
but,
how could I ONLY to get the two office-phone WITHOUT USE the specific attribute ":office-phone"?? Some owl: restriction or definition??
Thanks!
First, you have to use the property hierarchy in your data, otherwise, this can't work (it would be good if you provide proper sample data next time...):
#prefix : <http://example.org/> .
#prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
#prefix rdfs: <http://www.w3.org/2000/01/rdf-schema/> .
:officePhone rdfs:subPropertyOf :phone .
:officePhone1 rdfs:subPropertyOf :officePhone .
:officePhone2 rdfs:subPropertyOf :officePhone .
:personalPhone rdfs:subPropertyOf :phone .
:instance-member rdf:type :member .
:instance-member :personalPhone "value-of-personal-phone" .
:instance-member :officePhone1 "value-of-office-phone-1" .
:instance-member :officePhone2 "value-of-office-phone-2" .
Regarding the querying, there are at least two options:
1) The triple store supports reasoning, RDFS would be enough in your example - then your query would be sufficient.
2) You rewrite your query such that the property hierarchy is taken into account using SPARQL 1.1 property paths:
prefix : <http://example.org/>
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema/>
SELECT ?s ?attr ?data
WHERE
{
values ?p {:officePhone}
?s rdf:type :member .
?attr rdfs:subPropertyOf* ?p .
?s ?attr ?data .
}
Result:
----------------------------------------------------------------
| s | attr | data |
================================================================
| :instance-member | :officePhone1 | "value-of-office-phone-1" |
| :instance-member | :officePhone2 | "value-of-office-phone-2" |
----------------------------------------------------------------
...I got it in a similar way, showing the superproperty, restrincting the domain and including a distinct...
SELECT distinct ?s ?attr_father ?data
WHERE
{
values ?attr_father {:phone} .
?s rdf:type :member .
?attr rdfs:subPropertyOf* ?attr_father .
?attr rdfs:domain :member .
?s ?attr ?data .
}
In this way, we get,
instance-member :phone "value-of-office-phone-1" .
instance-member :phone "value-of-office-phone-2" .
Regards!

print multiple path in rdf graph using sparql query

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.