def __add_wait_net(self):
     '''
     Words don't have the same length. To compare them we add a "wait" transition at the end of the model and the
     traces.
     :return:
     '''
     wait_transition = PetriNet.Transition(WAIT_TRANSITION, WAIT_TRANSITION)
     for place in self.__pn.places:
         if len(place.out_arcs) == 0:
             arcIn = PetriNet.Arc(place, wait_transition)
             arcOut = PetriNet.Arc(wait_transition, place)
             self.__pn.arcs.add(arcIn)
             self.__pn.arcs.add(arcOut)
             wait_transition.in_arcs.add(arcIn)
             wait_transition.out_arcs.add(arcOut)
             place.out_arcs.add(arcIn)
             place.in_arcs.add(arcOut)
     self.__pn.transitions.add(wait_transition)
     return wait_transition
Exemplo n.º 2
0
def add_wait_net_end(pn, wait_label):
    '''
    Words don't have the same length. To compare them we add a "wait" transition at the end of the model and the
    traces.
    :return:
    '''
    wait_transition = PetriNet.Transition(wait_label, wait_label)
    for place in pn.places:
        if len(place.out_arcs) == 0:
            arcIn = PetriNet.Arc(place, wait_transition)
            arcOut = PetriNet.Arc(wait_transition, place)
            pn.arcs.add(arcIn)
            pn.arcs.add(arcOut)
            wait_transition.in_arcs.add(arcIn)
            wait_transition.out_arcs.add(arcOut)
            place.out_arcs.add(arcIn)
            place.in_arcs.add(arcOut)
    pn.transitions.add(wait_transition)
    return wait_transition
Exemplo n.º 3
0
def add_arc_from_to(fr, to, net, weight=1):
    """
    Adds an arc from a specific element to another element in some net. Assumes from and to are in the net!

    Parameters
    ----------
    fr: transition/place from
    to:  transition/place to
    net: net to use
    weight: weight associated to the arc

    Returns
    -------
    None
    """
    a = PetriNet.Arc(fr, to, weight)
    net.arcs.add(a)
    fr.out_arcs.add(a)
    to.in_arcs.add(a)

    return a
Exemplo n.º 4
0
def preprocess(graph):
    """
    Takes in graph, a json string representing a petrinet.
    Checks if graph conforms to the standard, if it doesn't an exception is thrown.
    If it does, creates a pm4py PetriNet object out of the petri net encoded in the graph object.
    :return: A PetriNet object.
    """

    graph = json.loads(graph)

    # Check if the given json has all the keys we need
    if not has_keys(graph):
        return None

    # Check if the labels of all nodes are unique
    if not unique_labels(graph):
        return None

    # Check if all nodes (place/transition) have a non empty label
    if not no_empty_label(graph):
        return None

    # Create PetriNet object
    net = PetriNet("converted_graph")

    # Store an ordered list of the vertices to make it easier to connect them through arcs
    ordered_vertices = []

    # Add all vertices (places and transitions) to net
    for vertex in graph['vertices']:
        label = vertex['label'].strip()
        if vertex['petri_type'] == "place":
            new_place = PetriNet.Place(label)
            new_place.properties['position'] = vertex['position']
            new_place.properties['tokens'] = vertex['tokens']

            net.places.add(new_place)
            ordered_vertices.append(new_place)
        elif vertex['petri_type'] == "transition":
            new_transition = PetriNet.Transition(label, label)
            new_transition.properties['position'] = vertex['position']

            net.transitions.add(new_transition)
            ordered_vertices.append(new_transition)

    # Add all edges (arcs) to net
    for edge in graph['edges']:
        source = ordered_vertices[edge['from']]
        target = ordered_vertices[edge['to']]

        # TODO: Change this if you want to get the edge weight another way.
        edge_weight = 1
        if edge['label'] != '':
            try:
                num = int(edge['label'])
                edge_weight = num
            except:
                raise Exception(
                    "There is an edge with a label that is not a number.")

        # Create arc object of weight 1
        arc = PetriNet.Arc(source, target, edge_weight)
        # Add extra properties
        arc.properties['name'] = edge['label']
        arc.properties['bend'] = edge['bend']

        # Add the arc to the net and the source and target objects
        net.arcs.add(arc)
        source.out_arcs.add(arc)
        target.in_arcs.add(arc)

    return net
Exemplo n.º 5
0
def ex_petrinet():
    net = PetriNet()

    # transitions
    a = PetriNet.Transition('a', label='a')
    b = PetriNet.Transition('b', label='b')
    c = PetriNet.Transition('c', label='c')
    d = PetriNet.Transition('d', label='d')
    e = PetriNet.Transition('e', label='e')
    f = PetriNet.Transition('f', label='f')
    g = PetriNet.Transition('g', label='g')
    h = PetriNet.Transition('h', label='h')
    inv0 = PetriNet.Transition('inv0', label=None)
    inv1 = PetriNet.Transition('inv1', label=None)

    trans = [a, b, c, d, e, f, g, h, inv0, inv1]

    # places
    p0 = PetriNet.Place('p0')
    p1 = PetriNet.Place('p1')
    p2 = PetriNet.Place('p2')
    p3 = PetriNet.Place('p3')
    p4 = PetriNet.Place('p4')
    p5 = PetriNet.Place('p5')
    p6 = PetriNet.Place('p6')
    p7 = PetriNet.Place('p7')
    p8 = PetriNet.Place('p8')
    p9 = PetriNet.Place('p9')
    p10 = PetriNet.Place('p10')
    p11 = PetriNet.Place('p11')

    places = [p0, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11]

    # arcs
    p0_a = PetriNet.Arc(p0, a)
    p1_c = PetriNet.Arc(p1, c)
    p2_inv0 = PetriNet.Arc(p2, inv0)
    p3_e = PetriNet.Arc(p3, e)
    p4_f = PetriNet.Arc(p4, f)
    p5_g = PetriNet.Arc(p5, g)
    p6_inv1 = PetriNet.Arc(p6, inv1)
    p7_inv1 = PetriNet.Arc(p7, inv1)
    p8_inv1 = PetriNet.Arc(p8, inv1)
    p9_d = PetriNet.Arc(p9, d)
    p10_h = PetriNet.Arc(p10, h)
    p10_b = PetriNet.Arc(p10, b)

    a_p1 = PetriNet.Arc(a, p1)
    c_p2 = PetriNet.Arc(c, p2)
    inv0_p3 = PetriNet.Arc(inv0, p3)
    inv0_p4 = PetriNet.Arc(inv0, p4)
    inv0_p5 = PetriNet.Arc(inv0, p5)
    e_p6 = PetriNet.Arc(e, p6)
    f_p7 = PetriNet.Arc(f, p7)
    g_p8 = PetriNet.Arc(g, p8)
    inv1_p9 = PetriNet.Arc(inv1, p9)
    d_p10 = PetriNet.Arc(d, p10)
    h_p1 = PetriNet.Arc(h, p1)
    b_p11 = PetriNet.Arc(b, p11)

    arcs = [
        p0_a, p1_c, p2_inv0, p3_e, p4_f, p5_g, p6_inv1, p7_inv1, p8_inv1, p9_d,
        p10_h, p10_b, a_p1, c_p2, inv0_p3, inv0_p4, inv0_p5, e_p6, f_p7, g_p8,
        inv1_p9, d_p10, h_p1, b_p11
    ]

    for arc in arcs:
        arc.source.out_arcs.add(arc)
        arc.target.in_arcs.add(arc)

    net.transitions.update(trans)
    net.places.update(places)
    net.arcs.update(arcs)

    init_marking = Marking([p0])
    final_marking = Marking([p11])

    return net, init_marking, final_marking