I would like to write a query which filters out some results by using some other properties of the graph. I explain with this example.
I am modelling a permissions system. There are some subjects, resources and permissions. Subjects have certain permissions on resources. Some permissions imply other permissions. A simplified example with 3 permissions, and 3 resources that the single subject has one (two in one case) of those permissions on.
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX : <http://schema#>
PREFIX d: <http://data#>
INSERT DATA {
:Permission rdf:type rdf:Property .
:Read rdf:type rdf:Property ;
rdfs:subPropertyOf :Permission .
:Write rdf:type rdf:Property ;
rdfs:subPropertyOf :Permission ;
:implies :Read .
:Admin rdf:type rdf:Property ;
rdfs:subPropertyOf :Permission ;
:implies :Write ,
:Read .
d:s1 rdf:type :Subject .
d:r1 rdf:type :Resource .
d:r2 rdf:type :Resource .
d:r3 rdf:type :Resource .
d:s1 :Admin d:r1 .
d:s1 :Read d:r1 .
d:s1 :Write d:r2 .
d:s1 :Read d:r3 .
}
I can easily write a query to see for a given subject, what the implied permissions are on some resources.
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX : <http://schema#>
PREFIX d: <http://data#>
SELECT DISTINCT ?subject ?resource ?implied (?permission AS ?implied_by)
WHERE {
BIND(d:s1 AS ?subject)
?resource rdf:type :Resource .
?subject rdf:type :Subject .
?permission rdfs:subPropertyOf :Permission ;
:implies* ?implied .
?subject ?permission ?resource .
}
This will give some results like:
+----------------+----------------+---------------------+---------------------+
| subject | resource | implied | implied_by |
+----------------+----------------+---------------------+---------------------+
| http://data#s1 | http://data#r1 | http://schema#Admin | http://schema#Admin |
| http://data#s1 | http://data#r1 | http://schema#Read | http://schema#Admin |
| http://data#s1 | http://data#r1 | http://schema#Write | http://schema#Admin |
| http://data#s1 | http://data#r1 | http://schema#Read | http://schema#Read |
| http://data#s1 | http://data#r3 | http://schema#Read | http://schema#Read |
| http://data#s1 | http://data#r2 | http://schema#Write | http://schema#Write |
| http://data#s1 | http://data#r2 | http://schema#Read | http://schema#Write |
+----------------+----------------+---------------------+---------------------+
What I would like to do is write a query which only returns the minimum list of permissions per resource which imply all the others. Any ideas on how I would achieve this efficiently would be most welcome. This seems a bit like a MAX
aggregation, but where a graph is actually needed to determine the results. For example, the ideal results would be:
+----------------+----------------+---------------------+---------------------+
| subject | resource | implied | implied_by |
+----------------+----------------+---------------------+---------------------+
| http://data#s1 | http://data#r1 | http://schema#Admin | http://schema#Admin |
| http://data#s1 | http://data#r3 | http://schema#Read | http://schema#Read |
| http://data#s1 | http://data#r2 | http://schema#Write | http://schema#Write |
+----------------+----------------+---------------------+---------------------+
Note that the data and queries are actually more complicated that this so it is absolutely necessary for me to use the parameter path even though I then want to get rid of most of the information it introduces.
?permission rdfs:subPropertyOf :Permission ;
:implies* ?implied .
Note also that I thought of adding an integer value to each permission so that I could simply get the minimum value of this field, but my permission structure is not single rooted in reality, so sometimes this would not be appropriate.
Effectively the question is how to remove any permissions (for a resource) which are implied by other results (for that resource)