I created a little metro map with RDF/XML and wonder, how to query the distance between two stops. I'm very new to SPARQL and don't know how to start.
"Distance" means, that I want to know, how many stations are between the two ones. Later, I want to calculate the duration, but that's another point.
Thats my first approach:
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX ex: <http://example.com>
SELECT (count(?mid) as ?distance) WHERE {
<http://example.com/StopD> ex:via* ?mid .
?mid ex:via+ <http://example.com/StopC> .
}
I think, that my query doesn't work because I'm using blank nodes? Doesn't work means, that I don't get the number of graphs that are between the two stops (like StopA and StopB). I have something like this in my mind: http://answers.semanticweb.com/questions/3491/how-can-i-calculate-the-length-of-a-path-between-2-graph-nodes-in-sparql/24609
Thats a sketch of my map. The numbers beside the lines represents the travel duration between two stations:
My RDF code describe each station and its neighbour stops with available lines and travel duration. At the first look it looks quite redundant, but I want to include one-direction routes (e.g. for buses) in the future, so I think it's ok for the first try.
RDF (download the file here: http://gopeter.de/misc/metro.rdf)
<?xml version="1.0"?>
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:ex="http://example.com/">
<rdf:Description rdf:about="http://example.com/StopA">
<ex:via rdf:parseType="Resource">
<ex:Stop rdf:resource="http://example.com/StopB" />
<ex:Line rdf:resource="http://example.com/Line1" />
<ex:Duration>2</ex:Duration>
</ex:via>
<ex:via rdf:parseType="Resource">
<ex:Stop rdf:resource="http://example.com/StopB" />
<ex:Line rdf:resource="http://example.com/Line2" />
<ex:Duration>7</ex:Duration>
</ex:via>
<ex:via rdf:parseType="Resource">
<ex:Stop rdf:resource="http://example.com/StopD" />
<ex:Line rdf:resource="http://example.com/Line4" />
<ex:Duration>2</ex:Duration>
</ex:via>
<ex:via rdf:parseType="Resource">
<ex:Stop rdf:resource="http://example.com/StopD" />
<ex:Line rdf:resource="http://example.com/Line2" />
<ex:Duration>6</ex:Duration>
</ex:via>
<ex:via rdf:parseType="Resource">
<ex:Stop rdf:resource="http://example.com/StopE" />
<ex:Line rdf:resource="http://example.com/Line1" />
<ex:Duration>1</ex:Duration>
</ex:via>
<ex:via rdf:parseType="Resource">
<ex:Stop rdf:resource="http://example.com/StopF" />
<ex:Line rdf:resource="http://example.com/Line4" />
<ex:Duration>3</ex:Duration>
</ex:via>
</rdf:Description>
<rdf:Description rdf:about="http://example.com/StopB">
<ex:via rdf:parseType="Resource">
<ex:Stop rdf:resource="http://example.com/StopA" />
<ex:Line rdf:resource="http://example.com/Line1" />
<ex:Duration>2</ex:Duration>
</ex:via>
<ex:via rdf:parseType="Resource">
<ex:Stop rdf:resource="http://example.com/StopA" />
<ex:Line rdf:resource="http://example.com/Line2" />
<ex:Duration>7</ex:Duration>
</ex:via>
<ex:via rdf:parseType="Resource">
<ex:Stop rdf:resource="http://example.com/StopC" />
<ex:Line rdf:resource="http://example.com/Line2" />
<ex:Duration>10</ex:Duration>
</ex:via>
<ex:via rdf:parseType="Resource">
<ex:Stop rdf:resource="http://example.com/StopF" />
<ex:Line rdf:resource="http://example.com/Line3" />
<ex:Duration>2</ex:Duration>
</ex:via>
</rdf:Description>
<rdf:Description rdf:about="http://example.com/StopC">
<ex:via rdf:parseType="Resource">
<ex:Stop rdf:resource="http://example.com/StopB" />
<ex:Line rdf:resource="http://example.com/Line2" />
<ex:Duration>10</ex:Duration>
</ex:via>
</rdf:Description>
<rdf:Description rdf:about="http://example.com/StopD">
<ex:via rdf:parseType="Resource">
<ex:Stop rdf:resource="http://example.com/StopA" />
<ex:Line rdf:resource="http://example.com/Line2" />
<ex:Duration>6</ex:Duration>
</ex:via>
<ex:via rdf:parseType="Resource">
<ex:Stop rdf:resource="http://example.com/StopA" />
<ex:Line rdf:resource="http://example.com/Line4" />
<ex:Duration>2</ex:Duration>
</ex:via>
<ex:via rdf:parseType="Resource">
<ex:Stop rdf:resource="http://example.com/StopF" />
<ex:Line rdf:resource="http://example.com/Line3" />
<ex:Duration>2</ex:Duration>
</ex:via>
</rdf:Description>
<rdf:Description rdf:about="http://example.com/StopE">
<ex:via rdf:parseType="Resource">
<ex:Stop rdf:resource="http://example.com/StopA" />
<ex:Line rdf:resource="http://example.com/Line1" />
<ex:Duration>1</ex:Duration>
</ex:via>
</rdf:Description>
<rdf:Description rdf:about="http://example.com/StopF">
<ex:via rdf:parseType="Resource">
<ex:Stop rdf:resource="http://example.com/StopA" />
<ex:Line rdf:resource="http://example.com/Line4" />
<ex:Duration>3</ex:Duration>
</ex:via>
<ex:via rdf:parseType="Resource">
<ex:Stop rdf:resource="http://example.com/StopB" />
<ex:Line rdf:resource="http://example.com/Line3" />
<ex:Duration>2</ex:Duration>
</ex:via>
<ex:via rdf:parseType="Resource">
<ex:Stop rdf:resource="http://example.com/StopD" />
<ex:Line rdf:resource="http://example.com/Line3" />
<ex:Duration>2</ex:Duration>
</ex:via>
</rdf:Description>
</rdf:RDF>