Gremlin: Add an edge and drop an edge in single gremlin query - datastax

how to add an edge and dropping an edge to same vertex type in single gremlin query.
say we have two types of vertex types A --is--> B now i have vertex which is connected to some other vertex of B.
I want to update the A's vertex to some other vertex of B.
currently, i am dropping the current B vertex and then adding the new vertex .

You can do it in one traversal using a sideEffect():
gremlin> g.V().has('person','name','marko').as('m').
......1> outE('knows').
......2> filter(inV().has('person','name','vadas')).
......3> sideEffect(drop()).
......4> V().has('person','name','peter').
......5> addE('knows').from('m')
==>e[13][1-knows->6]
At line 1 we basically identify the edge we want to get rid of (i.e. a "knows" edge from "marko" to "vadas") and we drop() that on line 3. At line 4 we lookup the vertex to whom we wish to connect "marko" to now and then add the edge at line 5.

Related

How to obtain accurate value of segment-distances and segment-weights for creating bendpoints?

I am trying to create edges with bends for a layout. Normally, I am using a haystack edge in the graph but whenever an edge bend has to be created, the curve-style of the edge is changed to segments. Currently, I am only creating edges with single bends. I have tried the code provided in this post but it is not creating proper edges. Currently, I am using the code from cytoscape.js-edge-editing, since it is creating better results.
The main problem is that the segment-distances values which cause the bendpoint to be created at the wrong location. Since, the functions in the above provided codes are not creating proper bendpoints, what is the right way to go about this?
A sample problem is as shown:
An edge bend has to created created in the edge from n12 to n15 where n12 is the source. The values of segment-distances and segment-weights are shown in the console. Having a positive value of segment-distances creates the bendpoint at the wrong position. It was actually supposed to be to the right of n12 and to the top of n15.
Whereas in another scenario, as shown in the following figure, an edge bend has to be created in the edge from n3 and n2. And the positions of these nodes are quite similar to n12 and n15 wrt each other. Their edge is given the same values of segment-distances and segment-weights as for the edge in the previous figure. And yet, the bendpoint is created (not accurately) but almost near to the expected location. Whereas the same value of segment-distance creates the bendpoint at the opposite location in the previous scenario.
I do not understand why this is happening. Can someone please guide me as how to solve this problem?
Refer to edge-distances:
edge-distances : With value intersection (default), the line from source to target for segment-weights is from the outside of the source node’s shape to the outside of the target node’s shape. With value node-position, the line is from the source position to the target position. The node-position option makes calculating edge points easier — but it should be used carefully because you can create invalid points that intersection would have automatically corrected.
https://js.cytoscape.org/#style/segments-edges
The intersection point is different from the node centre point (position).
If you want right-angled edges, you should probably just use taxi edges: https://js.cytoscape.org/#style/taxi-edges

How to test if a vertex has been deleted in a polyhedron?

Specially, the problem i am trying to solve is deleting points that are very close together in a closed polyhedron.
I start first by collecting all the halfedges that are very short in distance (which implies that two points are very close together)
After which i go through the list of halfedges and start deleting the vertices using "erase_center_vertex".
The problem that comes is such that I do not know how to test if a vertex has already been deleted .. so that if some other halfedges references it again and tries to delete it, an error is prompted..
i tried the following....
if ( my_halfedge != Halfedge_handle() ) erase_center_vertex(my_halfedge);
if ( my_halfedge->vertex() != vertex_handle() ) erase_center_vertex(my_halfedge);
In all case, although the vertex is already deleted from the polyhedron, but somehow the halfedge pointing to it still exist, and pointing to a valid vertex (which should have been deleted).
Question : How to test if a vertex or halfedge has been removed?

Why I can't choose only a number as variable's name in titan graph database

I've worked with TinkerFactory.createModern & TinkerFactory.createTheCrew and I've noticed only numbers have been chosen as variables if I'm not mistaking...what I mean is that by "g.V(1)" you can reach Vertex number 1 so I want to do the same but i get the error shown in the picture.
for instance, I want to reach 'V[5]' by typing "g.V(5)"
This is the Picture of the error that I get
The numbers you refer to in g.V(1) are the ids which are automatically assigned to each vertex. So when you say g.V(1) you are asking for the vertex with ID 1. Which is not necessarily the first vertex. Titan uses quite large numbers for example
The error you are having is a different issue though. Variables cannot start with number. They must start with a letter. So do this instead:
v1 = graph.addVertex('name', 'something');

How to find the exact points that an edge connects in jgraphx?

For an edge in jgraphx which has its geometry set to relative, there are no points provided as they are derived from the source and target for the edge.
However, the points found there are the top left corner of the objects: what I would like to know is if there is a way to get the exact points in which the edge connects to the source and target vertex rather than just the position or center point of the object.
Try graph.getView().getPerimeterPoint(mxCellState, mxPoint...)
Provide as cell state your local cell (mxGraphView.getState(myLocalCell)) and as point the distant center point of your other cell linked by the edge (you may compute it from its geometry X, Y, Width and Height).
An jGraphx can be exported to svg format. Maybe the drawCell() in the com.mxgraph.canvas.mxSvgCanvas can help you out. It is the case when shape.equals(mxConstants.SHAPE_LINE) and how the M and L commands are being calculated.

connect line between two boxes avoiding passing others

I have several boxes (x,y,width,height) randomly scattered around, and some of them need to be linked from point (x1,y1) in box1 to point (x2,y2) in box2 by drawing a line. I am trying to figure a way to make such line avoid passing through any other boxes (other than box1 and box2) by drawing several straight interconnected lines to go around any box in the way (if it is not possible to go with one straight line). The problem is that I don't know an algorithm for such thing (let alone having a technical/common name for it). Would appreciate any help in the form of algorithm or expressed ideas.
Thanks
Assuming that the lines can't be diagonal, here's one simple way. It's based on BFS and will also find the shortest line connecting the points:
Just create a graph, containing one vertex for each point (x, y) and for each point the edges:
((x,y),(x+1,y)) ((x,y),(x-1,y)) ((x,y),(x,y+1)) ((x,y),(x,y-1))
But each of this edges must be present only if it doesn't overlap a box.
Now just do a plain BFS from point (x1,y1) to (x2,y2)
It's really easy to obtain also diagonal lines the same way but you will need 8 edges for each vertex, that are, in addition to the previouses 4:
((x,y),(x-1,y+1)) ((x,y),(x-1,y-1)) ((x,y),(x+1,y-1)) ((x,y),(x+1,y+1))
Still, each edge must be present only if it doesn't overlap a box.
EDIT
If you can't consider space divided into a grid, here's another possibility, it won't give you the very shortest path, though.
Create a graph, in which each box is a vertex and has an edge to any other box that can be reached without the line to overlap a third box. Now find the shortet path using dijkstra between box1 and box2 containing the two points.
Now consider each box to have a small countour that doesn't overlap any other box. This way you can link the entering and the exiting point of each box in the path found through dijistra, passing through the countour.
Put all (x,y) coords of the corners of the boxes in a set V
Add the start- and end coordinates to V.
Create a set of edges E connecting each corner that does not cross any box-side (except for the diagonals in the boxes).
How to check if a line crosses a box side can be done with this algorithm
Now use a path-finding algorithm of your choice, to find a path in the graph (V, E).
If you need a simple algorithm that finds the shortest path, just go with a BFS.
(This will produce a path that goes along the sides of some boxes. If this is undesirable, you could in step 1 put the points at some distance delta from the actual corners.)
If the edges may not be diagonal:
Create a large grid of lines that goes between the boxes.
Throw away the grid-edges that cross a box-side.
Find a path in the grid using a path-finding algorithm of your choice.