def apply_playout(net, initialMarking, noTraces=100, maxTraceLength=100):
    """
    Do the playout of a Petrinet generating a log

    Parameters
    ----------
    net
        Petri net to play-out
    initialMarking
        Initial marking of the Petri net
    noOfTraces
        Number of traces to generate
    maxTraceLength
        Maximum number of events per trace (do break)
    """
    log = log_instance.TraceLog()
    for i in range(noTraces):
        trace = log_instance.Trace()
        trace.attributes["concept:name"] = str(i)
        marking = copy(initialMarking)
        while semantics.enabled_transitions(net, marking):
            allEnabledTrans = semantics.enabled_transitions(net, marking)
            allEnabledTrans = list(allEnabledTrans)
            shuffle(allEnabledTrans)
            trans = allEnabledTrans[0]
            if trans.label is not None:
                event = log_instance.Event()
                event["concept:name"] = trans.label
                trace.append(event)
            marking = semantics.execute(trans, net, marking)
            if len(trace) > maxTraceLength:
                break
        if len(trace) > 0:
            log.append(trace)
    return log
Exemple #2
0
def get_visible_transitions_eventually_enabled_by_marking(net, marking):
    """
    Get visible transitions eventually enabled by marking (passing possibly through hidden transitions)

    Parameters
    ----------
    net
        Petri net
    marking
        Current marking
    """
    allEnabledTransitions = list(semantics.enabled_transitions(net, marking))
    visibleTransitions = set()
    visitedTransitions = set()

    i = 0
    while i < len(allEnabledTransitions):
        t = allEnabledTransitions[i]
        if not t in visitedTransitions:
            if t.label is not None:
                visibleTransitions.add(t)
            else:
                markingCopy = copy(marking)
                if semantics.is_enabled(t, net, markingCopy):
                    newMarking = semantics.execute(t, net, markingCopy)
                    newEnabledTransitions = list(
                        semantics.enabled_transitions(net, newMarking))
                    allEnabledTransitions = allEnabledTransitions + newEnabledTransitions
            visitedTransitions.add(t)
        i = i + 1

    return visibleTransitions
Exemple #3
0
def enableHiddenTransitions(net, marking, activatedTransitions,
                            visitedTransitions, allVisitedMarkings,
                            hiddenTransitionsToEnable, t):
    """
    Actually enable hidden transitions on the Petri net

    Parameters
    -----------
    net
        Petri net
    marking
        Current marking
    activatedTransitions
        All activated transitions during the replay
    visitedTransitions
        All visited transitions by the recursion
    allVisitedMarkings
        All visited markings
    hiddenTransitionsToEnable
        List of hidden transition to enable
    t
        Transition against we should check the enabledness
    """
    somethingChanged = True
    jIndexes = [0 for x in hiddenTransitionsToEnable]
    z = 0
    while somethingChanged:
        somethingChanged = False
        while jIndexes[z % len(hiddenTransitionsToEnable)] < len(
                hiddenTransitionsToEnable[z % len(hiddenTransitionsToEnable)]):
            t3 = hiddenTransitionsToEnable[z % len(hiddenTransitionsToEnable)][
                jIndexes[z % len(hiddenTransitionsToEnable)]]
            if not t3 == t:
                if semantics.is_enabled(t3, net, marking):
                    if not t3 in visitedTransitions:
                        marking = semantics.execute(t3, net, marking)
                        activatedTransitions.append(t3)
                        visitedTransitions.add(t3)
                        allVisitedMarkings.append(marking)
                        enabledTransitions = semantics.enabled_transitions(
                            net, marking)
                        somethingChanged = True
            jIndexes[z % len(hiddenTransitionsToEnable)] = jIndexes[
                z % len(hiddenTransitionsToEnable)] + 1
            if semantics.is_enabled(t, net, marking):
                break
        if semantics.is_enabled(t, net, marking):
            break
        z = z + 1
    return [
        marking, activatedTransitions, visitedTransitions, allVisitedMarkings
    ]
Exemple #4
0
def apply_trace(trace,
                net,
                initialMarking,
                finalMarking,
                transMap,
                enable_placeFitness,
                place_fitness,
                placesShortestPathByHidden,
                consider_remaining_in_fitness,
                activity_key="concept:name",
                tryToReachFinalMarkingThroughHidden=True,
                stopImmediatelyWhenUnfit=False,
                useHiddenTransitionsToEnableCorrespondingTransitions=True,
                postFixCaching=None,
                markingToActivityCaching=None):
    """
    Apply the token replaying algorithm to a trace

    Parameters
    ----------
    trace
        Trace in the event log
    net
        Petri net
    initialMarking
        Initial marking
    finalMarking
        Final marking
    transMap
        Map between transitions labels and transitions
    enable_placeFitness
        Enable fitness calculation at place level
    """
    trace_activities = [event[activity_key] for event in trace]
    activatedTransitions = []
    allVisitedMarkings = []
    activatingTransitionIndex = {}
    activatingTransitionInterval = []
    usedPostfixCache = False
    marking = copy(initialMarking)
    allVisitedMarkings.append(marking)
    missing = 0
    consumed = 0
    produced = 0
    i = 0
    while i < len(trace):
        if True and (str(trace_activities) in postFixCaching.cache
                     and hash(marking)
                     in postFixCaching.cache[str(trace_activities)]):
            transToAct = postFixCaching.cache[str(trace_activities)][hash(
                marking)]["transToAct"]
            z = 0
            while z < len(transToAct):
                t = transToAct[z]
                activatedTransitions.append(t)
                z = z + 1
            usedPostfixCache = True
            marking = postFixCaching.cache[str(trace_activities)][hash(
                marking)]["finalMarking"]
            break
        else:
            prevLenActivatedTransitions = len(activatedTransitions)
            if True and (hash(marking) in markingToActivityCaching.cache
                         and trace[i][activity_key]
                         in markingToActivityCaching.cache[hash(marking)]
                         and trace[i - 1][activity_key]
                         == markingToActivityCaching.cache[hash(marking)][
                             trace[i][activity_key]]["previousActivity"]):
                thisEndMarking = markingToActivityCaching.cache[hash(marking)][
                    trace[i][activity_key]]["endMarking"]
                thisActTrans = markingToActivityCaching.cache[hash(marking)][
                    trace[i][activity_key]]["thisActTrans"]
                thisVisMarkings = markingToActivityCaching.cache[hash(
                    marking)][trace[i][activity_key]]["thisVisMarkings"]
                activatedTransitions = activatedTransitions + thisActTrans
                allVisitedMarkings = allVisitedMarkings + thisVisMarkings
                marking = copy(thisEndMarking)
            else:
                if trace[i][activity_key] in transMap:
                    t = transMap[trace[i][activity_key]]
                    if useHiddenTransitionsToEnableCorrespondingTransitions and not semantics.is_enabled(
                            t, net, marking):
                        visitedTransitions = 0
                        visitedTransitions = set()
                        prevLenActivatedTransitions = len(activatedTransitions)
                        [
                            net, marking, activatedTransitions,
                            allVisitedMarkings
                        ] = apply_hiddenTrans(t, net, marking,
                                              placesShortestPathByHidden,
                                              activatedTransitions, 0,
                                              visitedTransitions,
                                              allVisitedMarkings)
                    if not semantics.is_enabled(t, net, marking):
                        if stopImmediatelyWhenUnfit:
                            missing = missing + 1
                            break
                        [m, tokensAdded
                         ] = add_missingTokens(t, net, marking, trace, i)
                        missing = missing + m
                        if enable_placeFitness:
                            for place in tokensAdded.keys():
                                if place in place_fitness:
                                    place_fitness[place]["underfedTraces"].add(
                                        trace)
                    else:
                        m = 0
                    c = get_consumedTokens(t, net)
                    p = get_producedTokens(t, net)
                    consumed = consumed + c
                    produced = produced + p
                    if semantics.is_enabled(t, net, marking):
                        marking = semantics.execute(t, net, marking)
                        activatedTransitions.append(t)
                        allVisitedMarkings.append(marking)
            del trace_activities[0]
            if len(trace_activities) < MAX_POSTFIX_SUFFIX_LENGTH:
                activatingTransitionIndex[str(trace_activities)] = {
                    "index": len(activatedTransitions),
                    "marking": hash(marking)
                }
            if i > 0:
                activatingTransitionInterval.append([
                    trace[i][activity_key], prevLenActivatedTransitions,
                    len(activatedTransitions), trace[i - 1][activity_key]
                ])
            else:
                activatingTransitionInterval.append([
                    trace[i][activity_key], prevLenActivatedTransitions,
                    len(activatedTransitions), ""
                ])
        i = i + 1

    if tryToReachFinalMarkingThroughHidden and not usedPostfixCache:
        i = 0
        while i < MAX_IT_FINAL:
            if not break_condition_final_marking(marking, finalMarking):
                hiddenTransitionsToEnable = getReqTransitionsForFinalMarking(
                    marking, finalMarking, placesShortestPathByHidden)

                for group in hiddenTransitionsToEnable:
                    for t in group:
                        if semantics.is_enabled(t, net, marking):
                            marking = semantics.execute(t, net, marking)
                            activatedTransitions.append(t)
                            allVisitedMarkings.append(marking)
                    if break_condition_final_marking(marking, finalMarking):
                        break
            else:
                break
            i = i + 1

    markingBeforeCleaning = copy(marking)

    remaining = 0
    for p in marking:
        if p in finalMarking:
            marking[p] = max(0, marking[p] - finalMarking[p])
            if enable_placeFitness:
                if marking[p] > 0:
                    if p in place_fitness:
                        if not trace in place_fitness[p]["underfedTraces"]:
                            place_fitness[p]["overfedTraces"].add(trace)
        remaining = remaining + marking[p]
    if consider_remaining_in_fitness:
        is_fit = (missing == 0) and (remaining == 0)
    else:
        is_fit = (missing == 0)

    if consumed > 0 and produced > 0:
        trace_fitness = (1.0 - float(missing) / float(consumed)) * (
            1.0 - float(remaining) / float(produced))
    else:
        trace_fitness = 1.0

    if is_fit:
        for suffix in activatingTransitionIndex:
            if not suffix in postFixCaching.cache:
                postFixCaching.cache[suffix] = {}
            if not activatingTransitionIndex[suffix][
                    "marking"] in postFixCaching.cache[suffix]:
                postFixCaching.cache[suffix][activatingTransitionIndex[suffix]["marking"]] =\
                    {"transToAct":activatedTransitions[activatingTransitionIndex[suffix]["index"]:],"finalMarking":marking}
        for trans in activatingTransitionInterval:
            activity = trans[0]
            startMarkingIndex = trans[1]
            endMarkingIndex = trans[2]
            previousActivity = trans[3]
            if endMarkingIndex < len(allVisitedMarkings):
                startMarkingObject = allVisitedMarkings[startMarkingIndex]
                startMarkingHash = hash(startMarkingObject)
                endMarkingObject = allVisitedMarkings[endMarkingIndex]
                if activity in transMap:
                    thisActivatedTrans = activatedTransitions[
                        startMarkingIndex:endMarkingIndex]
                    thisVisitedMarkings = allVisitedMarkings[
                        startMarkingIndex + 1:endMarkingIndex + 1]

                    if not startMarkingHash in markingToActivityCaching.cache:
                        markingToActivityCaching.cache[startMarkingHash] = {}
                    if not activity in markingToActivityCaching.cache[
                            startMarkingHash]:
                        markingToActivityCaching.cache[startMarkingHash][
                            activity] = {
                                "startMarking": startMarkingObject,
                                "endMarking": endMarkingObject,
                                "thisActTrans": thisActivatedTrans,
                                "thisVisMarkings": thisVisitedMarkings,
                                "previousActivity": previousActivity
                            }

    return [
        is_fit, trace_fitness, activatedTransitions, markingBeforeCleaning,
        get_visible_transitions_eventually_enabled_by_marking(
            net, markingBeforeCleaning)
    ]
Exemple #5
0
def apply_hiddenTrans(t, net, marking, placesShortestPathByHidden,
                      activatedTransitions, recDepth, visitedTransitions,
                      allVisitedMarkings):
    """
    Apply hidden transitions in order to enable a given transition

    Parameters
    ----------
    t
        Transition to eventually enable
    net
        Petri net
    marking
        Marking
    placesShortestPathByHidden
        Shortest paths between places connected by hidden transitions
    activatedTransitions
        All activated transitions
    recDepth
        Current recursion depth
    visitedTransitions
        All visited transitions by hiddenTrans method
    allVisitedMarkings
        All visited markings
    """
    if recDepth >= MAX_REC_DEPTH_HIDTRANSENABL or t in visitedTransitions:
        return [net, marking, activatedTransitions, allVisitedMarkings]
    visitedTransitions.add(t)
    markingAtStart = copy(marking)
    placesWithMissing = get_placesWithMissingTokens(t, net, marking)
    hiddenTransitionsToEnable = getHiddenTransitionsToEnable(
        marking, placesWithMissing, placesShortestPathByHidden)

    if hiddenTransitionsToEnable:
        [
            marking, activatedTransitions, visitedTransitions,
            allVisitedMarkings
        ] = enableHiddenTransitions(net, marking, activatedTransitions,
                                    visitedTransitions, allVisitedMarkings,
                                    hiddenTransitionsToEnable, t)
        if not semantics.is_enabled(t, net, marking):
            hiddenTransitionsToEnable = getHiddenTransitionsToEnable(
                marking, placesWithMissing, placesShortestPathByHidden)
            z = 0
            while z < len(hiddenTransitionsToEnable):
                k = 0
                while k < len(hiddenTransitionsToEnable[z]):
                    t4 = hiddenTransitionsToEnable[z][k]
                    if not t4 == t:
                        if not t4 in visitedTransitions:
                            if not semantics.is_enabled(t4, net, marking):
                                [
                                    net, marking, activatedTransitions,
                                    allVisitedMarkings
                                ] = apply_hiddenTrans(
                                    t4, net, marking,
                                    placesShortestPathByHidden,
                                    activatedTransitions, recDepth + 1,
                                    visitedTransitions, allVisitedMarkings)
                            if semantics.is_enabled(t4, net, marking):
                                marking = semantics.execute(t4, net, marking)
                                activatedTransitions.append(t4)
                                visitedTransitions.add(t4)
                                allVisitedMarkings.append(marking)
                    k = k + 1
                z = z + 1
        if not semantics.is_enabled(t, net, marking):
            if not (markingAtStart == marking):
                [net, marking, activatedTransitions, allVisitedMarkings
                 ] = apply_hiddenTrans(t, net, marking,
                                       placesShortestPathByHidden,
                                       activatedTransitions, recDepth + 1,
                                       visitedTransitions, allVisitedMarkings)

    return [net, marking, activatedTransitions, allVisitedMarkings]