Example #1
0
    def test_multiple_optimal_alignments(self):
        net = PetriNet()
        for i in range(1, 5):
            net.add_place(i)
        transitions = ['A', 'B', 'C', 'D']
        for t in transitions:
            net.add_transition(t)
        edges = [(1, -1), (1, -2), (-1, 2), (-2, 2), (2, -3), (-3, 3), (3, -4),
                 (-4, 4)]
        for e in edges:
            net.add_edge(e[0], e[1])

        trace = ['C']
        trace_net = TraceNet(trace)

        sync_prod = SynchronousProduct(net, trace_net)

        ilp_searcher = A_Star(sync_prod,
                              trace,
                              heuristic='ilp',
                              n_alignments=2)
        ilp_searcher.search()

        # ----------------------------------
        self.assertTrue(len(ilp_searcher.alignments) == 2)
        self.assertTrue(
            [('B', '>>'), ('C', 'C'), ('D',
                                       '>>')] in ilp_searcher.alignment_moves)
        self.assertTrue(
            [('A', '>>'), ('C', 'C'), ('D',
                                       '>>')] in ilp_searcher.alignment_moves)
Example #2
0
    def test_add_marking_df_pn(self):
        dpn = PetriNet()

        dpn.add_place(1)
        dpn.add_place(2)
        dpn.add_place(3)

        # ----------------------------------
        dpn.add_marking(3)

        self.assertListEqual(dpn.marking, [0, 0, 1])
Example #3
0
    def test_add_edge_df_pn(self):
        dpn = PetriNet()
        dpn.add_transition('A')

        dpn.add_place(1)
        dpn.add_place(2)

        # ----------------------------------
        dpn.add_edge(1, -1, True)

        self.assertEqual(dpn.edges, [(1, -1), (-1, 1)])
Example #4
0
    def test_remove_transition_df_pn(self):
        dpn = PetriNet()
        dpn.add_transition('A')
        dpn.add_transition('B')
        dpn.add_transition('C')
        dpn.add_transition('A')

        # ----------------------------------
        dpn.remove_transition(-2)
        dpn.remove_transition(-1)

        self.assertDictEqual(dpn.transitions, {'A': [-4], 'C': [-3]})
Example #5
0
    def test_add_transition_df_pn(self):
        dpn = PetriNet()
        dpn.add_transition('A')
        dpn.add_transition('B')
        dpn.add_transition('A')

        self.assertDictEqual(dpn.transitions, {'A': [-1, -3], 'B': [-2]})
Example #6
0
    def test_chain_operations_df_pn(self):
        dpn = PetriNet()

        dpn.add_place(1) \
         .add_place(2) \
         .add_place(3) \
         .add_transition('A') \
         .add_transition('B') \
         .add_edge(1, -1) \
         .add_edge(-1, 2) \
         .add_edge(1, -2) \
         .add_edge(-2, 2) \
         .add_transition('C') \
         .add_transition('A') \
         .add_marking(1, 2) \
         .remove_place(3) \
         .add_edge(1, -3) \
         .remove_edge(1, -3) \
         .remove_transition(-3)

        self.assertDictEqual(dpn.places, {0: 1, 1: 2})
        self.assertDictEqual(dpn.transitions, {'A': [-1, -4], 'B': [-2]})
        self.assertListEqual(dpn.marking, [2, 0])
Example #7
0
    def test_add_place_df_pn(self):
        dpn = PetriNet()
        dpn.add_place(1)
        dpn.add_place(2, 3)

        # ----------------------------------
        self.assertDictEqual(dpn.places, {0: 1, 1: 2})
        self.assertListEqual(dpn.marking, [0, 0])
        self.assertListEqual(dpn.capacity, [1, 3])
Example #8
0
    def tree_to_petri_net(self, tree: ProcessTree) -> PetriNet:
        net = PetriNet()
        net.add_place(1)

        # net.add_marking(1, 1)

        def add_region(sub_tree, place_in, place_out):

            if isinstance(sub_tree, ProcessTree):
                if sub_tree.cut == Cut.PARA:
                    # add additional input place
                    add_region(sub_tree.children[0], place_in, place_out)
                    for child in sub_tree.children[1:]:
                        new_place_in = max(list(net.places.values()))[0] + 1
                        add_region(sub_tree.children[0], new_place_in,
                                   place_out)

                elif sub_tree.cut == Cut.EXCL:
                    pass
                elif sub_tree.cut == Cut.SEQ:
                    pass
                elif sub_tree.cut == Cut.LOOP:
                    pass
                else:
                    next = min(list(net.transitions.values()))[0] - 1
                    net.add_transition(''.join(['sub region', str(next)]))
                    trans = min(list(net.transitions.values()))[0]
                    net.add_edge(place_in, trans)
                    net.add_edge(trans, place_out)
            else:
                net.add_transition(sub_tree)
                trans = min(list(net.transitions.values()))[0]
                net.add_edge(place_in, trans)
                net.add_edge(trans, place_out)

        for child in tree.children:
            pre_place = max(list(net.places.values()))
            net.add_place(pre_place + 1)
            add_region(child, pre_place, pre_place + 1)

        return net
Example #9
0
import unittest
from pmlab_lite.pn import PetriNet, TraceNet, SynchronousProduct
from pmlab_lite.alignments import a_star
from pmlab_lite.helper.viz.dot import draw_petri_net, draw_synchronous_product, draw_a_star_search_space
from numpy.linalg import lstsq, matrix_rank

#create running example from conformance checking, p. 126
running_example = PetriNet()
transitions = ['As', 'Aa', 'Da1', 'Fa', 'Sso', 'Ro', 'Co', 'tau', 'Ao', 'Do', 'Aaa', 'Da2', 'Af']
edges = [(1,-1), (-1,2), (2,-3), (2,-2), (-3,11), (-2,3), (-2,5), (3,-4), (-4,4), (4,-8), (5,-5), (-5,6), (6,-6), (-6,7), (7,-7), (-7,5), (7,-8),
         (-8,8), (8,-9), (-9,9), (9,-11), (-11,11), (8,-10), (-10,10), (10,-12), (-12,11), (11,-13), (-13,12)]

for p in range(1,13):
  running_example.add_place(p)
for t in transitions:
  running_example.add_transition(t)
for e in edges:
  running_example.add_edge(e[0], e[1])

#create trace net
trace1 = ['As', 'Aa', 'Sso', 'Ro', 'Ao', 'Aaa', 'Aaa']
trace_net1 = TraceNet(trace1)

#create synchronous product
sp1 = SynchronousProduct(running_example, trace_net1)

# draw nets for first example
#draw_petri_net(running_example, filename='running_example')
#draw_petri_net(trace_net1, filename='trace_net1')
#draw_synchronous_product(sp1, filename='synchronous_product1')
Example #10
0
    def test_remove_place_df_pn(self):
        dpn = PetriNet()
        dpn.add_place(1)
        dpn.add_place(2)
        dpn.add_place(3)

        dpn.add_marking(3)

        # ----------------------------------
        dpn.remove_place(2)
        self.assertDictEqual(dpn.places, {0: 1, 1: 3})
        self.assertListEqual(dpn.marking, [0, 1])
        self.assertListEqual(dpn.capacity, [1, 1])

        dpn.remove_place(3)
        self.assertDictEqual(dpn.places, {0: 1})
        self.assertListEqual(dpn.marking, [0])
        self.assertListEqual(dpn.capacity, [1])
Example #11
0
    def test_is_enabled(self):
        net = PetriNet()
        net.add_transition('A')
        net.add_place(1).add_place(2).add_place(3)
        net.add_edge(1, -1).add_edge(-1, 3).add_edge(2, -1)

        # ----------------------------------
        self.assertFalse(net.is_enabled(-1))

        net.add_marking(1)
        self.assertFalse(net.is_enabled(-1))

        net.add_marking(2)
        self.assertTrue(net.is_enabled(-1))
Example #12
0
    def test_fire_transition(self):
        net = PetriNet()
        net.add_transition('A').add_transition('B')
        net.add_place(1).add_place(2).add_place(3).add_place(4)
        net.add_edge(1, -1).add_edge(-1, 3).add_edge(2, -1) \
         .add_edge(3, -2).add_edge(-2, 4)

        net.add_marking(1).add_marking(2)

        # ----------------------------------
        net.fire_transition(-1)
        transitions = net.all_enabled_transitions()
        self.assertListEqual(transitions, [-2])
Example #13
0
    def test_place_capacity(self):
        net = PetriNet()
        for i in range(1, 4):
            net.add_place(i)
        net.add_transition('A')
        net.add_transition('B')
        net.add_edge(1, -1)
        net.add_edge(-1, 2)
        net.add_edge(-1, 3)
        net.add_edge(2, -2)

        net.add_marking(1, 1)
        net.add_marking(2, 4)

        net.fire_transition(-1)

        print(net.get_exceeded_places())
        net.fire_transition(-2)
        net.fire_transition(-2)
        net.fire_transition(-2)
        net.fire_transition(-2)
        print(net.get_exceeded_places())
Example #14
0
    def test_replay_df_pn(self):
        dpn = PetriNet()
        dpn.add_transition('A')
        dpn.add_transition('B')
        dpn.add_transition('C')

        dpn.add_place(1)
        dpn.add_place(2)
        dpn.add_place(3)

        dpn.add_edge(1, -1)
        dpn.add_edge(-1, 2)

        dpn.add_edge(1, -3)
        dpn.add_edge(-3, 3)

        # dpn.add_edge(1, -2)  # deadlock
        dpn.add_edge(2, -2)
        # dpn.add_edge(3, -2)
        dpn.add_edge(-2, 3)

        dpn.add_marking(1)
        # dpn.add_marking(2, 2)
        # dpn.add_marking(3, 2)

        # start replay
        dpn.replay(5)
        self.assertListEqual(dpn.marking, [0, 0, 1])
Example #15
0
    def test_remove_all_edges_of_df_pn(self):
        dpn = PetriNet()
        dpn.add_transition('A')
        dpn.add_transition('B')

        dpn.add_place(1)
        dpn.add_place(2)

        dpn.add_edge(1, -1, True)
        dpn.add_edge(-1, 2)
        dpn.add_edge(1, -2)

        # ----------------------------------
        dpn.remove_all_edges_of(-1)

        self.assertEqual(dpn.edges, [(1, -2)])
Example #16
0
import unittest
from pmlab_lite.pn import PetriNet, TraceNet, SynchronousProduct
from pmlab_lite.alignments import a_star
from pmlab_lite.helper.viz.dot import draw_petri_net, draw_synchronous_product, draw_a_star_search_space
import numpy as np
from numpy.linalg import lstsq, matrix_rank
from cvxopt import matrix, solvers

net = PetriNet()
transitions = ['A', 'B']
edges = [(1, -1), (-1, 2), (2, -2), (-2, 3)]
for p in range(1, 4):
    net.add_place(p)
for t in transitions:
    net.add_transition(t)
for e in edges:
    net.add_edge(e[0], e[1])

trace = ['A', 'B']
trace_net = TraceNet(trace)

sp = SynchronousProduct(net, trace_net)
im = sp.incidence_matrix()
imv = sp.get_init_marking()
fmv = sp.get_final_marking()

b = fmv - imv
x = np.linalg.lstsq(im, b, rcond=None)[0]
#print(x)
#x[x > 0] = 1
#x[x <= 0] = 0