本文整理汇总了Python中networkx.DiGraph.predecessors_iter方法的典型用法代码示例。如果您正苦于以下问题:Python DiGraph.predecessors_iter方法的具体用法?Python DiGraph.predecessors_iter怎么用?Python DiGraph.predecessors_iter使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类networkx.DiGraph
的用法示例。
在下文中一共展示了DiGraph.predecessors_iter方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from networkx import DiGraph [as 别名]
# 或者: from networkx.DiGraph import predecessors_iter [as 别名]
class Codebase:
def __init__(self):
self.counter = 0
self._revisions = []
with open(path.join(path.dirname(__file__), '..', 'App.java')) as java_file:
parser = Parser()
tree = parser.parse_file(java_file)
initial_classes = tree.type_declarations
self._inheritance_graph = DiGraph()
self._method_call_graph = DiGraph()
for c in initial_classes:
self._inheritance_graph.add_node(c.name, {'class': c})
for m in c.body:
if isinstance(m, MethodDeclaration):
self._method_call_graph.add_node(m.name,
{'method': m,
'class_name': c.name,
'fitness': random()
})
def get_class_name(self, method_name):
return self._method_call_graph.node[method_name]['class_name']
def size_of(self, method_name):
return len(self._method_call_graph.node[method_name]['method'].body)
def number_of_methods(self):
return len(self._method_call_graph)
def number_of_classes(self):
return len(self._inheritance_graph)
def has_method(self, method_name):
return self._method_call_graph.has_node(method_name)
def choose_random_method(self):
"""
Choose a random method, weighted by its size
:return: the method name
"""
return sample([(method_name, len(data['method'].body) + 1)
for method_name, data in self._method_call_graph.nodes_iter(True)])
def choose_random_class(self):
"""
Choose a random class, weighted by its size
:return: the class name
"""
return sample([(class_name, len(data['class'].body) + 1)
for class_name, data in self._inheritance_graph.nodes_iter(data=True)])
def least_fit_methods(self, n=1):
"""
:return: the name of the method with smallest fitness value
"""
return nsmallest(n, self._method_call_graph,
key=lambda method_name: self._method_call_graph.node[method_name]['fitness'])
def choose_random_neighbor(self, method_name):
neighbors = self._method_call_graph.neighbors(method_name)
num_neighbors = len(neighbors)
if num_neighbors > 0:
return neighbors[floor(random() * num_neighbors)]
else:
return None
def caller_names(self, method_name):
"""
:param method_name:
:return: caller method names iterator
"""
return self._method_call_graph.predecessors_iter(method_name)
def method_invocations(self, method_name):
"""
Generator for MethodInvocation instances of method_name
:param method_name:
:return:
"""
for caller_name in self._method_call_graph.predecessors_iter(method_name):
caller = self._method_call_graph.node[caller_name]['method']
for stmt in caller.body:
if Codebase.is_invocation(stmt, method_name):
yield stmt.expression
def create_method(self, class_name):
"""
The created methods are static methods for now
"""
klass = self._inheritance_graph.node[class_name]['class']
method = MethodDeclaration(
'method' + str(self.counter),
body=[], modifiers=['static'])
self.counter += 1
klass.body.append(method)
method_info = {'method': method, 'class_name': class_name, 'fitness': random()}
self._method_call_graph.add_node(method.name, method_info)
return 1, method.name
#.........这里部分代码省略.........