Hi All,

I have following sparql query:

PREFIX spec: <http://rdfs.org/sioc/spec/> 
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX constraint: <http://purl.org/vocab/constraint/constraint#>

SELECT  
(sample(?FName) as ?fName)
(sample(?Midname) as ?midname) 
(sample(?User) as ?user)

(GROUP_CONCAT(DISTINCT ?userRole;  separator='; ') AS ?UserRole)
WHERE {
    GRAPH <http://example.com/> 
    {
        ?User spec:email ?Email .   
        ?User foaf:firstName ?FirstName .
        ?User foaf:surname ?Midname .
        OPTIONAL
        {
            ?User constraint:containsConstraint ?constraint .               
            ?constraint constraint:constrains ?userRoleUri .
            ?userRoleUri spec:name ?userRole.
        }
FILTER (str(?userRoleUri) IN("http://example.com/roles/admin", "http://example.com/roles/moderator"))

    }
} GROUP BY ?FName  ORDER BY ASC(?FName)

What this query does is it returns the matching user. So, if a user "A" has 3 roles listed : editor, admin and moderator, it displays only two roles for user. like :

A - Mid - a@abc.om - admin;moderator

What i wanted to do from query is : since user A matches any of the given filter conditions, all it's roles should be listed. Is there a way to improve this query? I have tried using sub select and regex in filters as well, but :(

Thanks in advance

asked 13 Dec '12, 06:40

sparql-learner's gravatar image

sparql-learner
111
accept rate: 0%

Not quite sure I'm clear what you mean. Do you want all roles for A if A matches one of the two roles of admin or moderator?

(13 Dec '12, 08:02) AndyS ♦ AndyS's gravatar image

@sparql-learner By the way, you don't need the str() function to use the IN operator. This should handle IRIs as well. In your case you could rewrite that as FILTER (?userRoleUri IN(<http://example.com/roles/admin>, <http://example.com/roles/moderator>). This should be more efficient.

(13 Dec '12, 09:12) Tomasz Plusk... Tomasz%20Pluskiewicz's gravatar image

Your problem is the FILTER. Imagine a query without a filter and grouping. It would return

A - Mid - a@abc.om - editor
A - Mid - a@abc.om - moderator
A - Mid - a@abc.om - admin

The filter elimintates the first result and the the remaining two are concatenated.

Off the top of my head you could use a subquery to get all user URIs and the select the needed triples (not sure this will work but you you should get the idea).

SELECT 
    ?FName ?Midname ?User
    (GROUP_CONCAT(DISTINCT ?userRole;  separator='; ') AS ?UserRole)
WHERE 
{
    ?User spec:email ?Email .   
    ?User foaf:firstName ?FirstName .
    ?User foaf:surname ?Midname .
    OPTIONAL
    {
        ?User constraint:containsConstraint ?constraint .               
        ?constraint constraint:constrains ?userRoleUri .
        ?userRoleUri spec:name ?userRole.
    }
    {
        SELECT DISTINCT ?User
        WHERE 
        {
            { 
                ?User constraint:containsConstraint ?constraint .               
                ?constraint constraint:constrains <http://example.com/roles/admin> .
            }
            UNION
            { 
                ?User constraint:containsConstraint ?constraint .               
                ?constraint constraint:constrains <http://example.com/roles/moderator> .
            }
        }
    }
} GROUP BY ?FName  ORDER BY ASC(?FName)

The plus of this should no need for any FILTERs, which should positively affect performance. I'm not sure though ;)

link

answered 13 Dec '12, 09:08

Tomasz%20Pluskiewicz's gravatar image

Tomasz Plusk...
1.3k19
accept rate: 31%

edited 13 Dec '12, 09:09

hi @tomasz thanks for your response... I'll try this...

(14 Dec '12, 01:16) sparql-learner sparql-learner's gravatar image
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×1,248
×22

Asked: 13 Dec '12, 06:40

Seen: 793 times

Last updated: 14 Dec '12, 01:16