Recursive (query directive)
Description
Applies a directive recursively. Can either return all results or only leaf results, that is results where further applications of the directive would not return any more results. Maximum recursion depth can also be specified, give -1 for no limit, this is the default behavior.
The input to Recursive directive is used as the first input to the inner directive. After this, results of the inner directive are used to advance the recursion. The original input to Recursive directive is modified by replacing the active value with the active value from results of the inner directive. These modified input rows are then used as input for the inner directive.
Then normal behavior is to return all rows from the recursive applications of the inner directive as results of the Recursive directive. If the onlyLeaves parameter is true, only such rows will be returned where further application of the inner directive returns no more rows. This is typically used on tree-like structures to get only the leaf nodes.
Recursion remembers what values have been used in recursion before and will not use same value twice. This means that the recursion will not get stuck in an infinite loop. It also has the side effect of removing duplicate rows from the results. In fact, it removes rows where the active column has duplicate values, even if other columns would be different.
Constructor
Recursive(Directive recursion[,int maxDepth[,boolean onlyLeaves]])
Examples
Following example gets super classes of the input, the super classes of those and so on.
importPackage(org.wandora.query2); importPackage(org.wandora.topicmap); new Roles(XTMPSI.SUPERCLASS).from( new Recursive( new Players(XTMPSI.SUPERCLASS_SUBCLASS,XTMPSI.SUPERCLASS) .whereInputIs(XTMPSI.SUBCLASS) ) );
Following example traverses the superclass-subclass tree towards subclasses and only returns topics which do not have subclasses of their own.
importPackage(org.wandora.query2); importPackage(org.wandora.topicmap); new Roles(XTMPSI.SUBCLASS).from( new Recursive( new Players(XTMPSI.SUPERCLASS_SUBCLASS,XTMPSI.SUBCLASS) .whereInputIs(XTMPSI.SUPERCLASS),-1,true ) )
Following example gets all instances of the input, it's subclasses, their subclasses and so on.
importPackage(org.wandora.query2); importPackage(org.wandora.topicmap); new Unique( new Roles("#instance").from( new Union( new Instances().as("#instance").from( new Recursive( new Players(XTMPSI.SUPERCLASS_SUBCLASS,XTMPSI.SUBCLASS) .whereInputIs(XTMPSI.SUPERCLASS) ) ), new Instances().as("#instance") ) ) )