{ "cells": [ { "cell_type": "markdown", "id": "0009d44f-be72-4bb2-9ca6-487a2be24ce1", "metadata": { "tags": [] }, "source": [ "# Generate pm4py log using ASP Generator\n", "\n", "ASP log generator uses the decl model which converts the model into abudction logic programming and pass to the clingo.\n", "Clingo generates the output which is turned into the pm4py log or can be created a .xes file.\n" ] }, { "cell_type": "code", "execution_count": 1, "id": "1eb21bf2-bf41-46b2-96e3-806047cc8cb1", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Python 3.9.12\r\n" ] } ], "source": [ "!python --version" ] }, { "cell_type": "code", "execution_count": 2, "id": "2938753f-7b7c-4095-b918-1a667621e50d", "metadata": { "tags": [] }, "outputs": [], "source": [ "import os\n", "import pathlib\n", "\n", "from Declare4Py.ProcessMiningTasks.ASPLogGeneration.asp_generator import AspGenerator\n", "from Declare4Py.ProcessModels.DeclareModel import DeclareModel" ] }, { "cell_type": "code", "execution_count": 3, "id": "0b9d5123-dc4f-4a75-bdd1-d4235d636772", "metadata": { "tags": [] }, "outputs": [], "source": [ "# Setting up required files\n", "\n", "# possible files to test\n", "decl_model_1 = \"./decl_files/Response.decl\"\n", "decl_model_2 = \"./decl_files/MikeModel.decl\"\n", "decl_model_3 = \"./decl_files/reference10.decl\"\n", "decl_model_4 = \"./decl_files/diagonisis.decl\"\n", "\n", "decl_file = os.path.join(pathlib.Path(decl_model_2).resolve())\n", "output_file = os.path.join(pathlib.Path(\"./generated_exporter.xes\").resolve())" ] }, { "cell_type": "code", "execution_count": 4, "id": "4b2d84b6-6a5a-4b3c-a0bb-335981eb6374", "metadata": {}, "outputs": [], "source": [ "# Create Declare model by reading declare model from a file.\n", "\n", "model: DeclareModel = DeclareModel().parse_from_file(decl_file)" ] }, { "cell_type": "code", "execution_count": 5, "id": "f4b87ba4-2740-4006-b40f-b0ae95bab202", "metadata": {}, "outputs": [], "source": [ "#general Setting\n", "\n", "# Number of traces that should be generated\n", "num_of_traces = 4\n", "\n", "# Minimum and maximum number of events a trace can contain\n", "(num_min_events, num_max_events) = (2, 14)\n" ] }, { "cell_type": "code", "execution_count": 6, "id": "b9dc4474-319b-40e0-95a7-c8ac36a18f2b", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "DEBUG:ASP generator:Distribution for traces uniform\n", "DEBUG:ASP generator:traces: 4, events can have a trace min(2) max(14)\n", "INFO:ASP generator:Computing distribution\n", "DEBUG:Distributor:Distribution() uniform min_mu: 2 max_sigma: 14 num_traces: 4 custom_prob: None\n", "DEBUG:Distributor:Uniform() probabilities: [Fraction(1, 13), Fraction(1, 13), Fraction(1, 13), Fraction(1, 13), Fraction(1, 13), Fraction(1, 13), Fraction(1, 13), Fraction(1, 13), Fraction(1, 13), Fraction(1, 13), Fraction(1, 13), Fraction(1, 13), Fraction(1, 13)]\n", "DEBUG:Distributor:Custom_dist() min_mu:2 max_sigma:14 num_traces:4\n", "DEBUG:Distributor:Probabilities sum 1\n", "DEBUG:Distributor:Distribution result: [ 8 6 12 11]\n", "INFO:ASP generator:Distribution result Counter({8: 1, 6: 1, 12: 1, 11: 1})\n" ] } ], "source": [ "# Initializing ASP generator with default distributor which is uniform.\n", "\n", "logging.basicConfig(level=logging.DEBUG)\n", "asp = AspGenerator(\n", " model,\n", " num_of_traces,\n", " num_min_events,\n", " num_max_events,\n", ")\n" ] }, { "cell_type": "code", "execution_count": 7, "id": "1a35131f-9c1c-44d3-9660-a6c657fed23a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# activity A\n", "# bind A: attribute\n", "# activity B\n", "# activity C\n", "# attribute: value1, value2, value3\n", "# Response[A,B] | A.attribute is value1 | |\n", "\n", "\n", "# A user may want the activation of the Response constraint (i.e., activity A with\n", "# attribute attribute = value1) to happen at least 3 times within each generated trace.\n", "\n", "\n", "asp.set_activation_conditions({\n", " 'Response[Driving_Test, Resit] |A.Grade<=2 | |': [0, 3] # activation should occur between at most 3 times\n", "})\n", "\n", "asp.set_activation_conditions({'Response[Driving_Test, Resit] |A.Grade<=2 | |': [3, 5]}) # activation should occur between 3 to 5 times\n", "\n", "asp.set_activation_conditions_by_template_index({1: [2, 4]}) # activation should occur between 3 to 5 times and as key you can also define the template index instead str\n" ] }, { "cell_type": "code", "execution_count": 8, "id": "974a92ad-6497-433f-99e4-2f0d79a6e150", "metadata": { "scrolled": true, "tags": [] }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "DEBUG:ASP generator:Using custom traces length\n", "INFO:ASP generator:Computing distribution\n", "DEBUG:Distributor:Distribution() uniform min_mu: 2 max_sigma: 14 num_traces: 4 custom_prob: None\n", "DEBUG:Distributor:Uniform() probabilities: [Fraction(1, 13), Fraction(1, 13), Fraction(1, 13), Fraction(1, 13), Fraction(1, 13), Fraction(1, 13), Fraction(1, 13), Fraction(1, 13), Fraction(1, 13), Fraction(1, 13), Fraction(1, 13), Fraction(1, 13), Fraction(1, 13)]\n", "DEBUG:Distributor:Custom_dist() min_mu:2 max_sigma:14 num_traces:4\n", "DEBUG:Distributor:Probabilities sum 1\n", "DEBUG:Distributor:Distribution result: [ 9 4 5 14]\n", "INFO:ASP generator:Distribution result Counter({9: 1, 4: 1, 5: 1, 14: 1})\n", "INFO:ASP generator:Computing distribution\n", "DEBUG:ASP generator:Generating traces\n", "DEBUG:ASP generator:Translate declare model to ASP\n", "DEBUG:ASP generator:Declare model translated to ASP. Total Facts 4\n", "DEBUG:ASP generator:ASP encoding generated\n", "DEBUG:ASP generator:Start generating traces: Counter({9: 1, 4: 1, 5: 1, 14: 1})\n", "DEBUG:ASP generator: Total trace to generate and events: Traces:1, Events: 9, RandFrequency: 0.9\n", "DEBUG:ASP generator: Generating trace:1/1 with events:9)\n", "DEBUG:ASP generator: Initializing clingo trace(1/1) with length:9), Seed 2546638776\n", "DEBUG:ASP generator: Clingo Result: UNSAT\n", "/home/mani/Desktop/projects/python/Declare4Py/src/Declare4Py/ProcessMiningTasks/ASPLogGeneration/asp_generator.py:330: UserWarning: WARNING: Cannot generate 1 positive trace/s exactly with 9 events with this Declare model.\n", " warnings.warn(\n", "DEBUG:ASP generator: Total trace to generate and events: Traces:1, Events: 4, RandFrequency: 0.9\n", "DEBUG:ASP generator: Generating trace:1/1 with events:4)\n", "DEBUG:ASP generator: Initializing clingo trace(1/1) with length:4), Seed 1166910333\n", "DEBUG:ASP generator: Clingo Result: UNSAT\n", "/home/mani/Desktop/projects/python/Declare4Py/src/Declare4Py/ProcessMiningTasks/ASPLogGeneration/asp_generator.py:330: UserWarning: WARNING: Cannot generate 1 positive trace/s exactly with 4 events with this Declare model.\n", " warnings.warn(\n", "DEBUG:ASP generator: Total trace to generate and events: Traces:1, Events: 5, RandFrequency: 0.9\n", "DEBUG:ASP generator: Generating trace:1/1 with events:5)\n", "DEBUG:ASP generator: Initializing clingo trace(1/1) with length:5), Seed 3020005515\n", "DEBUG:ASP generator: Clingo Result: UNSAT\n", "/home/mani/Desktop/projects/python/Declare4Py/src/Declare4Py/ProcessMiningTasks/ASPLogGeneration/asp_generator.py:330: UserWarning: WARNING: Cannot generate 1 positive trace/s exactly with 5 events with this Declare model.\n", " warnings.warn(\n", "DEBUG:ASP generator: Total trace to generate and events: Traces:1, Events: 14, RandFrequency: 0.9\n", "DEBUG:ASP generator: Generating trace:1/1 with events:14)\n", "DEBUG:ASP generator: Initializing clingo trace(1/1) with length:14), Seed 381720\n", "DEBUG:ASP generator: Clingo Result: UNSAT\n", "/home/mani/Desktop/projects/python/Declare4Py/src/Declare4Py/ProcessMiningTasks/ASPLogGeneration/asp_generator.py:330: UserWarning: WARNING: Cannot generate 1 positive trace/s exactly with 14 events with this Declare model.\n", " warnings.warn(\n", "DEBUG:ASP generator:Traces generated. Positive: 0 Neg: 0. Parsing Trace results.\n", "DEBUG:ASP generator:Trace results parsed\n", "DEBUG:ASP generator:Generating Pm4py log\n", "WARNING:ASP generator:PM4PY log generated: 0/4 only.\n", "DEBUG:ASP generator:Pm4py generated but not saved yet\n" ] } ], "source": [ "# Generate the traces and parse the result produced by clingo\n", "asp.run() # Run accets 1 optional value whether to create file for the ASP generated from given declare model\n" ] }, { "cell_type": "code", "execution_count": 35, "id": "a03b7b40-e718-4783-a8e4-36528bd4d01d", "metadata": { "scrolled": true }, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "1022954d8e9b49e691cf8fca159d5351", "version_major": 2, "version_minor": 0 }, "text/plain": [ "exporting log, completed traces :: : 0it [00:00, ?it/s]" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# Save file to xes\n", "asp.to_xes(output_file)" ] }, { "cell_type": "code", "execution_count": null, "id": "1817e728", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.4" } }, "nbformat": 4, "nbformat_minor": 5 }