def test_simple_case(self): g = ts.TransitionSystem() m0 = ts.TransitionSystem.State('m0') m1 = ts.TransitionSystem.State('m1') m2 = ts.TransitionSystem.State('m2') t1 = ts.TransitionSystem.Transition('t1', m0, m1) t2 = ts.TransitionSystem.Transition('t2', m1, m2) m0.outgoing.add(t1) m1.outgoing.add(t2) m1.incoming.add(t1) m2.incoming.add(t2) g.states.add(m0) g.states.add(m1) g.states.add(m2) g.transitions.add(t1) g.transitions.add(t2) is_inv = lambda t: t.name is None state2int = {'m0': 0, 'm1': 1, 'm2': 2} distmat = hmmconf_setup.compute_distmat(g, state2int, is_inv) expected = np.zeros((3, 3)) expected[0, 1] = 1 expected[0, 2] = 2 expected[1, 0] = 1 expected[1, 2] = 1 expected[2, 0] = 2 expected[2, 1] = 1 assert distmat.shape == expected.shape assert (distmat == expected).all()
def apply(log, parameters=None): if parameters is None: parameters = {} for parameter in DEFAULT_PARAMETERS: if parameter not in parameters: parameters[parameter] = DEFAULT_PARAMETERS[parameter] activity_key = parameters[ PARAMETER_CONSTANT_ACTIVITY_KEY] if PARAMETER_CONSTANT_ACTIVITY_KEY in parameters else DEFAULT_NAME_KEY transition_system = ts.TransitionSystem() #여기서 control flow 말고 event 자체를 추출하고 view sequence 만들 때 activity_key로 생성 print("project traces") control_flow_log = log_util.log.project_traces( log, [activity_key, 'time:timestamp']) #control_flow_log = log_util.log.project_traces(log, activity_key) #print(control_flow_log) print("produce view sequences") view_sequence = (list( map(lambda t: __modi_compute_view_sequence(t, parameters), control_flow_log))) #view_sequence = (list(map(lambda t: __compute_view_sequence(t, parameters), control_flow_log))) #print(view_sequence) print("construct state path") for vs in view_sequence: __construct_state_path(vs, transition_system) return transition_system
def apply(log, parameters=None): if parameters is None: parameters = {} activity_key = exec_utils.get_param_value(Parameters.ACTIVITY_KEY, parameters, DEFAULT_NAME_KEY) transition_system = ts.TransitionSystem() control_flow_log = log_util.log.project_traces(log, activity_key) view_sequence = (list(map(lambda t: __compute_view_sequence(t, parameters), control_flow_log))) for vs in view_sequence: __construct_state_path(vs, transition_system) return transition_system
def test_multixor_inv_case(self): g = ts.TransitionSystem() m0 = ts.TransitionSystem.State('m0') m1 = ts.TransitionSystem.State('m1') m2 = ts.TransitionSystem.State('m2') m3 = ts.TransitionSystem.State('m3') m4 = ts.TransitionSystem.State('m4') m5 = ts.TransitionSystem.State('m5') t3 = ts.TransitionSystem.Transition('t3', m2, m3) t4 = ts.TransitionSystem.Transition('t4', m0, m4) t5 = ts.TransitionSystem.Transition('t5', m1, m5) inv0 = ts.TransitionSystem.Transition(None, m0, m1) inv1 = ts.TransitionSystem.Transition(None, m1, m2) m0.outgoing.add(inv0) m1.incoming.add(inv0) m0.outgoing.add(t4) m4.incoming.add(t4) m1.outgoing.add(inv1) m2.incoming.add(inv1) m1.outgoing.add(t5) m5.incoming.add(t5) m2.outgoing.add(t3) m3.incoming.add(t3) is_inv = lambda t: t.name is None state2int = { m0.name: 0, m1.name: 1, m2.name: 2, m3.name: 3, m4.name: 4, m5.name: 5, } obs2int = { t3.name: 0, t4.name: 1, t5.name: 2, } confmat = hmmconf_setup.compute_confmat(g, m0, is_inv, state2int, obs2int) expected = np.zeros((3, 6)) expected[0][0] = 1 expected[1][0] = 1 expected[2][0] = 1 expected[0][1] = 1 expected[2][1] = 1 expected[0][2] = 1 assert confmat.shape == expected.shape assert (confmat == expected).all()
def apply(trace_log, parameters=None): if parameters is None: parameters = {} for parameter in DEFAULT_PARAMETERS: if parameter not in parameters: parameters[parameter] = DEFAULT_PARAMETERS[parameter] activity_key = parameters[ PARAMETER_CONSTANT_ACTIVITY_KEY] if PARAMETER_CONSTANT_ACTIVITY_KEY in parameters else DEFAULT_NAME_KEY transition_system = ts.TransitionSystem() control_flow_log = log_util.trace_log.project_traces(trace_log, activity_key) view_sequence = (list(map(lambda t: __compute_view_sequence(t, parameters), control_flow_log))) for vs in view_sequence: __construct_state_path(vs, transition_system) return transition_system
def construct_reachability_graph(net, initial_marking, use_trans_name=False): """ Creates a reachability graph of a certain Petri net. DO NOT ATTEMPT WITH AN UNBOUNDED PETRI NET, EVER. TODO: graphviz do not show labeling for the arcs. Add the labeling. Parameters ---------- net: Petri net initial_marking: initial marking of the Petri net. Returns ------- re_gr: Transition system that represents the reachability graph of the input Petri net. """ active = [initial_marking] visited = [] re_gr = ts.TransitionSystem() re_gr.states.add(ts.TransitionSystem.State(staterep( repr(initial_marking)))) while active: curr_mark = active.pop(0) curr_state = next((state for state in re_gr.states if state.name == staterep(repr(curr_mark))), None) en_tr = petri.semantics.enabled_transitions(net, curr_mark) for t in en_tr: next_mark = petri.semantics.execute(t, net, curr_mark) next_state = next((state for state in re_gr.states if state.name == staterep(repr(next_mark))), None) if next_state is None: next_state = ts.TransitionSystem.State( staterep(repr(next_mark))) re_gr.states.add(next_state) if use_trans_name: utils.add_arc_from_to(t.name, curr_state, next_state, re_gr) else: utils.add_arc_from_to(repr(t), curr_state, next_state, re_gr) # If the next marking hash is not in visited, if the next marking itself is not already in active # and if the next marking is different from the current one if hash(next_mark) not in visited and next( (mark for mark in active if hash(mark) == hash(next_mark)), None) is None and hash(curr_mark) != hash(next_mark): active.append(next_mark) visited.append(hash(curr_mark)) return re_gr
def test_fwdprop_inv_case(self): g = ts.TransitionSystem() m0 = ts.TransitionSystem.State('m0') m1 = ts.TransitionSystem.State('m1') m2 = ts.TransitionSystem.State('m2') m3 = ts.TransitionSystem.State('m3') t1 = ts.TransitionSystem.Transition('t1', m0, m1) inv0 = ts.TransitionSystem.Transition(None, m1, m2) inv1 = ts.TransitionSystem.Transition(None, m2, m3) m0.outgoing.add(t1) m1.outgoing.add(inv0) m2.outgoing.add(inv1) m1.incoming.add(t1) m2.incoming.add(inv0) m3.incoming.add(inv1) g.states.add(m0) g.states.add(m1) g.states.add(m2) g.states.add(m3) g.transitions.add(t1) g.transitions.add(inv0) g.transitions.add(inv1) is_inv = lambda t: t.name is None state2int = { m0.name: 0, m1.name: 1, m2.name: 2, m3.name: 3, } obs2int = { t1.name: 0, } confmat = hmmconf_setup.compute_confmat(g, m0, is_inv, state2int, obs2int) expected = np.zeros((1, 4)) expected[0][0] = 1 assert confmat.shape == expected.shape assert (confmat == expected).all()
def test_inv_loop_case(self): g = ts.TransitionSystem() m0 = ts.TransitionSystem.State('m0') m1 = ts.TransitionSystem.State('m1') m2 = ts.TransitionSystem.State('m2') m3 = ts.TransitionSystem.State('m3') inv0 = ts.TransitionSystem.Transition(None, m0, m1) inv1 = ts.TransitionSystem.Transition(None, m1, m2) inv2 = ts.TransitionSystem.Transition(None, m2, m3) t3 = ts.TransitionSystem.Transition(None, m0, m3) m0.outgoing.add(inv0) m1.outgoing.add(inv1) m2.outgoing.add(inv2) m0.outgoing.add(t3) m1.incoming.add(inv0) m2.incoming.add(inv1) m3.incoming.add(inv2) m3.incoming.add(t3) g.states.add(m0) g.states.add(m1) g.states.add(m2) g.states.add(m3) g.transitions.add(inv0) g.transitions.add(inv1) g.transitions.add(inv2) g.transitions.add(t3) is_inv = lambda t: t.name is None state2int = { 'm0': 0, 'm1': 1, 'm2': 2, 'm3': 3, } distmat = hmmconf_setup.compute_distmat(g, state2int, is_inv) expected = np.zeros((4, 4)) assert distmat.shape == expected.shape assert (distmat == expected).all()
def construct_reachability_graph_from_flow(incoming_transitions, outgoing_transitions, use_trans_name=False, parameters=None): """ Construct the reachability graph from the marking flow Parameters ---------------- incoming_transitions Incoming transitions outgoing_transitions Outgoing transitions use_trans_name Use the transition name Returns ---------------- re_gr Transition system that represents the reachability graph of the input Petri net. """ if parameters is None: parameters = {} re_gr = ts.TransitionSystem() map_states = {} for s in incoming_transitions: map_states[s] = ts.TransitionSystem.State(staterep(repr(s))) re_gr.states.add(map_states[s]) for s1 in outgoing_transitions: for t in outgoing_transitions[s1]: s2 = outgoing_transitions[s1][t] if use_trans_name: utils.add_arc_from_to(t.name, map_states[s1], map_states[s2], re_gr) else: utils.add_arc_from_to(repr(t), map_states[s1], map_states[s2], re_gr) return re_gr
def test_simple_case(self): g = ts.TransitionSystem() m0 = ts.TransitionSystem.State('m0') m1 = ts.TransitionSystem.State('m1') m2 = ts.TransitionSystem.State('m2') m3 = ts.TransitionSystem.State('m3') t1 = ts.TransitionSystem.Transition('t1', m0, m1) t2 = ts.TransitionSystem.Transition('t2', m0, m2) t3 = ts.TransitionSystem.Transition('t3', m1, m3) m0.outgoing.add(t1) m1.incoming.add(t1) m0.outgoing.add(t2) m2.incoming.add(t2) m1.outgoing.add(t3) m3.incoming.add(t3) g.states.add(m0) g.states.add(m1) g.states.add(m2) g.states.add(m3) g.transitions.add(t1) g.transitions.add(t2) g.transitions.add(t3) is_inv = lambda t: t.name is None state2int = {m0.name: 0, m1.name: 1, m2.name: 2, m3.name: 3} obs2int = {t1.name: 0, t2.name: 1, t3.name: 2} confmat = hmmconf_setup.compute_confmat(g, m0, is_inv, state2int, obs2int) expected = np.zeros((3, 4)) expected[0][0] = 1 expected[1][0] = 1 expected[2][1] = 1 print(confmat) assert confmat.shape == expected.shape assert (confmat == expected).all()
def apply(log, parameters=None): if parameters is None: parameters = {} activity_key = exec_utils.get_param_value(Parameters.ACTIVITY_KEY, parameters, DEFAULT_NAME_KEY) include_data = exec_utils.get_param_value(Parameters.INCLUDE_DATA, parameters, False) transition_system = ts.TransitionSystem() control_flow_log = log_util.log.project_traces(log, activity_key) view_sequence = [] for i in range(len(log)): view_sequence.append( __compute_view_sequence(control_flow_log[i], log[i], parameters=parameters)) for vs in view_sequence: __construct_state_path(vs, transition_system, include_data=include_data) return transition_system
def test_xor_with_inv_case(self): g = ts.TransitionSystem() m0 = ts.TransitionSystem.State('m0') m1 = ts.TransitionSystem.State('m1') m2 = ts.TransitionSystem.State('m2') inv = ts.TransitionSystem.Transition(None, m0, m1) t1 = ts.TransitionSystem.Transition('t1', m0, m1) t2 = ts.TransitionSystem.Transition('t2', m1, m2) m0.outgoing.add(inv) m0.outgoing.add(t1) m1.outgoing.add(t2) m1.incoming.add(inv) m1.incoming.add(t1) m2.incoming.add(t2) g.states.add(m0) g.states.add(m1) g.states.add(m2) g.transitions.add(inv) g.transitions.add(t1) g.transitions.add(t2) is_inv = lambda t: t.name is None state2int = {m0.name: 0, m1.name: 1, m2.name: 2} obs2int = {t1.name: 0, t2.name: 1} confmat = hmmconf_setup.compute_confmat(g, m0, is_inv, state2int, obs2int) expected = np.zeros((2, 3)) expected[0][0] = 1 expected[1][0] = 1 expected[1][1] = 1 assert confmat.shape == expected.shape assert (confmat == expected).all()
def build_reachability_graph(net, init_marking, is_inv, staterep=default_staterep): """ Build reachability graph and keep track of to and from states connected by invisible transitions. :param net: the petrinet :param init_marking: initial marking :param is_inv: function that indicate if a transition is invisible :type is_inv: function :staterep: function that gives a state a string representation :return reachability graph, list of (from_state, inv_tran, to_state) """ # BFS with queue mark_queue = [init_marking] rg = ts.TransitionSystem(name='Reachability graph of {}'.format(net.name)) inv_states = list() init_state = ts.TransitionSystem.State(staterep(repr(init_marking))) init_state.data['disc'] = 0 rg.states.add(init_state) # mapping visited states to marking mark_to_state = dict() mark_to_state[init_marking] = init_state while mark_queue and len(rg.states) < MAX_RG_STATE: cur_mark = mark_queue.pop(0) cur_state = mark_to_state[cur_mark] enabled_trans = list(semantics.enabled_transitions(net, cur_mark)) # workout the transition arc weight n_vis = len(list(map(lambda t: not is_inv(t), enabled_trans))) weight = 1. / n_vis if n_vis > 0 else 0 for t in enabled_trans: next_mark = semantics.execute(t, net, cur_mark) next_state = mark_to_state.get(next_mark, None) if next_state is None: next_state = ts.TransitionSystem.State( staterep(repr(next_mark))) # discovered one step away from parent node next_state.data['disc'] = cur_state.data['disc'] + 1 rg.states.add(next_state) mark_to_state[next_mark] = next_state mark_queue.append(next_mark) # doesnt matter that invisible transitions also get weight since # they will be removed ultimately as well data = {'weight': weight} t_label = t.label if t.name is not None else None rg_t = add_arc_from_to(t_label, cur_state, next_state, rg, data) if is_inv(t): inv_states.append((cur_state, rg_t, next_state)) if mark_queue: msg = 'Computation of reachability graph surpass the max number of states: {}'.format( MAX_RG_STATE) warnings.warn(msg, category=UserWarning) return rg, inv_states