@@ -132,19 +132,26 @@ def get_nodeIDs(self, attributes: dict, node_type: str) -> List[int]:
132
132
def get_current_nodeID (self , attributes : dict , node_type : str ) -> int :
133
133
result = self .get_current_node (attributes , node_type )
134
134
return result .attributes .get ("ID" )
135
- return result .get ("ID" )
136
135
137
136
def get_current_edgeID (self , src_id , dst_id , edeg_type :str = None ):
138
137
if not isinstance (src_id , int ) or not isinstance (dst_id , int ):
139
138
result = self .get_current_edge (src_id , dst_id , edeg_type )
140
139
logger .debug (f"{ result } " )
141
140
return result .attributes .get ("srcId" ), result .attributes .get ("dstId" ), result .attributes .get ("timestamp" )
142
- return result .get ("srcId" ), result .get ("dstId" ),
143
141
else :
144
142
return src_id , dst_id , 1
145
143
146
144
def get_current_node (self , attributes : dict , node_type : str = None , return_keys : list = []) -> GNode :
147
145
return self .get_current_nodes (attributes , node_type , return_keys )[0 ]
146
+
147
+ def get_nodes_by_ids (self , ids : List [int ] = []) -> List [GNode ]:
148
+ where_str = f'@id in { ids } '
149
+ gql = f"MATCH (n0 WHERE { where_str } ) RETURN n0"
150
+ #
151
+ result = self .execute (gql , return_keys = [])
152
+ result = self .decode_result (result , gql )
153
+ nodes = result .get ("n0" , []) or result .get ("n0.attr" , [])
154
+ return [GNode (id = node ["id" ], type = node ["type" ], attributes = node ) for node in nodes ]
148
155
149
156
def get_current_nodes (self , attributes : dict , node_type : str = None , return_keys : list = []) -> List [GNode ]:
150
157
#
@@ -170,8 +177,6 @@ def get_current_edge(self, src_id, dst_id, edge_type:str = None, return_keys: li
170
177
171
178
edges = result .get ("e" , []) or result .get ("e.attr" , [])
172
179
return [GEdge (start_id = edge ["start_id" ], end_id = edge ["end_id" ], type = edge ["type" ], attributes = edge ) for edge in edges ][0 ]
173
-
174
- return result [0 ]
175
180
176
181
def get_neighbor_nodes (self , attributes : dict , node_type : str = None , return_keys : list = []) -> List [GNode ]:
177
182
#
@@ -184,7 +189,6 @@ def get_neighbor_nodes(self, attributes: dict, node_type: str = None, return_key
184
189
result = self .decode_result (result , gql )
185
190
nodes = result .get ("n1" , []) or result .get ("n1.attr" , [])
186
191
return [GNode (id = node ["id" ], type = node ["type" ], attributes = node ) for node in nodes ]
187
- return result .get ("n1" , []) or result .get ("n1.attr" , [])
188
192
189
193
def get_neighbor_edges (self , attributes : dict , node_type : str = None , return_keys : list = []) -> List [GEdge ]:
190
194
#
@@ -198,22 +202,23 @@ def get_neighbor_edges(self, attributes: dict, node_type: str = None, return_key
198
202
199
203
edges = result .get ("e" , []) or result .get ("e.attr" , [])
200
204
return [GEdge (start_id = edge ["start_id" ], end_id = edge ["end_id" ], type = edge ["type" ], attributes = edge ) for edge in edges ]
201
- return result .get ("e" , []) or result .get ("e.attr" , [])
202
205
203
206
def check_neighbor_exist (self , attributes : dict , node_type : str = None , check_attributes : dict = {}) -> bool :
204
207
result = self .get_neighbor_nodes (attributes , node_type ,)
205
208
filter_result = [i for i in result if all ([item in i .attributes .items () for item in check_attributes .items ()])]
206
209
return len (filter_result ) > 0
207
210
208
- def get_hop_infos (self , attributes : dict , node_type : str = None , hop : int = 2 , block_attributes : dict = {}, select_attributes : dict = {}) -> Graph :
211
+ def get_hop_infos (self , attributes : dict , node_type : str = None , hop : int = 2 , block_attributes : dict = {}, select_attributes : dict = {}, reverse = False ) -> Graph :
209
212
'''
210
213
hop >= 2, 表面需要至少两跳
211
214
'''
212
215
hop_max = 10
213
- hop_list = []
214
216
#
215
217
where_str = ' and ' .join ([f"n0.{ k } ='{ v } '" for k , v in attributes .items ()])
216
- gql = f"MATCH p = (n0:{ node_type } WHERE { where_str } )-[e]->{{1,{ min (hop , hop_max )} }}(n1) RETURN n0, n1, e, p"
218
+ if reverse :
219
+ gql = f"MATCH p = (n0:{ node_type } WHERE { where_str } )<-[e]-{{1,{ min (hop , hop_max )} }}(n1) RETURN n0, n1, e, p"
220
+ else :
221
+ gql = f"MATCH p = (n0:{ node_type } WHERE { where_str } )-[e]->{{1,{ min (hop , hop_max )} }}(n1) RETURN n0, n1, e, p"
217
222
last_node_ids , last_node_types = [], []
218
223
219
224
result = {}
@@ -238,19 +243,16 @@ def get_hop_infos(self, attributes: dict, node_type: str = None, hop: int = 2, b
238
243
nodes = [GNode (id = node ["id" ], type = node ["type" ], attributes = node ) for node in result .get ("n1" , [])]
239
244
edges = [GEdge (start_id = edge ["start_id" ], end_id = edge ["end_id" ], type = edge ["type" ], attributes = edge ) for edge in result .get ("e" , [])]
240
245
return Graph (nodes = nodes , edges = edges , paths = result .get ("p" , []))
241
- return result
242
-
246
+
243
247
def get_hop_nodes (self , attributes : dict , node_type : str = None , hop : int = 2 , block_attributes : dict = []) -> List [GNode ]:
244
248
#
245
249
result = self .get_hop_infos (attributes , node_type , hop , block_attributes )
246
250
return result .nodes
247
- return result .get ("n1" , []) or result .get ("n1.attr" , [])
248
251
249
252
def get_hop_edges (self , attributes : dict , node_type : str = None , hop : int = 2 , block_attributes : dict = []) -> List [GEdge ]:
250
253
#
251
254
result = self .get_hop_infos (attributes , node_type , hop , block_attributes )
252
255
return result .edges
253
- return result .get ("e" , []) or result .get ("e.attr" , [])
254
256
255
257
def get_hop_paths (self , attributes : dict , node_type : str = None , hop : int = 2 , block_attributes : dict = []) -> List [str ]:
256
258
#
@@ -353,14 +355,28 @@ def decode_result(self, geabase_result, gql: str) -> Dict:
353
355
return output
354
356
355
357
def decode_path (self , col_data , k ) -> List :
356
- path = []
357
358
steps = col_data .get ("pathVal" , {}).get ("steps" , [])
359
+ connections = {}
358
360
for step in steps :
359
361
props = step ["props" ]
360
- if path == []:
361
- path .append (props ["original_src_id1__" ].get ("strVal" , "" ) or props ["original_src_id1__" ].get ("intVal" , - 1 ))
362
-
363
- path .append (props ["original_dst_id2__" ].get ("strVal" , "" ) or props ["original_dst_id2__" ].get ("intVal" , - 1 ))
362
+ # if path == []:
363
+ # path.append(props["original_src_id1__"].get("strVal", "") or props["original_src_id1__"].get("intVal", -1))
364
+ # path.append(props["original_dst_id2__"].get("strVal", "") or props["original_dst_id2__"].get("intVal", -1))
365
+
366
+ start = props ["original_src_id1__" ].get ("strVal" , "" ) or props ["original_src_id1__" ].get ("intVal" , - 1 )
367
+ end = props ["original_dst_id2__" ].get ("strVal" , "" ) or props ["original_dst_id2__" ].get ("intVal" , - 1 )
368
+ connections [start ] = end
369
+
370
+ # 找到头部(1)
371
+ for k in connections :
372
+ if k not in connections .values ():
373
+ head = k
374
+ path = [head ]
375
+
376
+ # 根据连通关系构建路径
377
+ while head in connections :
378
+ head = connections [head ]
379
+ path .append (head )
364
380
365
381
return path
366
382
0 commit comments