9 from collections
import defaultdict
10 from m_util
import log
14 """docstring for Tree"""
16 assert type(op) != type(
None)
21 '''docstring for is_leaf'''
29 op = property(get_op, set_op)
35 children = property(get_children, set_children)
41 return '(' + str(self.
op) +
' '+
' '.join(map(str, self.
children)) +
')'
52 ''' transfer a simpler and more efficient tree StructureNode
53 to Viz_Graph for visualisation purpose
57 assert isinstance(tree.op, str)
58 for i, child
in enumerate(tree.children):
60 child_name = graph.unique_id(child.op)
63 graph.add_edge(tree.op, child_name, order = i)
75 self.
gephi = pygephi.JSONClient(
'http://localhost:8080/workspace0', autoflush=
True)
85 def write(self, filename = None):
90 self.gephi.add_node(str(node_id), label=attr.get(
'label',
None), **attr)
92 self.gephi.add_node(str(node_id), label=attr.get(
'label',
None), **self.default_node_attr)
95 edge_id = attr.get(
'edge_id',
None)
97 self.gephi.add_edge(str(edge_id), source, target, attr.get(
"directed",
None), label = attr.get(
"label",
None))
100 """docstring for Dot_output"""
105 '''docstring for output_node'''
106 line =
'"%s" '% str(node_id)
111 str_attr +=
"color=%s," % attr[
'color']
115 str_attr +=
"shape=%s," % attr[
'shape']
119 str_attr +=
"style=%s," % attr[
'style']
122 str_attr = str_attr.strip(
',')
123 line = line % str_attr
124 self.
body += line +
";\n"
127 line =
'"%s" -> "%s" ' %(str(source), str(target))
132 str_attr +=
"color=%s," % attr[
'color']
137 str_attr +=
"color=%s," % attr[
'attr'][
'color']
141 str_attr +=
"shape=%s," % attr[
'shape']
146 str_attr +=
"shape=%s," % attr[
'attr'][
'shape']
150 str_attr +=
"style=%s," % attr[
'style']
155 str_attr +=
'label="%s",' % attr[
'attr'][
'style']
159 str_attr +=
'label="%s",' % attr[
'order']
163 str_attr +=
'label="%s",' % attr[
'attr'][
'order']
166 str_attr = str_attr.strip(
',')
167 line = line % str_attr
168 self.
body += line +
";\n"
171 '''docstring for write'''
173 f = open(filename,
'w')
175 digraph visualisation{
180 content = content % self.
body
183 log.error(
"can't write dot file: %s" % filename)
188 """ a wrapper to networkx, which work as a graph drawer"""
195 self._nx_graph.add_edge(str(source), str(target))
200 '''docstring for add_edge_unique'''
203 self.
add_edge(source, target, **attr)
208 ''' supposed to added this node later'''
214 node = node +
"[%s]" % str(no_node)
218 '''docstring for reset_unique_no'''
219 self.no_nodes.clear()
222 self._nx_graph.add_node(str(node_id))
223 for key, value
in attr.items():
224 self._nx_graph.node[str(node_id)][key] = value
227 '''docstring for output_node'''
228 assert self.
viz and callable(getattr(self.
viz,
"output_node"))
229 self.viz.output_node(str(node_id))
232 '''docstring for output_edge'''
233 assert self.
viz and callable(getattr(self.
viz,
"output_edge"))
234 self.viz.output_edge(str(edge_id), str(source), str(target))
237 '''return a list of node's neighbors'''
238 return self._nx_graph.neighbors(str(node_id))
241 for key, value
in attr.items():
242 self._nx_graph.node[str(node_id)][key] = value
245 return self._nx_graph.node[str(node_id)]
248 for key, value
in attr.items():
249 self.
_nx_graph[str(source)][str(target)][key] = value
253 return self.
_nx_graph[str(source)][str(target)]
255 '''docstring for number_of_nodes'''
256 return self._nx_graph.number_of_nodes()
259 """ draw the graph"""
262 for node
in self._nx_graph.nodes():
263 attr_dict = self._nx_graph.node[str(node)]
264 self.viz.output_node(node, attr = attr_dict)
267 for edge
in self._nx_graph.edges():
268 attr_dict = self._nx_graph.edge[edge[0]][edge[1]]
269 self.viz.output_edge(edge[0], edge[1], attr = attr_dict)
270 self.viz.write(filename)
293 """docstring for clear"""
294 self._nx_graph.clear()
298 """ abstract class that help to abserve the graph according to the given filter imfo"""
299 def __init__(self, source, e_types, n_types, inheritance = True):
308 '''docstring for write_dot'''
309 self.graph.write(filename)
312 '''docstring for run()'''
322 self.graph.add_edge(str(nodes[0]), str(nodes[1]))
327 '''docstring for graph_info'''
347 '''type of e_type should be consistency with valid_edge_types '''
350 '''type of edge should be consistency with valid_edge_types '''
354 '''type of node should be consistency with valid_node_types '''
358 '''type of source and target is consistency with valid_edge_types '''
359 return source == target
362 '''type of source and target is consistency with valid_node_types '''
363 return source == target
367 """make sure the type edge and it targets are required type,
368 if one of the target is invalid, then the edge is invalid
388 __all__ = [
"Dotty_Output",
"Gephi_Output",
"Tree",
"tree_to_viz_graphic",
"Graph_Abserver" ]