1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package eu.ehri.project.models.utils;
21
22 import com.tinkerpop.blueprints.Direction;
23 import com.tinkerpop.blueprints.Edge;
24 import com.tinkerpop.blueprints.Element;
25 import com.tinkerpop.blueprints.Vertex;
26 import com.tinkerpop.gremlin.java.GremlinPipeline;
27 import com.tinkerpop.pipes.PipeFunction;
28 import com.tinkerpop.pipes.branch.LoopPipe;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32
33
34
35
36 public final class JavaHandlerUtils {
37
38 public static final Logger logger = LoggerFactory.getLogger(JavaHandlerUtils.class);
39
40 private static final int LOOP_MAX = 20;
41
42
43
44
45
46
47
48 private static <S> PipeFunction<LoopPipe.LoopBundle<S>, Boolean> maxLoopFuncFactory(final int maxLoops) {
49 return vertexLoopBundle -> vertexLoopBundle.getLoops() < maxLoops;
50 }
51
52
53
54
55 public static final PipeFunction<LoopPipe.LoopBundle<Vertex>, Boolean> defaultMaxLoops
56 = maxLoopFuncFactory(LOOP_MAX);
57
58
59
60
61 public static final PipeFunction<LoopPipe.LoopBundle<Vertex>, Boolean> noopLoopFunc
62 = vertexLoopBundle -> true;
63
64
65
66
67
68
69
70
71 public static <S, E> void cacheCount(Element element, GremlinPipeline<S, E> pipe, String propName) {
72 element.setProperty(propName, pipe.count());
73 }
74
75
76
77
78
79
80
81
82
83
84 public static boolean addSingleRelationship(Vertex from, Vertex to, String label) {
85 if (!from.equals(to)) {
86 for (Edge edge : from.getEdges(Direction.OUT, label)) {
87 if (edge.getVertex(Direction.IN).equals(to)) {
88 logger.warn("Attempting to add relationship '{}' that already exists: {}", label, to);
89 return false;
90 } else {
91 edge.remove();
92 logger.warn("Removed prior '{}' relationship added in single mode: {}",
93 label, from);
94 }
95 }
96 from.addEdge(label, to);
97 return true;
98 } else {
99 logger.warn("Attempt to add self-referential '{}' relationship " +
100 "where single relationship specified: {}",
101 label, to);
102 return false;
103 }
104 }
105
106
107
108
109
110
111
112
113
114 public static boolean addUniqueRelationship(Vertex from, Vertex to, String label) {
115 for (Edge edge : from.getEdges(Direction.OUT, label)) {
116 if (edge.getVertex(Direction.IN).equals(to)) {
117 return false;
118 }
119 }
120 from.addEdge(label, to);
121 return true;
122 }
123
124
125
126
127
128
129
130
131
132
133 public static boolean hasRelationship(Vertex from, Vertex to, String... labels) {
134 for (Edge edge : from.getEdges(Direction.OUT, labels)) {
135 if (edge.getVertex(Direction.IN).equals(to)) {
136 return true;
137 }
138 }
139 return false;
140 }
141
142
143
144
145
146
147
148
149
150 public static boolean removeAllRelationships(Vertex from, Vertex to, String... labels) {
151 int removed = 0;
152 for (Edge edge : from.getEdges(Direction.OUT, labels)) {
153 if (edge.getVertex(Direction.IN).equals(to)) {
154 edge.remove();
155 removed++;
156 }
157 }
158 return removed > 0;
159 }
160
161
162
163
164
165
166
167
168
169 public static boolean hasEdge(Vertex vertex, Direction direction, String... labels) {
170 return vertex.getEdges(direction, labels).iterator().hasNext();
171 }
172 }