def apply(tree, parameters=None): ''' Only supports loops with 2 children! :param tree: :return: ''' net = petrinet.PetriNet(name=str(tree)) if len(tree.children) == 0: pn_util.add_transition(net, label=tree.label, name=str(id(tree))) else: sub_nets = list() for c in tree.children: sub_net, ini, fin = apply(c) sub_nets.append(sub_net) pn_util.merge(net, sub_nets) switch = { pt_opt.SEQUENCE: construct_sequence_pattern, pt_opt.XOR: construct_xor_pattern, pt_opt.PARALLEL: construct_and_pattern, pt_opt.LOOP: construct_loop_pattern } net, ini, fin = switch[tree.operator](net, sub_nets) if tree.parent is None: p_ini = pn_util.add_place(net) p_fin = pn_util.add_place(net) pn_util.add_arc_from_to(p_ini, _get_src_transition(net), net) pn_util.add_arc_from_to(_get_sink_transition(net), p_fin, net) return net, petrinet.Marking({p_ini: 1}), petrinet.Marking({p_fin: 1}) return net, petrinet.Marking(), petrinet.Marking()
def marking2str(marking, sep='_'): multiset = [str(p.name) + sep + str(count) for p, count in marking.items()] multiset = sorted(multiset) marking_str = '_'.join(multiset) # logger.info('marking multiset: {} to {}'.format(multiset, marking_str)) return marking_str if __name__ == '__main__': print('Testing build_reachability_graph...') from pm4py.objects.petri import exporter from pm4py.visualization.transition_system import util net = petri.PetriNet() # transitions a = petri.PetriNet.Transition('a', label='Activity A') b = petri.PetriNet.Transition('b', label='Activity B') c = petri.PetriNet.Transition('c', label='Activity C') d = petri.PetriNet.Transition('d', label='Activity D') e = petri.PetriNet.Transition('e', label='Activity E') f = petri.PetriNet.Transition('f', label='Activity F') g = petri.PetriNet.Transition('g', label='Activity G') h = petri.PetriNet.Transition('h', label='Activity H') inv0 = petri.PetriNet.Transition('inv0', label=None) inv1 = petri.PetriNet.Transition('inv1', label=None) trans = [a, b, c, d, e, f, g, h, inv0, inv1]
def build_net1(): net = petri.PetriNet(name='net1') # transitions a = petri.PetriNet.Transition('a', label='a') b = petri.PetriNet.Transition('b', label='b') c = petri.PetriNet.Transition('c', label='c') d = petri.PetriNet.Transition('d', label='d') e = petri.PetriNet.Transition('e', label='e') f = petri.PetriNet.Transition('f', label='f') g = petri.PetriNet.Transition('g', label='g') trans = [a, b, c, d, e, f, g] # places p1 = petri.PetriNet.Place('p1') p2 = petri.PetriNet.Place('p2') p3 = petri.PetriNet.Place('p3') p4 = petri.PetriNet.Place('p4') p5 = petri.PetriNet.Place('p5') p6 = petri.PetriNet.Place('p6') p7 = petri.PetriNet.Place('p7') places = [p1, p2, p3, p4, p5, p6, p7] # arcs p1_a = petri.PetriNet.Arc(p1, a) p2_b = petri.PetriNet.Arc(p2, b) p2_c = petri.PetriNet.Arc(p2, c) p3_d = petri.PetriNet.Arc(p3, d) p4_e = petri.PetriNet.Arc(p4, e) p5_e = petri.PetriNet.Arc(p5, e) p6_f = petri.PetriNet.Arc(p6, f) p6_g = petri.PetriNet.Arc(p6, g) a_p2 = petri.PetriNet.Arc(a, p2) a_p3 = petri.PetriNet.Arc(a, p3) b_p4 = petri.PetriNet.Arc(b, p4) c_p4 = petri.PetriNet.Arc(c, p4) d_p5 = petri.PetriNet.Arc(d, p5) e_p6 = petri.PetriNet.Arc(e, p6) f_p2 = petri.PetriNet.Arc(f, p2) f_p3 = petri.PetriNet.Arc(f, p3) g_p7 = petri.PetriNet.Arc(g, p7) arcs = [ p1_a, p2_b, p2_c, p3_d, p4_e, p5_e, p6_f, p6_g, a_p2, a_p3, b_p4, c_p4, d_p5, e_p6, f_p2, f_p3, g_p7 ] 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 = petri.Marking([p1]) final_marking = petri.Marking([p7]) return net, init_marking, final_marking