@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>.
@prefix nepo: <http://neptune.aws.com/ontology/airroutes/>.
@prefix list: <http://www.w3.org/2000/10/swap/list#>.
@prefix math: <http://www.w3.org/2000/10/swap/math#>.
@prefix log: <http://www.w3.org/2000/10/swap/log#>.
@prefix var: <http://www.w3.org/2000/10/swap/var#>.
@prefix pd: <http://example.org/pd#>.

# connectivity
[ log:graph (
    [ log:triple ((var:from var:to var:visited var:length var:max) pd:route (var:from var:to))]
)] log:isImpliedBy [ log:graph (
    [ log:triple (var:length math:notGreaterThan var:max)]
    [ log:triple (var:from nepo:hasOutboundRouteTo var:to)]
    [ log:triple (var:visited list:notMember var:to)]
)].

[ log:graph (
    [ log:triple ((var:from var:to var:visited var:length var:max) pd:route var:route)]
)] log:isImpliedBy [ log:graph (
    [ log:triple (var:length math:notGreaterThan var:max)]
    [ log:triple (var:from nepo:hasOutboundRouteTo var:via)]
    [ log:triple (var:visited list:notMember var:via)]
    [ log:triple (var:newVisited list:firstRest (var:from var:visited))]
    [ log:triple ((var:length 1) math:sum var:newLength)]
    [ log:triple ((var:via var:to var:newVisited var:newLength var:max) pd:route var:newRoute)]
    [ log:triple (var:route list:firstRest (var:from var:newRoute))]
)].

# query: find routes from Manchester-Boston Regional Airport to Helsinki Vantaa Airport with at most 2 stopovers
[ log:graph (
    [ log:triple (var:source rdfs:label "Manchester-Boston Regional Airport")]
    [ log:triple (var:destination rdfs:label "Helsinki Vantaa Airport")]
    [ log:triple ((var:source var:destination () 0 2) pd:route var:airports)]
    [ log:triple ((var:city [ log:graph (
        [ log:triple (var:airports list:member var:airport)]
        [ log:triple (var:airport rdfs:label var:city)]
    )] var:route) log:collectAllIn var:scope)]
)] log:impliesAnswer [ log:graph (
    [ log:triple (pd:discovered pd:airroute var:route)]
)].
