how can we get 2 vertexes path? - gremlin

Now I have the query below and I would like to get all the edge and It is interesting why we got 2 same path and I want to get the path detail. How can I implement?
Vertex fromNode = g.V().has('name', 'alice').next();Vertex toNode = g.V().has('name', 'bobby').next();g.V(fromNode).repeat(both().simplePath()).until(is(toNode)).path()
==>[v[4224],v[40964296]]
==>[v[4224],v[40964296]]
==>[v[4224],v[4144],v[40964256],v[4096],v[40964296]]
We have the Graph below.
gremlin> a = graph.addVertex("name", "alice")
==>v[4208]
gremlin> b = graph.addVertex("name", "bobby")
==>v[40968424]
gremlin> c = graph.addVertex("name", "cindy")
==>v[4192]
gremlin> d = graph.addVertex("name", "david")
==>v[40972520]
gremlin> e = graph.addVertex("name", "eliza")
==>v[40964272]
gremlin> a.addEdge("rates",b,"tag","ruby","value",9)
==>e[2ry-38w-azv9-oe3fs][4208-rates->40968424]
gremlin> b.addEdge("rates",c,"tag","ruby","value",8)
==>e[odzq5-oe3fs-azv9-38g][40968424-rates->4192]
gremlin> c.addEdge("rates",d,"tag","ruby","value",7)
==>e[170-38g-azv9-oe6lk][4192-rates->40972520]
gremlin> d.addEdge("rates",e,"tag","ruby","value",6)
==>e[oe04d-oe6lk-azv9-oe08g][40972520-rates->40964272]
gremlin> a.addEdge("rates",e,"tag","java","value",9)
==>e[366-38w-azv9-oe08g][4208-rates->40964272]
gremlin> g.E().values("tag")
==>ruby
==>ruby
==>ruby
==>ruby
==>java
gremlin> graph.tx().commit()
I would like to get the path detail like below:
==>bobby=[v[0], e[10][0-rates->2], v[2]]
==>cindy=[v[0], e[10][0-rates->2], v[2], e[11][2-rates->4], v[4]]
==>david=[v[0], e[10][0-rates->2], v[2], e[11][2-rates->4], v[4], e[12][4-rates->6], v[6]]

You just need to specify that you want the edges as well by traversing over them explicitly:
gremlin> g.V(fromNode).repeat(bothE().otherV().simplePath()).until(is(toNode)).path()
==>[v[0],e[10][0-rates->2],v[2]]
==>[v[0],e[14][0-rates->8],v[8],e[13][6-rates->8],v[6],e[12][4-rates->6],v[4],e[11][2-rates->4],v[2]]

Related

Why can't I drop vertex properties?

I can't drop any properties, they're just stubbornly remaining. What am I doing wrong?
gremlin> g.V(28712).valueMap()
==>{entity_id=[256a631b-3c19-49b7-84f3-01911d66c744], prop=[0]}
gremlin> g.V().properties('prop').drop()
gremlin> g.V(28712).valueMap()
==>{entity_id=[256a631b-3c19-49b7-84f3-01911d66c744], prop=[0]}
gremlin> g.V().properties('prop').drop().iterate()
gremlin> g.V(28712).valueMap()
==>{entity_id=[256a631b-3c19-49b7-84f3-01911d66c744], prop=[0]}
gremlin> g.V(28712).properties('prop').drop()
gremlin> g.V(28712).valueMap()
==>{entity_id=[256a631b-3c19-49b7-84f3-01911d66c744], prop=[0]}
gremlin> g.V(28712).properties('prop').drop().iterate()
gremlin> g.V(28712).valueMap()
==>{entity_id=[256a631b-3c19-49b7-84f3-01911d66c744], prop=[0]}
What's doubly strange is that it seems to show the drop was successful (there appears to be no vertices with the property prop), but calling valueMap on a specific vertex shows otherwise.
gremlin> g.V().properties('prop').drop()
gremlin> g.V().has('prop').count()
==>0
gremlin> g.V().has('prop', 0).count()
==>0
gremlin> g.V(28712).valueMap()
==>{entity_id=[256a631b-3c19-49b7-84f3-01911d66c744], prop=[0]}

How can I use until in janusgraph?

gremlin> a = graph.addVertex("name", "alice")
gremlin> b = graph.addVertex("name", "bobby")
gremlin> c = graph.addVertex("name", "cindy")
gremlin> d = graph.addVertex("name", "david")
gremlin> e = graph.addVertex("name", "eliza")
gremlin> a.addEdge("rates",b,"tag","ruby","value",9)
gremlin> b.addEdge("rates",c,"tag","ruby","value",8)
gremlin> c.addEdge("rates",d,"tag","ruby","value",7)
gremlin> d.addEdge("rates",e,"tag","ruby","value",6)
gremlin> e.addEdge("rates",a,"tag","java","value",9)
g.V().has('name', 'alice').repeat(out()).times(6).cyclicPath().path().by('name')
I want to end with alice node. and I want to repeat all the step not want to specify time as 6. The requirement is I want to get all the loop from alice or get all the loops from the graph.
You can refer to the Cycle Detection section in the TinkerPop Recipes - it adapts fairly easily to your sample graph:
gremlin> g.V().has('name', 'alice').as('a').
......1> repeat(out().simplePath()).
......2> emit(loops().is(gt(1))).
......3> both().where(eq('a')).
......4> path().
......5> by('name').
......6> dedup().
......7> by(unfold().order().dedup().fold())
==>[alice,bobby,cindy,david,eliza,alice]

Gremlin CSV parse creating extra vertices

My code should read the 4 columns, split them into vertices for the first 2 columns, and edge properties for the last two columns.The CSV file has 33 unique vertices in 37 lines of data. What I don't understand is why I get 74 vertices instead and 37 edges. Interestingly enough, if I omit the addE statment I just get 37 vertices.
Obviously the property portion hasn't been included as I've been trying to resolve my current issues.
1\t2\tstep\tcmp
2\t3\tconductor\tna
3\t4\tswitch\tZ300
\t for tab
etc.
My code is:
graph = TinkerGraph.open()
graph.createIndex('myId', Vertex.class)
g = graph.traversal()
getOrCreate = { myid ->
p = g.V('myId', myid)
if (!p.hasNext())
{g.addV('connector').property('myId',myid) }
else
{p.next()}
}
new File('Continuity.txt').eachLine {
if (!it.startsWith("#")){
def row = it .split('\t')
def fromVertex = getOrCreate(row[0])
def toVertex = getOrCreate(row[1])
g.addE("connection").from(fromVertex).to(toVertex).iterate()
}
}
There's at least on problem in the code that I see. In this line:
p = g.V('myId', myid)
you are telling gremlin to find vertices with ids "myId" and whatever the value of the variable myid is. You instead want:
p = g.V().has('myId', myid)
The syntax you were using is from TinkerPop 2.x. I tested your code this way with some other changes and it seems to work properly now:
gremlin> graph = TinkerGraph.open()
==>tinkergraph[vertices:0 edges:0]
gremlin> graph.createIndex('myId', Vertex.class)
gremlin> g = graph.traversal()
==>graphtraversalsource[tinkergraph[vertices:0 edges:0], standard]
gremlin> getOrCreate = { myid ->
......1> if (!g.V().has('myId', myid).hasNext())
......2> g.addV('connector').property('myId',myid)
......3> else
......4> g.V().has('myId', myid)
......5> }
==>groovysh_evaluate$_run_closure1#29d37757
gremlin> g.addE('connection').from(getOrCreate(1)).to(getOrCreate(2)).iterate()
gremlin> g.addE('connection').from(getOrCreate(1)).to(getOrCreate(2)).iterate()
gremlin> g.V()
==>v[0]
==>v[2]
gremlin> g.E()
==>e[4][2-connection->0]
==>e[5][2-connection->0]

TinkerPop 3: Gremlin query to group count by edge direction

I have a vertex id to start with and wanted to get counts of in and out edges.
g.traversal().V().has("__id", "1234").groupCount().by(Direction.BOTH)
As .by() step wont accept Direction type., is there any alternative way?
You can do this nicely with project step:
gremlin> graph = TinkerFactory.createModern()
==>tinkergraph[vertices:6 edges:6]
gremlin> g = graph.traversal()
==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
gremlin> g.V().has('name','marko').
project('out','in').
by(outE().count()).
by(inE().count())
==>[out:3,in:0]
For TinkerPop 3.0.x, which didn't have project you could do:
gremlin> g.V().has('name','marko').as('out','in').
select('out','in').
by(outE().count()).
by(inE().count())
==>[out:3,in:0]

find all paths between 2 vertices by only considering vertex where propertyname = value

I am having a a traversal use case as follows .Does orientDb supports it?
1)Find all paths from node1 to node2 .ONLY TRAVERSE those nodes which has property xyz="val1"
2)Find shortest path .ONLY TRAVERSE those nodes which has property xyz="val1"
3)Find longest path .ONLY TRAVERSE those nodes which has property xyz="val1"
abstract class TraversGraph{
public Path getPath(Node src,Node dest, Propery property,Value value);
}
Note :Please note the condition which i mentioned in caps
You can use Gremlin to get paths between two nodes. You can firstly find all the paths, so the shortest and the longest are included in all these paths.
Here is an example:
As you can see in the image, from A to B there are three paths(A->B, A->F->C->B, A->E->D->C->B), but node F doesn't have property "xyz" with value "val1", so this paths should not be included.
Code:
gremlin> g = new TinkerGraph()
==>tinkergraph[vertices:0 edges:0]
gremlin> a = g.addVertex(null,[name: "A", xyz: "val1"])
==>v[0]
gremlin> b = g.addVertex(null,[name: "B", xyz: "val1"])
==>v[1]
gremlin> c = g.addVertex(null,[name: "C", xyz: "val1"])
==>v[2]
gremlin> d = g.addVertex(null,[name: "D", xyz: "val1"])
==>v[3]
gremlin> e = g.addVertex(null,[name: "E", xyz: "val1"])
==>v[4]
gremlin> f = g.addVertex(null,[name: "F"])
==>v[5]
gremlin> g.addEdge(a, b, "KNOWS")
==>e[6][0-KNOWS->1]
gremlin> g.addEdge(a, e, "KNOWS")
==>e[7][0-KNOWS->4]
gremlin> g.addEdge(a, f, "KNOWS")
==>e[8][0-KNOWS->5]
gremlin> g.addEdge(f, c, "KNOWS")
==>e[9][5-KNOWS->2]
gremlin> g.addEdge(e, d, "KNOWS")
==>e[10][4-KNOWS->3]
gremlin> g.addEdge(d, c, "KNOWS")
==>e[11][3-KNOWS->2]
gremlin> g.addEdge(c, b, "KNOWS")
==>e[12][2-KNOWS->1]
And traversel between node A and node B(Here we do not filter the property "xyz"), so we get three paths:
gremlin> a.out('KNOWS').loop(1){it.loops<100}{true}.has('name', 'B').path{it.name}
==>[A, B]
==>[A, F, C, B]
==>[A, E, D, C, B]
And add the filter of property "xyz"
gremlin> a.out('KNOWS').loop(1){it.loops<100 && it.object.hasNot('xyz', null)}{true}.has('name', 'B').path{it.name}
==>[A, B]
==>[A, E, D, C, B]
Thus we get the shortest path: [A, B]
and the longest path: [A, E, D, C, B]
English is not my mother language, so if any confusion, feel free to contact me.

Resources