# 需要导入模块: import tkSimpleDialog [as 别名]
# 或者: from tkSimpleDialog import askstring [as 别名]
def grow_until(self, disp_node, stop_condition=None, levels=0):
# Find condition to stop growing
if stop_condition is None:
stop_condition = tkd.askstring("Stop Condition", "Enter lambda "
"function which returns True when stop condition is met.\n"
"Parameters are:\n - u, the node's name, and \n "
"- d, the data dictionary.\n\nExample: "
"d['color']=='red' \nwould grow until a red node is found.")
if stop_condition is None: return
data_node = self.dispG.node[disp_node]['dataG_id']
existing_data_nodes = set([ v['dataG_id']
for k,v in self.dispG.node.items() ])
max_iters = 10
stop_node = None # Node which met stop condition
grow_nodes = set([data_node]) # New nodes
# Iterate until we find a node that matches the stop condition (or,
# worst case, we reach max iters)
for i in range(1,max_iters+1):
old_grow_nodes = grow_nodes.copy()
grow_nodes.clear()
for n in old_grow_nodes:
grow_graph = self._neighbors(n, levels=i)
grow_nodes = grow_nodes.union(set(grow_graph.nodes())) - \
existing_data_nodes - old_grow_nodes
if len(grow_nodes) == 0:
# Start out next iteration with the entire graph
grow_nodes = existing_data_nodes.copy()
continue
for u in grow_nodes:
d = self.dataG.node[u]
try:
stop = eval(stop_condition, {'u':u, 'd':d})
except Exception as e:
tkm.showerror("Invalid Stop Condition",
"Evaluating the stop condition\n\n" +
stop_condition + "\n\nraise the following " +
"exception:\n\n" + str(e))
return
if stop:
stop_node = u
break
if stop_node is not None:
break
if stop_node is None:
tkm.showerror("Stop Condition Not Reached", "Unable to find a node "
"which meet the stop condition within %d levels."%i)
return
## Grow the number of times it took to find the node
#self.grow_node(disp_node, i)
# Find shortest path to stop_node
self.plot_path(data_node, stop_node, levels=levels, add_to_exsting=True)