Gremlin Query - Repeat with conditions on edges

1.8k Views Asked by At

I have a gremlin graph structure of a train network. The Vertices are stations, with station id and name as properties. The Edges are tracks with route, kms and stop sequence.

There are around 840 stops and 24 routes connecting them. As we can imagine, these stops are connected by the routes and any two stops can be connected by more than one route. There are cyclic routes as well, i.e. start and stop at the same stop. Some stops are both origin and destination of different routes.

The question, I am trying to ask is, if a given stop is out of commission, how can I get the impacted sources and destinations. i.e. Get the routes going through it and get the source and destination of the given routes.

Seems to be a simple question, but I do not know gremlin enough to answer this. :)

So far, the below one is the closest I have come to the answer. Given the station, the route and the final terminating station, I am able to get all the stations in between.

The ideal situation is, given the station, give all impacted routes, with route to final destination and routes from source.

I have not been able to connect vertices based on property of edges. Like connect vertices based on route without giving the value. Also, terminating the repeat at the leaf node. i.e. No further destinations to connect. If I could remove the until hasID, that would be a win. :)

g.V().has("id", "17892")
     .repeat(outE().has("route","3-96-mjp-1").inV())
     .until(hasId("6147"))
     .path()

Sample structure below:

g.V().has("id", "17892").outE().path() -->>  "objects": [
  {
    "id": "17892",
    "label": "stop",
    "type": "vertex",
    "properties": {
      "stop_id": [
        {
          "id": "f2eb562e-e362-427c-91ef-02769cfda721",
          "value": "17892"
        }
      ],
      "stop_name": [
        {
          "id": "b873d980-8d07-44e7-aa45-5be1afad4a63",
          "value": "10-Albert St/Nicholson St (East Melbourne)"
        }
      ]
    }
  },
  {
    "id": "242fa97f-134d-439a-b8ab-0b4d8b8da9bc",
    "label": "nextStop",
    "type": "edge",
    "inVLabel": "stop",
    "outVLabel": "stop",
    "inV": "17893",
    "outV": "17892",
    "properties": {
      "distance": 125,
      "route": "3-35-mjp-1",
      "stopSeq": 17
    }
  }
]

I am doing this in Azure CosmosDB Graph.

1

There are 1 best solutions below

0
On

Without a sample graph I'm really just trying to make a good guess:

g.V().has("id", "17892").
  bothE("nextStop").as("e").outV().
  until(__.not(inE("nextStop").
                 where(eq("e")).by("route").
                 where(lt("e")).by("stopSeq"))).
    repeat(inE("nextStop").
             where(eq("e")).by("route").
             where(lt("e")).by("stopSeq").as("e").outV()).as("origin").
  outE("nextStop").as("e").inV().
  until(__.not(outE("nextStop").
                 where(eq("e")).by("route").
                 where(gt("e")).by("stopSeq"))).
    repeat(outE("nextStop").
             where(eq("e")).by("route").
             where(gt("e")).by("stopSeq").as("e").inV()).
  path().
    from("origin").
    by("stop_id").
    by("distance")

This traversal is supposed to find all the origins and from there traverse to the final destination stops and emit those paths.