1
votes

I try to develop a routing system, based on data in a Neo4j 3.0.4 Database. The graph contains multiple stops. Some of these stops are scheduled like bus stops, train stops, but not all of them. Therefore, the are connected to a schedule node. Each schedule node is connected to an offer.

A subgraph looks like this:

Example_Graph

My question is: How can I create a query that returns this subgraph? Up to now I wrote this query in cypher:

MATCH (from:Stop{poiId:'A'}), (to:Stop{poiId:'Z'}) , 
path = allShortestPaths((from)-[r*]->(to))
RETURN path

This results in all shortest paths from stop A to stop Z. Between A and Z are to more stops, that are included in the returned path. I want to get for all stops the related schedules and for these schedules the related offers.

Furthermore it would be great if it would be possible to use constrains, based on the schedule node, e. g. allShortestPath from A to Z where filter(time in schedule.monday WHERE x > 1100).

If that is not possible, is it possilbe to create a new query with this constrain based on the previous query?

EDIT1: Further information: In the schedules are departure times for each stop. I want to calculate based on a desired departure time (alternatively a desired arrival time) the full travel time and get the 5 best connections (less time). E.g. I want to start at 7:00: the switch relation has cost time of 2. so check schedule 1 if there is a departure after 7:02. if yes, take the first departure after 7:02. The connected_by relation has a cost time of 12 min. the last switch_to relation has no cost time. So I will arrive at 07:14. Note: If I have to switch the service line during travelling, I have to check the schedule again. If the schedule fits not the desired time windows, exclude it from the result. I want to get the 5 best paths (based on travel time or arrival time), the number of hops is not important. If there is a connection with e. g. 6 stops, but with less travel time (or earlier arrival time) that prefer this one. I know this is a difficult and big problem, but I have no idea how to start... If there is a way to do this via REST (or if not in Java) I would be glad for each hint!

1
The output will be in rows and columns, though the values returned can be lists of related results. It would help to have some idea of what you're looking for in the format of your returned data. For example, would you rather have one result row per path, with lists of maps consisting of each stop and its related schedule and offer? Or do you want a row per stop/schedule/offer? Or something else?InverseFalcon
I will use this cypher via rest api, so I will get JSON Objects, but the idea is to see a row for each seperate path with all stops and relationships between them. The "stop column" could include more information: {stop1 {attributes}}, {switch_to{attributes}}, {stop2 {attributes}, schedule{attributes}, offer{attributes}}...Stefan

1 Answers

2
votes

You can use the UNWIND construct in Cypher to get the nodes of a path and use OPTIONAL MATCH to look for schedules & offers.

I created a sample dataset:

CREATE
  (offer: Offer),
  (sch1: Schedule),
  (sch2: Schedule),
  (stop1: Stop {name: "stop1"}),
  (stop2: Stop {name: "stop2"}),
  (stop3: Stop {name: "stop3"}),
  (stop4: Stop {name: "stop4"}),
  (stop1)-[:SWITCH_TO]->(stop2),
  (stop2)-[:CONNECTED_BY]->(stop3),
  (stop3)-[:SWITCH_TO]->(stop4),
  (stop2)-[:SCHEDULED_BY]->(sch1),
  (stop3)-[:SCHEDULED_BY]->(sch2),
  (sch1)-[:OFFERED_BY]->(offer),
  (sch2)-[:OFFERED_BY]->(offer)

To get the subgraph, you can issue this query:

MATCH
  (from:Stop {name:'stop1'}), (to:Stop {name:'stop4'}), 
  path = allShortestPaths((from)-[r*]->(to))
UNWIND nodes(path) AS stopNode
OPTIONAL MATCH (stopNode)-[sb:SCHEDULED_BY]->(schedule:Schedule)-[ob:OFFERED_BY]-(offer:Offer)
RETURN stopNode, sb, ob, schedule, offer

Using this approach, the edges in r are dropped, so it does not return the whole subgraph. The visualization on Neo4j's web UI adds those edges, so the result looks like this:

enter image description here

Anyways, I hope the post contains useful information - let me know how it works for you.