Discussion:
SPARQL query
Claude Warren
2017-03-01 20:12:02 UTC
Permalink
I have a graph where resources have a number of controlled properties.
Call them A thorough F. Some resources may have one property other may
have all 6.

I have a resource with a with properties A, B, and C.

I want to find all resources in the graph where the resource has matching
values but does not have any non specified properties.

So
x with A, B, and C will match
y with A and C will match
z with A, B, C and D will not match
w with A and D will not match.

Any idea how to construct a query that will do this?

It seems backwards to the way I normally think about queries.

Any help would be appreciated,
Claude
--
I like: Like Like - The likeliest place on the web
<http://like-like.xenei.com>
LinkedIn: http://www.linkedin.com/in/claudewarren
Welch, Bill
2017-03-01 20:19:41 UTC
Permalink
I believe you need to AND together the NOT EXISTS of D, E, and F in a
filter.

https://www.w3.org/TR/sparql11-query/#neg-notexists
Post by Claude Warren
I have a graph where resources have a number of controlled properties.
Call them A thorough F. Some resources may have one property other may
have all 6.
I have a resource with a with properties A, B, and C.
I want to find all resources in the graph where the resource has matching
values but does not have any non specified properties.
So
x with A, B, and C will match
y with A and C will match
z with A, B, C and D will not match
w with A and D will not match.
Any idea how to construct a query that will do this?
It seems backwards to the way I normally think about queries.
Any help would be appreciated,
Claude
--
I like: Like Like - The likeliest place on the web
<http://like-like.xenei.com>
LinkedIn: http://www.linkedin.com/in/claudewarren
Notice: This e-mail message, together with any attachments, contains
information of Merck & Co., Inc. (2000 Galloping Hill Road, Kenilworth,
New Jersey, USA 07033), and/or its affiliates Direct contact information
for affiliates is available at
http://www.merck.com/contact/contacts.html) that may be confidential,
proprietary copyrighted and/or legally privileged. It is intended solely
for the use of the individual or entity named on this message. If you are
not the intended recipient, and have received this message in error,
please notify us immediately by reply e-mail and then delete it from
your system.
Paul Tyson
2017-03-01 22:02:34 UTC
Permalink
Maybe something like:

Select ?s ?p ?o
Where {
?s ?p ?o.
Filter (?p in (:A, :B, :C))
Minus {?s ?p2 ?o2.
Filter (!(?p2 in (:A, :B, :C)))}

Untested.

Regards,
--Paul
Post by Claude Warren
I have a graph where resources have a number of controlled properties.
Call them A thorough F. Some resources may have one property other may
have all 6.
I have a resource with a with properties A, B, and C.
I want to find all resources in the graph where the resource has matching
values but does not have any non specified properties.
So
x with A, B, and C will match
y with A and C will match
z with A, B, C and D will not match
w with A and D will not match.
Any idea how to construct a query that will do this?
It seems backwards to the way I normally think about queries.
Any help would be appreciated,
Claude
--
I like: Like Like - The likeliest place on the web
<http://like-like.xenei.com>
LinkedIn: http://www.linkedin.com/in/claudewarren
Adrian Walker
2017-03-02 01:24:35 UTC
Permalink
Hi All,

Here is what Claude's example looks like in executable English.





































*some-resource has the property some-propertynot : that-resource has a
non-matching
property--------------------------------------------------------------that-resource
having that-property matchessome-resource has the property
some-propertythat-resource has a non-matching
property------------------------------------------------------------------that-resource
having that-property does not matchsome-resource has the property
some-propertythat-property is a non matching
one---------------------------------------------------------------that-resource
has a non-matching propertythis-resource has the property
this-property==================================
x A x B
x C y A
y C z A
z B z C
z D w A
w Dthis-property is a non matching
one=========================== D*
and here is SQL generated from the above.




*select tt1.RES , tt1.PROP from mysql.matchT1 tt1 where tt1.RES not in
(select tt2.RES from mysql.matchT1 tt2 , mysql.matchT2 where
mysql.matchT2.PROP = tt2.PROP and tt1.RES = tt2.RES )*

Presumably,SPARQL could be similar.

HTH, - Adrian

Adrian Walker
Reengineering LLC
San Jose, CA, USA
860 830 2085
www.executable-english.com
Post by Paul Tyson
Select ?s ?p ?o
Where {
?s ?p ?o.
Filter (?p in (:A, :B, :C))
Minus {?s ?p2 ?o2.
Filter (!(?p2 in (:A, :B, :C)))}
Untested.
Regards,
--Paul
Post by Claude Warren
I have a graph where resources have a number of controlled properties.
Call them A thorough F. Some resources may have one property other may
have all 6.
I have a resource with a with properties A, B, and C.
I want to find all resources in the graph where the resource has matching
values but does not have any non specified properties.
So
x with A, B, and C will match
y with A and C will match
z with A, B, C and D will not match
w with A and D will not match.
Any idea how to construct a query that will do this?
It seems backwards to the way I normally think about queries.
Any help would be appreciated,
Claude
--
I like: Like Like - The likeliest place on the web
<http://like-like.xenei.com>
LinkedIn: http://www.linkedin.com/in/claudewarren
Osma Suominen
2017-03-02 07:30:47 UTC
Permalink
Hi Claude,

Do you mean something like this?

SELECT ?r
WHERE {
{ # must have at least one of A, B, C
{ ?r :p :A }
UNION
{ ?r :p :B }
UNION
{ ?r :p :B }
}
# must not have D, E or F
FILTER NOT EXISTS { ?r :p :D }
FILTER NOT EXISTS { ?r :p :E }
FILTER NOT EXISTS { ?r :p :F }
}

I'm sure there are other possible variations, for example the FILTER NOT
EXISTS patterns could be combined to a single pattern using UNION. UNION
is generally more efficient than filter rules such as IN or NOT IN,
that's why I used it above. Using a VALUES block would have been an
option as well.

Or did you mean that this has to be generic, so that any resource ?a can
be used as input and the query has to figure out the restrictions based
on properties that ?a has or doesn't have? That's going to be trickier...

-Osma
Post by Claude Warren
I have a graph where resources have a number of controlled properties.
Call them A thorough F. Some resources may have one property other may
have all 6.
I have a resource with a with properties A, B, and C.
I want to find all resources in the graph where the resource has matching
values but does not have any non specified properties.
So
x with A, B, and C will match
y with A and C will match
z with A, B, C and D will not match
w with A and D will not match.
Any idea how to construct a query that will do this?
It seems backwards to the way I normally think about queries.
Any help would be appreciated,
Claude
--
Osma Suominen
D.Sc. (Tech), Information Systems Specialist
National Library of Finland
P.O. Box 26 (Kaikukatu 4)
00014 HELSINGIN YLIOPISTO
Tel. +358 50 3199529
***@helsinki.fi
http://www.nationallibrary.fi
Claude Warren
2017-03-02 07:52:59 UTC
Permalink
Osma, I think I am asking for the latter. Table wise it looks like this

{noformat}

A B C D Match
w 1 1 No
x 1 1 1 Yes
y 1 1 Yes
z 1 1 1 1 No

Looking for matches for
P 1 1 1


Items in the table may not specify values for properties that P does not
have

Claude
Post by Osma Suominen
Hi Claude,
Do you mean something like this?
SELECT ?r
WHERE {
{ # must have at least one of A, B, C
{ ?r :p :A }
UNION
{ ?r :p :B }
UNION
{ ?r :p :B }
}
# must not have D, E or F
FILTER NOT EXISTS { ?r :p :D }
FILTER NOT EXISTS { ?r :p :E }
FILTER NOT EXISTS { ?r :p :F }
}
I'm sure there are other possible variations, for example the FILTER NOT
EXISTS patterns could be combined to a single pattern using UNION. UNION is
generally more efficient than filter rules such as IN or NOT IN, that's why
I used it above. Using a VALUES block would have been an option as well.
Or did you mean that this has to be generic, so that any resource ?a can
be used as input and the query has to figure out the restrictions based on
properties that ?a has or doesn't have? That's going to be trickier...
-Osma
Post by Claude Warren
I have a graph where resources have a number of controlled properties.
Call them A thorough F. Some resources may have one property other may
have all 6.
I have a resource with a with properties A, B, and C.
I want to find all resources in the graph where the resource has matching
values but does not have any non specified properties.
So
x with A, B, and C will match
y with A and C will match
z with A, B, C and D will not match
w with A and D will not match.
Any idea how to construct a query that will do this?
It seems backwards to the way I normally think about queries.
Any help would be appreciated,
Claude
--
Osma Suominen
D.Sc. (Tech), Information Systems Specialist
National Library of Finland
P.O. Box 26 (Kaikukatu 4)
00014 HELSINGIN YLIOPISTO
Tel. +358 50 3199529
http://www.nationallibrary.fi
--
I like: Like Like - The likeliest place on the web
<http://like-like.xenei.com>
LinkedIn: http://www.linkedin.com/in/claudewarren
Osma Suominen
2017-03-02 08:01:54 UTC
Permalink
Hi Claude,

Okay, let me try. I think I've done something similar actually - and ran
into performance problems with those queries using the latest Jena
releases. But that's a different story...

SELECT ?r
WHERE {
# we have a specific resource for which we are looking for matches
BIND(:inputresource AS ?a)

# the input resource has one or more properties
?a :p ?val .
# the matching resource should have at least one of those properties
?r :p ?val .

# but the targeted resource shouldn't have any property that the
input resource doesn't have
FILTER NOT EXISTS {
?r :p ?val2 .
FILTER NOT EXISTS {
?a :p ?val2 .
}
}
}

I think one or both of the FILTER NOT EXISTS may be changed to a MINUS
as well.

Performance is likely to be poor with large data.

-Osma
Post by Claude Warren
Osma, I think I am asking for the latter. Table wise it looks like this
{noformat}
A B C D Match
w 1 1 No
x 1 1 1 Yes
y 1 1 Yes
z 1 1 1 1 No
Looking for matches for
P 1 1 1
Items in the table may not specify values for properties that P does not
have
Claude
Post by Osma Suominen
Hi Claude,
Do you mean something like this?
SELECT ?r
WHERE {
{ # must have at least one of A, B, C
{ ?r :p :A }
UNION
{ ?r :p :B }
UNION
{ ?r :p :B }
}
# must not have D, E or F
FILTER NOT EXISTS { ?r :p :D }
FILTER NOT EXISTS { ?r :p :E }
FILTER NOT EXISTS { ?r :p :F }
}
I'm sure there are other possible variations, for example the FILTER NOT
EXISTS patterns could be combined to a single pattern using UNION. UNION is
generally more efficient than filter rules such as IN or NOT IN, that's why
I used it above. Using a VALUES block would have been an option as well.
Or did you mean that this has to be generic, so that any resource ?a can
be used as input and the query has to figure out the restrictions based on
properties that ?a has or doesn't have? That's going to be trickier...
-Osma
Post by Claude Warren
I have a graph where resources have a number of controlled properties.
Call them A thorough F. Some resources may have one property other may
have all 6.
I have a resource with a with properties A, B, and C.
I want to find all resources in the graph where the resource has matching
values but does not have any non specified properties.
So
x with A, B, and C will match
y with A and C will match
z with A, B, C and D will not match
w with A and D will not match.
Any idea how to construct a query that will do this?
It seems backwards to the way I normally think about queries.
Any help would be appreciated,
Claude
--
Osma Suominen
D.Sc. (Tech), Information Systems Specialist
National Library of Finland
P.O. Box 26 (Kaikukatu 4)
00014 HELSINGIN YLIOPISTO
Tel. +358 50 3199529
http://www.nationallibrary.fi
--
Osma Suominen
D.Sc. (Tech), Information Systems Specialist
National Library of Finland
P.O. Box 26 (Kaikukatu 4)
00014 HELSINGIN YLIOPISTO
Tel. +358 50 3199529
***@helsinki.fi
http://www.nationallibrary.fi
Loading...