{ "cells": [ { "metadata": { "ExecuteTime": { "end_time": "2026-05-29T14:57:29.019350100Z", "start_time": "2026-05-29T14:57:28.994345800Z" } }, "cell_type": "code", "source": [ "import warnings\n", "warnings.filterwarnings(\"ignore\", category=UserWarning)" ], "id": "231966f11a470c15", "outputs": [], "execution_count": 1 }, { "cell_type": "markdown", "id": "4f5a9a59", "metadata": {}, "source": [ "# Synthetic Log Generation from DECLARE Models\n", "\n", "DECLARE4Py implements the generation of synthetic logs from DECLARE models with a solution based on Answer Set Programming that uses a Clingo solver. More details can be found in the paper of Chiariello, F., Maggi, F. M., & Patrizi, F. (2022, June). ASP-Based Declarative Process Mining. In *Proceedings of the AAAI Conference on Artificial Intelligence* (Vol. 36, No. 5, pp. 5539-5547).\n", "\n", "As first step, it is necessary to import a `.decl` file containing the DECLARE constraints." ] }, { "metadata": { "ExecuteTime": { "end_time": "2026-05-29T14:57:31.182952200Z", "start_time": "2026-05-29T14:57:29.429568700Z" } }, "cell_type": "code", "source": [ "import os\n", "from Declare4Py.ProcessModels.DeclareModel import DeclareModel\n", "from Declare4Py.ProcessMiningTasks.LogGenerator.ASP.ASPLogGenerator import AspGenerator\n", "\n", "model_name = 'data-model1'\n", "model: DeclareModel = DeclareModel().parse_from_file(os.path.join(\"../../../\", \"tests\", \"test_models\", f\"{model_name}.decl\"))" ], "id": "bdf42d09", "outputs": [], "execution_count": 2 }, { "cell_type": "markdown", "source": [ "Then, some general settings are needed to set the number of cases to generate and the minimum and maximum number of events for each case" ], "metadata": { "collapsed": false }, "id": "7a9b8417" }, { "cell_type": "code", "id": "785a61eb", "metadata": { "ExecuteTime": { "end_time": "2026-05-29T14:57:38.485728Z", "start_time": "2026-05-29T14:57:31.182952200Z" } }, "source": [ "%%time\n", "# Number of cases that have be generated\n", "num_of_cases = 10\n", "\n", "# Minimum and maximum number of events a case can contain\n", "(num_min_events, num_max_events) = (8,15)\n", "\n", "# Shows some feedback from the Generator (Set it too false to ignore all debug messages)\n", "verbose = False\n", "\n", "asp_gen: AspGenerator = AspGenerator(model, num_of_cases, num_min_events, num_max_events, verbose=verbose)\n", "asp_gen.run()" ], "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: total: 21.3 s\n", "Wall time: 7.27 s\n" ] } ], "execution_count": 3 }, { "cell_type": "markdown", "id": "6a06484d", "metadata": {}, "source": [ "The class `AspGenerator` has to be instantiated with the DECLARE model and the settings of above. Then, the `run` method will generate the cases and the `to_xes` method will save them in a `.xes` event log or the `to_csv` method will save them in a `.csv` file." ] }, { "cell_type": "code", "source": [ "asp_gen.to_xes(f\"{model_name}.xes\")\n", "asp_gen.to_csv(f\"{model_name}.csv\")" ], "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2026-05-29T14:57:38.687915600Z", "start_time": "2026-05-29T14:57:38.512729900Z" } }, "id": "7467c274549de47f", "outputs": [ { "data": { "text/plain": [ "exporting log, completed traces :: 0%| | 0/10 [00:00= 45) AND (A.InfectionSuspected is true) AND (A.DiagnosticLacticAcid is true) AND (A.DiagnosticSputum is true) AND (A.Hypoxie is false) AND (A.DiagnosticUrinaryCulture is true) AND (A.DiagnosticLiquor is false) AND (A.SIRSCritTemperature is true) AND (A.Infusion is true) AND (A.Hypotensie is false) AND (A.DiagnosticUrinarySediment is true) AND (A.Oligurie is false) AND (A.Age <= 80) AND (A.SIRSCritTachypnea is true) AND (A.DiagnosticOther is false) AND (A.SIRSCritLeucos is false) AND (A.DiagnosticIC is true) AND (A.SIRSCriteria2OrMore is true) AND (A.DiagnosticXthorax is true) |T.org:group is C |52,2154,s\n", "5 - Chain Precedence[Release A, Return ER] |A.org:group is ? |T.org:group is E |1121801,1121801,s\n", "6 - Chain Precedence[ER Sepsis Triage, IV Antibiotics] |A.org:group is L |T.org:group is L |15,11000,s\n", "7 - Chain Response[ER Sepsis Triage, IV Antibiotics] |A.org:group is L |T.org:group is L |15,11000,s\n", "8 - Chain Precedence[Admission IC, Admission NC] |A.org:group is J |T.org:group is J |\n", "9 - Chain Precedence[IV Antibiotics, Admission NC] |A.org:group is F |T.org:group is A |92,14459,s\n", "10 - Chain Precedence[Admission NC, Release B] |A.org:group is E |T.org:group is K |48225,48225,s\n", "11 - Chain Response[Admission IC, Admission NC] |A.org:group is J |T.org:group is J |61534,61534,s\n", "12 - Chain Response[LacticAcid, Leucocytes] |A.LacticAcid <= 0.8 |T.Leucocytes >= 13.8 |0,2778,m\n", "13 - Chain Precedence[ER Registration, ER Triage] |A.org:group is C |(T.InfectionSuspected is true) AND (T.SIRSCritTemperature is true) AND (T.DiagnosticLacticAcid is true) AND (T.DiagnosticBlood is true) AND (T.DiagnosticIC is true) AND (T.SIRSCriteria2OrMore is true) AND (T.DiagnosticECG is true) |52,2154,s\n" ] } ], "execution_count": 11 }, { "metadata": {}, "cell_type": "markdown", "source": [ "## Setting up Rules for the Activation Conditions\n", "\n", "Users can specify the number of activations of a DECLARE constraint in the synthetic cases. This can be done with the `set_activation_conditions` method by specifying an interval of activations for specific DECLARE constraints in the loaded model" ], "id": "9832b4eaaaa61ed0" }, { "metadata": { "jupyter": { "is_executing": true }, "ExecuteTime": { "end_time": "2026-05-29T14:59:04.124849Z", "start_time": "2026-05-29T14:58:57.058960100Z" } }, "cell_type": "code", "source": [ "asp_gen: AspGenerator = AspGenerator(model, num_of_cases, num_min_events, num_max_events)\n", "\n", "activation_constraint = model.serialized_constraints[3]\n", "asp_gen.set_activation_conditions({\n", " activation_constraint: [2, 3]\n", "}) # activation should occur between 2 to 3 times\n", "\n", "asp_gen.run()\n", "asp_gen.to_csv(f'{model_name}_Activation_Conditions_Test_1.csv')" ], "id": "5c5c995e9c1ace", "outputs": [], "execution_count": 12 }, { "metadata": {}, "cell_type": "markdown", "source": "In addition, instead of giving the explicit text of the DECLARE constraints, an index can be used in the `set_activation_conditions_by_template_index` method", "id": "7eb11a89ca32618" }, { "metadata": { "jupyter": { "is_executing": true }, "ExecuteTime": { "end_time": "2026-05-29T14:59:10.062052200Z", "start_time": "2026-05-29T14:59:04.139855900Z" } }, "cell_type": "code", "source": [ "asp_gen: AspGenerator = AspGenerator(model, num_of_cases, num_min_events, num_max_events)\n", "\n", "for id, constr_text in enumerate(model.serialized_constraints):\n", " print(f\"{id} - {constr_text}\")\n", "\n", "asp_gen.set_activation_conditions_by_template_index({3: [2, 3]})\n", "asp_gen.run()\n", "asp_gen.to_csv(f'{model_name}.csv')" ], "id": "3014ddfa6c357452", "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 - Existence2[Admission NC] | |\n", "1 - Chain Response[Admission NC, Release B] |A.org:group is K |T.org:group is E |\n", "2 - Chain Response[Admission NC, Release A] |A.org:group is I |T.org:group is E |133020,957701,s\n", "3 - Chain Precedence[IV Liquid, Admission NC] |A.org:group is I |T.org:group is A |92,14473,s\n", "4 - Chain Response[ER Registration, ER Triage] |(A.DiagnosticArtAstrup is false) AND (A.SIRSCritHeartRate is true) AND (A.org:group is A) AND (A.DiagnosticBlood is true) AND (A.DisfuncOrg is false) AND (A.DiagnosticECG is true) AND (A.Age >= 45) AND (A.InfectionSuspected is true) AND (A.DiagnosticLacticAcid is true) AND (A.DiagnosticSputum is true) AND (A.Hypoxie is false) AND (A.DiagnosticUrinaryCulture is true) AND (A.DiagnosticLiquor is false) AND (A.SIRSCritTemperature is true) AND (A.Infusion is true) AND (A.Hypotensie is false) AND (A.DiagnosticUrinarySediment is true) AND (A.Oligurie is false) AND (A.Age <= 80) AND (A.SIRSCritTachypnea is true) AND (A.DiagnosticOther is false) AND (A.SIRSCritLeucos is false) AND (A.DiagnosticIC is true) AND (A.SIRSCriteria2OrMore is true) AND (A.DiagnosticXthorax is true) |T.org:group is C |52,2154,s\n", "5 - Chain Precedence[Release A, Return ER] |A.org:group is ? |T.org:group is E |1121801,1121801,s\n", "6 - Chain Precedence[ER Sepsis Triage, IV Antibiotics] |A.org:group is L |T.org:group is L |15,11000,s\n", "7 - Chain Response[ER Sepsis Triage, IV Antibiotics] |A.org:group is L |T.org:group is L |15,11000,s\n", "8 - Chain Precedence[Admission IC, Admission NC] |A.org:group is J |T.org:group is J |\n", "9 - Chain Precedence[IV Antibiotics, Admission NC] |A.org:group is F |T.org:group is A |92,14459,s\n", "10 - Chain Precedence[Admission NC, Release B] |A.org:group is E |T.org:group is K |48225,48225,s\n", "11 - Chain Response[Admission IC, Admission NC] |A.org:group is J |T.org:group is J |61534,61534,s\n", "12 - Chain Response[LacticAcid, Leucocytes] |A.LacticAcid <= 0.8 |T.Leucocytes >= 13.8 |0,2778,m\n", "13 - Chain Precedence[ER Registration, ER Triage] |A.org:group is C |(T.InfectionSuspected is true) AND (T.SIRSCritTemperature is true) AND (T.DiagnosticLacticAcid is true) AND (T.DiagnosticBlood is true) AND (T.DiagnosticIC is true) AND (T.SIRSCriteria2OrMore is true) AND (T.DiagnosticECG is true) |52,2154,s\n" ] } ], "execution_count": 13 } ], "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 }