MarkLogic 12 Product Documentation
ModifyPlan.prototype.transitiveClosure

ModifyPlan.prototype.transitiveClosure(
   start as ColumnIdentifier,
   end as ColumnIdentifier,
   [options as String[]|objectLiteral|null]
) as ModifyPlan

Summary

This method performs a transitive closure operation over a graph-like structure, identifying all reachable node pairs from a given start node to an end node through one or more intermediate steps. A set of (start, end) node pairs where a path exists between them with a length between minLength and maxLength, inclusive. This models the SPARQL one-or-more (+) operator, enabling recursive or chained relationships to be queried efficiently.

Parameters
start The column is the starting node of the traversal. The column can be named with a string or a column function such as op.col, op.viewCol, or op.schemaCol, or constructed from an expression with the op.as function.
end The column is the end node of the traversal. The column can be named with a string or a column function such as op.col, op.viewCol, or op.schemaCol, or constructed from an expression with the op.as function.
options This is either an array of strings or an object containing keys and values for the options to this operator. Options include:
  • minLength

    This option is the minimum number of steps (edges) required in the path. It should be a non-negative integer, and the default is 1.

  • maxLength

    This option Maximum number of steps (edges) allowed in the path. It should be a non-negative integer, and the default is Infinity.

Usage Notes

transitiveClosure is a method of the following classes:

Example

// Insert a graph in SPARQL Update query format
PREFIX ex: <http://example.org/>
INSERT DATA
{
"Alice" ex:parent "Bob" .
"Bob" ex:parent "Carol" .
"Carol" ex:parent "David" .
"David" ex:parent "Eve" .
"Eve" ex:parent "Frank" .
"George" ex:parent "Helen" .
"Helen" ex:parent "Ian" .
"Alice" ex:parent "Cindy" .
"Cindy" ex:parent "John" .
};

//Calculate the node pairs with the number of edges between 4 and 5
'use strict';
const sem = require("/MarkLogic/semantics.xqy");
const op = require('/MarkLogic/optic');

op.fromTriples(op.pattern(op.col("person"), sem.iri("http://example.org/parent"), op.col("ancestor")))
.transitiveClosure(op.col("person"), op.col("ancestor"), {minLength:4, maxLength:5})
.orderBy(['person','ancestor'])
.result()

/* It returns
{"person": "Alice", "ancestor": "Eve"}
{"person": "Alice", "ancestor": "Frank"}
{"person": "Bob", "ancestor": "Frank"}
*/
  

Example

//Same setup as above, with string-format options
//Calculate the node pairs with the number of edges between 4 and 5
'use strict';
const sem = require("/MarkLogic/semantics.xqy");
const op = require('/MarkLogic/optic');

op.fromTriples(op.pattern(op.col("person"), sem.iri("http://example.org/parent"), op.col("ancestor")))
.transitiveClosure(op.col("person"), op.col("ancestor"), ["minLength=4", "maxLength=5"])
.orderBy(['person','ancestor'])
.result()

/* It returns
{"test.person": "Alice", "test.ancestor": "Eve"}
{"test.person": "Alice", "test.ancestor": "Frank"}
{"test.person": "Bob", "test.ancestor": "Frank"}
*/
    
Powered by MarkLogic Server | Terms of Use | Privacy Policy