Skip to content

Instantly share code, notes, and snippets.

@dkuppitz
Last active November 19, 2016 09:18
Show Gist options
  • Save dkuppitz/60c99aa693956b7c6e9bba4fa64e47dc to your computer and use it in GitHub Desktop.
Save dkuppitz/60c99aa693956b7c6e9bba4fa64e47dc to your computer and use it in GitHub Desktop.
// RULES:
//
// * only follow edges with the same speed values
// * if there are multiple edges to the same vertex, pick the one with the smallest difference between departure time (from outV) and arrival time (from inV)
// * ignore negative differences
//
graph = TinkerGraph.open()
v0 = graph.addVertex(id, 0, "code", "0")
v1 = graph.addVertex(id, 1, "code", "1")
v2 = graph.addVertex(id, 2, "code", "2")
v3 = graph.addVertex(id, 3, "code", "3")
v0.addEdge("tsw", v1, "speed", "2", "arrTime", 20l, "depTime", 10l)
v0.addEdge("tsw", v1, "speed", "1", "arrTime", 10l, "depTime", 5l)
v1.addEdge("tsw", v2, "speed", "1", "arrTime", 15l, "depTime", 9l)
v1.addEdge("tsw", v2, "speed", "1", "arrTime", 20l, "depTime", 17l)
v2.addEdge("tsw", v3, "speed", "1", "arrTime", 30l, "depTime", 25l)
g = graph.traversal()
g.withSack(0).V().outE("tsw").as("e").inV().emit().repeat(
flatMap(
outE("tsw").filter(__.as("edge").select(last, "e").where(eq("edge")).by("speed")).
group().by(inV()).
by(project("curr","time").by().
by(sack(assign).by("depTime").select(last, "e").sack(minus).by("arrTime").sack()).
filter(select("time").is(gte(0))).fold()).
select(values).unfold().order(local).by(select("time")).limit(local, 1).select("curr")
).as("e").inV().simplePath()
).times(20).map(union(select(last, "e").by("speed"), path().by(id).by(valueMap("arrTime","depTime"))).fold())
gremlin> graph = TinkerGraph.open()
==>tinkergraph[vertices:0 edges:0]
gremlin>
gremlin> v0 = graph.addVertex(id, 0, "code", "0")
==>v[0]
gremlin> v1 = graph.addVertex(id, 1, "code", "1")
==>v[1]
gremlin> v2 = graph.addVertex(id, 2, "code", "2")
==>v[2]
gremlin> v3 = graph.addVertex(id, 3, "code", "3")
==>v[3]
gremlin>
gremlin> v0.addEdge("tsw", v1, "speed", "2", "arrTime", 20l, "depTime", 10l)
==>e[4][0-tsw->1]
gremlin> v0.addEdge("tsw", v1, "speed", "1", "arrTime", 10l, "depTime", 5l)
==>e[5][0-tsw->1]
gremlin> v1.addEdge("tsw", v2, "speed", "1", "arrTime", 15l, "depTime", 9l)
==>e[6][1-tsw->2]
gremlin> v1.addEdge("tsw", v2, "speed", "1", "arrTime", 20l, "depTime", 17l)
==>e[7][1-tsw->2]
gremlin> v2.addEdge("tsw", v3, "speed", "1", "arrTime", 30l, "depTime", 25l)
==>e[8][2-tsw->3]
gremlin>
gremlin> g = graph.traversal()
==>graphtraversalsource[tinkergraph[vertices:4 edges:5], standard]
gremlin>
gremlin> g.withSack(0).V().outE("tsw").as("e").inV().emit().repeat(
......1> flatMap(
......2> outE("tsw").filter(__.as("edge").select(last, "e").where(eq("edge")).by("speed")).
......3> group().by(inV()).
......4> by(project("curr","time").by().
......5> by(sack(assign).by("depTime").select(last, "e").sack(minus).by("arrTime").sack()).
......6> filter(select("time").is(gte(0))).fold()).
......7> select(values).unfold().order(local).by(select("time")).limit(local, 1).select("curr")
......8> ).as("e").inV().simplePath()
......9> ).times(20).map(union(select(last, "e").by("speed"), path().by(id).by(valueMap("arrTime","depTime"))).fold())
==>[2,[0,[depTime:10,arrTime:20],1]]
==>[1,[0,[depTime:5,arrTime:10],1]]
==>[1,[0,[depTime:5,arrTime:10],1,[depTime:17,arrTime:20],2]]
==>[1,[0,[depTime:5,arrTime:10],1,[depTime:17,arrTime:20],2,[depTime:25,arrTime:30],3]]
==>[1,[1,[depTime:9,arrTime:15],2]]
==>[1,[1,[depTime:9,arrTime:15],2,[depTime:25,arrTime:30],3]]
==>[1,[1,[depTime:17,arrTime:20],2]]
==>[1,[1,[depTime:17,arrTime:20],2,[depTime:25,arrTime:30],3]]
==>[1,[2,[depTime:25,arrTime:30],3]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment