If (query directive)
Description
Returns results of one of two directives depending on the input. The conditional directive is given the input of this directive. If it returns a non empty result then the then directive is used. Otherwise the else directive is used if provided or an empty result is returned.
You can use a pseudo directive If.COND to return the results of the conditional directive. This is useful in a typical case where you want to return results of some other directive if it is not empty and something else if it is empty. using If.COND avoids having to write and execute the same sub-query in both the conditional and then part. See examples on how to use this in practice.
Constructor
If(Directive cond,Directive then,Directive else) - Depending on cond, returns the results of either then or else directive.
If(Directive cond,Directive then) - Depending on cond, returns the results of then directive or an empty result.
Examples
The example below returns a string "has instances" if the input topic has instances or "no instances" if it doesn't.
importPackage(org.wandora.query2); new If( new Instances(), new Literals("has instances"), new Literals("no instances") )
Next example returns the base name of input or first of its subject identifier if it doesn't have a base name.
importPackage(org.wandora.query2); new If( new BaseName().where("#DEFAULT","!=",null), new BaseName(), new First(new SubjectIdentifiers()) )
Next example demonstrates the If.COND pseudo directive.
importPackage(org.wandora.query2); importPackage(org.wandora.topicmap); new If( new Players(XTMPSI.SUPERCLASS_SUBCLASS,XTMPSI.SUPERCLASS).whereInputIs(XTMPSI.SUBCLASS), new If.COND(), new Null().as(XTMPSI.SUPERCLASS) )
This returns the superclass of input or null if it doesn't have one. Note how the condition is reused with pseudo If.COND directive. This returns whatever the conditional returned. You could achieve the same results by simply duplicating the conditional query instead of using If.COND but using If.COND is both more readable and executes faster since the condition part only needs to be executed once. This is a typical case where you first check if some query returns something, if it does you return that and otherwise return something else.