Re: Filtering out resources from a sequence that appear in another sequence

On 28/01/13 12:56, Rob Walpole wrote:
> Hello,
>
> Hoping for some help with troublesome query...
>
> I have a number of resources that are related in a parent/child
> hierarchy. I need to perform a query that identifies the other children
> of resources belonging to an ancestor path. In other words if I have
> resource x then i want to find x's aunt, great-aunt, great-great-aunt
> and so on, if they exist (we are not talking about people here - I am
> only using the term aunt to explain the relationship).
>
> My resources have a property which identifies their parent resource
> (there is only ever one parent) and I am using a SPARQL 1.1 property
> path to find the ancestors, e.g.
>
> ?resource :parent+ ?ancestor .
>
> I am then finding the immediate children of these ancestors as follows:
>
> ?child :parent ?ancestor .
>
> ...and I have confirmed that ?ancestor and ?child contain the exact
> resources that I would expect. The final solution however requires me to
> find all of the child resources that are not ancestors and this I cannot
> get to work. I thought the answer would be simply be...
>
> FILTER(?child != ?ancestor)
>
> but this has no affect and ?child still contains duplicates of resources
> in ?ancestors.

FILTER tests the values of a variable in the current row, not all 
possible values of a variable anywhere.  Only loops in the data will

:X :parent :X

will be != excluded.

What you need to do is check the data to see if what ever value ?child 
has, then it is not used as an ancestor.  The value of ?ancestor in the 
row is only one match, not all matches.

Something like:

FILTER NOT EXISTS { [] :parent ?child  }

or
FILTER NOT EXISTS { ?someNewVar :parent ?child  }

which checks to that ?child is not also used as an object of :parent 
(and hence would be in ?ancestor).

>
> For completeness the full query is attached. I am executing this query
> against a Jena Fuseki SPARQL endpoint.

Other:

FILTER(?ancestor IN(?member)) .

?member is not a list of values - in any given row, it is one value so 
IN is not going to be any different from

FILTER(?ancestor = ?member)

Ditto use of "dri:parent+ ?ancestor , ?ancestorPath ."

I suspect you mean a NOT EXISTS or EXISTS test of some kind.

>
> Thanks
> Rob
>

	Andy

Received on Monday, 28 January 2013 13:10:29 UTC