-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtraversal.py
More file actions
308 lines (242 loc) · 10.1 KB
/
traversal.py
File metadata and controls
308 lines (242 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# Copyright 2024 JanusGraph-Python Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from numbers import Number
from gremlin_python.process.traversal import P
from janusgraph_python.structure.io.util import LongEncoding
# Marked as 'private' class as it should not be used directly.
# If one uses _JanusGraphP.eq(), this will result invalid operator error.
class _JanusGraphP(P):
def __init__(self, operator, value, other=None):
self.operator = operator
self.value = value
self.other = other
def __eq__(self, other):
return isinstance(other, self.__class__) and self.operator == other.operator and self.value == other.value and self.other == other.other
def __repr__(self):
return self.operator + "(" + str(self.value) + ")" if self.other is None else self.operator + "(" + str(self.value) + "," + str(self.other) + ")"
class Text(object):
"""
Provides text search predicates.
"""
@staticmethod
def text_contains(*args):
"""
Is true if (at least) one word inside the text string matches the query string.
:param query: string, The query to search.
:return The text predicate.
"""
return _JanusGraphP("textContains", *args)
@staticmethod
def text_not_contains(*args):
"""
Is true if no words inside the text string match the query string.
:param query: string, The query to search.
:return The text predicate.
"""
return _JanusGraphP("textNotContains", *args)
@staticmethod
def text_contains_prefix(*args):
"""
Is true if (at least) one word inside the text string begins with the query string.
:param query: string, The query to search.
:return The text predicate.
"""
return _JanusGraphP("textContainsPrefix", *args)
@staticmethod
def text_not_contains_prefix(*args):
"""
Is true if no words inside the text string begin with the query string.
:param query: string, The query to search.
:return The text predicate.
"""
return _JanusGraphP("textNotContainsPrefix", *args)
@staticmethod
def text_contains_regex(*args):
"""
Is true if (at least) one word inside the text string matches the given regular expression.
:param query: string, The query to search.
:return The text predicate.
"""
return _JanusGraphP("textContainsRegex", *args)
@staticmethod
def text_not_contains_regex(*args):
"""
Is true if no words inside the text string match the given regular expression.
:param query: string, The query to search.
:return The text predicate.
"""
return _JanusGraphP("textNotContainsRegex", *args)
@staticmethod
def text_contains_fuzzy(*args):
"""
Is true if (at least) one word inside the text string is similar to the query String (based on
Levenshtein edit distance).
:param query: string, The query to search.
:return The text predicate.
"""
return _JanusGraphP("textContainsFuzzy", *args)
@staticmethod
def text_not_contains_fuzzy(*args):
"""
Is true if no words inside the text string are similar to the query string
(based on Levenshtein edit distance).
:param query: string, The query to search.
:return The text predicate.
"""
return _JanusGraphP("textNotContainsFuzzy", *args)
@staticmethod
def text_contains_phrase(*args):
"""
Is true if the text string does contain the sequence of words in the query string.
:param query: string, The query to search.
:return The text predicate.
"""
return _JanusGraphP("textContainsPhrase", *args)
@staticmethod
def text_not_contains_phrase(*args):
"""
Is true if the text string does not contain the sequence of words in the query string.
:param query: string, The query to search.
:return The text predicate.
"""
return _JanusGraphP("textNotContainsPhrase", *args)
@staticmethod
def text_prefix(*args):
"""
Is true if the string value starts with the given query string.
:param query: string, The query to search.
:return The text predicate.
"""
return _JanusGraphP("textPrefix", *args)
@staticmethod
def text_not_prefix(*args):
"""
Is true if the string value does not start with the given query string.
:param query: string, The query to search.
:return The text predicate.
"""
return _JanusGraphP("textNotPrefix", *args)
@staticmethod
def text_regex(*args):
"""
Is true if the string value matches the given regular expression in its entirety.
:param query: string, The query to search.
:return The text predicate.
"""
return _JanusGraphP("textRegex", *args)
@staticmethod
def text_not_regex(*args):
"""
Is true if the string value does not match the given regular expression in its entirety.
:param query: string, The query to search.
:return The text predicate.
"""
return _JanusGraphP("textNotRegex", *args)
@staticmethod
def text_fuzzy(*args):
"""
Is true if the string value is similar to the given query string (based on Levenshtein edit distance).
:param query: string, The query to search.
:return The text predicate.
"""
return _JanusGraphP("textFuzzy", *args)
@staticmethod
def text_not_fuzzy(*args):
"""
Is true if the string value is not similar to the given query string (based on Levenshtein edit distance).
:param query: string, The query to search.
:return The text predicate.
"""
return _JanusGraphP("textNotFuzzy", *args)
class RelationIdentifier(object):
_TO_STRING_DELIMITER = '-'
out_vertex_id = None
type_id = 0
relation_id = 0
in_vertex_id = None
string_representation = None
def __init__(self, out_vertex_id, type_id, relation_id, in_vertex_id, string_representation):
"""
Initializes a new instance of the RelationIdentifier class
:param out_vertex_id: object, The id of the outgoing vertex.
:param type_id: long, The JanusGraph internal type id.
:param relation_id: long, The JanusGraph internal relation id.
:param in_vertex_id: object, The id of the incoming vertex.
:param string_representation: string, The underlying relation id.
"""
self.out_vertex_id = out_vertex_id
self.type_id = type_id
self.relation_id = relation_id
self.in_vertex_id = in_vertex_id
self.string_representation = string_representation
@classmethod
def from_string(cls, string_representation):
"""
Initializes a new instance of the RelationIdentifier class.
:param string_representation: string, The underlying relation id.
"""
in_vertex_id = None
elements = string_representation.split(cls._TO_STRING_DELIMITER)
if len(elements) != 3 and len(elements) != 4:
raise ValueError(f"Not a valid relation identifier: {string_representation}")
if elements[1][0] == LongEncoding.STRING_ENCODING_MARKER:
out_vertex_id = elements[1][1:]
else:
out_vertex_id = LongEncoding.decode(elements[1])
type_id = LongEncoding.decode(elements[2])
relation_id = LongEncoding.decode(elements[0])
if len(elements) == 4:
if elements[3][0] == LongEncoding.STRING_ENCODING_MARKER:
in_vertex_id = elements[3][1:]
else:
in_vertex_id = LongEncoding.decode(elements[3])
return cls(out_vertex_id, type_id, relation_id, in_vertex_id, string_representation)
@classmethod
def from_ids(cls, out_vertex_id, type_id, relation_id, in_vertex_id):
"""
Initializes a new instance of the RelationIdentifier class.
:param out_vertex_id: object, The id of the outgoing vertex.
:param type_id: long, The JanusGraph internal type id.
:param relation_id: long, The JanusGraph internal relation id.
:param in_vertex_id: object, The id of the incoming vertex.
"""
parts = []
parts.append(LongEncoding.encode(relation_id))
parts.append(cls._TO_STRING_DELIMITER)
if isinstance(out_vertex_id, Number):
parts.append(LongEncoding.encode(out_vertex_id))
else:
parts.append(LongEncoding.STRING_ENCODING_MARKER)
parts.append(out_vertex_id)
parts.append(cls._TO_STRING_DELIMITER)
parts.append(LongEncoding.encode(type_id))
if in_vertex_id:
parts.append(cls._TO_STRING_DELIMITER)
if isinstance(in_vertex_id, Number):
parts.append(LongEncoding.encode(in_vertex_id))
else:
parts.append(LongEncoding.STRING_ENCODING_MARKER)
parts.append(in_vertex_id)
string_representation = ''.join(parts)
return cls(out_vertex_id, type_id, relation_id, in_vertex_id, string_representation)
def __eq__(self, other):
if other is None:
return False
if other is self:
return True
return self.string_representation == other.string_representation
def __repr__(self):
return self.string_representation
def __hash__(self):
return hash(self.string_representation)