How can I add an edge from one vertex to many vertices in one call? I know I can call the usual addEdge with a single vertex but I'd like to avoid having to call gremlin server for each edge I need to add as there may be a quite a few.
This works in version 3.2.3 but the V method is not available in version 3.0.1 so looking to replicate it another way.
// Get vertices I want to add edge to
g.V().has("id",within(["2","3","4"])).as("toV").
// Now get the vertex I want to add edge from
V("1").as("fromV").
// And add an edge between them
addE("likes").from("fromV").to("toV")
Here's the docs from TinkerPop 3.0.1 on the addEdge step, and you can find an example there for the syntax that you can use in Titan 1.0. You can test this out in the Gremlin Console.
graph = TitanFactory.open('inmemory'); g = graph.traversal()
g.addV('name', '1').addV('name', '2').addV('name', '3').addV('name', '4')
g.withSideEffect('a', g.V().has('name', within('2', '3', '4')).toList()).
V().has('name', '1').addOutE('likes', 'a')
Related
I am trying to port code from python NetworkX to R igraph. In NetworkX there is a function with the name has_path that finds if two vertices have a path. I want to find in an efficient way all the vertices of a graph that don't have an edge between them but they have a path.
I think you can use the code below to check if there is a path from vertex V1 to V2 (the graph can be directed or undirected)
c(!is.infinite(distances(g, V1, V2, mode = "out")))
If you need to check this repeatedly in an undirected graph, simply break it into connected components and check if both vertices are within the same component. This will be very efficient, as the components need to be found only once.
See the components function. It gives you a membership vector. You need to check if the position corresponding to the two vertices has the same value (same component index).
If the graph is directed, the simplest solution is the one posted by #ThomasIsCoding. This is perfectly fine for a one-time check. Speeding up repeated checks is more trouble and warrants its own question.
Using TinkerPop 3 Java APIs:
My graph looks like this:
james {risk: 'n'} --> chris {risk: 'n'} --> raj {risk: 'n'} --> joanne {risk: 'y'}
The edge label is 'travelledWith' and a property called 'pnrLocator'
Now, I want to traverse from james till a vertex where the risk is set to 'y'. Along the way, I'd want to collect the vertex and edge properties.
This is what I have which only works for vertex properties. How can I add a 'by' and collect the 'pnrLocator' too?
GraphTraversal<Vertex, ?> values =
g.traversal()
.V()
.has("personId", "james")
.repeat(out("travelledWith"))
.until(has("risk", "y"))
.limit(100)
.path()
.by("personId");
values.forEachRemaining(v -> System.out.println(v));
The path() step is going to output any elements Gremlin traverses given the steps you provided so, using the "modern" TinkerPop toy graph:
gremlin> g.V().repeat(out()).emit().path().by("name")
==>[marko,lop]
==>[marko,vadas]
==>[marko,josh]
==>[marko,josh,ripple]
==>[marko,josh,lop]
==>[josh,ripple]
==>[josh,lop]
==>[peter,lop]
I traversed out() which returns vertices so that's the only output I see in the path output. If I change my traversal a little to explicitly traverse the edges (i.e. out() to outE().inV()) then I can do this:
gremlin> g.V().repeat(outE().inV()).emit().path().by("name").by('weight')
==>[marko,0.4,lop]
==>[marko,0.5,vadas]
==>[marko,1.0,josh]
==>[marko,1.0,josh,1.0,ripple]
==>[marko,1.0,josh,0.4,lop]
==>[josh,1.0,ripple]
==>[josh,0.4,lop]
==>[peter,0.2,lop]
I'm able to find all of my tag vertex points which have an edge labeled tagged using:
gremlin> g.V().hasLabel('tag').inE().hasLabel('tagged')
==>e[eas0-109ds-e8l-y8oo][1691776-tagged->1597560]
==>e[ed5c-109ds-e8l-1181s][1691776-tagged->1736704]
Now, I would like to remove all of the tag vertices which do not have an edge labeled tagged. When I use this command to find these vertices:
gremlin> g.V().hasLabel('tag').inE().hasNot('label', 'tagged')
I get the error message No signature of method: org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal.hasNot() is applicable for argument types: (java.lang.String, java.lang.String) values: [label, tagged]
How would I phrase this query?
Use the not() filter step:
g.V().hasLabel('tag').not(inE('tagged')).drop()
I tried to create a self-loops edge in a OrientDB Graph through Java API.
graph.addEdge("class:hasChild",
graph.getVerticesOfClass(domain).iterator().next(),
graph.getVerticesOfClass(range).iterator().next(),
"hasChild");
From Orient Studio, I saw in SCHEMA that it creates the edge "hasChild" with 0 records.
When I displayed the graph with select from V, I didn't see the edge "hasChild", but it allow me to create it manually.
Why is this happening?
I need to create it by Java API
Try this:
Vertex v1 = graph.getVerticesOfClass(domain).iterator().next();
Vertex v2 = graph.getVerticesOfClass(range).iterator().next();
v1.addEdge("hasChild", v2);
i am using mxgraph to show the process. I need to select a edge programtically. The mxgraph provids selectEdges(true,false,false) which will select all the edges in the graph. But i need to select a particular edge in the graph.
Suppose if there is vertex1, vertex2 and vertex3. Each of these vertex is connected using edges. I need to select the edge between vertex2 and vertex3. Whether mxgraph provides any api to dod so? Please help me.
The mxGraph class hold a mxGraphSelectionModel instance, which is where you perform most of the selection operations. clear() and addCell(Object) will give you the result you require.
You should use GraphComponent.
Use graphComponent.getCellAt(x,y) ( x y are you mouse/click coordinates)
when you get the returned object, make sure it is an edge by checking the boolean isEdge().
So if you have the x, y programatically you can use the same process I described.
For example u can get the common edge of two vertices and it would be your edge.