File size: 6,892 Bytes
e60e568
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
'''
    This file is part of PM4Py (More Info: https://pm4py.fit.fraunhofer.de).

    PM4Py is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    PM4Py is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with PM4Py.  If not, see <https://www.gnu.org/licenses/>.
'''
import math

from pm4py.objects.process_tree.obj import Operator


def get_max_trace_length(tree, parameters=None):
    """
    Get the maximum length of a trace allowed by the process tree
    (can be infty)

    Parameters
    ---------------
    tree
        Process tree
    parameters
        Possible parameters of the algorithm

    Returns
    --------------
    max_trace_length
        The maximum length of a trace
    """
    if parameters is None:
        parameters = {}

    bottomup = get_bottomup_nodes(tree, parameters=parameters)
    max_length_dict = {}
    for i in range(len(bottomup)):
        get_max_length_dict(bottomup[i], max_length_dict, len(bottomup))

    return max_length_dict[tree]


def get_min_trace_length(tree, parameters=None):
    """
    Get the minimum length of a trace allowed by the process tree

    Parameters
    ---------------
    tree
        Process tree
    parameters
        Possible parameters of the algorithm

    Returns
    --------------
    min_trace_length
        The minimum length of a trace
    """
    if parameters is None:
        parameters = {}

    bottomup = get_bottomup_nodes(tree, parameters=parameters)
    min_length_dict = {}
    for i in range(len(bottomup)):
        get_min_length_dict(bottomup[i], min_length_dict)

    return min_length_dict[tree]


def get_max_rem_dict(tree, parameters=None):
    """
    Gets for each node of the tree the maximum number of activities
    that are inserted to 'complete' a trace of the overall tree

    Parameters
    ----------------
    tree
        Process tree
    parameters
        Parameters of the algorithm

    Returns
    ---------------
    max_rem_dict
        Dictionary described in the docstring
    """
    if parameters is None:
        parameters = {}

    bottomup = get_bottomup_nodes(tree, parameters=parameters)
    max_length_dict = {}
    for i in range(len(bottomup)):
        get_max_length_dict(bottomup[i], max_length_dict, len(bottomup))

    max_rem_dict = {}
    for i in range(len(bottomup)):
        max_rem_dict[bottomup[i]] = max_length_dict[tree] - max_length_dict[bottomup[i]]

    return max_rem_dict


def get_min_rem_dict(tree, parameters=None):
    """
    Gets for each node of the tree the minimum number of activities
    that are inserted to 'complete' a trace of the overall tree

    Parameters
    ----------------
    tree
        Process tree
    parameters
        Parameters of the algorithm

    Returns
    ---------------
    min_rem_dict
        Dictionary described in the docstring
    """
    if parameters is None:
        parameters = {}

    bottomup = get_bottomup_nodes(tree, parameters=parameters)
    min_length_dict = {}
    for i in range(len(bottomup)):
        get_min_length_dict(bottomup[i], min_length_dict)

    min_rem_dict = {}
    for i in range(len(bottomup)):
        min_rem_dict[bottomup[i]] = min_length_dict[tree] - min_length_dict[bottomup[i]]

    return min_rem_dict


def get_max_length_dict(node, max_length_dict, num_nodes):
    """
    Populates, given the nodes of a tree in a bottom-up order, the maximum length dictionary
    (every trace generated from that point of the tree has at most length N)

    Parameters
    ---------------
    node
        Node
    max_length_dict
        Dictionary that is populated in-place
    num_nodes
        Number of nodes in the process tree
    """
    if len(node.children) == 0:
        if node.label is None:
            max_length_dict[node] = 0
        else:
            max_length_dict[node] = 1
    elif node.operator == Operator.XOR:
        max_length_dict[node] = max(max_length_dict[x] for x in node.children)
    elif node.operator == Operator.PARALLEL or node.operator == Operator.SEQUENCE or node.operator == Operator.OR:
        max_length_dict[node] = sum(max_length_dict[x] for x in node.children)
    elif node.operator == Operator.LOOP:
        max_length_dict[node] = sum(max_length_dict[x] for x in node.children) + 2 ** (
                    48 - math.ceil(math.log(num_nodes) / math.log(2)))


def get_min_length_dict(node, min_length_dict):
    """
    Populates, given the nodes of a tree in a bottom-up order, the minimum length dictionary
    (every trace generated from that point of the tree has at least length N)

    Parameters
    ---------------
    node
        Node
    min_length_dict
        Dictionary that is populated in-place
    """
    if len(node.children) == 0:
        if node.label is None:
            min_length_dict[node] = 0
        else:
            min_length_dict[node] = 1
    elif node.operator == Operator.XOR:
        min_length_dict[node] = min(min_length_dict[x] for x in node.children)
    elif node.operator == Operator.PARALLEL or node.operator == Operator.SEQUENCE or node.operator == Operator.OR:
        min_length_dict[node] = sum(min_length_dict[x] for x in node.children)
    elif node.operator == Operator.LOOP:
        min_length_dict[node] = min_length_dict[node.children[0]]


def get_bottomup_nodes(tree, parameters=None):
    """
    Gets the nodes of a tree in a bottomup order (leafs come first, the master node comes after)

    Parameters
    --------------
    tree
        Process tree
    parameters
        Parameters of the algorithm

    Returns
    -------------
    bottomup_nodes
        Nodes of the tree in a bottomup order
    """
    if parameters is None:
        parameters = {}

    to_visit = [tree]
    all_nodes = set()
    while len(to_visit) > 0:
        n = to_visit.pop(0)
        all_nodes.add(n)
        for child in n.children:
            to_visit.append(child)
    # starts to visit the tree from the leafs
    bottomup = [x for x in all_nodes if len(x.children) == 0]
    # then add iteratively the parent
    i = 0
    while i < len(bottomup):
        parent = bottomup[i].parent
        if parent is not None and parent not in bottomup:
            is_ok = True
            for child in parent.children:
                if not child in bottomup:
                    is_ok = False
                    break
            if is_ok:
                bottomup.append(parent)
        i = i + 1

    return bottomup