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);
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.
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')
Consider a vertex (V1) in Titan Graph having few out going edges having labels L1,L2,L3 and L4. Now how to find all edges from V1 having labels either L1 or L2 using TitanVertexQuery.
I have tried using GremlinPipeline, it works but unable to achieve the same using TitanVertexQuery.
Regards,
Karthik
Pretty short answer:
v.query().direction(Direction.OUT).labels("L1", "L2").edges()
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.
I have a graph in Neo4j in which nodes represent random points in the plane, each node having the coordinates stored as properties (x and y, the value type is double).
I create the nodes and index them:
IndexManager index = graph.index();
Index<Node> nodesIndex = index.forNodes("points");
for (int i = 0; i < points.length; i++) {
Point p = points[i];
Node n;
Transaction tx = graph.beginTx();
try {
n = graph.createNode();
n.setProperty("x", p.getX());
n.setProperty("y", p.getY());
nodesIndex.add(n, "x", new ValueContext(p.getX()).indexNumeric());
nodesIndex.add(n, "y", new ValueContext(p.getY()).indexNumeric());
tx.success();
} finally {
tx.finish();
}
}
Now, what I need to do, is query for the nodes which are in a square area. So for example I made this query:
http://localhost:7474/db/data/index/node/points?query=x:[0.0 TO 3.0] AND y:[0.0 TO 3.0]
And this is the response:
Node
Properties
y 1.0
x 14.0
Node info
self /db/data/node/10
Node
Properties
y 1.0
x 2.0
Node info
self /db/data/node/7
Node
Properties
y 1.0
x 6.0
Node info
self /db/data/node/8
Node
Properties
y 1.0
x 7.0
Node info
self /db/data/node/9
[Etc...]
As you see it is not working. And I don't understand why (maybe I need to configure the index?).
Note that I haven't got to use Lucene. If there is a way to gather that information with Cypher (starting from a node centered in the square area) would be actually better, for I also need the relationships between the nodes found.
Additional informations
If that matters, the graph represents a Delaunay triangolation on a random set of points on the plane. In more abstract terms, I need to "extract" the entire subgraph that lays in a given area.
Any help is really appreciated!
I am afraid you can't do this via Cypher. There is no way Cypher can infer that you want to use a numeric Value context for this query (could probably be in some indexing meta info in future releases), and you need that to be able to query Lucene the way you want to. Easiest way over REST is probably to use Groovy, see custom sorting (same issue) at http://docs.neo4j.org/chunked/snapshot/gremlin-plugin.html#rest-api-send-an-arbitrary-groovy-script---lucene-sorting