{ "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": null, "id": "1eb21bf2-bf41-46b2-96e3-806047cc8cb1", "metadata": {}, "outputs": [], "source": [ "!python --version" ] }, { "cell_type": "code", "execution_count": 9, "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": 10, "id": "0b9d5123-dc4f-4a75-bdd1-d4235d636772", "metadata": { "tags": [] }, "outputs": [], "source": [ "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 = \"diagonisis.decl\"\n", "decl_folder = \"decl_files\"\n", "\n", "decl_filename = decl_model_4.split(\".\")[0]\n", "output_file = pathlib.Path(\".\", f\"{decl_filename}.xes\")\n", "decl_file = pathlib.Path(\".\", decl_folder, decl_model_4)" ] }, { "cell_type": "code", "execution_count": 11, "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": 12, "id": "f4b87ba4-2740-4006-b40f-b0ae95bab202", "metadata": {}, "outputs": [], "source": [ "#general Setting\n", "\n", "# Number of traces that should be generated\n", "num_of_traces = 50\n", "\n", "# Minimum and maximum number of events a trace can contain\n", "(num_min_events, num_max_events) = (2, 5)\n" ] }, { "cell_type": "code", "execution_count": 13, "id": "b9dc4474-319b-40e0-95a7-c8ac36a18f2b", "metadata": {}, "outputs": [], "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", " encode_decl_model=True #\n", ")\n", "# NON CAPISCO QUESTO ULTIMO TRUE" ] }, { "cell_type": "code", "execution_count": 14, "id": "1a35131f-9c1c-44d3-9660-a6c657fed23a", "metadata": {}, "outputs": [], "source": [ "# Violates some constraints for declare model\n", "\n", "# NOTE: this examples or cell is only configured according to the `decl_model_4` file\n", "\n", "# You can add constraint templates as a string array to be violated from the declared model lines.\n", "# You can use the flag `violate_all_constraints_in_subset` in order to tell clingo, to violate all\n", "# templates or some of them( will be decided by clingo). The second param = True means\n", "# all the contstraint should be violate which are available in the subset (which you add in the array string/int).\n", "\n", "\n", "\n", "asp.set_constraints_to_violate(1, True, [\n", " # \"Existence[act2] | |\",\n", " # \"Existence[act4] | |\"\n", " \"Response[Driving_Test, Resit] |A.Grade<=2 | |\"\n", " # \"Chain Response[Admission IC, Admission NC] |A.org:group is J |T.org:group is J |61534,61534,s\",\n", " # \"Chain Response[LacticAcid, Leucocytes] |A.LacticAcid <= 0.8 |T.Leucocytes >= 13.8 |0,2778,m\",\n", "])\n", "\n", "\n", "# the deference between these two methods is that you can decide if you want to pass the constraint\n", "# template list of strings or the indexes of constraint templates generated by model itself in order\n", "\n", "# asp.set_constraints_to_violate_by_template_index(1, True, [2]) \n" ] }, { "cell_type": "code", "execution_count": 15, "id": "974a92ad-6497-433f-99e4-2f0d79a6e150", "metadata": { "scrolled": true, "tags": [] }, "outputs": [ { "ename": "AttributeError", "evalue": "'NoneType' object has no attribute 'event_name'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m/var/folders/5w/6k152p214xbc6ghcldxtvf2r0000gq/T/ipykernel_31137/4122125268.py\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# Generate the traces and parse the result produced by clingo\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0masp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrun\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# Run accets 1 optional value whether to create file for the ASP generated from given declare model\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;32m~/PycharmProjects/declare4py-v2.0/src/Declare4Py/ProcessMiningTasks/ASPLogGeneration/asp_generator.py\u001b[0m in \u001b[0;36mrun\u001b[0;34m(self, generated_asp_file_path)\u001b[0m\n\u001b[1;32m 218\u001b[0m dupl_decl_model, violation)\n\u001b[1;32m 219\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 220\u001b[0;31m \u001b[0mlp\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mgenerate_asp_from_decl_model\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mencode_decl_model\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdupl_decl_model\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mviolation\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 221\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__generate_traces\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlp\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mneg_traces_dist\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"negative\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 222\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m~/PycharmProjects/declare4py-v2.0/src/Declare4Py/ProcessMiningTasks/ASPLogGeneration/asp_generator.py\u001b[0m in \u001b[0;36mgenerate_asp_from_decl_model\u001b[0;34m(self, encode, save_file, process_model, violation)\u001b[0m\n\u001b[1;32m 122\u001b[0m \u001b[0mprocess_model\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mprocess_model\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 123\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpy_logger\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdebug\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Translate declare model to ASP\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 124\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlp_model\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mASPModel\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mencode\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfrom_decl_model\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mprocess_model\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mviolation\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 125\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__handle_activations_condition_asp_generation\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 126\u001b[0m \u001b[0mlp\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlp_model\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mto_str\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m~/PycharmProjects/declare4py-v2.0/src/Declare4Py/ProcessMiningTasks/ASPLogGeneration/ASPTranslator/asp_translator.py\u001b[0m in \u001b[0;36mfrom_decl_model\u001b[0;34m(self, decl_model, constraint_violation)\u001b[0m\n\u001b[1;32m 164\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mtemplate_idx\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mmodel\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtemplates\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 165\u001b[0m \u001b[0mct\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmodel\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtemplates\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mtemplate_idx\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 166\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0madd_template\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mct\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmodel\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mattributes_list\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 167\u001b[0m \u001b[0mconstraints_violate\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mtemplate_idx\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mct\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mviolate\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 168\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m~/PycharmProjects/declare4py-v2.0/src/Declare4Py/ProcessMiningTasks/ASPLogGeneration/ASPTranslator/asp_translator.py\u001b[0m in \u001b[0;36madd_template\u001b[0;34m(self, ct, props)\u001b[0m\n\u001b[1;32m 107\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtemplates_s\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34mf\"template({ct.template_index},\\\"{ct.get_template_name()}\\\").\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 108\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 109\u001b[0;31m \u001b[0mls\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcondition_resolver\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mresolve_to_asp\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mct\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mprops\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 110\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mls\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mls\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m>\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 111\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtemplates_s\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtemplates_s\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mls\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m\"\\n\"\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m~/PycharmProjects/declare4py-v2.0/src/Declare4Py/ProcessMiningTasks/ASPLogGeneration/ASPTranslator/declare_constraint_resolver.py\u001b[0m in \u001b[0;36mresolve_to_asp\u001b[0;34m(self, ct, attrs)\u001b[0m\n\u001b[1;32m 38\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mct\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtemplate\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mis_binary\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 39\u001b[0m \u001b[0mtar_ev\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mDeclareModelEvent\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mct\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mevents_activities\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 40\u001b[0;31m \u001b[0mtar_ev_name\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtar_ev\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mevent_name\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_encoded_name\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mis_encoded\u001b[0m \u001b[0;32melse\u001b[0m \u001b[0mtar_ev\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mevent_name\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_name\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 41\u001b[0m \u001b[0mls\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'target({},{}).'\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0midx\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtar_ev_name\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 42\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mactivation\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mAttributeError\u001b[0m: 'NoneType' object has no attribute 'event_name'" ] } ], "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": null, "id": "a03b7b40-e718-4783-a8e4-36528bd4d01d", "metadata": { "scrolled": true }, "outputs": [], "source": [ "# Save file to xes\n", "asp.to_xes(output_file.as_posix())" ] }, { "cell_type": "code", "execution_count": null, "id": "c7ace184", "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 }