import networkx as nx
class SecDepGraph:
def init(self):
self.graph = nx.DiGraph()
# Map aliases to vertex names
self.aliases = dict()
# Map vertex names to flags (strings) that will be used when processing the graph
# Map vertex names to auxiliary data for reporting etc. but not when processing the graph
self.auxdata = dict()
# Collection of edges to add in second pass
self.cached_edges = set()
def add_vertex(self, vertexname, flags, auxdata=None):
# TODO: check if vertex already exists or is an alias. What to do in that case?
# add the vertex
# Always adding auxdata, but will be None if not passed in.
self.auxdata[vertexname] = auxdata
def add_alias(self, vertexname, alias):
# TODO: check if alias already exists or is a vertex. What to do in that case?
self.aliases[alias] = vertexname
def cache_edge(self, fromvertex, tovertex):
'''Store an edge to be added later, after vertices and aliases are finalised.'''
def add_edge(self, fromvertex, tovertex):
'''Add edge to the graph immediately.'''
# check that both vertices exist or resolve aliases
# add the edge
pass
def finalise_edges(self):
'''Add cached edges to the graph after all vertices and alias are finalised (stage 2).'''
for (u,v) in self.cached_edges:
self.add_edge(u, v)
self.cached_edges = set()