Appends a new Edge to the graph. An optional base parameter can be used to pass a subclass of Edge: Graph.add_edge("cold", "winter", base=IsPropertyOf)
(self, id1, id2, *args, **kwargs)
| 360 | return n |
| 361 | |
| 362 | def add_edge(self, id1, id2, *args, **kwargs): |
| 363 | """ Appends a new Edge to the graph. |
| 364 | An optional base parameter can be used to pass a subclass of Edge: |
| 365 | Graph.add_edge("cold", "winter", base=IsPropertyOf) |
| 366 | """ |
| 367 | # Create nodes that are not yet part of the graph. |
| 368 | n1 = self.add_node(id1) |
| 369 | n2 = self.add_node(id2) |
| 370 | # Creates an Edge instance. |
| 371 | # If an edge (in the same direction) already exists, yields that edge instead. |
| 372 | e1 = n1.links.edge(n2) |
| 373 | if e1 and e1.node1 == n1 and e1.node2 == n2: |
| 374 | return e1 |
| 375 | e2 = kwargs.pop("base", Edge) |
| 376 | e2 = e2(n1, n2, *args, **kwargs) |
| 377 | self.edges.append(e2) |
| 378 | # Synchronizes Node.links: |
| 379 | # A.links.edge(B) yields edge A->B |
| 380 | # B.links.edge(A) yields edge B->A |
| 381 | n1.links.append(n2, edge=e2) |
| 382 | n2.links.append(n1, edge=e1 or e2) |
| 383 | # Clear adjacency cache. |
| 384 | self._adjacency = None |
| 385 | return e2 |
| 386 | |
| 387 | def remove(self, x): |
| 388 | """ Removes the given Node (and all its edges) or Edge from the graph. |