Count nodes in SPARQL property path to find nearest node of type

48 views Asked by At

Image of example graph

I have an RDF graph, representing duct networks. All components are connected with the symmetric property exchangesFluidWith (see picture for example). For all air terminals, I would like to find the nearest Mechanical Damper, no matter if it is 5 nodes away or 100 nodes away. I suppose property paths are the way to go, but is there a way to count the number of steps and choose the one with the fewest? I thought about introducing an intermediary node between the damper and the air terminal and sort by count(?intermediary), but since the exchangesFluidWith is symmetric, all components in the network will be intermediary, as I understand.

Any suggestions?

PREFIX ex: <https://example.com/ex#>
select ?damper where { 
    ?airTerminal a ex:AirTerminal .
    ?airTerminal ex:exchangesFluidWith* ?damper .
    ?damper a ex:MechanicalDamper .
    ?airTerminal fso:exchangesFluidWith* ?intermediary .
    ?intermediary fso:exchangesFluidWith* ?damper .
    ?damper a fso:MechanicalDamper .
} group by ?damper order by (count(?intermediary)) limit 1
0

There are 0 answers