Пример #1
0
def recursively_add_tree(tree,
                         net,
                         initial_entity_subtree,
                         final_entity_subtree,
                         counts,
                         rec_depth,
                         force_add_skip=False):
    """
    Recursively add the subtrees to the Petri net

    Parameters
    -----------
    tree
        Current subtree
    net
        Petri net
    initial_entity_subtree
        Initial entity (place/transition) that should be attached from the subtree
    final_entity_subtree
        Final entity (place/transition) that should be attached from the subtree
    counts
        Counts object (keeps the number of places, transitions and hidden transitions)
    rec_depth
        Recursion depth of the current iteration
    force_add_skip
        Boolean value that tells if the addition of a skip is mandatory

    Returns
    ----------
    net
        Updated Petri net
    counts
        Updated counts object (keeps the number of places, transitions and hidden transitions)
    final_place
        Last place added in this recursion
    """
    if type(initial_entity_subtree) is PetriNet.Transition:
        initial_place = get_new_place(counts)
        net.places.add(initial_place)
        petri.utils.add_arc_from_to(initial_entity_subtree, initial_place, net)
    else:
        initial_place = initial_entity_subtree
    if final_entity_subtree is not None and type(
            final_entity_subtree) is PetriNet.Place:
        final_place = final_entity_subtree
    else:
        final_place = get_new_place(counts)
        net.places.add(final_place)
        if final_entity_subtree is not None and type(
                final_entity_subtree) is PetriNet.Transition:
            petri.utils.add_arc_from_to(final_place, final_entity_subtree, net)
    tree_childs = [child for child in tree.children]

    if tree.operator is None:
        trans = tree
        if trans.label is None:
            petri_trans = get_new_hidden_trans(counts, type_trans="skip")
        else:
            petri_trans = get_transition(counts, trans.label)
        net.transitions.add(petri_trans)
        petri.utils.add_arc_from_to(initial_place, petri_trans, net)
        petri.utils.add_arc_from_to(petri_trans, final_place, net)

    if tree.operator == Operator.XOR:
        for subtree in tree_childs:
            net, counts, intermediate_place = recursively_add_tree(
                subtree,
                net,
                initial_place,
                final_place,
                counts,
                rec_depth + 1,
                force_add_skip=force_add_skip)
    elif tree.operator == Operator.PARALLEL:
        new_initial_trans = get_new_hidden_trans(counts, type_trans="tauSplit")
        net.transitions.add(new_initial_trans)
        petri.utils.add_arc_from_to(initial_place, new_initial_trans, net)
        new_final_trans = get_new_hidden_trans(counts, type_trans="tauJoin")
        net.transitions.add(new_final_trans)
        petri.utils.add_arc_from_to(new_final_trans, final_place, net)

        for subtree in tree_childs:
            net, counts, intermediate_place = recursively_add_tree(
                subtree,
                net,
                new_initial_trans,
                new_final_trans,
                counts,
                rec_depth + 1,
                force_add_skip=force_add_skip)
    elif tree.operator == Operator.SEQUENCE:
        intermediate_place = initial_place
        for i in range(len(tree_childs)):
            final_connection_place = None
            if i == len(tree_childs) - 1:
                final_connection_place = final_place
            net, counts, intermediate_place = recursively_add_tree(
                tree_childs[i],
                net,
                intermediate_place,
                final_connection_place,
                counts,
                rec_depth + 1,
                force_add_skip=force_add_skip)
    elif tree.operator == Operator.LOOP:
        loop_trans = get_new_hidden_trans(counts, type_trans="loop")
        net.transitions.add(loop_trans)
        if len(tree_childs) == 1:
            net, counts, intermediate_place = recursively_add_tree(
                tree_childs[0],
                net,
                initial_place,
                final_place,
                counts,
                rec_depth + 1,
                force_add_skip=force_add_skip)
            petri.utils.add_arc_from_to(final_place, loop_trans, net)
            petri.utils.add_arc_from_to(loop_trans, initial_place, net)
        else:
            if len(tree_childs) == 2:
                # IMDFA method
                net, counts, int1 = recursively_add_tree(tree_childs[0],
                                                         net,
                                                         initial_place,
                                                         None,
                                                         counts,
                                                         rec_depth + 1,
                                                         force_add_skip=True)
                net, counts, int2 = recursively_add_tree(tree_childs[1],
                                                         net,
                                                         int1,
                                                         final_place,
                                                         counts,
                                                         rec_depth + 1,
                                                         force_add_skip=True)
                looping_place = final_place
            else:
                # IMDFB method
                net, counts, int1 = recursively_add_tree(
                    tree_childs[0],
                    net,
                    initial_place,
                    None,
                    counts,
                    rec_depth + 1,
                    force_add_skip=force_add_skip)

                if tree_childs[2].children and (
                        tree_childs[2].children[0].operator is None
                        and tree_childs[2].children[0].label is None):
                    # when REDO and EXIT part are united
                    net, counts, int2 = recursively_add_tree(
                        tree_childs[1],
                        net,
                        int1,
                        final_place,
                        counts,
                        rec_depth + 1,
                        force_add_skip=force_add_skip)
                else:
                    # otherwise
                    net, counts, int2 = recursively_add_tree(
                        tree_childs[1],
                        net,
                        int1,
                        None,
                        counts,
                        rec_depth + 1,
                        force_add_skip=force_add_skip)
                    net, counts, int3 = recursively_add_tree(
                        tree_childs[2],
                        net,
                        int1,
                        final_place,
                        counts,
                        rec_depth + 1,
                        force_add_skip=force_add_skip)

                looping_place = int2
            petri.utils.add_arc_from_to(looping_place, loop_trans, net)
            petri.utils.add_arc_from_to(loop_trans, initial_place, net)
    if force_add_skip:
        skip_trans = get_new_hidden_trans(counts, type_trans="skip")
        net.transitions.add(skip_trans)
        petri.utils.add_arc_from_to(initial_place, skip_trans, net)
        petri.utils.add_arc_from_to(skip_trans, final_place, net)

    return net, counts, final_place
Пример #2
0
def recursively_add_tree(tree, net, initial_entity_subtree,
                         final_entity_subtree, counts, index, node_end,
                         parameters):
    parameters = {} if parameters is None else parameters
    param_child_lock = False if parameters.get(
        'PARAM_CHILD_LOCK') is None else parameters['PARAM_CHILD_LOCK']
    param_loop_lock = False if parameters.get(
        'PARAM_LOOP_LOCK') is None else parameters['PARAM_LOOP_LOCK']
    param_lock = False or param_child_lock or param_loop_lock \
        if parameters.get('PARAM_LOCK') is None else parameters['PARAM_LOCK'] or param_child_lock or param_loop_lock

    cur_pos = index
    if type(initial_entity_subtree) is PetriNet.Transition:
        initial_place = get_new_place(counts)
        net.places.add(initial_place)
        petri.utils.add_arc_from_to(initial_entity_subtree, initial_place, net)
    else:
        initial_place = initial_entity_subtree
    if final_entity_subtree is not None and type(
            final_entity_subtree) is PetriNet.Place:
        final_place = final_entity_subtree
    else:
        final_place = get_new_place(counts)
        net.places.add(final_place)
        if final_entity_subtree is not None and type(
                final_entity_subtree) is PetriNet.Transition:
            petri.utils.add_arc_from_to(final_place, final_entity_subtree, net)

    intermediate_place_s, intermediate_place_e = initial_place, final_place
    if param_lock and ((param_child_lock or tree.operator is not None) and not param_loop_lock) or \
            (param_loop_lock and (tree.parent is not None and tree.parent.operator == Operator.LOOP
                                  or (tree.label is None and tree.operator is None))):
        # if ((param_child_lock or tree.operator is not None) and not param_loop_lock) or \
        #         (param_loop_lock and (tree.parent is not None and tree.parent.operator == Operator.LOOP)):
        intermediate_place_s = get_new_place(counts)
        net.places.add(intermediate_place_s)
        # petri_trans = get_new_hidden_trans(counts, type_trans=str(index)+"_start")
        petri_trans = get_transition(counts, str(cur_pos) + LOCK_START)
        net.transitions.add(petri_trans)

        petri.utils.add_arc_from_to(initial_place, petri_trans, net)
        petri.utils.add_arc_from_to(petri_trans, intermediate_place_s, net)

        intermediate_place_e = get_new_place(counts)
        net.places.add(intermediate_place_e)
        # petri_trans = get_new_hidden_trans(counts, type_trans=str(index)+"_end")
        petri_trans = get_transition(counts, str(cur_pos) + LOCK_END)
        net.transitions.add(petri_trans)
        petri.utils.add_arc_from_to(intermediate_place_e, petri_trans, net)
        petri.utils.add_arc_from_to(petri_trans, final_place, net)

    if tree.operator is not None:
        # intermediate_place_s = initial_place
        # intermediate_place_e = final_place

        tree_children = [child for child in tree.children]
        if tree.operator == Operator.XOR:
            for subtree in tree_children:
                recursively_add_tree(subtree, net, intermediate_place_s,
                                     intermediate_place_e, counts, index + 1,
                                     node_end, parameters)
                index = node_end[index + 1]

        elif tree.operator == Operator.PARALLEL:
            new_initial_trans = get_new_hidden_trans(counts,
                                                     type_trans=str(cur_pos) +
                                                     "_tau")
            net.transitions.add(new_initial_trans)
            petri.utils.add_arc_from_to(intermediate_place_s,
                                        new_initial_trans, net)
            new_final_trans = get_new_hidden_trans(counts,
                                                   type_trans=str(cur_pos) +
                                                   "_tau")
            net.transitions.add(new_final_trans)
            petri.utils.add_arc_from_to(new_final_trans, intermediate_place_e,
                                        net)

            for subtree in tree_children:
                recursively_add_tree(subtree, net, new_initial_trans,
                                     new_final_trans, counts, index + 1,
                                     node_end, parameters)
                index = node_end[index + 1]

        elif tree.operator == Operator.SEQUENCE:
            intermediate_place = intermediate_place_s
            for i in range(len(tree_children)):
                if i == len(tree_children) - 1:
                    final_connection_place = intermediate_place_e
                else:
                    final_connection_place = None
                intermediate_place = recursively_add_tree(
                    tree_children[i], net, intermediate_place,
                    final_connection_place, counts, index + 1, node_end,
                    parameters)
                index = node_end[index + 1]

        elif tree.operator == Operator.LOOP:
            if not param_child_lock:
                petri_trans = get_new_hidden_trans(counts,
                                                   type_trans=str(cur_pos) +
                                                   "_tau")
                net.transitions.add(petri_trans)
                petri.utils.add_arc_from_to(intermediate_place_s, petri_trans,
                                            net)
                intermediate_place_s = get_new_place(counts)
                net.places.add(intermediate_place_s)
                petri.utils.add_arc_from_to(petri_trans, intermediate_place_s,
                                            net)

            intermediate_place = recursively_add_tree(tree_children[0], net,
                                                      intermediate_place_s,
                                                      None, counts, index + 1,
                                                      node_end, parameters)
            index = node_end[index + 1]
            recursively_add_tree(tree_children[1], net, intermediate_place,
                                 intermediate_place_s, counts, index + 1,
                                 node_end, parameters)
            # if not param_lock:
            #     loop_trans = get_new_hidden_trans(counts, type_trans=str(cur_pos) + "_tau")
            #     net.transitions.add(loop_trans)
            #     petri.utils.add_arc_from_to(intermediate_place, loop_trans, net)
            #     petri.utils.add_arc_from_to(loop_trans, intermediate_place_e, net)
            # else:
            index = node_end[index + 1]
            recursively_add_tree(tree_children[2], net, intermediate_place,
                                 intermediate_place_e, counts, index + 1,
                                 node_end, parameters)

    elif tree.operator is None:
        if tree.label is None:
            petri_trans = get_new_hidden_trans(counts,
                                               type_trans=str(cur_pos) +
                                               "_skip")
        else:
            petri_trans = get_transition(counts, tree.label)
        net.transitions.add(petri_trans)
        petri.utils.add_arc_from_to(intermediate_place_s, petri_trans, net)
        petri.utils.add_arc_from_to(petri_trans, intermediate_place_e, net)

    return final_place
Пример #3
0
def recursively_add_tree(tree,
                         net,
                         initial_entity_subtree,
                         final_entity_subtree,
                         counts,
                         rec_depth,
                         force_add_skip=False):
    """
    Recursively add the subtrees to the Petri net

    Parameters
    -----------
    tree
        Current subtree
    net
        Petri net
    initial_entity_subtree
        Initial entity (place/transition) that should be attached from the subtree
    final_entity_subtree
        Final entity (place/transition) that should be attached from the subtree
    counts
        Counts object (keeps the number of places, transitions and hidden transitions)
    rec_depth
        Recursion depth of the current iteration
    force_add_skip
        Boolean value that tells if the addition of a skip is mandatory

    Returns
    ----------
    net
        Updated Petri net
    counts
        Updated counts object (keeps the number of places, transitions and hidden transitions)
    final_place
        Last place added in this recursion
    """
    if type(initial_entity_subtree) is PetriNet.Transition:
        initial_place = get_new_place(counts)
        net.places.add(initial_place)
        petri.utils.add_arc_from_to(initial_entity_subtree, initial_place, net)
    else:
        initial_place = initial_entity_subtree
    if final_entity_subtree is not None and type(
            final_entity_subtree) is PetriNet.Place:
        final_place = final_entity_subtree
    else:
        final_place = get_new_place(counts)
        net.places.add(final_place)
        if final_entity_subtree is not None and type(
                final_entity_subtree) is PetriNet.Transition:
            petri.utils.add_arc_from_to(final_place, final_entity_subtree, net)
    tree_subtrees = [
        child for child in tree.children
        if type(child) is process_tree.ProcessTree
    ]
    tree_transitions = [
        child for child in tree.children
        if type(child) is process_tree.PTTransition
    ]

    for trans in tree_transitions:
        if trans.label is None:
            petri_trans = get_new_hidden_trans(counts, type_trans="skip")
        else:
            petri_trans = get_transition(counts, trans.label)
        net.transitions.add(petri_trans)
        petri.utils.add_arc_from_to(initial_place, petri_trans, net)
        petri.utils.add_arc_from_to(petri_trans, final_place, net)

    if tree.operator == tree_constants.EXCLUSIVE_OPERATOR:
        for subtree in tree_subtrees:
            net, counts, intermediate_place = recursively_add_tree(
                subtree,
                net,
                initial_place,
                final_place,
                counts,
                rec_depth + 1,
                force_add_skip=force_add_skip)
    elif tree.operator == tree_constants.PARALLEL_OPERATOR:
        new_initial_trans = get_new_hidden_trans(counts, type_trans="tauSplit")
        net.transitions.add(new_initial_trans)
        petri.utils.add_arc_from_to(initial_place, new_initial_trans, net)
        new_final_trans = get_new_hidden_trans(counts, type_trans="tauJoin")
        net.transitions.add(new_final_trans)
        petri.utils.add_arc_from_to(new_final_trans, final_place, net)

        for subtree in tree_subtrees:
            net, counts, intermediate_place = recursively_add_tree(
                subtree,
                net,
                new_initial_trans,
                new_final_trans,
                counts,
                rec_depth + 1,
                force_add_skip=force_add_skip)
    elif tree.operator == tree_constants.SEQUENTIAL_OPERATOR:
        intermediate_place = initial_place
        for i in range(len(tree_subtrees)):
            final_connection_place = None
            if i == len(tree_subtrees) - 1:
                final_connection_place = final_place
            net, counts, intermediate_place = recursively_add_tree(
                tree_subtrees[i],
                net,
                intermediate_place,
                final_connection_place,
                counts,
                rec_depth + 1,
                force_add_skip=force_add_skip)
    elif tree.operator == tree_constants.LOOP_OPERATOR:
        loop_trans = get_new_hidden_trans(counts, type_trans="loop")
        net.transitions.add(loop_trans)
        petri.utils.add_arc_from_to(final_place, loop_trans, net)
        petri.utils.add_arc_from_to(loop_trans, initial_place, net)

        if len(tree_subtrees) == 1:
            net, counts, intermediate_place = recursively_add_tree(
                tree_subtrees[0],
                net,
                initial_place,
                final_place,
                counts,
                rec_depth + 1,
                force_add_skip=True)
        else:
            intermediate_place = initial_place
            for i in range(len(tree_subtrees)):
                final_connection_place = None
                if i == len(tree_subtrees) - 1:
                    final_connection_place = final_place
                net, counts, intermediate_place = recursively_add_tree(
                    tree_subtrees[i],
                    net,
                    intermediate_place,
                    final_connection_place,
                    counts,
                    rec_depth + 1,
                    force_add_skip=True)
    if force_add_skip and tree_transitions:
        skip_trans = get_new_hidden_trans(counts, type_trans="skip")
        net.transitions.add(skip_trans)
        petri.utils.add_arc_from_to(initial_place, skip_trans, net)
        petri.utils.add_arc_from_to(skip_trans, final_place, net)

    return net, counts, final_place