@@ -53,6 +53,10 @@ class KG:
5353 Defaults to False.
5454 skip_predicates: The label predicates to skip from the KG.
5555 Defaults to set.
56+ skip_verify: To skip or not the verification of existing entities in a
57+ Knowledge Graph. Its deactivation can improve HTTP latency for KG
58+ remotes.
59+ Defaults to False.
5660
5761 """
5862
@@ -95,6 +99,13 @@ class KG:
9599 validator = attr .validators .instance_of (bool ),
96100 )
97101
102+ skip_verify = attr .ib (
103+ kw_only = True ,
104+ type = bool ,
105+ default = False ,
106+ validator = attr .validators .instance_of (bool ),
107+ )
108+
98109 cache = attr .ib (
99110 kw_only = True ,
100111 type = Cache ,
@@ -206,9 +217,6 @@ def add_walk(self, subj: Vertex, pred: Vertex, obj: Vertex) -> bool:
206217 return True
207218 return False
208219
209- @cachedmethod (
210- operator .attrgetter ("cache" ), key = partial (hashkey , "fetch_hops" )
211- )
212220 def fetch_hops (self , vertex : Vertex ) -> List [Hop ]:
213221 """Fetchs the hops of the vertex from a SPARQL endpoint server and
214222 add the hops for this vertex in a cache dictionary.
@@ -229,7 +237,7 @@ def fetch_hops(self, vertex: Vertex) -> List[Hop]:
229237 "https://"
230238 ):
231239 res = self .connector .fetch (self .connector .get_query (vertex .name ))
232- hops = self ._res2hops (vertex , res )
240+ hops = self ._res2hops (vertex , res [ "results" ][ "bindings" ] )
233241 return hops
234242
235243 def get_hops (self , vertex : Vertex , is_reverse : bool = False ) -> List [Hop ]:
@@ -283,7 +291,10 @@ def get_literals(self, entities: Entities, verbose: int = 0) -> Literals:
283291 responses = [self .connector .fetch (query ) for query in queries ]
284292
285293 literals_responses = [
286- self .connector .res2literals (res ) for res in responses
294+ self .connector .res2literals (
295+ res ["results" ]["bindings" ] # type: ignore
296+ )
297+ for res in responses
287298 ]
288299 return [
289300 literals_responses [
@@ -340,6 +351,31 @@ def get_pliterals(self, entity: str, preds: List[str]) -> List[str]:
340351 frontier = new_frontier
341352 return list (frontier )
342353
354+ def is_exist (self , entities : Entities ) -> bool :
355+ """Checks that all provided entities exists in the Knowledge Graph.
356+
357+ Args:
358+ entities: The entities to check the existence
359+
360+ Returns:
361+ True if all the entities exists, False otherwise.
362+
363+ """
364+ if self ._is_remote :
365+ queries = [
366+ f"ASK WHERE {{ <{ entity } > ?p ?o . }}" for entity in entities
367+ ]
368+ if self .mul_req :
369+ responses = [
370+ res ["boolean" ] # type: ignore
371+ for res in asyncio .run (self .connector .afetch (queries ))
372+ ]
373+ else :
374+ responses = [self .connector .fetch (query ) for query in queries ]
375+ responses = [res ["boolean" ] for res in responses ]
376+ return False not in responses
377+ return all ([Vertex (entity ) in self ._vertices for entity in entities ])
378+
343379 def remove_edge (self , v1 : Vertex , v2 : Vertex ) -> bool :
344380 """Removes the edge (v1 -> v2) if present.
345381
@@ -403,7 +439,9 @@ def _fill_hops(self, entities: Entities) -> None:
403439 entities ,
404440 asyncio .run (self .connector .afetch (queries )),
405441 ):
406- hops = self ._res2hops (Vertex (entity ), res )
442+ hops = self ._res2hops (
443+ Vertex (entity ), res ["results" ]["bindings" ] # type: ignore
444+ )
407445 self ._entity_hops .update ({entity : hops })
408446
409447 @cachedmethod (
@@ -418,8 +456,8 @@ def _get_hops(self, vertex: Vertex, is_reverse: bool = False) -> List[Hop]:
418456 vertex. Otherwise, get the child nodes for this vertex.
419457 Defaults to False.
420458
421- Returns:
422- The hops of a vertex in a (predicate, object) form.
459+ Returns:
460+ The hops of a vertex in a (predicate, object) form.
423461
424462 """
425463 matrix = self ._transition_matrix
@@ -452,6 +490,6 @@ def _res2hops(self, vertex: Vertex, res) -> List[Hop]:
452490 vprev = vertex ,
453491 vnext = obj ,
454492 )
455- if self . add_walk ( vertex , pred , obj ) :
493+ if pred . name not in self . skip_predicates :
456494 hops .append ((pred , obj ))
457495 return hops
0 commit comments