GraphBuilder | index ../../../../piana/code/Graph/GraphBuilder.py |
File : GraphBuilder.py
Author : R. Aragues & D. Jaeggi
Creation : 31.07.2003
Contents : class for building a graph from a database
Called from: Graph.py and piana.py
=======================================================================================================
A class that defines how a graph should be built and that provides functions to
allow a graph to be automatically be built from a database by calling Graph.build_graph()
Common to all these methods and, importantly, problem independent, are methods to
recursively expand a graph and update a Graph object with the appropriate data. This
methods are contained in this class.
Making a GraphBuilder instance problem specific relies only on making one method
problem specific: GraphBuilder.get_links(node). This method gets all the links for a node and
should be overridden by the user to be adapted to the data source and type being used.
The concept is that, once a specific GraphBuilder class has been written (and possibly
GraphNodeAttribute and GraphEdgeAttribute) the following calls would be sufficient to generate
a complete graph (containing, in this case, all actors linked to Jack Nicholson by a
maximum hop of 2 co-actors):
my_graph = Graph("My Graph")
my_builder = MyGraphBuilder(depth=2, node1_id="Jack Nicholson")
my_graph.build_graph(my_builder)
In this instance MyGraphBuilder may look something like:
class MyGraphBuilder(GraphBuilder):
def get_links(self, actor):
....SQL statements....
links = []
for linked_actor in linked_actors:
new_link = LinkDetail(actor, linked_actor)
links.append(new_link)
return links
That's all it needs to do, on a basic level. Easy peasy, non?!
Of course, more complex problems may need more complexity here, but
this is fundamentally all that needs to be done.
Modules | ||||||
|
Classes | ||||||||||
|
Data | ||
verbose = 0 verbose_build = 0 verbose_build_shallow = 0 verbose_build_very_shallow = 0 verbose_get_all_links = 0 verbose_has_edge = 0 |