def soap_wsse(payload_node, signing): '''Stores payload_node into a SOAP envelope and calculates the wsse signature Keyword arguments: payload_node - top node for the payload (lxml.Element) signing - signing object (eet.Signing) ''' # Prepare parser parser = etree.XMLParser(remove_blank_text=True, ns_clean=False) # Prepare IDs for header body_id = 'id-' + uuid.uuid4().hex cert_id = 'X509-' + uuid.uuid4().hex sig_id = 'SIG-' + uuid.uuid4().hex key_id = 'KI-' + uuid.uuid4().hex sec_token_id = 'STR-' + uuid.uuid4().hex values = dict(namespaces_dict) values.update({ 'body_id': body_id, 'cert_id': cert_id, 'sig_id': sig_id, 'key_id': key_id, 'sec_token_id': sec_token_id, 'sec_token': base64.b64encode(signing.get_cert_binary()).decode('utf8') }) # Create SOAP envelope envelope = etree.XML(envelope_template.substitute(values), parser=parser) # Find soap:Body body = find_node(envelope, 'Body', NS_SOAP_URL) # Fill in Trzby into soap:Body body.append(payload_node) # Calculate digest of soap:Body body_digest = calculate_node_digest(body) # Find ds:DigestValue and store the computed digest digest_node = find_node(envelope, 'DigestValue', NS_DS_URL) digest_node.text = base64.b64encode(body_digest) # Find ds:SignedInfo node and get normalized text of it signature_node = find_node(envelope, 'SignedInfo', NS_DS_URL) normalized_signing = get_normalized_subtree(signature_node, ['soap']) # FInd ds:SignatureValue and store there signature of ds:SignedInfo signature_value_node = find_node(envelope, 'SignatureValue', NS_DS_URL) signature_value_node.text = base64.b64encode( signing.sign_text(normalized_signing, 'sha256')) return envelope
def send_payment(self, payment): trzba_xml = payment.xml(self._signing) soap_xml = wsse.soap_wsse(trzba_xml, self._signing) soap_string = etree.tostring(soap_xml) open('/tmp/y.xml', 'wb').write(soap_string) resp = requests.post(self._eet_url, soap_string) resp.raise_for_status() try: reply = etree.XML(resp.content) # print(etree.tostring(reply, pretty_print=4)) header = utils.find_node(reply, 'Hlavicka', NS_EET_URL) except etree.XMLSyntaxError as e: raise eet_exceptions.BadResponse( 'Failed to parse response from server (%s)' % (str(e))) except eet_exceptions.NodeNotFound: raise eet_exceptions.BadResponse( 'Failed to process response - missing node Hlavicka') try: confirmation = utils.find_node(reply, 'Potvrzeni', NS_EET_URL) orig_bkp = utils.find_node(trzba_xml, 'bkp') bkp = header.get('bkp') # print('received bkp: %s, original: %s' % (bkp, orig_bkp.text)) if bkp != orig_bkp.text: raise eet_exceptions.BadResponse('Wrong BKP in response!') response = { 'date_received': header.get('dat_prij'), 'uuid_reply': header.get('uuid_zpravy'), 'bkp': bkp, 'fik': confirmation.get('fik'), 'test': confirmation.get('test') } except eet_exceptions.NodeNotFound: try: error = utils.find_node(reply, 'Chyba', NS_EET_URL) response = { 'date_rejected': header.get('dat_odmit'), 'fik': None, 'test': error.get('test'), 'kod': error.get('kod'), 'message': error.text } except eet_exceptions.NodeNotFound: raise eet_exceptions.BadResponse( 'Failed to get data from server response') return response
def get_conceptual_independence(data): comm = [] count = 0 comm_data = data["clusters"] for i in comm_data: temp = [] for j in i["nodes"]: if find_node_type(data, j): name = find_node(data, j) name = name.split(".")[-1] temp.append(name) comm.append(temp) community_score = [] for i in comm: score = 0 words = np.asarray(i) for j in words: for k in words: # print (distance.levenshtein(j, k)/max(len(j), len(k))) score += 1 - distance.levenshtein(j, k) / max( len(j), len(k)) community_score.append(score / len(i)**2) total_score = 0 for i in community_score: total_score += i total_score = total_score / len(community_score) return total_score
def modify_conds(label1, conds_dict, cop, ast): if label1 in conds_dict: # print label1 context = [] get_context(get_extern_while_body(cop), context) conds = conds_dict[label1] for tuple in conds: ifelem = tuple[0] new_vals = tuple[1] # print new_vals node_cop = find_node(cop, ifelem) node_ast = find_node(ast, ifelem) if node_cop: modify_cond(node_cop.cond, new_vals) if node_ast: modify_cond(node_ast.cond, new_vals)
def prob4_8(T1, T2): """ You have two very large binary trees: T1 with millions of nodes and T2 with hundreds of nodes. Create an algorithm to decide if T2 is a sub-tree of T1. """ def is_subtree(T1, T2): # Empty tree is *always* subset of another tree if T2 is None: return True # We've gone through both trees and ended at same time, means it's same if T1 is None and T2 is None: return True # If either one finishes first, then it's not the same if T1 is None or T2 is None: return False return is_subtree(T1.left, T2.left) and is_subtree(T1.right, T2.right) subtree = find_node(T1, T2.data) # We couldn't find the node if not subtree: return False return is_subtree(subtree, T2)
def get_modularity(data): for i in data["edges"]: if i["type"] == "inter_class_connections": data_read = i["relationship"] break g = nx.DiGraph() #Directed Graph for i in data_read: g.add_edge(find_node(data, i["properties"]["start"]), find_node(data, i["properties"]["end"])) B = nx.directed_modularity_matrix(g) node_list = g.nodes() comm = [] count = 0 comm_data = data["clusters"] for i in comm_data: temp = [] for j in i["nodes"]: if find_node_type(data, j): temp.append(find_node(data, j)) comm.append(temp) count += len(temp) """ Calculating modularity score """ modularity_score = 0 for i, i_node in enumerate(node_list): for j, j_node in enumerate(node_list): value = check_community(i_node, j_node, comm) if value == 1: modularity_score += B.item((i, j)) number_of_edges = g.number_of_edges() score = modularity_score / number_of_edges return score
def get_paths_trees(ast, labels, labels_sorted, labelname): aux_dict = {} trees_dict = {} trees_paths_dict = {} used_old_vars = [] added_ifs_assignment = [] is_job = False labels_sorted.reverse() for label1 in labels_sorted: if label1 == "AUX_ROUND" or label1 == "ERR_ROUND": continue trees_list = [] trees_paths_list = [] labels_start = get_label(ast, labelname, label1) for label2 in labels_sorted: if label2 != label1: labels_end = get_label(ast, labelname, label2) # print label1, label2 for start in labels_start: # la final sa bag conditiile pt urmatorul element de iterat for end in labels_end: cop = duplicate_element(ast) # prune_tree(get_extern_while_body_from_func(cop),start,end,[],[]) dest_list = [] source_list = [] prune_tree(get_extern_while_body(cop), start, end, dest_list, source_list) if dest_list and source_list: context = [] get_context(get_extern_while_body(cop), context) add_ghost_assign_in_tree(ast, context, used_old_vars, added_ifs_assignment, aux_dict) for elem in aux_dict: test = find_node(cop, elem) # print generator.visit(elem.cond), aux_dict[elem] if test and test in context: # print test # print label1, label2 # print generator.visit(test.cond), "before" modify_cond(test.cond, aux_dict[elem]) # print generator.visit(test.cond), "after" # print generator.visit(elem.cond) # print generator.visit(cop), "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" # break # print generator.visit(ast) # for elem in context: # print generator.visit(elem.cond), elem.coord.line # list = [] # take_cond_name(elem.cond,list) # print list assign = get_label_assign_num(cop, labelname) # print get_extern_while_body(cop) if assign <= 2: check_if_gen = CheckIfGenerator(start, end) # print TreeGenerator().visit(get_extern_while_body(cop)) check_if_gen.visit(get_extern_while_body(cop)) # print check_if_gen.is_jumping # print check_if_gen.is_blocking # print "\n\nUNUL\n\n" # print ifs_to_dest if not check_if_gen.is_blocking and not check_if_gen.true_jump: # new_conds = add_ghost_assign_in_tree(cop, ifs_to_dest, label1) trees_list.append(cop) else: # if labels != labels_sorted: is_job = True aux, is_job_aux = find_all_paths_between_two_nodes( cop, start, end) test = take_2assigns_to_label_only( aux, labelname) if test: trees_paths_list.append(test) else: aux, is_job_aux = find_all_paths_between_two_nodes( cop, start, end) test = take_2assigns_to_label_only( aux, labelname) if test: if is_job_aux: is_job = True trees_paths_list.append(test) # aici bag conditiile trees_dict[label1] = trees_list trees_paths_dict[label1] = trees_paths_list if label1 == "FIFTH_ROUND" or label1 == "SIXTH_ROUND": print trees_dict[label1] print trees_paths_dict[label1] # break # print conds_dict.keys() # print generator.visit(ast) return trees_dict, trees_paths_dict, is_job
if T2 is a sub-tree of T1. """ def is_subtree(T1, T2): # Empty tree is *always* subset of another tree if T2 is None: return True # We've gone through both trees and ended at same time, means it's same if T1 is None and T2 is None: return True # If either one finishes first, then it's not the same if T1 is None or T2 is None: return False return is_subtree(T1.left, T2.left) and is_subtree(T1.right, T2.right) subtree = find_node(T1, T2.data) # We couldn't find the node if not subtree: return False return is_subtree(subtree, T2) if __name__ == '__main__': tree = TreeNode(5, TreeNode(1), TreeNode(10)) root = TreeNode(100) root.left = tree another = TreeNode(5, TreeNode(1), TreeNode(10)) print(find_node(root, 5))
def find_all_paths_util(current_node, source_node, dest_node, path, parent_list, grandparent_list, paths_list, source_reached, tree, last_if, parent_index, last_if_child, is_job): """ :param current_node: program's extern while loop body just pruned with prune_tree function in the first place, then changes at each recursive call :param source_node: currently used with Assignment object, but it can be anything :param dest_node: currently used with Assignment object, but it can be anything :param path: a list where all the nodes along the path are stored :param parent_list: a list with all the parents that were visited :param grandparent_list: a list with all the grandparents that were visited If after visiting the children of a node we want to continue the search, we have to move to the next brother on the right side of that node. So we identify that brother by knowing the parent - the node that was just visited - and the grandparent, i.e, the parent of the if node :param paths_list: a list of tuples; a tuple is a pair of (tree, path), where the path is the path argument, i.e, a list of nodes and the tree is a clone of original tree modified to reflect the path. So each valid path discovered has its own tree. :param source_reached: boolean var that is set to True if the source was reached :param tree: the tree we are currently working with; for each modification, a clone is created, the nodes are moved/deleted/inserted by the case and the clone (now modified) is passed to the next recursive call :param last_if: a reference to an If node; the new discovered nodes have to be moved from their current place to become his children; if this argument is None, the discovered nodes remain in their places. it changes from None when an artificial else branch is created or there is a jump of rounds or a blocking while loop :param parent_index: sometimes the node of which the next right side brother is needed to continue the search is already deleted, so we have to keep in this list the node's old index so we can still detect his right side brother :param last_if_child: boolean var that is set according to the position of the current node as a child of his parent. if the current node is not the rightmost child of his parent, i.e, it has some brothers not yet visited in the right, we forbid this node to go back in the hierarchy more than 1 level to continue the search; if it would do that, the paths that will be find from those higher levels won't contain his right side brothers and this is not logical correct. :param is_job: is (j)ump (o)r (b)locking - if in the process of computing the paths the algorithm discovers a jump of rounds or a blocking while loop this argument is set to True and is used later in Round Generathing phase to wrap the rounds with the required if condition :return: nothing. at the end of the algorithm, the paths_list list given as an argument will be filled with tuples of (tree, path). is_job will also say if we need a wrapping """ to_delete = [] ok = True if current_node is not None: grandparent = current_node path.append(grandparent) ok = True for tupleChild in current_node.children(): child = tupleChild[1] parent = child last_if_child_aux = True if isinstance(child, If): # exploring the tree - there's an if node and we recusively explore it check_if_gen = CheckIfGenerator(source_node, dest_node) check_if_gen.visit(child.iftrue) jump_on_iftrue = check_if_gen.is_jumping blocking_on_iftrue = check_if_gen.is_blocking jump_on_iffalse = False blocking_on_iffalse = False if check_if_gen.true_jump or check_if_gen.is_blocking: if not is_job: is_job.append(True) check_if_gen.true_jump = False check_if_gen.is_jumping = False check_if_gen.is_blocking = False if child.iffalse is not None: check_if_gen.visit(child.iffalse) jump_on_iffalse = check_if_gen.is_jumping blocking_on_iffalse = check_if_gen.is_blocking if check_if_gen.true_jump or check_if_gen.is_blocking: if not is_job: is_job.append(True) if not jump_on_iffalse and not jump_on_iftrue \ and not blocking_on_iftrue and not blocking_on_iffalse: path.append(child) if last_if is not None: if find_node(last_if, child) is None: last_if.block_items.append(child) to_delete.append(child) continue parent_list.append(parent) grandparent_list.append(grandparent) index = grandparent.block_items.index(parent) parent_index.append(index) other_children = current_node.block_items[(index + 1):] for other_child in other_children: if isinstance(other_child, If): last_if_child_aux = False break path1 = path[:] path2 = path[:] path1.append(parent) path2.append(parent) pi1 = parent_index[:] pi2 = parent_index[:] if jump_on_iftrue or blocking_on_iftrue: if child.iffalse is not None: # exploring the tree - jump on iftrue branch and there's an iffalse branch too new_tree_1 = duplicate_element(tree) new_tree_2 = duplicate_element(tree) new_parent_list_1 = [] new_grandparent_list_1 = [] new_grandparent_list_2 = [] new_parent_list_2 = [] for parent_node in parent_list: new_parent_list_1.append(find_node(new_tree_1, parent_node)) new_parent_list_2.append(find_node(new_tree_2, parent_node)) for grandparent_node in grandparent_list: new_grandparent_list_1.append(find_node(new_tree_1, grandparent_node)) new_grandparent_list_2.append(find_node(new_tree_2, grandparent_node)) if last_if is not None: # exploring the three - jump on iftrue and there's an iffalse branch too # last_if is valid last_if_in_new_tree_1 = find_node(new_tree_1, last_if) if find_node(last_if_in_new_tree_1, child) is None: last_if_in_new_tree_1.block_items.append(child) to_delete.append(child) child_copy = duplicate_element(child) last_if_in_new_tree_2 = find_node(new_tree_2, last_if) if find_node(last_if_in_new_tree_2, child) is None: last_if_in_new_tree_2.block_items.append(child_copy) if child not in to_delete: to_delete.append(child) new_grandparent_1 = find_node(new_tree_1, grandparent) new_grandparent_2 = find_node(new_tree_2, grandparent) for node in to_delete: new_grandparent_1.block_items.remove(node) new_grandparent_2.block_items.remove(node) if len(to_delete) >= 2: pi1[-1] = pi1[-1] - len(to_delete) + 1 pi2[-1] = pi2[-1] - len(to_delete) + 1 find_all_paths_util(child.iftrue, source_node, dest_node, path1, new_parent_list_1, new_grandparent_list_1, paths_list, source_reached, new_tree_1, last_if_in_new_tree_1, pi1, last_if_child_aux, is_job) find_all_paths_util(child.iffalse, source_node, dest_node, path2, new_parent_list_2, new_grandparent_list_2, paths_list, source_reached, new_tree_2, child_copy.iffalse, pi2, last_if_child_aux, is_job) else: # exploring the three - jump on iftrue and there's an iffalse branch too # last_if is None new_grandparent_1 = find_node(new_tree_1, grandparent) new_grandparent_2 = find_node(new_tree_2, grandparent) for node in to_delete: new_grandparent_1.block_items.remove(node) new_grandparent_2.block_items.remove(node) if len(to_delete) >= 2: pi1[-1] = pi1[-1] - len(to_delete) + 1 pi2[-1] = pi2[-1] - len(to_delete) + 1 new_last_if = find_node(new_tree_2, child.iffalse) find_all_paths_util(child.iftrue, source_node, dest_node, path1, new_parent_list_1, new_grandparent_list_1, paths_list, source_reached, new_tree_1, None, pi1, last_if_child_aux, is_job) find_all_paths_util(child.iffalse, source_node, dest_node, path2, new_parent_list_2, new_grandparent_list_2, paths_list, source_reached, new_tree_2, new_last_if, pi2, last_if_child_aux, is_job) else: # exploring the three - jump on iftrue and there's no iffalse branch new_tree_1 = duplicate_element(tree) new_parent_list_1 = [] new_grandparent_list_1 = [] for parent_node in parent_list: new_parent_list_1.append(find_node(new_tree_1, parent_node)) for grandparent_node in grandparent_list: new_grandparent_list_1.append(find_node(new_tree_1, grandparent_node)) if last_if is not None: # exploring the three - jump on iftrue and there's no iffalse branch # last_if is valid last_if_in_new_tree_1 = find_node(new_tree_1, last_if) if find_node(last_if_in_new_tree_1, child) is None: last_if_in_new_tree_1.block_items.append(child) to_delete.append(child) new_grandparent_1 = find_node(new_tree_1, grandparent) for node in to_delete: new_grandparent_1.block_items.remove(node) if len(to_delete) >= 2: pi1[-1] = pi1[-1] - len(to_delete) + 1 find_all_paths_util(child.iftrue, source_node, dest_node, path1, new_parent_list_1, new_grandparent_list_1, paths_list, source_reached, new_tree_1, last_if_in_new_tree_1, pi1, last_if_child_aux, is_job) else: # exploring the three - jump on iftrue there's no iffalse branch # last_if is None new_grandparent_1 = find_node(new_tree_1, grandparent) for node in to_delete: new_grandparent_1.block_items.remove(node) if len(to_delete) >= 2: pi1[-1] = pi1[-1] - len(to_delete) + 1 find_all_paths_util(child.iftrue, source_node, dest_node, path1, new_parent_list_1, new_grandparent_list_1, paths_list, source_reached, new_tree_1, None, pi1, last_if_child_aux, is_job) new_tree = duplicate_element(tree) new_parent = find_node(new_tree, child) gen = c_generator.CGenerator() condition = '' condition += gen.visit(child.cond) new_parent.cond = ID(condition, child.coord) new_parent.iffalse = Compound([], new_parent.iftrue.coord) new_parent.iftrue = None path.append(new_parent) path.append(new_parent.iffalse) new_parent_list = [] new_grandparent_list = [] for parent_node in parent_list: new_parent_list.append(find_node(new_tree, parent_node)) for grandparent_node in grandparent_list: new_grandparent_list.append(find_node(new_tree, grandparent_node)) if last_if is not None: last_if_in_new_tree = find_node(new_tree, last_if) if find_node(last_if_in_new_tree, new_parent) is None: last_if_in_new_tree.block_items.append(new_parent) new_grandparent = find_node(new_tree, grandparent) for node in to_delete: new_grandparent.block_items.remove(node) if len(to_delete) >= 2: pi2[-1] = pi2[-1] - len(to_delete) + 1 find_all_paths_util(None, source_node, dest_node, path, new_parent_list, new_grandparent_list, paths_list, source_reached, new_tree, new_parent.iffalse, pi2, last_if_child_aux, is_job) elif jump_on_iffalse or blocking_on_iffalse: new_tree_1 = duplicate_element(tree) new_tree_2 = duplicate_element(tree) new_parent_list_1 = [] new_grandparent_list_1 = [] new_grandparent_list_2 = [] new_parent_list_2 = [] for parent_node in parent_list: new_parent_list_1.append(find_node(new_tree_1, parent_node)) new_parent_list_2.append(find_node(new_tree_2, parent_node)) for grandparent_node in grandparent_list: new_grandparent_list_1.append(find_node(new_tree_1, grandparent_node)) new_grandparent_list_2.append(find_node(new_tree_2, grandparent_node)) if last_if is not None: # exploring the tree - jump on iffalse branch # last_if is valid child_copy = duplicate_element(child) last_if_in_new_tree_1 = find_node(new_tree_1, last_if) if find_node(last_if_in_new_tree_1, child) is None: last_if_in_new_tree_1.block_items.append(child_copy) to_delete.append(child) last_if_in_new_tree_2 = find_node(new_tree_2, last_if) if find_node(last_if_in_new_tree_2, child) is None: last_if_in_new_tree_2.block_items.append(child) if child not in to_delete: to_delete.append(child) new_grandparent_1 = find_node(new_tree_1, grandparent) new_grandparent_2 = find_node(new_tree_2, grandparent) for node in to_delete: new_grandparent_1.block_items.remove(node) new_grandparent_2.block_items.remove(node) if len(to_delete) >= 2: pi1[-1] = pi1[-1] - len(to_delete) + 1 pi2[-1] = pi2[-1] - len(to_delete) + 1 find_all_paths_util(child.iftrue, source_node, dest_node, path1, new_parent_list_1, new_grandparent_list_1, paths_list, source_reached, new_tree_1, child_copy.iftrue, pi1, last_if_child_aux, is_job) find_all_paths_util(child.iffalse, source_node, dest_node, path2, new_parent_list_2, new_grandparent_list_2, paths_list, source_reached, new_tree_2, last_if_in_new_tree_2, pi2, last_if_child_aux, is_job) else: # exploring the tree - jump on iffalse branch # last_if is None new_grandparent_1 = find_node(new_tree_1, grandparent) new_grandparent_2 = find_node(new_tree_2, grandparent) for node in to_delete: new_grandparent_1.block_items.remove(node) new_grandparent_2.block_items.remove(node) if len(to_delete) >= 2: pi1[-1] = pi1[-1] - len(to_delete) + 1 pi2[-1] = pi2[-1] - len(to_delete) + 1 new_last_if = find_node(new_tree_1, child.iftrue) find_all_paths_util(child.iftrue, source_node, dest_node, path1, new_parent_list_1, new_grandparent_list_1, paths_list, source_reached, new_tree_1, new_last_if, pi1, last_if_child_aux, is_job) find_all_paths_util(child.iffalse, source_node, dest_node, path2, new_parent_list_2, new_grandparent_list_2, paths_list, source_reached, new_tree_2, None, pi2, last_if_child_aux, is_job) ok = False break else: # exploring the tree - normal node != if node path.append(child) if last_if is not None: if find_node(last_if, child) is None: last_if.block_items.append(child) to_delete.append(child) if child == source_node: source_reached = True if child == dest_node: if source_reached is True: paths_list.append((tree, path)) ok = False break for node in to_delete: current_node.block_items.remove(node) # back on the ancestors to continue the tree exploration if parent_list and grandparent_list and ok is True: while grandparent_list: to_delete = [] grandparent = grandparent_list.pop() parent = parent_list.pop() p_index = parent_index.pop() j = 0 found_parent = False for j, tupleChild in enumerate(grandparent.children()): if tupleChild[1] == parent: found_parent = True break if found_parent: remained_children = grandparent.children()[(j + 1):] else: remained_children = grandparent.children()[p_index:] return_after_call = False for tupleChild in remained_children: child = tupleChild[1] parent = child last_if_child_aux = True if isinstance(child, If): # back on the ancestors - if node left unvisited check_if_gen = CheckIfGenerator(source_node, dest_node) check_if_gen.visit(child.iftrue) jump_on_iftrue = check_if_gen.is_jumping blocking_on_iftrue = check_if_gen.is_blocking if check_if_gen.true_jump or check_if_gen.is_blocking: if not is_job: is_job.append(True) jump_on_iffalse = False blocking_on_iffalse = False check_if_gen.is_jumping = False check_if_gen.is_blocking = False check_if_gen.true_jump = False if child.iffalse is not None: check_if_gen.visit(child.iffalse) jump_on_iffalse = check_if_gen.is_jumping blocking_on_iffalse = check_if_gen.is_blocking if check_if_gen.true_jump or check_if_gen.is_blocking: if not is_job: is_job.append(True) if not jump_on_iffalse and not jump_on_iftrue \ and not blocking_on_iftrue and not blocking_on_iffalse: path.append(child) if last_if is not None: if find_node(last_if, child) is None: last_if.block_items.append(child) to_delete.append(child) if remained_children.index(tupleChild) == len(remained_children) - 1: last_if_child = True continue path1 = path[:] path2 = path[:] path1.append(parent) path2.append(parent) index = grandparent.block_items.index(parent) other_children = grandparent.block_items[(index + 1):] for other_child in other_children: if isinstance(other_child, If): last_if_child_aux = False break pl1 = parent_list[:] pl1.append(parent) gp1 = grandparent_list[:] gp1.append(grandparent) pl2 = parent_list[:] pl2.append(parent) gp2 = grandparent_list[:] gp2.append(grandparent) pi1 = parent_index[:] pi1.append(grandparent.block_items.index(parent)) pi2 = parent_index[:] pi2.append(grandparent.block_items.index(parent)) if jump_on_iftrue or blocking_on_iftrue: if child.iffalse is not None: # exploring the tree - jump on iftrue and there's an iffalse branch new_tree_1 = duplicate_element(tree) new_tree_2 = duplicate_element(tree) new_parent_list_1 = [] new_grandparent_list_1 = [] new_grandparent_list_2 = [] new_parent_list_2 = [] for parent_node in pl1: new_parent_list_1.append(find_node(new_tree_1, parent_node)) new_parent_list_2.append(find_node(new_tree_2, parent_node)) for grandparent_node in gp1: new_grandparent_list_1.append(find_node(new_tree_1, grandparent_node)) new_grandparent_list_2.append(find_node(new_tree_2, grandparent_node)) if last_if is not None: # exploring the tree - jump on iftrue and there's an iffalse branch # last_if is valid last_if_in_new_tree_1 = find_node(new_tree_1, last_if) if find_node(last_if_in_new_tree_1, child) is None: last_if_in_new_tree_1.block_items.append(child) to_delete.append(child) child_copy = duplicate_element(child) last_if_in_new_tree_2 = find_node(new_tree_2, last_if) if find_node(last_if_in_new_tree_2, child) is None: last_if_in_new_tree_2.block_items.append(child_copy) if child not in to_delete: to_delete.append(child) new_grandparent_1 = find_node(new_tree_1, grandparent) new_grandparent_2 = find_node(new_tree_2, grandparent) for node in to_delete: new_grandparent_1.block_items.remove(node) new_grandparent_2.block_items.remove(node) if len(to_delete) >= 2: pi1[-1] = pi1[-1] - len(to_delete) + 1 pi2[-1] = pi2[-1] - len(to_delete) + 1 find_all_paths_util(child.iftrue, source_node, dest_node, path1, new_parent_list_1, new_grandparent_list_1, paths_list, source_reached, new_tree_1, last_if_in_new_tree_1, pi1, last_if_child_aux, is_job) find_all_paths_util(child.iffalse, source_node, dest_node, path2, new_parent_list_2, new_grandparent_list_2, paths_list, source_reached, new_tree_2, child_copy.iffalse, pi2, last_if_child_aux, is_job) else: # exploring the tree - jump on iftrue and there's an iffalse branch # last_if is None new_grandparent_1 = find_node(new_tree_1, grandparent) new_grandparent_2 = find_node(new_tree_2, grandparent) for node in to_delete: new_grandparent_1.block_items.remove(node) new_grandparent_2.block_items.remove(node) if len(to_delete) >= 2: pi1[-1] = pi1[-1] - len(to_delete) + 1 pi2[-1] = pi2[-1] - len(to_delete) + 1 new_last_if = find_node(new_tree_2, child.iffalse) find_all_paths_util(child.iftrue, source_node, dest_node, path1, new_parent_list_1, new_grandparent_list_1, paths_list, source_reached, new_tree_1, None, pi1, last_if_child_aux, is_job) find_all_paths_util(child.iffalse, source_node, dest_node, path2, new_parent_list_2, new_grandparent_list_2, paths_list, source_reached, new_tree_2, new_last_if, pi2, last_if_child_aux, is_job) else: # exploring the tree - jump on iftrue and there's no iffalse branch new_tree_1 = duplicate_element(tree) new_parent_list_1 = [] new_grandparent_list_1 = [] for parent_node in pl1: new_parent_list_1.append(find_node(new_tree_1, parent_node)) for grandparent_node in gp1: new_grandparent_list_1.append(find_node(new_tree_1, grandparent_node)) if last_if is not None: # exploring the tree - jump on iftrue and there's no iffalse branch # last_if is valid last_if_in_new_tree_1 = find_node(new_tree_1, last_if) if find_node(last_if_in_new_tree_1, child) is None: last_if_in_new_tree_1.block_items.append(child) to_delete.append(child) new_grandparent_1 = find_node(new_tree_1, grandparent) for node in to_delete: new_grandparent_1.block_items.remove(node) if len(to_delete) >= 2: pi1[-1] = pi1[-1] - len(to_delete) + 1 find_all_paths_util(child.iftrue, source_node, dest_node, path1, new_parent_list_1, new_grandparent_list_1, paths_list, source_reached, new_tree_1, last_if_in_new_tree_1, pi1, last_if_child_aux, is_job) else: # exploring the tree - jump on iftrue and there's no iffalse branch # last_if is None new_grandparent_1 = find_node(new_tree_1, grandparent) for node in to_delete: new_grandparent_1.block_items.remove(node) if len(to_delete) >= 2: pi1[-1] = pi1[-1] - len(to_delete) + 1 find_all_paths_util(child.iftrue, source_node, dest_node, path1, new_parent_list_1, new_grandparent_list_1, paths_list, source_reached, new_tree_1, None, pi1, last_if_child_aux, is_job) new_tree = duplicate_element(tree) new_parent = find_node(new_tree, child) gen = c_generator.CGenerator() condition = '' condition += gen.visit(child.cond) new_parent.cond = ID(condition, child.coord) new_parent.iffalse = Compound([], new_parent.iftrue.coord) new_parent.iftrue = None path.append(new_parent) path.append(new_parent.iffalse) new_parent_list = [] new_grandparent_list = [] for parent_node in pl2: new_parent_list.append(find_node(new_tree, parent_node)) for grandparent_node in gp2: new_grandparent_list.append(find_node(new_tree, grandparent_node)) if last_if is not None: last_if_in_new_tree = find_node(new_tree, last_if) if find_node(last_if_in_new_tree, new_parent) is None: last_if_in_new_tree.block_items.append(new_parent) new_grandparent = find_node(new_tree, grandparent) for node in to_delete: new_grandparent.block_items.remove(node) if len(to_delete) >= 2: pi2[-1] = pi2[-1] - len(to_delete) + 1 find_all_paths_util(None, source_node, dest_node, path, new_parent_list, new_grandparent_list, paths_list, source_reached, new_tree, new_parent.iffalse, pi2, last_if_child_aux, is_job) elif jump_on_iffalse or blocking_on_iffalse: new_tree_1 = duplicate_element(tree) new_tree_2 = duplicate_element(tree) new_parent_list_1 = [] new_grandparent_list_1 = [] new_grandparent_list_2 = [] new_parent_list_2 = [] for parent_node in pl1: new_parent_list_1.append(find_node(new_tree_1, parent_node)) new_parent_list_2.append(find_node(new_tree_2, parent_node)) for grandparent_node in gp1: new_grandparent_list_1.append(find_node(new_tree_1, grandparent_node)) new_grandparent_list_2.append(find_node(new_tree_2, grandparent_node)) if last_if is not None: # exploring the tree - jump on iffalse # last_if is valid child_copy = duplicate_element(child) last_if_in_new_tree_1 = find_node(new_tree_1, last_if) if find_node(last_if_in_new_tree_1, child) is None: last_if_in_new_tree_1.block_items.append(child_copy) to_delete.append(child) last_if_in_new_tree_2 = find_node(new_tree_2, last_if) if find_node(last_if_in_new_tree_2, child) is None: last_if_in_new_tree_2.block_items.append(child) if child not in to_delete: to_delete.append(child) new_grandparent_1 = find_node(new_tree_1, grandparent) new_grandparent_2 = find_node(new_tree_2, grandparent) for node in to_delete: new_grandparent_1.block_items.remove(node) new_grandparent_2.block_items.remove(node) if len(to_delete) >= 2: pi1[-1] = pi1[-1] - len(to_delete) + 1 pi2[-1] = pi2[-1] - len(to_delete) + 1 find_all_paths_util(child.iftrue, source_node, dest_node, path1, new_parent_list_1, new_grandparent_list_1, paths_list, source_reached, new_tree_1, child_copy.iftrue, pi1, last_if_child_aux, is_job) find_all_paths_util(child.iffalse, source_node, dest_node, path2, new_parent_list_2, new_grandparent_list_2, paths_list, source_reached, new_tree_2, last_if_in_new_tree_2, pi2, last_if_child_aux, is_job) else: # exploring the tree - jump on iffalse # last_if is None new_grandparent_1 = find_node(new_tree_1, grandparent) new_grandparent_2 = find_node(new_tree_2, grandparent) for node in to_delete: new_grandparent_1.block_items.remove(node) new_grandparent_2.block_items.remove(node) if len(to_delete) >= 2: pi1[-1] = pi1[-1] - len(to_delete) + 1 pi2[-1] = pi2[-1] - len(to_delete) + 1 new_last_if = find_node(new_tree_1, child.iftrue) find_all_paths_util(child.iftrue, source_node, dest_node, path1, new_parent_list_1, new_grandparent_list_1, paths_list, source_reached, new_tree_1, new_last_if, pi1, last_if_child_aux, is_job) find_all_paths_util(child.iffalse, source_node, dest_node, path2, new_parent_list_2, new_grandparent_list_2, paths_list, source_reached, new_tree_2, None, pi2, last_if_child_aux, is_job) return_after_call = True break else: # back on the ancestors - normal children are handled on this case, not if nodes path.append(child) if last_if is not None: if find_node(last_if, child) is None: last_if.block_items.append(child) to_delete.append(child) if child == source_node: source_reached = True if child == dest_node: if source_reached is True: paths_list.append((tree, path)) break for node in to_delete: grandparent.block_items.remove(node) if last_if_child is False or return_after_call: break