op:shortest-path( $plan as map:map, $start as item(), $end as item(), [$path as item()?], [$length as item()?] ) as map:map
This method can be used to find the shortest path between two nodes in a given graph.
Parameters | |
---|---|
$plan | The Optic Plan. You can either use the XQuery => chaining operator or specify the variable that captures the return value from the previous operation. |
$start | The column representing the input starting subject of the shortest path search. The column can be named with a string or a column function such as op:col, op:view-col, or op:schema-col, or constructed from an expression with the op:as function. |
$end | The column representing the input ending object of the shortest path search. The column can be named with a string or a column function such as op:col, op:view-col, or op:schema-col, or constructed from an expression with the op:as function. |
$path | The column is the output column representing the actual shortest path(s) taken from start to end. Values are not returned for this column if this is the empty sequence.The columns can be named with a string or a column function such as op:col, op:view-col, or op:schema-col. |
$length | The column is the output column representing the length of the shortest path. Value is not returned for this column if this is the empty sequence.The columns can be named with a string or a column function such as op:col, op:view-col, or op:schema-col. |
(: Calculate the shortest path from person1 to person19 :) (: Insert data :) xquery version "1.0-ml"; import module namespace sem = "http://marklogic.com/semantics" at "MarkLogic/semantics.xqy"; let $node := text { '@prefix ken: <http://example.org/kennedy/> . @prefix foaf: <http://xmlns.com/foaf/0.1/> . ken:person1 foaf:knows ken:person2 . ken:person1 foaf:knows ken:person3 . ken:person1 foaf:knows ken:person5 . ken:person1 foaf:knows ken:person7 . ken:person2 foaf:knows ken:person11 . ken:person2 foaf:knows ken:person3 . ken:person3 foaf:knows ken:person13 . ken:person13 foaf:knows ken:person17 . ken:person13 foaf:knows ken:person19 . ken:person4 foaf:knows ken:person6 . ken:person6 foaf:knows ken:person8 . ken:person8 foaf:knows ken:person10 . ken:person10 foaf:knows ken:person12 . ken:person12 foaf:knows ken:person14 . ken:person14 foaf:knows ken:person16 . ken:person16 foaf:knows ken:person18 . ken:person18 foaf:knows ken:person20 . ken:person20 foaf:knows ken:person20 . ken:person9 foaf:knows ken:person12 . ken:person12 foaf:knows ken:person15 . ken:person15 foaf:knows ken:person18 . ken:person18 foaf:knows ken:person9 .' } let $insert := sem:rdf-insert(sem:rdf-parse($node, "turtle"), "triplexml", (), (), ()) return () (: Calculate the shortest path :) xquery version '1.0-ml'; import module namespace op = 'http://marklogic.com/optic' at 'MarkLogic/optic.xqy'; let $ken := op:prefixer('http://example.org/kennedy') let $start := op:col('start') let $predicate := op:col('predicate') let $end := op:col('end') let $path := op:col('path') let $length := op:col('length') return op:from-triples((op:pattern($start, $predicate, $end))) => op:shortest-path($start, $end, $path, $length) => op:where(op:and(op:eq($start,$ken('person1')), op:eq($end, $ken('person19')))) => op:result()
(: Insert the data as in previous example :) (: Calculate the paths between two people which length is 5 :) xquery version '1.0-ml'; import module namespace op = 'http://marklogic.com/optic' at 'MarkLogic/optic.xqy'; let $ken := op:prefixer('http://example.org/kennedy') let $start := op:col('start') let $predicate := op:col('predicate') let $end := op:col('end') let $path := op:col('path') let $length := op:col('length') return op:from-sparql("select * where {?start ?predicate ?end.} ") => op:shortest-path('start','end','path','length') => op:where(op:eq($length, 5)) => op:result()
Stack Overflow: Get the most useful answers to questions from the MarkLogic community, or ask your own question.