text
stringlengths
0
828
if self._neighbors.get(edge_key) or self._neighbors.get((edge_key[1], edge_key[0])):
return # The neighbor is already added.
self._neighbors[edge_key] = edge
self.dispatch_event(NeighborAddedEvent(other))"
4429,"def _load_neighbors(self) -> None:
""""""
Loads all neighbors of the node from the local database and
from the external data source if needed.
""""""
if not self.are_neighbors_cached:
self._load_neighbors_from_external_source()
db: GraphDatabaseInterface = self._graph.database
db_node: DBNode = db.Node.find_by_name(self.name)
db_node.are_neighbors_cached = True
db.session.commit()
self.are_neighbors_cached = True
if not self._are_neighbors_loaded:
self._load_neighbors_from_database()"
4430,"def _load_neighbors_from_database(self) -> None:
""""""
Loads the neighbors of the node from the local database.
""""""
self._are_neighbors_loaded = True
graph: Graph = self._graph
neighbors: List[DBNode] = graph.database.Node.find_by_name(self.name).neighbors
nodes: NodeList = graph.nodes
for db_node in neighbors:
graph.add_node(db_node.name, db_node.external_id)
neighbor: Node = nodes.get_node_by_name(db_node.name)
graph.add_edge(self, neighbor, 1, False)"
4431,"def key(self) -> Tuple[int, int]:
""""""
The unique identifier of the edge consisting of the indexes of its
source and target nodes.
""""""
return self._source.index, self._target.index"
4432,"def add_node_by_name(self, node_name: str, external_id: Optional[str] = None) -> None:
""""""
Adds a new node to the graph if it doesn't exist.
Arguments:
node_name (str): The name of the node to add.
external_id (Optional[str]): The external ID of the node.
""""""
if node_name is None:
return
node_name = node_name.strip()
if len(node_name) == 0:
return
node: Node = self.get_node_by_name(node_name, external_id=external_id)
if node is None:
self._internal_add_node(node_name=node_name,
external_id=external_id,
are_neighbors_cached=False,
add_to_cache=True)"
4433,"def get_node(self, index: int) -> Optional[Node]:
""""""
Returns the node with the given index if such a node currently exists in the node list.
Arguments:
index (int): The index of the queried node.
Returns:
The node with the given index if such a node currently exists in the node list,
`None` otherwise.
""""""
return self._nodes.get(index)"
4434,"def get_node_by_name(self, node_name: str,
can_validate_and_load: bool = False,
external_id: Optional[str] = None) -> Optional[Node]:
""""""
Returns the node with the given name if it exists either in the graph
or in its database cache or `None` otherwise.
Arguments:
node_name (str): The name of the node to return.
can_validate_and_load (bool): Whether `self._graph.get_authentic_node_name(node_name)`
can be called to validate the node name and add the node
to the graph if the node name is valid.
external_id (Optional[str]): An optional external ID that is used only if there no node
with the given name in the graph or in the cache and
`can_validate_and_load` is `True`.
Returns:
The node with the given name if it exists either in the graph
or in its database cache, `None` otherwise.
""""""
node: Node = self._node_name_map.get(node_name)
if node is not None:
return node
db_node: DBNode = self._graph.database.Node.find_by_name(node_name)
if db_node is None:
if can_validate_and_load:
node_name = self._graph.get_authentic_node_name(node_name)