def construct_trace_net(trace, trace_name_key=xes_util.DEFAULT_NAME_KEY, activity_key=xes_util.DEFAULT_NAME_KEY): """ Creates a trace net, i.e. a trace in Petri net form. Parameters ---------- trace: :class:`list` input trace, assumed to be a list of events trace_name_key: :class:`str` key of the attribute that defines the name of the trace activity_key: :class:`str` key of the attribute of the events that defines the activity name Returns ------- tuple: :class:`tuple` of the net, initial marking and the final marking """ net = PetriNet('trace net of %s' % trace.attributes[trace_name_key] if trace_name_key in trace.attributes else ' ') place_map = {0: PetriNet.Place('p_0')} net.places.add(place_map[0]) for i in range(0, len(trace)): t = PetriNet.Transition('t_' + trace[i][activity_key] + '_' + str(i), trace[i][activity_key]) # 16/02/2021: set the trace index as property of the transition of the trace net t.properties[properties.TRACE_NET_TRANS_INDEX] = i net.transitions.add(t) place_map[i + 1] = PetriNet.Place('p_' + str(i + 1)) # 16/02/2021: set the place index as property of the place of the trace net place_map[i + 1].properties[properties.TRACE_NET_PLACE_INDEX] = i + 1 net.places.add(place_map[i + 1]) add_arc_from_to(place_map[i], t, net) add_arc_from_to(t, place_map[i + 1], net) return net, Marking({place_map[0]: 1}), Marking({place_map[len(trace)]: 1})
def __copy_into(source_net, target_net, upper, skip): t_map = {} p_map = {} for t in source_net.transitions: name = (t.name, skip) if upper else (skip, t.name) label = (t.label, skip) if upper else (skip, t.label) t_map[t] = PetriNet.Transition(name, label) if properties.TRACE_NET_TRANS_INDEX in t.properties: # 16/02/2021: copy the index property from the transition of the trace net t_map[t].properties[ properties.TRACE_NET_TRANS_INDEX] = t.properties[ properties.TRACE_NET_TRANS_INDEX] target_net.transitions.add(t_map[t]) for p in source_net.places: name = (p.name, skip) if upper else (skip, p.name) p_map[p] = PetriNet.Place(name) if properties.TRACE_NET_PLACE_INDEX in p.properties: # 16/02/2021: copy the index property from the place of the trace net p_map[p].properties[ properties.TRACE_NET_PLACE_INDEX] = p.properties[ properties.TRACE_NET_PLACE_INDEX] target_net.places.add(p_map[p]) for t in source_net.transitions: for a in t.in_arcs: add_arc_from_to(p_map[a.source], t_map[t], target_net) for a in t.out_arcs: add_arc_from_to(t_map[t], p_map[a.target], target_net) return t_map, p_map
def construct(pn1, im1, fm1, pn2, im2, fm2, skip): """ Constructs the synchronous product net of two given Petri nets. :param pn1: Petri net 1 :param im1: Initial marking of Petri net 1 :param fm1: Final marking of Petri net 1 :param pn2: Petri net 2 :param im2: Initial marking of Petri net 2 :param fm2: Final marking of Petri net 2 :param skip: Symbol to be used as skip Returns ------- :return: Synchronous product net and associated marking labels are of the form (a,>>) """ sync_net = PetriNet('synchronous_product_net of %s and %s' % (pn1.name, pn2.name)) t1_map, p1_map = __copy_into(pn1, sync_net, True, skip) t2_map, p2_map = __copy_into(pn2, sync_net, False, skip) for t1 in pn1.transitions: for t2 in pn2.transitions: if t1.label == t2.label: sync = PetriNet.Transition((t1.name, t2.name), (t1.label, t2.label)) sync_net.transitions.add(sync) # copy the properties of the transitions inside the transition of the sync net for p1 in t1.properties: sync.properties[p1] = t1.properties[p1] for p2 in t2.properties: sync.properties[p2] = t2.properties[p2] for a in t1.in_arcs: add_arc_from_to(p1_map[a.source], sync, sync_net) for a in t2.in_arcs: add_arc_from_to(p2_map[a.source], sync, sync_net) for a in t1.out_arcs: add_arc_from_to(sync, p1_map[a.target], sync_net) for a in t2.out_arcs: add_arc_from_to(sync, p2_map[a.target], sync_net) sync_im = Marking() sync_fm = Marking() for p in im1: sync_im[p1_map[p]] = im1[p] for p in im2: sync_im[p2_map[p]] = im2[p] for p in fm1: sync_fm[p1_map[p]] = fm1[p] for p in fm2: sync_fm[p2_map[p]] = fm2[p] # update 06/02/2021: to distinguish the sync nets that are output of this method, put a property in the sync net sync_net.properties[properties.IS_SYNC_NET] = True return sync_net, sync_im, sync_fm
def project_net_on_place(place): """ Project a Petri net on a place Parameters ------------- place Place Returns ------------- net (Place) net im Empty initial marking fm Empty final marking """ place_net = PetriNet() place_net_im = Marking() place_net_fm = Marking() input_trans = [arc.source for arc in place.in_arcs] output_trans = [arc.target for arc in place.out_arcs] if len(input_trans) == 0 or len(output_trans) == 0: raise Exception("place projection not available on source/sink places") input_trans_visible = [trans for trans in input_trans if trans.label] output_trans_visible = [trans for trans in output_trans if trans.label] if not len(input_trans) == len(input_trans_visible) or not len( output_trans) == len(output_trans_visible): raise Exception( "place projection not available on places that have invisible transitions as preset/postset" ) new_place = PetriNet.Place(place.name) place_net.places.add(new_place) for trans in input_trans: new_trans = PetriNet.Transition(trans.name, trans.label) place_net.transitions.add(new_trans) add_arc_from_to(new_trans, new_place, place_net) for trans in output_trans: new_trans = PetriNet.Transition(trans.name, trans.label) place_net.transitions.add(new_trans) add_arc_from_to(new_place, new_trans, place_net) return place_net, place_net_im, place_net_fm
def postprocessing(net: PetriNet, initial_marking: Marking, final_marking: Marking, A, B, pairs, loop_one_list) -> Tuple[PetriNet, Marking, Marking]: """ Adding the filtered transitions to the Petri net Parameters ------------ loop_list List of looped activities classical_alpha_result Result after applying the classic alpha algorithm to the filtered log A See Paper for definition B See Paper for definition Returns ------------ net Petri net im Initial marking fm Final marking """ label_transition_dict = {} for label in loop_one_list: label_transition_dict[label] = PetriNet.Transition(label, label) net.transitions.add(label_transition_dict[label]) # F L1L # Key is specific loop element for key, value in A.items(): if key in B: A_without_B = value - B[key] B_without_A = B[key] - value pair = (A_without_B, B_without_A) for pair_try in pairs: in_part = pair_try[0] out_part = pair_try[1] if pair[0].issubset(in_part) and pair[1].issubset(out_part): pair_try_place = PetriNet.Place(str(pair_try)) net.places.add(pair_try_place) add_arc_from_to(label_transition_dict[key], pair_try_place, net) add_arc_from_to(pair_try_place, label_transition_dict[key], net) return net, initial_marking, final_marking
def add_arc_from_to(fr, to, net: PetriNet, weight=1, type=None) -> PetriNet.Arc: """ 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) if type is not None: a.properties[properties.ARCTYPE] = type net.arcs.add(a) fr.out_arcs.add(a) to.in_arcs.add(a) return a
def merge(trgt: Optional[PetriNet] = None, nets=None) -> PetriNet: trgt = trgt if trgt is not None else PetriNet() nets = nets if nets is not None else list() for net in nets: trgt.transitions.update(net.transitions) trgt.places.update(net.places) trgt.arcs.update(net.arcs) return trgt
def add_source(net, start_activities, label_transition_dict): """ Adding source pe """ source = PetriNet.Place('start') net.places.add(source) for s in start_activities: add_arc_from_to(source, label_transition_dict[s], net) return source
def add_sink(net, end_activities, label_transition_dict): """ Adding sink pe """ end = PetriNet.Place('end') net.places.add(end) for e in end_activities: add_arc_from_to(label_transition_dict[e], end, net) return end
def add_transition(net: PetriNet, name=None, label=None) -> PetriNet.Transition: name = name if name is not None else 't_' + str(len( net.transitions)) + '_' + str(time.time()) + str( random.randint(0, 10000)) t = PetriNet.Transition(name=name, label=label) net.transitions.add(t) return t
def get_strongly_connected_subnets(net): """ Get the strongly connected components subnets in the Petri net Parameters ------------- net Petri net Returns ------------- strongly_connected_transitions List of strongly connected transitions of the Petri net """ import networkx as nx graph, inv_dictionary = create_networkx_directed_graph(net) sccg = list(nx.strongly_connected_components(graph)) strongly_connected_subnets = [] for sg in list(sccg): if len(sg) > 1: subnet = PetriNet() imarking = Marking() fmarking = Marking() corr = {} for node in sg: if node in inv_dictionary: if type(inv_dictionary[node]) is PetriNet.Transition: prev_trans = inv_dictionary[node] new_trans = PetriNet.Transition( prev_trans.name, prev_trans.label) corr[node] = new_trans subnet.transitions.add(new_trans) if type(inv_dictionary[node]) is PetriNet.Place: prev_place = inv_dictionary[node] new_place = PetriNet.Place(prev_place.name) corr[node] = new_place subnet.places.add(new_place) for edge in graph.edges: if edge[0] in sg and edge[1] in sg: add_arc_from_to(corr[edge[0]], corr[edge[1]], subnet) strongly_connected_subnets.append([subnet, imarking, fmarking]) return strongly_connected_subnets
def short_circuit_petri_net(net, print_diagnostics=False): """ Fist, sink and source place are identified. Then, a transition from source to sink is added to short-circuited the given petri net. If there is no unique source and sink place, an error gets returned :param net: Petri net that is going to be short circuited :return: """ s_c_net = copy.deepcopy(net) no_source_places = 0 no_sink_places = 0 sink = None source = None for place in s_c_net.places: if len(place.in_arcs) == 0: source = place no_source_places += 1 if len(place.out_arcs) == 0: sink = place no_sink_places += 1 if (sink is not None) and ( source is not None) and no_source_places == 1 and no_sink_places == 1: # If there is one unique source and sink place, short circuit Petri Net is constructed t_1 = PetriNet.Transition("short_circuited_transition", "short_circuited_transition") s_c_net.transitions.add(t_1) # add arcs in short-circuited net petri_utils.add_arc_from_to(sink, t_1, s_c_net) petri_utils.add_arc_from_to(t_1, source, s_c_net) return s_c_net else: if sink is None: if print_diagnostics: print("There is no sink place.") return None elif source is None: if print_diagnostics: print("There is no source place.") return None elif no_source_places > 1: if print_diagnostics: print("There is more than one source place.") return None elif no_sink_places > 1: if print_diagnostics: print("There is more than one sink place.") return None
def _short_circuit_petri_net(net): """ Creates a short circuited Petri net, whether an unique source place and sink place are there, by connecting the sink with the source Parameters --------------- net Petri net Returns --------------- boolean Boolean value """ s_c_net = copy.deepcopy(net) no_source_places = 0 no_sink_places = 0 sink = None source = None for place in s_c_net.places: if len(place.in_arcs) == 0: source = place no_source_places += 1 if len(place.out_arcs) == 0: sink = place no_sink_places += 1 if (sink is not None) and ( source is not None) and no_source_places == 1 and no_sink_places == 1: # If there is one unique source and sink place, short circuit Petri Net is constructed t_1 = PetriNet.Transition("short_circuited_transition", "short_circuited_transition") s_c_net.transitions.add(t_1) # add arcs in short-circuited net pn_utils.add_arc_from_to(sink, t_1, s_c_net) pn_utils.add_arc_from_to(t_1, source, s_c_net) return s_c_net else: return None
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
def apply(heu_net, parameters=None): """ Converts an Heuristics Net to a Petri net Parameters -------------- heu_net Heuristics net parameters Possible parameters of the algorithm Returns -------------- net Petri net im Initial marking fm Final marking """ if parameters is None: parameters = {} net = PetriNet("") im = Marking() fm = Marking() source_places = [] sink_places = [] hid_trans_count = 0 for index, sa_list in enumerate(heu_net.start_activities): source = PetriNet.Place("source" + str(index)) source_places.append(source) net.places.add(source) im[source] = 1 for index, ea_list in enumerate(heu_net.end_activities): sink = PetriNet.Place("sink" + str(index)) sink_places.append(sink) net.places.add(sink) fm[sink] = 1 act_trans = {} who_is_entering = {} who_is_exiting = {} for act1_name in heu_net.nodes: act1 = heu_net.nodes[act1_name] if act1_name not in act_trans: act_trans[act1_name] = PetriNet.Transition(act1_name, act1_name) net.transitions.add(act_trans[act1_name]) who_is_entering[act1_name] = set() who_is_exiting[act1_name] = set() for index, sa_list in enumerate(heu_net.start_activities): if act1_name in sa_list: who_is_entering[act1_name].add((None, index)) for index, ea_list in enumerate(heu_net.end_activities): if act1_name in ea_list: who_is_exiting[act1_name].add((None, index)) for act2 in act1.output_connections: act2_name = act2.node_name if act2_name not in act_trans: act_trans[act2_name] = PetriNet.Transition( act2_name, act2_name) net.transitions.add(act_trans[act2_name]) who_is_entering[act2_name] = set() who_is_exiting[act2_name] = set() for index, sa_list in enumerate(heu_net.start_activities): if act2_name in sa_list: who_is_entering[act2_name].add((None, index)) for index, ea_list in enumerate(heu_net.end_activities): if act2_name in ea_list: who_is_exiting[act2_name].add((None, index)) who_is_entering[act2_name].add((act1_name, None)) who_is_exiting[act1_name].add((act2_name, None)) places_entering = {} for act1 in who_is_entering: cliques = find_bindings(heu_net.nodes[act1].and_measures_in) places_entering[act1] = {} entering_activities = list(who_is_entering[act1]) entering_activities_wo_source = sorted( [x for x in entering_activities if x[0] is not None], key=lambda x: x[0]) entering_activities_only_source = [ x for x in entering_activities if x[0] is None ] if entering_activities_wo_source: master_place = PetriNet.Place("pre_" + act1) net.places.add(master_place) add_arc_from_to(master_place, act_trans[act1], net) if len(entering_activities) == 1: places_entering[act1][entering_activities[0]] = master_place else: for index, act in enumerate(entering_activities_wo_source): if act[0] in heu_net.nodes[act1].and_measures_in: z = 0 while z < len(cliques): if act[0] in cliques[z]: hid_trans_count = hid_trans_count + 1 hid_trans = PetriNet.Transition( "hid_" + str(hid_trans_count), None) net.transitions.add(hid_trans) add_arc_from_to(hid_trans, master_place, net) for act2 in cliques[z]: if (act2, None) not in places_entering[act1]: s_place = PetriNet.Place("splace_in_" + act1 + "_" + act2 + "_" + str(index)) net.places.add(s_place) places_entering[act1][(act2, None)] = s_place add_arc_from_to( places_entering[act1][(act2, None)], hid_trans, net) del cliques[z] continue z = z + 1 pass elif act not in places_entering[act1]: hid_trans_count = hid_trans_count + 1 hid_trans = PetriNet.Transition( "hid_" + str(hid_trans_count), None) net.transitions.add(hid_trans) add_arc_from_to(hid_trans, master_place, net) if act not in places_entering[act1]: s_place = PetriNet.Place("splace_in_" + act1 + "_" + str(index)) net.places.add(s_place) places_entering[act1][act] = s_place add_arc_from_to(places_entering[act1][act], hid_trans, net) for el in entering_activities_only_source: if len(entering_activities) == 1: add_arc_from_to(source_places[el[1]], act_trans[act1], net) else: hid_trans_count = hid_trans_count + 1 hid_trans = PetriNet.Transition("hid_" + str(hid_trans_count), None) net.transitions.add(hid_trans) add_arc_from_to(source_places[el[1]], hid_trans, net) add_arc_from_to(hid_trans, master_place, net) for act1 in who_is_exiting: cliques = find_bindings(heu_net.nodes[act1].and_measures_out) exiting_activities = list(who_is_exiting[act1]) exiting_activities_wo_sink = sorted( [x for x in exiting_activities if x[0] is not None], key=lambda x: x[0]) exiting_activities_only_sink = [ x for x in exiting_activities if x[0] is None ] if exiting_activities_wo_sink: if len(exiting_activities) == 1 and len( exiting_activities_wo_sink) == 1: ex_act = exiting_activities_wo_sink[0] if (act1, None) in places_entering[ex_act[0]]: add_arc_from_to(act_trans[act1], places_entering[ex_act[0]][(act1, None)], net) else: int_place = PetriNet.Place("intplace_" + str(act1)) net.places.add(int_place) add_arc_from_to(act_trans[act1], int_place, net) for ex_act in exiting_activities_wo_sink: if (act1, None) in places_entering[ex_act[0]]: if ex_act[0] in heu_net.nodes[act1].and_measures_out: z = 0 while z < len(cliques): if ex_act[0] in cliques[z]: hid_trans_count = hid_trans_count + 1 hid_trans = PetriNet.Transition( "hid_" + str(hid_trans_count), None) net.transitions.add(hid_trans) add_arc_from_to(int_place, hid_trans, net) for act in cliques[z]: add_arc_from_to( hid_trans, places_entering[act][(act1, None)], net) del cliques[z] continue z = z + 1 else: hid_trans_count = hid_trans_count + 1 hid_trans = PetriNet.Transition( "hid_" + str(hid_trans_count), None) net.transitions.add(hid_trans) add_arc_from_to(int_place, hid_trans, net) add_arc_from_to( hid_trans, places_entering[ex_act[0]][(act1, None)], net) for el in exiting_activities_only_sink: if len(exiting_activities) == 1: add_arc_from_to(act_trans[act1], sink_places[el[1]], net) else: hid_trans_count = hid_trans_count + 1 hid_trans = PetriNet.Transition("hid_" + str(hid_trans_count), None) net.transitions.add(hid_trans) add_arc_from_to(int_place, hid_trans, net) add_arc_from_to(hid_trans, sink_places[el[1]], net) net = remove_rendundant_invisible_transitions(net) from pm4py.objects.petri_net.utils import reduction reduction.apply_simple_reduction(net) return net, im, fm
def decompose(net, im, fm): places = {x.name: x for x in net.places} inv_trans = {x.name: x for x in net.transitions if x.label is None} tmap = {} for t in net.transitions: if t.label is not None: if t.label not in tmap: tmap[t.label] = [] tmap[t.label].append(t) trans_dup_label = { x.label: x for x in net.transitions if x.label is not None and len(tmap[x.label]) > 1 } trans_labels = {x.name: x.label for x in net.transitions} conn_comp = get_graph_components(places, inv_trans, trans_dup_label, tmap) list_nets = [] for cmp in conn_comp: net_new = PetriNet("") im_new = Marking() fm_new = Marking() lmap = {} for el in cmp: if el in places: lmap[el] = PetriNet.Place(el) net_new.places.add(lmap[el]) elif el in inv_trans: lmap[el] = PetriNet.Transition(el, None) net_new.transitions.add(lmap[el]) elif el in trans_labels: lmap[el] = PetriNet.Transition(el, trans_labels[el]) net_new.transitions.add(lmap[el]) for el in cmp: if el in places: old_place = places[el] for arc in old_place.in_arcs: st = arc.source if st.name not in lmap: lmap[st.name] = PetriNet.Transition( st.name, trans_labels[st.name]) net_new.transitions.add(lmap[st.name]) add_arc_from_to(lmap[st.name], lmap[el], net_new) for arc in old_place.out_arcs: st = arc.target if st.name not in lmap: lmap[st.name] = PetriNet.Transition( st.name, trans_labels[st.name]) net_new.transitions.add(lmap[st.name]) add_arc_from_to(lmap[el], lmap[st.name], net_new) if old_place in im: im_new[lmap[el]] = im[old_place] if old_place in fm: fm_new[lmap[el]] = fm[old_place] lvis_labels = sorted( [t.label for t in net_new.transitions if t.label is not None]) t_tuple = tuple( sorted( list( int( hashlib.md5(t.name.encode( constants.DEFAULT_ENCODING)).hexdigest(), 16) for t in net_new.transitions))) net_new.lvis_labels = lvis_labels net_new.t_tuple = t_tuple if len(net_new.places) > 0 or len(net_new.transitions) > 0: list_nets.append((net_new, im_new, fm_new)) return list_nets
def apply(dfg, parameters=None): """ Applies the DFG mining on a given object (if it is a Pandas dataframe or a log, the DFG is calculated) Parameters ------------- dfg Object (DFG) (if it is a Pandas dataframe or a log, the DFG is calculated) parameters Parameters """ if parameters is None: parameters = {} dfg = dfg start_activities = exec_utils.get_param_value( Parameters.START_ACTIVITIES, parameters, dfg_utils.infer_start_activities(dfg)) end_activities = exec_utils.get_param_value( Parameters.END_ACTIVITIES, parameters, dfg_utils.infer_end_activities(dfg)) activities = dfg_utils.get_activities_from_dfg(dfg) net = PetriNet("") im = Marking() fm = Marking() source = PetriNet.Place("source") net.places.add(source) im[source] = 1 sink = PetriNet.Place("sink") net.places.add(sink) fm[sink] = 1 places_corr = {} index = 0 for act in activities: places_corr[act] = PetriNet.Place(act) net.places.add(places_corr[act]) for act in start_activities: if act in places_corr: index = index + 1 trans = PetriNet.Transition(act + "_" + str(index), act) net.transitions.add(trans) pn_util.add_arc_from_to(source, trans, net) pn_util.add_arc_from_to(trans, places_corr[act], net) for act in end_activities: if act in places_corr: index = index + 1 inv_trans = PetriNet.Transition(act + "_" + str(index), None) net.transitions.add(inv_trans) pn_util.add_arc_from_to(places_corr[act], inv_trans, net) pn_util.add_arc_from_to(inv_trans, sink, net) for el in dfg.keys(): act1 = el[0] act2 = el[1] index = index + 1 trans = PetriNet.Transition(act2 + "_" + str(index), act2) net.transitions.add(trans) pn_util.add_arc_from_to(places_corr[act1], trans, net) pn_util.add_arc_from_to(trans, places_corr[act2], net) return net, im, fm
def apply_dfg_sa_ea( dfg: Dict[str, int], start_activities: Union[None, Dict[str, int]], end_activities: Union[None, Dict[str, int]], parameters: Optional[Dict[Union[str, Parameters], Any]] = None ) -> Tuple[PetriNet, Marking, Marking]: """ Applying Alpha Miner starting from the knowledge of the Directly Follows graph, and of the start activities and end activities in the log (possibly inferred from the DFG) Parameters ------------ dfg Directly-Follows graph start_activities Start activities end_activities End activities parameters Parameters of the algorithm including: activity key -> name of the attribute that contains the activity Returns ------- net : :class:`pm4py.entities.petri.petrinet.PetriNet` A Petri net describing the event log that is provided as an input initial marking : :class:`pm4py.models.net.Marking` marking object representing the initial marking final marking : :class:`pm4py.models.net.Marking` marking object representing the final marking, not guaranteed that it is actually reachable! """ if parameters is None: parameters = {} activity_key = exec_utils.get_param_value( Parameters.ACTIVITY_KEY, parameters, pm_util.xes_constants.DEFAULT_NAME_KEY) if start_activities is None: start_activities = dfg_utils.infer_start_activities(dfg) if end_activities is None: end_activities = dfg_utils.infer_end_activities(dfg) labels = set() for el in dfg: labels.add(el[0]) labels.add(el[1]) for a in start_activities: labels.add(a) for a in end_activities: labels.add(a) labels = list(labels) alpha_abstraction = alpha_classic_abstraction.ClassicAlphaAbstraction( start_activities, end_activities, dfg, activity_key=activity_key) pairs = list( map( lambda p: ({p[0]}, {p[1]}), filter( lambda p: __initial_filter(alpha_abstraction.parallel_relation, p), alpha_abstraction.causal_relation))) for i in range(0, len(pairs)): t1 = pairs[i] for j in range(i, len(pairs)): t2 = pairs[j] if t1 != t2: if t1[0].issubset(t2[0]) or t1[1].issubset(t2[1]): if not (__check_is_unrelated( alpha_abstraction.parallel_relation, alpha_abstraction.causal_relation, t1[0], t2[0]) or __check_is_unrelated( alpha_abstraction.parallel_relation, alpha_abstraction.causal_relation, t1[1], t2[1])): new_alpha_pair = (t1[0] | t2[0], t1[1] | t2[1]) if new_alpha_pair not in pairs: pairs.append((t1[0] | t2[0], t1[1] | t2[1])) internal_places = filter(lambda p: __pair_maximizer(pairs, p), pairs) net = PetriNet('alpha_classic_net_' + str(time.time())) label_transition_dict = {} for i in range(0, len(labels)): label_transition_dict[labels[i]] = PetriNet.Transition( labels[i], labels[i]) net.transitions.add(label_transition_dict[labels[i]]) src = __add_source(net, alpha_abstraction.start_activities, label_transition_dict) sink = __add_sink(net, alpha_abstraction.end_activities, label_transition_dict) for pair in internal_places: place = PetriNet.Place(str(pair)) net.places.add(place) for in_arc in pair[0]: add_arc_from_to(label_transition_dict[in_arc], place, net) for out_arc in pair[1]: add_arc_from_to(place, label_transition_dict[out_arc], net) return net, Marking({src: 1}), Marking({sink: 1})
def merge_comp(comp1, comp2): net = PetriNet("") im = Marking() fm = Marking() places = {} trans = {} for pl in comp1[0].places: places[pl.name] = PetriNet.Place(pl.name) net.places.add(places[pl.name]) if pl in comp1[1]: im[places[pl.name]] = comp1[1][pl] if pl in comp1[2]: fm[places[pl.name]] = comp1[2][pl] for pl in comp2[0].places: places[pl.name] = PetriNet.Place(pl.name) net.places.add(places[pl.name]) if pl in comp2[1]: im[places[pl.name]] = comp2[1][pl] if pl in comp2[2]: fm[places[pl.name]] = comp2[2][pl] for tr in comp1[0].transitions: trans[tr.name] = PetriNet.Transition(tr.name, tr.label) net.transitions.add(trans[tr.name]) for tr in comp2[0].transitions: if not tr.name in trans: trans[tr.name] = PetriNet.Transition(tr.name, tr.label) net.transitions.add(trans[tr.name]) for arc in comp1[0].arcs: if type(arc.source) is PetriNet.Place: add_arc_from_to(places[arc.source.name], trans[arc.target.name], net) else: add_arc_from_to(trans[arc.source.name], places[arc.target.name], net) for arc in comp2[0].arcs: if type(arc.source) is PetriNet.Place: add_arc_from_to(places[arc.source.name], trans[arc.target.name], net) else: add_arc_from_to(trans[arc.source.name], places[arc.target.name], net) lvis_labels = sorted( [t.label for t in net.transitions if t.label is not None]) t_tuple = tuple( sorted( list( int( hashlib.md5(t.name.encode( constants.DEFAULT_ENCODING)).hexdigest(), 16) for t in net.transitions))) net.lvis_labels = lvis_labels net.t_tuple = t_tuple return (net, im, fm)
def apply(dfg: Dict[Tuple[str, str], int], parameters: Optional[Dict[Any, Any]] = None): """ Applies the DFG mining on a given object (if it is a Pandas dataframe or a log, the DFG is calculated) Parameters ------------- dfg Object (DFG) (if it is a Pandas dataframe or a log, the DFG is calculated) parameters Parameters: - Parameters.START_ACTIVITIES: the start activities of the DFG - Parameters.END_ACTIVITIES: the end activities of the DFG Returns ------------- net Petri net im Initial marking fm Final marking """ if parameters is None: parameters = {} start_activities = exec_utils.get_param_value( Parameters.START_ACTIVITIES, parameters, {x: 1 for x in dfg_utils.infer_start_activities(dfg)}) end_activities = exec_utils.get_param_value( Parameters.END_ACTIVITIES, parameters, {x: 1 for x in dfg_utils.infer_end_activities(dfg)}) artificial_start_activity = exec_utils.get_param_value( Parameters.PARAM_ARTIFICIAL_START_ACTIVITY, parameters, constants.DEFAULT_ARTIFICIAL_START_ACTIVITY) artificial_end_activity = exec_utils.get_param_value( Parameters.PARAM_ARTIFICIAL_END_ACTIVITY, parameters, constants.DEFAULT_ARTIFICIAL_END_ACTIVITY) enriched_dfg = copy(dfg) for act in start_activities: enriched_dfg[(artificial_start_activity, act)] = start_activities[act] for act in end_activities: enriched_dfg[(act, artificial_end_activity)] = end_activities[act] activities = set(x[1] for x in enriched_dfg).union( set(x[0] for x in enriched_dfg)) net = PetriNet("") im = Marking() fm = Marking() left_places = {} transes = {} right_places = {} for act in activities: pl1 = PetriNet.Place("source_" + act) pl2 = PetriNet.Place("sink_" + act) trans = PetriNet.Transition("trans_" + act, act) if act in [artificial_start_activity, artificial_end_activity]: trans.label = None net.places.add(pl1) net.places.add(pl2) net.transitions.add(trans) petri_utils.add_arc_from_to(pl1, trans, net) petri_utils.add_arc_from_to(trans, pl2, net) left_places[act] = pl1 right_places[act] = pl2 transes[act] = trans for arc in enriched_dfg: hidden = PetriNet.Transition(arc[0] + "_" + arc[1], None) net.transitions.add(hidden) petri_utils.add_arc_from_to(right_places[arc[0]], hidden, net) petri_utils.add_arc_from_to(hidden, left_places[arc[1]], net) im[left_places[artificial_start_activity]] = 1 fm[right_places[artificial_end_activity]] = 1 return net, im, fm
def import_net_from_xml_object(root, parameters=None): """ Import a Petri net from an etree XML object Parameters ---------- root Root object of the XML parameters Other parameters of the algorithm """ if parameters is None: parameters = {} net = PetriNet('imported_' + str(time.time())) marking = Marking() fmarking = Marking() nett = None page = None finalmarkings = None stochastic_information = {} for child in root: nett = child places_dict = {} trans_dict = {} if nett is not None: for child in nett: if "page" in child.tag: page = child if "finalmarkings" in child.tag: finalmarkings = child if page is None: page = nett if page is not None: for child in page: if "place" in child.tag: position_X = None position_Y = None dimension_X = None dimension_Y = None place_id = child.get("id") place_name = place_id number = 0 for child2 in child: if child2.tag.endswith('name'): for child3 in child2: if child3.text: place_name = child3.text if child2.tag.endswith('initialMarking'): for child3 in child2: if child3.tag.endswith("text"): number = int(child3.text) if child2.tag.endswith('graphics'): for child3 in child2: if child3.tag.endswith('position'): position_X = float(child3.get("x")) position_Y = float(child3.get("y")) elif child3.tag.endswith("dimension"): dimension_X = float(child3.get("x")) dimension_Y = float(child3.get("y")) places_dict[place_id] = PetriNet.Place(place_id) places_dict[place_id].properties[constants.PLACE_NAME_TAG] = place_name net.places.add(places_dict[place_id]) if position_X is not None and position_Y is not None and dimension_X is not None and dimension_Y is not None: places_dict[place_id].properties[constants.LAYOUT_INFORMATION_PETRI] = ( (position_X, position_Y), (dimension_X, dimension_Y)) if number > 0: marking[places_dict[place_id]] = number del place_name if page is not None: for child in page: if child.tag.endswith("transition"): position_X = None position_Y = None dimension_X = None dimension_Y = None trans_id = child.get("id") trans_name = trans_id trans_visible = True random_variable = None for child2 in child: if child2.tag.endswith("name"): for child3 in child2: if child3.text: if trans_name == trans_id: trans_name = child3.text if child2.tag.endswith("graphics"): for child3 in child2: if child3.tag.endswith("position"): position_X = float(child3.get("x")) position_Y = float(child3.get("y")) elif child3.tag.endswith("dimension"): dimension_X = float(child3.get("x")) dimension_Y = float(child3.get("y")) if child2.tag.endswith("toolspecific"): tool = child2.get("tool") if "ProM" in tool: activity = child2.get("activity") if "invisible" in activity: trans_visible = False elif "StochasticPetriNet" in tool: distribution_type = None distribution_parameters = None priority = None weight = None for child3 in child2: key = child3.get("key") value = child3.text if key == "distributionType": distribution_type = value elif key == "distributionParameters": distribution_parameters = value elif key == "priority": priority = int(value) elif key == "weight": weight = float(value) random_variable = RandomVariable() random_variable.read_from_string(distribution_type, distribution_parameters) random_variable.set_priority(priority) random_variable.set_weight(weight) # 15/02/2021: the name associated in the PNML to invisible transitions was lost. # at least save that as property. if trans_visible: trans_label = trans_name else: trans_label = None trans_dict[trans_id] = PetriNet.Transition(trans_id, trans_label) trans_dict[trans_id].properties[constants.TRANS_NAME_TAG] = trans_name net.transitions.add(trans_dict[trans_id]) if random_variable is not None: trans_dict[trans_id].properties[constants.STOCHASTIC_DISTRIBUTION] = random_variable if position_X is not None and position_Y is not None and dimension_X is not None and dimension_Y is not None: trans_dict[trans_id].properties[constants.LAYOUT_INFORMATION_PETRI] = ( (position_X, position_Y), (dimension_X, dimension_Y)) if page is not None: for child in page: if child.tag.endswith("arc"): arc_source = child.get("source") arc_target = child.get("target") arc_weight = 1 for arc_child in child: if arc_child.tag.endswith("inscription"): for text_arcweight in arc_child: if text_arcweight.tag.endswith("text"): arc_weight = int(text_arcweight.text) if arc_source in places_dict and arc_target in trans_dict: add_arc_from_to(places_dict[arc_source], trans_dict[arc_target], net, weight=arc_weight) elif arc_target in places_dict and arc_source in trans_dict: add_arc_from_to(trans_dict[arc_source], places_dict[arc_target], net, weight=arc_weight) if finalmarkings is not None: for child in finalmarkings: for child2 in child: place_id = child2.get("idref") for child3 in child2: if child3.tag.endswith("text"): number = int(child3.text) if number > 0: fmarking[places_dict[place_id]] = number # generate the final marking in the case has not been found if len(fmarking) == 0: fmarking = final_marking.discover_final_marking(net) return net, marking, fmarking
def transform_net(self, net0, initial_marking0, final_marking0, s_map, avg_time_starts): """ Transform the source Petri net removing the initial and final marking, and connecting to each "initial" place a hidden timed transition mimicking the case start Parameters ------------- net0 Initial Petri net provided to the object initial_marking0 Initial marking of the Petri net provided to the object final_marking0 Final marking of the Petri net provided to the object s_map Stochastic map of transitions (EXPONENTIAL distribution since we assume a Markovian process) avg_time_starts Average time interlapsed between case starts Returns ------------- net Petri net that will be simulated initial_marking Initial marking of the Petri net that will be simulated (empty) final_marking Final marking of the Petri net that will be simulated (empty) s_map Stochastic map of transitions enriched by new hidden case-generator transitions """ # copy the Petri net object (assure that we do not change the original Petri net) [net1, initial_marking1, final_marking1] = copy([net0, initial_marking0, final_marking0]) # on the copied Petri net, add a sucking transition for the final marking for index, place in enumerate(final_marking1): suck_transition = PetriNet.Transition("SUCK_TRANSITION" + str(index), None) net1.transitions.add(suck_transition) add_arc_from_to(place, suck_transition, net1) hidden_generator_distr = Exponential() hidden_generator_distr.scale = avg_time_starts s_map[suck_transition] = hidden_generator_distr # on the copied Petri net, remove both the place(s) in the initial marking and # the immediate transitions that are connected to it. target_places = [] for place in initial_marking1: out_arcs = list(place.out_arcs) for target_arc in out_arcs: target_trans = target_arc.target if len(target_trans.in_arcs) == 1: out_arcs_lev2 = list(target_trans.out_arcs) for arc in out_arcs_lev2: target_place = arc.target target_places.append(target_place) net1 = remove_transition(net1, target_trans) net1 = remove_place(net1, place) # add hidden case-generation transitions to the model. # all places that are left orphan by the previous operation are targeted. for index, place in enumerate(target_places): hidden_generator_trans = PetriNet.Transition("HIDDEN_GENERATOR_TRANS" + str(index), None) net1.transitions.add(hidden_generator_trans) add_arc_from_to(hidden_generator_trans, place, net1) hidden_generator_distr = Exponential() hidden_generator_distr.scale = avg_time_starts s_map[hidden_generator_trans] = hidden_generator_distr # the simulated Petri net is assumed to start from an empty initial and final marking initial_marking = Marking() final_marking = Marking() return net1, initial_marking, final_marking, s_map
def construct_cost_aware(self, pn1, im1, fm1, pn2, im2, fm2, skip, pn1_costs, pn2_costs, sync_costs): """ Constructs the synchronous product net of two given Petri nets. :param pn1: Petri net 1 :param im1: Initial marking of Petri net 1 :param fm1: Final marking of Petri net 1 :param pn2: Petri net 2 :param im2: Initial marking of Petri net 2 :param fm2: Final marking of Petri net 2 :param skip: Symbol to be used as skip :param pn1_costs: dictionary mapping transitions of pn1 to corresponding costs :param pn2_costs: dictionary mapping transitions of pn2 to corresponding costs :param pn1_costs: dictionary mapping pairs of transitions in pn1 and pn2 to costs :param sync_costs: Costs of sync moves Returns ------- :return: Synchronous product net and associated marking labels are of the form (a,>>) """ sync_net = PetriNet('synchronous_product_net') t1_map, p1_map = copy_into(pn1, sync_net, True, skip) t2_map, p2_map = copy_into(pn2, sync_net, False, skip) costs = dict() lst_t_pn1 = [] lst_t_pn2 = [] for t in pn1.transitions: lst_t_pn1.append(t) for t in pn2.transitions: lst_t_pn2.append(t) lst_t_pn1.sort(key=lambda k: k.name) lst_t_pn2.sort(key=lambda k: k.name) for t1 in lst_t_pn1: costs[t1_map[t1]] = pn1_costs[t1] for t2 in lst_t_pn2: costs[t2_map[t2]] = pn2_costs[t2] for t1 in lst_t_pn1: for t2 in lst_t_pn2: if t1.label == t2.label: sync = PetriNet.Transition((t1.name, t2.name), (t1.label, t2.label)) sync_net.transitions.add(sync) costs[sync] = sync_costs[(t1, t2)] # copy the properties of the transitions inside the transition of the sync net for p1 in t1.properties: sync.properties[p1] = t1.properties[p1] for p2 in t2.properties: sync.properties[p2] = t2.properties[p2] for a in t1.in_arcs: add_arc_from_to(p1_map[a.source], sync, sync_net) for a in t2.in_arcs: add_arc_from_to(p2_map[a.source], sync, sync_net) for a in t1.out_arcs: add_arc_from_to(sync, p1_map[a.target], sync_net) for a in t2.out_arcs: add_arc_from_to(sync, p2_map[a.target], sync_net) sync_im = SyncMarking() sync_fm = SyncMarking() for p in im1: sync_im[p1_map[p]] = im1[p] for p in im2: sync_im[p2_map[p]] = im2[p] for p in fm1: sync_fm[p1_map[p]] = fm1[p] for p in fm2: sync_fm[p2_map[p]] = fm2[p] # update 06/02/2021: to distinguish the sync nets that are output of this method, put a property in the sync net sync_net.properties[properties.IS_SYNC_NET] = True return sync_net, sync_im, sync_fm, costs
def get_transition(counts, label): """ Create a transitions with the specified label in the Petri net """ counts.inc_no_visible() return PetriNet.Transition(str(uuid.uuid4()), label)
def get_new_hidden_trans(counts, type_trans="unknown"): """ Create a new hidden transition in the Petri net """ counts.inc_no_hidden() return PetriNet.Transition(type_trans + '_' + str(counts.num_hidden), None)
def processing(log: EventLog, causal: Tuple[str, str], follows: Tuple[str, str]): """ Applying the Alpha Miner with the new relations Parameters ------------- log Filtered log causal Pairs that have a causal relation (->) follows Pairs that have a follow relation (>) Returns ------------- net Petri net im Initial marking fm Final marking """ # create list of all events labels = set() start_activities = set() end_activities = set() for trace in log: start_activities.add(trace.__getitem__(0)) end_activities.add(trace.__getitem__(len(trace) - 1)) for events in trace: labels.add(events) labels = list(labels) pairs = [] for key, element in causal.items(): for item in element: if get_sharp_relation(follows, key, key): if get_sharp_relation(follows, item, item): pairs.append(({key}, {item})) # combining pairs for i in range(0, len(pairs)): t1 = pairs[i] for j in range(i, len(pairs)): t2 = pairs[j] if t1 != t2: if t1[0].issubset(t2[0]) or t1[1].issubset(t2[1]): if get_sharp_relations_for_sets( follows, t1[0], t2[0]) and get_sharp_relations_for_sets( follows, t1[1], t2[1]): new_alpha_pair = (t1[0] | t2[0], t1[1] | t2[1]) if new_alpha_pair not in pairs: pairs.append((t1[0] | t2[0], t1[1] | t2[1])) # maximize pairs cleaned_pairs = list(filter(lambda p: __pair_maximizer(pairs, p), pairs)) # create transitions net = PetriNet('alpha_plus_net_' + str(time.time())) label_transition_dict = {} for label in labels: if label != 'artificial_start' and label != 'artificial_end': label_transition_dict[label] = PetriNet.Transition(label, label) net.transitions.add(label_transition_dict[label]) else: label_transition_dict[label] = PetriNet.Transition(label, None) net.transitions.add(label_transition_dict[label]) # and source and sink src = add_source(net, start_activities, label_transition_dict) sink = add_sink(net, end_activities, label_transition_dict) # create places for pair in cleaned_pairs: place = PetriNet.Place(str(pair)) net.places.add(place) for in_arc in pair[0]: add_arc_from_to(label_transition_dict[in_arc], place, net) for out_arc in pair[1]: add_arc_from_to(place, label_transition_dict[out_arc], net) return net, Marking({src: 1}), Marking({sink: 1}), cleaned_pairs
def test_figure42(self): net = PetriNet("figure_4_2") p_1 = PetriNet.Place("p_1") p_2 = PetriNet.Place("p_2") p_3 = PetriNet.Place("p_3") p_4 = PetriNet.Place("p_4") p_5 = PetriNet.Place("p_5") p_6 = PetriNet.Place("p_6") p_7 = PetriNet.Place("p_7") p_8 = PetriNet.Place("p_8") net.places.add(p_1) net.places.add(p_2) net.places.add(p_3) net.places.add(p_4) net.places.add(p_5) net.places.add(p_6) net.places.add(p_7) net.places.add(p_8) t_1 = PetriNet.Transition("t_1", "t_1") t_2 = PetriNet.Transition("t_2", "t_2") t_3 = PetriNet.Transition("t_3", "t_3") t_4 = PetriNet.Transition("t_4", "t_4") t_5 = PetriNet.Transition("t_5", "t_5") t_6 = PetriNet.Transition("t_6", "t_6") t_7 = PetriNet.Transition("t_7", "t_7") t_8 = PetriNet.Transition("t_8", "t_8") net.transitions.add(t_1) net.transitions.add(t_2) net.transitions.add(t_3) net.transitions.add(t_4) net.transitions.add(t_5) net.transitions.add(t_6) net.transitions.add(t_7) net.transitions.add(t_8) petri_utils.add_arc_from_to(p_1, t_1, net) petri_utils.add_arc_from_to(t_1, p_6, net) petri_utils.add_arc_from_to(t_1, p_4, net) petri_utils.add_arc_from_to(p_4, t_4, net) petri_utils.add_arc_from_to(p_4, t_5, net) petri_utils.add_arc_from_to(t_2, p_6, net) petri_utils.add_arc_from_to(t_2, p_4, net) petri_utils.add_arc_from_to(t_4, p_3, net) petri_utils.add_arc_from_to(t_4, p_5, net) petri_utils.add_arc_from_to(t_5, p_7, net) petri_utils.add_arc_from_to(t_7, p_4, net) petri_utils.add_arc_from_to(p_3, t_2, net) petri_utils.add_arc_from_to(p_3, t_3, net) petri_utils.add_arc_from_to(p_5, t_2, net) petri_utils.add_arc_from_to(p_5, t_3, net) petri_utils.add_arc_from_to(p_5, t_4, net) petri_utils.add_arc_from_to(p_7, t_6, net) petri_utils.add_arc_from_to(p_8, t_7, net) petri_utils.add_arc_from_to(p_8, t_8, net) petri_utils.add_arc_from_to(t_3, p_2, net) petri_utils.add_arc_from_to(p_6, t_6, net) petri_utils.add_arc_from_to(t_6, p_5, net) petri_utils.add_arc_from_to(t_8, p_8, net) initial_marking = Marking() initial_marking[p_1] = 1 final_marking = Marking() final_marking[p_2] = 1 self.assertFalse(woflan.apply(net, initial_marking, final_marking, parameters={"print_diagnostics": False}))
def test_mcg(self): net = PetriNet("mcg") p_1 = PetriNet.Place("p_1") p_2 = PetriNet.Place("p_2") p_3 = PetriNet.Place("p_3") p_4 = PetriNet.Place("p_4") p_5 = PetriNet.Place("p_5") net.places.add(p_1) net.places.add(p_2) net.places.add(p_3) net.places.add(p_4) net.places.add(p_5) t_1 = PetriNet.Transition("t_1", "t_1") t_2 = PetriNet.Transition("t_2", "t_2") t_3 = PetriNet.Transition("t_3", "t_3") t_4 = PetriNet.Transition("t_4", "t_4") t_5 = PetriNet.Transition("t_5", "t_5") t_6 = PetriNet.Transition("t_6", "t_6") net.transitions.add(t_1) net.transitions.add(t_2) net.transitions.add(t_3) net.transitions.add(t_4) net.transitions.add(t_5) net.transitions.add(t_6) petri_utils.add_arc_from_to(p_1, t_1, net) petri_utils.add_arc_from_to(t_1, p_2, net) petri_utils.add_arc_from_to(p_2, t_3, net) petri_utils.add_arc_from_to(t_3, p_3, net, weight=2) petri_utils.add_arc_from_to(p_3, t_4, net) petri_utils.add_arc_from_to(t_4, p_2, net) petri_utils.add_arc_from_to(p_1, t_2, net) petri_utils.add_arc_from_to(t_2, p_4, net) petri_utils.add_arc_from_to(p_4, t_5, net) petri_utils.add_arc_from_to(t_5, p_5, net, weight=2) petri_utils.add_arc_from_to(p_5, t_6, net) petri_utils.add_arc_from_to(t_6, p_4, net) initial_marking = Marking() initial_marking[p_1] = 1 mcg = minimal_coverability_graph.apply(net, initial_marking)
def apply(tree, parameters=None): """ Apply from Process Tree to Petri net Parameters ----------- tree Process tree parameters Parameters of the algorithm Returns ----------- net Petri net initial_marking Initial marking final_marking Final marking """ if parameters is None: parameters = {} del parameters counts = Counts() net = PetriNet('imdf_net_' + str(time.time())) initial_marking = Marking() final_marking = Marking() source = get_new_place(counts) source.name = "source" sink = get_new_place(counts) sink.name = "sink" net.places.add(source) net.places.add(sink) initial_marking[source] = 1 final_marking[sink] = 1 initial_mandatory = check_tau_mandatory_at_initial_marking(tree) final_mandatory = check_tau_mandatory_at_final_marking(tree) if initial_mandatory: initial_place = get_new_place(counts) net.places.add(initial_place) tau_initial = get_new_hidden_trans(counts, type_trans="tau") net.transitions.add(tau_initial) add_arc_from_to(source, tau_initial, net) add_arc_from_to(tau_initial, initial_place, net) else: initial_place = source if final_mandatory: final_place = get_new_place(counts) net.places.add(final_place) tau_final = get_new_hidden_trans(counts, type_trans="tau") net.transitions.add(tau_final) add_arc_from_to(final_place, tau_final, net) add_arc_from_to(tau_final, sink, net) else: final_place = sink net, counts, last_added_place = recursively_add_tree( tree, tree, net, initial_place, final_place, counts, 0) reduction.apply_simple_reduction(net) places = list(net.places) for place in places: if len(place.out_arcs) == 0 and not place in final_marking: remove_place(net, place) if len(place.in_arcs) == 0 and not place in initial_marking: remove_place(net, place) return net, initial_marking, final_marking
def get_new_place(counts): """ Create a new place in the Petri net """ counts.inc_places() return PetriNet.Place('p_' + str(counts.num_places))