[Mulgara-general] Using <mulgara:is> when $x $y $z doesn't exist

Alex Hall alexhall at revelytix.com
Tue Dec 16 18:47:58 UTC 2008


Rui Castro wrote:
> Hi,
> 
> I'm using Mulgara with Fedora 2.2.4 and I'm having trouble writing a
> query that uses a pre-determined value when a certain predicate
> doesn't exist. I'll try to explain the best I can.
> 
> Objects (resources) are connected by the predicate "roda:parent-of" to
> form a tree. Like this:
> <roda:1> <roda:parent-of> <roda:2>
> <roda:1> <roda:parent-of> <roda:3>
> 
> <roda:1> is the root of the tree.
> 
> Now, I want to do a query that, besides other attributes, returns the
> parent-of subject, if it exists, otherwise i would like the value to
> return '' or null or something predetermined.
> 
> I've tried this query
> 
> select
> 	$pid $level $parentPID from <#ri>
> where
> 	$pid <roda:description-level> $level
> 	and ($parentPID <roda:parent-of> $pid
> 		or
> 		$parentPID <mulgara:is> '')
> 
> But the results are:
> 
> "pid","level","parentPID"
> roda:1,fonds,
> roda:2,series,roda:1
> roda:2,series,
> roda:3,series,roda:1
> roda:3,series,
> 
> For objects in which "$parentPID <roda:parent-of> $pid" exist the
> results are doubled.

The <mulgara:is> predicate always binds its subject to the supplied object
value.  So the constraint ($parentPID <roda:parent-of> $pid or $parentPID
<mulgara:is> '') always binds $parentPID to the empty literal '', in addition to
any statements matched by the first <roda:parent-of> constraint.

> How can i do a query so that when the <roda:parent-of> predicate
> doesn't exist, an empty value is returned instead?

The simplest approach is a SPARQL query using an optional join:

SELECT ?pid ?level ?parentPID
WHERE {
   ?pid <roda:description-level> ?level
   OPTIONAL { ?parentPID <roda:parent-of> ?pid }
}

This will bind ?parentPID to the expected parent value if it's present, or null
(indicating an unbound variable) if it isn't.

Regards,
Alex



More information about the Mulgara-general mailing list