querying child data-properties on RDF-store - sparql

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!

Related

The mechanism of "FILTER NOT EXISTS" in SPARQL

Assuming the triples are following:
#prefix : <http://example/> .
#prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
#prefix foaf: <http://xmlns.com/foaf/0.1/> .
:alice rdf:type foaf:Person .
:alice foaf:name "Alice" .
:bob rdf:type foaf:Person .
and then we perform 3 queries based on SPARQL 1.1:
Q1:
SELECT ?s
WHERE
{
?s ?p ?o .
FILTER NOT EXISTS { ?s foaf:name ?y }
}
Q2:
SELECT ?s
WHERE
{
?s ?p ?o .
FILTER NOT EXISTS { ?x foaf:name ?y }
}
Q3:
SELECT ?s
WHERE
{
?s ?p ?o .
FILTER NOT EXISTS { ?x foaf:mailbox ?y }
}
These three queries return three different solutions. Could anyone help me figure out why Q2 evaluates to no query solution in contrast to Q1 and Q3? Many thanks in advance :)
Q2 returns no solution because in your data, there exists a statement that matches ?x foaf:name ?y: ?x = :alice and ?y = "Alice". You've put no further constraints on either ?x or ?y. So no matter what the other variables in your query (?s, ?p and ?o) are bound to, the NOT EXISTS condition will always fail and therefore the query returns no result.

SPARQL query for specific information

I am struggling a lot to create some SPARQL queries. I need 3 specific things, and this is what i have so far:
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dbpedia: <http://dbpedia.org/resource/>
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbp: <http://dbpedia.org/property/>
select distinct ?title ?author ?country ?genre ?language
where {
?s rdf:type dbo:Book;
dbp:title ?title;
dbp:author ?author;
dbp:country ?country;
dbp:genre ?genre;
dbp:language ?language.
}
This query will bring me a list of all books. What i really need is the ability to add some filters to this code. There are 3 things i want to filter by:
specific title name (e.g., search for title with "harry potter")
specific author name (e.g., search for author with "J. K. Rowling")
specific genre (e.g., search for genre with "adventure")
I've been struggling with this for too long and i simply cannot define these 3 queries. I am trying to implement a function that will execute a SPARQL statement using parameters passed by an user form. I found a few examples here and in the web but i just cannot build these 3 specific queries.
As noted, not every book has every property, and some of your properties may not exist at all. For instance, I changed dbp:genre to dbo:literaryGenre, based on the description of Harry Potter and the Goblet of Fire. See query form, and results.
SELECT *
WHERE
{ ?s rdf:type dbo:Book .
?s rdfs:label ?bookLabel .
FILTER(LANGMATCHES(LANG(?bookLabel), 'en'))
?s dbo:author ?author .
?author rdfs:label ?authorLabel .
FILTER(LANGMATCHES(LANG(?authorLabel), 'en'))
?authorLabel bif:contains "Rowling"
OPTIONAL { ?s dbp:country ?country .
?country rdfs:label ?countryLabel .
FILTER(LANGMATCHES(LANG(?countryLabel), 'en')) }
OPTIONAL { ?s dbo:literaryGenre ?genre .
?genre rdfs:label ?genreLabel .
FILTER(LANGMATCHES(LANG(?genreLabel), 'en')) }
OPTIONAL { ?s dbp:language ?language .
?language rdfs:label ?languageLabel .
FILTER(LANGMATCHES(LANG(?languageLabel), 'en')) }
}

Finding the relative position of elements in a list using SPARQL

I'm trying to return subjects based on the relative position of their subjects in an ordered list.
A subject can be associated with multiple objects (via a single predicate) and all objects are in an ordered list. Given a reference object in this list I'd like to return the subjects in order of relative distance of their objects from the reference object.
:a : :x
:b : :v
:b : :z
:c : :v
:c : :y
:ls :list (:v :w :x :y :z)
Taking x as our starting object in the list, the code below returns
:a :x :0
:c :y :1
:b :v :2
:b :z :2
:c :v :2
Instead of returning all positions I would like only the objects relating to the subject's minimum object 'distance' to be returned (which may mean up to two objects per subject - both up and down the list). So I'd like to return
:a :x :0
:c :y :1
:b :v :2
:b :z :2
The code so far...
(with a lot of help from Find lists containing ALL values in a set? and Is it possible to get the position of an element in an RDF Collection in SPARQL?)
SELECT ?s ?p (abs(?refPos-?pos) as ?dif)
WHERE {
:ls :list/rdf:rest*/rdf:first ?o .
?s : ?o .
{
SELECT ?o (count(?mid) as ?pos) ?refPos
WHERE {
[] :list/rdf:rest* ?mid . ?mid rdf:rest* ?node .
?node rdf:first ?o .
{
SELECT ?o (count(?mid2) as ?refPos)
WHERE {
[] :list/rdf:rest* ?mid2 . ?mid2 rdf:rest* ?node2 .
?node2 rdf:first :x .
}
}
}
GROUP BY ?o
}
}
GROUP BY ?s ?o
ORDER BY ?dif
I've been trying to get a minimum ?dif (difference/distance) by grouping by ?s but because I then have to apply this (something like ?dif = ?minDif) to the ?s ?o grouping from earlier I don't know how to go back and forward between these two groupings.
Thanks for any assistance you can provide
All you needed to compound a solution is yet another one Joshua Taylor's answer: this or this.
Here below I'm using Jena functions, but I hope the idea is clear.
Query 1
PREFIX list: <http://jena.hpl.hp.com/ARQ/list#>
SELECT ?s ?el ?dif {
?s : ?el .
:ls :list/list:index (?pos ?el) .
:ls :list/list:index (?ref :x) .
BIND (ABS(?pos -?ref) AS ?dif)
{
SELECT ?s (MIN (?dif_) AS ?dif) WHERE {
?s : ?el_ .
:ls :list/list:index (?pos_ ?el_) .
:ls :list/list:index (?ref_ :x) .
BIND (ABS(?pos_ - ?ref_) AS ?dif_)
} GROUP by ?s
}
}
Query 2
PREFIX list: <http://jena.apache.org/ARQ/list#>
SELECT ?s ?el ?dif {
?s : ?el .
:ls :list/list:index (?pos ?el) .
:ls :list/list:index (?ref :x) .
BIND (ABS(?pos -?ref) AS ?dif)
FILTER NOT EXISTS {
?s : ?el_ .
:ls :list/list:index (?pos_ ?el_) .
BIND (ABS(?pos_ - ?ref) AS ?dif_) .
FILTER(?dif_ < ?dif)
}
}
Update
Query 1 can be rewritten in this way:
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT ?s ?el ?dif {
?s : ?el
{ select (count(*) as ?pos) ?el {[] :list/rdf:rest*/rdf:rest*/rdf:first ?el} group by ?el }
{ select (count(*) as ?ref) {[] :list/rdf:rest*/rdf:rest*/rdf:first :x} }
BIND (ABS(?pos - ?ref) AS ?dif)
{
SELECT ?s (MIN(?dif_) AS ?diff) {
?s : ?el_
{ select (count(*) as ?pos_) ?el_ {[] :list/rdf:rest*/rdf:rest*/rdf:first ?el_} group by ?el_ }
{ select (count(*) as ?ref_) {[] :list/rdf:rest*/rdf:rest*/rdf:first :x} }
BIND (ABS(?pos_ - ?ref_) AS ?dif_)
} GROUP by ?s
}
FILTER (?dif = ?diff)
}
Notes
As you can see, this is not what SPARQL was designed for. For example, Blazegraph supports Gremlin...
Possibly this is not what RDF was designed for. Or try other modeling approach: do you really need RDF lists?
I haven't tested the above query in Virtuoso.

SPARQL query for individuals with same properties

I'd like to identify all individuals, that have the same properties as some other individuals. Imagine having different shopping lists with different items on them one can either buy or rent (object properties). To make things more complex, I also want to pay an exact amount for the parking and travel a certain distance (data properties). At the same time, there exist different stores offering different items.
Being a lazy person, I would like to identify the stores for each list, that offer all the items on the list.
I believe this is a generalization of this question but I somehow cannot wrap my head around how to do this.
I created some sample individuals:
#prefix : <http://www.shopping.org/model#> .
#prefix owl: <http://www.w3.org/2002/07/owl#> .
#prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
#prefix xml: <http://www.w3.org/XML/1998/namespace> .
#prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
#prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
#base <http://www.shopping.org/model> .
<http://www.shopping.org/model> rdf:type owl:Ontology .
# Object Properties
:buy rdf:type owl:ObjectProperty .
:rent rdf:type owl:ObjectProperty .
# Data properties
:distance rdf:type owl:DatatypeProperty .
:parking rdf:type owl:DatatypeProperty .
# Classes
:Product rdf:type owl:Class .
:ShoppingList rdf:type owl:Class .
:Store rdf:type owl:Class .
# Individuals
:Apples rdf:type owl:NamedIndividual ,
:Product .
:Cereal rdf:type owl:NamedIndividual ,
:Product .
:List1 rdf:type owl:NamedIndividual ,
:ShoppingList ;
:buy :Apples ,
:Milk ;
:distance "9.0"^^xsd:float ;
:parking "10.0"^^xsd:float .
:List2 rdf:type owl:NamedIndividual ,
:ShoppingList ;
:buy :Cereal ,
:Milk ;
:rent :TV ;
:distance "5.0"^^xsd:float ;
:parking "10.0"^^xsd:float .
:Milk rdf:type owl:NamedIndividual ,
:Product .
:Store1 rdf:type owl:NamedIndividual ,
:Store ;
:buy :Apples ,
:Cereal ,
:Milk ,
:TV ;
:distance "9.0"^^xsd:float ;
:parking "10.0"^^xsd:float .
:Store2 rdf:type owl:NamedIndividual ,
:Store ;
:buy :Cereal ,
:Milk ;
:rent :TV ;
:distance "5.0"^^xsd:float ;
:parking "10.0"^^xsd:float .
:TV rdf:type owl:NamedIndividual ,
:Product .
# General axioms
[ rdf:type owl:AllDisjointClasses ;
owl:members ( :Product
:ShoppingList
:Store
)
] .
and also tried some first queries. This is the best I have:
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 : <http://www.shopping.org/model#>
SELECT DISTINCT ?list ?store ?prop
WHERE {
?list a :ShoppingList .
?store a :Store .
?list ?prop [] .
FILTER NOT EXISTS {
?list ?prop ?value .
FILTER NOT EXISTS {
?store ?prop ?value .
}
}
}
ORDER BY ?list ?store
However, this query returns all combinations of stores and lists, since every store charges 10.0f for parking.
How can I filter only those stores, which meet all the requirements of a list?
An additional assumption is, that there may exist other business models than buy and rent, as well as other criteria, i.e. data properties, which are of interest, too. This is why I do not want to specify these properties, but want to use variables instead.
This query did what I intended to do:
PREFIXES [as above]
PREFIX : <http://www.shopping.org/model#>
SELECT DISTINCT ?list ?store
WHERE {
?list a :ShoppingList .
?store a :Store .
FILTER NOT EXISTS {
?compat a owl:DatatypeProperty
FILTER NOT EXISTS {
?list ?compat ?value .
?store ?compat ?value .
}
}
FILTER NOT EXISTS {
?subset a owl:ObjectProperty .
?list ?subset ?value .
FILTER NOT EXISTS {
?store ?subset ?value .
}
}
}
ORDER BY ?list ?store
My mistake was actually just that I defined the ?prop outside of the filters. This resulted in them being part of the solution, i.e. I was was unable to remove whole stores through the filter(s).
If you're willing to specific about the particular property that the things need to have in common, you can do something like this. I've cleaned up your data, because it didn't actually have the necessary prefix declarations. Please include complete data if you expect people to be able to work with what you're providing. I didn't add correct prefixes, but just enough to get things working.
Sample Data
#prefix : <urn:ex:> .
#prefix owl: <file:///home/taylorj/tmp/data.ttl> .
#prefix xsd: <file:///home/taylorj/tmp/data.ttl> .
:Store1 a owl:NamedIndividual , :Store ;
:buy :Apples , :Cereal , :Milk , :TV ;
:distance "9.0"^^owl:float ;
:parking "10.0"^^owl:float .
:List2 a owl:NamedIndividual , :ShoppingList ;
:buy :Cereal , :Milk ;
:distance "5.0"^^owl:float ;
:parking "10.0"^^owl:float ;
:rent :TV .
:List1 a owl:NamedIndividual , :ShoppingList ;
:buy :Apples , :Milk ;
:distance "9.0"^^owl:float ;
:parking "10.0"^^owl:float .
:Store2 a owl:NamedIndividual , :Store ;
:buy :Cereal , :Milk ;
:distance "5.0"^^owl:float ;
:parking "10.0"^^owl:float ;
:rent :TV .
Specific Solution
First, we can address the specific case where we need one individual to have a subset of the property values of another for one property (buy), and to have intersecting values for other properties (distance, parking).
Query
prefix : <urn:ex:>
select ?list ?store {
#-- For each list and store
?list a :ShoppingList .
?store a :Store .
#-- Check that they have the same distance and parking.
?distance ^:distance ?list, ?store .
?parking ^:parking ?list, ?store .
#-- Check that every item to buy (or rent) on the list is also
#-- in the store to buy (or rent).
filter not exists {
values ?get { :buy :rent }
?list ?get ?item .
filter not exists {
?store ?get ?item
}
}
}
Results
--------------------
| list | store |
====================
| :List1 | :Store1 |
| :List2 | :Store2 |
--------------------
General Approach
Often times it's helpful to think in terms of eliminating the results that you don't want. In this case, you have two kinds of properties: (i) properties where the individuals must have a compatible value, and (ii) properties where one individual must have a superset of the values of the other individual. Checking if either of those conditions doesn't hold is not too hard:
prefix : <urn:ex:>
select ?list ?store {
#-- For each list and store
?list a :ShoppingList .
?store a :Store .
#-- Check that for each "compatible property" ?compat,
#-- there must be some value that the ?list and the
#-- ?store have in common.
filter not exists {
values ?compat { :distance :parking }
filter not exists {
?list ?compat ?value .
?store ?compat ?value .
}
}
#-- Check that for each "subset property" ?subset,
#-- there is no value that the ?list contains that
#-- the store does not.
filter not exists {
values ?subset { :buy :rent }
?list ?subset ?value
filter not exists {
?store ?subset ?value
}
}
}
Results (the same)
--------------------
| list | store |
====================
| :List1 | :Store1 |
| :List2 | :Store2 |
--------------------

How to do a COUNT in SPARQL

Given this very simple model:
#prefix : <http://example.org/tags#> .
#prefix owl: <http://www.w3.org/2002/07/owl#> .
#prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
#prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
#prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
:tag rdf:type rdf:Property .
:item1
rdf:type owl:Thing ;
:tag "a"^^xsd:string .
:item2
rdf:type owl:Thing ;
:tag "a"^^xsd:string , "b"^^xsd:string .
:item3
rdf:type owl:Thing ;
:tag "a"^^xsd:string , "b"^^xsd:string , "c"^^xsd:string .
I am trying to get a list of the items and the count of tags that each has:
item tagCount
===== ========
item1 1
item2 2
item3 3
Here is my query:
SELECT ?item (count(?tag) as ?tagcount)
WHERE {
?item :tag ?tag
}
However it is returning:
item tagCount
===== ========
6
From what I have read, this should work. I am using Jena 2.6.4
I haven't tried this, but try adding GROUP BY ?item to the end of the query. I think without GROUP BY it just counts the total number of rows.
For the binding to appear in the results you do need to use the group by keyword so this becomes
SELECT ?item (count(?tag) as ?tagcount)
WHERE {
?item :tag ?tag
} group by ?item
If you want to count something in the middle of the query you would do the following, note how you must put the inner select query into its own block {}
SELECT * {
?item a owl:Thing .
{
SELECT ?item (count(?tag) as ?tagcount)
WHERE {
?item :tag ?tag
} group by ?item
}
}
The sub-select by #user2316243 is unnecessary, therefore the following query is equivalent:
SELECT ?item (count(?tag) as ?tagcount)
WHERE {
?item a owl:Thing .
?item :tag ?tag .
} GROUP BY ?item