def validate_graph_connection(data): """ 节点连接合法性校验 """ nodes = get_nodes_dict(data) result = { "result": True, "message": {}, "failed_nodes": [] } for i in nodes: node_type = nodes[i][PE.type] rule = NODE_RULES[node_type] message = "" for j in nodes[i][PE.target]: if nodes[j][PE.type] not in rule['allowed_out']: message += _(u"不能连接%s类型节点\n") % nodes[i][PE.type] if rule["min_in"] > len(nodes[i][PE.source]) or len(nodes[i][PE.source]) > rule['max_in']: message += _(u"节点的入度最大为%s,最小为%s\n") % (rule['max_in'], rule['min_in']) if rule["min_out"] > len(nodes[i][PE.target]) or len(nodes[i][PE.target]) > rule['max_out']: message += _(u"节点的出度最大为%s,最小为%s\n") % (rule['max_out'], rule['min_out']) if message: result['failed_nodes'].append(i) result["message"][i] = message if result['failed_nodes']: raise ConnectionValidateError(failed_nodes=result['failed_nodes'], detail=result['message'])
def validate_stream(tree): """ validate flow stream :param tree: pipeline tree :return: """ # data preparation start_event_id = tree[PE.start_event][PE.id] end_event_id = tree[PE.end_event][PE.id] nodes = get_nodes_dict(tree) nodes[start_event_id][STREAM] = {MAIN_STREAM} nodes[end_event_id][STREAM] = {MAIN_STREAM} parallel_converges = {} visited = set({}) for nid, node in nodes.items(): node.setdefault(STREAM, set()) # set allow streams for parallel's converge if node[PE.type] in PARALLEL_GATEWAYS: parallel_converges[node[PE.converge_gateway_id]] = { P_STREAM: streams_for_parallel(node), P: nid } # build stream from start node_queue = Queue.Queue() node_queue.put(nodes[start_event_id]) while not node_queue.empty(): # get node node = node_queue.get() if node[PE.id] in visited: # flow again to validate stream, but do not add target to queue flowing(where=node, to=nodes, parallel_converges=parallel_converges) continue # add to queue for target_id in node[PE.target]: node_queue.put(nodes[target_id]) # mark as visited visited.add(node[PE.id]) # flow flowing(where=node, to=nodes, parallel_converges=parallel_converges) # data clean for nid, n in nodes.items(): if len(n[STREAM]) != 1: raise exceptions.StreamValidateError(node_id=nid) # replace set to str n[STREAM] = n[STREAM].pop() # isolate node check for __, node in nodes.items(): if not node[STREAM]: raise exceptions.IsolateNodeError() return nodes