'''
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/>.
'''
from pm4py.objects.ocel.obj import OCEL
from typing import Optional, Dict, Any, Tuple, Collection, Set, List
from enum import Enum
from pm4py.util import exec_utils, constants
from pm4py.objects.ocel import constants as ocel_constants
from pm4py.util.business_hours import BusinessHours
[docs]class Parameters(Enum):
EVENT_ID = ocel_constants.PARAM_EVENT_ID
OBJECT_ID = ocel_constants.PARAM_OBJECT_ID
OBJECT_TYPE = ocel_constants.PARAM_OBJECT_TYPE
EVENT_ACTIVITY = ocel_constants.PARAM_EVENT_ACTIVITY
EVENT_TIMESTAMP = ocel_constants.PARAM_EVENT_TIMESTAMP
BUSINESS_HOURS = "business_hours"
WORKTIMING = "worktiming"
WEEKENDS = "weekends"
WORKCALENDAR = "workcalendar"
[docs]def aggregate_ev_couples(edges: Dict[str, Dict[Tuple[str, str], Collection[Any]]]) -> Dict[
str, Dict[Tuple[str, str], Set[Any]]]:
"""
Performs an aggregation of the occurrences of a given edge on the couple of events (source event, target event).
Parameters
-------------------
edges
Edges calculated using the find_associations_per_edge function
Returns
-------------------
aggregation
A dictionary associating to each object type another dictionary where to each edge (activity couple) all the
couples of related events are associated.
"""
ret = {}
for ot in edges:
ret[ot] = {}
for act in edges[ot]:
ret[ot][act] = set((x[0], x[1]) for x in edges[ot][act])
return ret
[docs]def aggregate_unique_objects(edges: Dict[str, Dict[Tuple[str, str], Collection[Any]]]) -> Dict[
str, Dict[Tuple[str, str], Set[Any]]]:
"""
Performs an aggregation of the occurrences of a given edge in the involved object.
Parameters
-------------------
edges
Edges calculated using the find_associations_per_edge function
Returns
-------------------
aggregation
A dictionary associating to each object type another dictionary where to each edge (activity couple) all the
involved objects are associated.
"""
ret = {}
for ot in edges:
ret[ot] = {}
for act in edges[ot]:
ret[ot][act] = set(x[2] for x in edges[ot][act])
return ret
[docs]def aggregate_total_objects(edges: Dict[str, Dict[Tuple[str, str], Collection[Any]]]) -> Dict[
str, Dict[Tuple[str, str], Set[Any]]]:
"""
Performs an aggregation of the occurrences of a given edge on the triple (source event, target event, object).
Parameters
-------------------
edges
Edges calculated using the find_associations_per_edge function
Returns
-------------------
aggregation
A dictionary associating to each object type another dictionary where to each edge (activity couple) all the
triples (source event, target event, object) are associated.
"""
ret = {}
for ot in edges:
ret[ot] = {}
for act in edges[ot]:
ret[ot][act] = set(edges[ot][act])
return ret
[docs]def find_associations_per_edge(ocel: OCEL, parameters: Optional[Dict[Any, Any]] = None) -> Dict[
str, Dict[Tuple[str, str], Collection[Any]]]:
"""
Finds all the occurrences of a given edge (activity couple), expressed as triples (source event, target event, object ID).
Parameters
-------------------
ocel
Object-centric event log
parameters
Parameters of the algorithm, including:
- Parameters.EVENT_ACTIVITY => the activity
- Parameters.EVENT_ID => the event identifier
- Parameters.OBJECT_ID => the object identifier
- Parameters.OBJECT_TYPE => the object type
Returns
------------------
edges
A dictionary associating to each object type a dictionary where to each edge (activity couple) the list of triples (source event, target event, object ID)
is associated.
"""
if parameters is None:
parameters = {}
event_activity = exec_utils.get_param_value(Parameters.EVENT_ACTIVITY, parameters, ocel.event_activity)
event_id = exec_utils.get_param_value(Parameters.EVENT_ID, parameters, ocel.event_id_column)
object_id = exec_utils.get_param_value(Parameters.OBJECT_ID, parameters, ocel.object_id_column)
object_type = exec_utils.get_param_value(Parameters.OBJECT_TYPE, parameters, ocel.object_type_column)
identifiers = list(ocel.events[event_id])
activities = ocel.events.groupby(event_id)[event_activity].apply(list).to_dict()
activities = {x: y[0] for x, y in activities.items()}
omap = ocel.relations.groupby(event_id)[object_id].apply(list).to_dict()
objtypes = ocel.objects.groupby(object_id)[object_type].apply(list).to_dict()
objtypes = {x: y[0] for x, y in objtypes.items()}
history = {}
edges = {}
for evid in identifiers:
if evid in omap:
for obj in omap[evid]:
if obj in history:
objtype = objtypes[obj]
if objtype not in edges:
edges[objtype] = {}
previd = history[obj]
acttup = (activities[previd], activities[evid])
if acttup not in edges[objtype]:
edges[objtype][acttup] = list()
edges[objtype][acttup].append((previd, evid, obj))
history[obj] = evid
return edges