{
"cells": [
{
"metadata": {
"ExecuteTime": {
"end_time": "2026-05-29T14:06:38.428305400Z",
"start_time": "2026-05-29T14:06:38.413301400Z"
}
},
"cell_type": "code",
"source": [
"import warnings\n",
"warnings.filterwarnings(\"ignore\", category=UserWarning)"
],
"id": "5190bc3bfe99a6e9",
"outputs": [],
"execution_count": 1
},
{
"cell_type": "markdown",
"id": "dac6605a",
"metadata": {},
"source": [
"# Query Checking with the DECLARE Checker\n",
"\n",
"This tutorial explains how to perform the query checking of a DECLARE constraint in a log and how to browse the results. We first import the classes `DeclareQueryChecker` and `DeclareResultsBrowser` and the log."
]
},
{
"cell_type": "code",
"id": "91723b33",
"metadata": {
"ExecuteTime": {
"end_time": "2026-05-29T14:06:41.199965600Z",
"start_time": "2026-05-29T14:06:38.432303300Z"
}
},
"source": [
"import os\n",
"\n",
"from Declare4Py.ProcessMiningTasks.QueryChecking.DeclareQueryChecker import DeclareQueryChecker\n",
"from Declare4Py.ProcessMiningTasks.QueryChecking.DeclareResultsBrowser import DeclareResultsBrowser\n",
"from Declare4Py.D4PyEventLog import D4PyEventLog\n",
"\n",
"log_path = os.path.join(\"../../../\", \"tests\", \"test_logs\",\"Sepsis Cases.xes.gz\")\n",
"event_log = D4PyEventLog(case_name=\"case:concept:name\")\n",
"event_log.parse_xes_log(log_path)"
],
"outputs": [
{
"data": {
"text/plain": [
"parsing log, completed traces :: 0%| | 0/1050 [00:00, ?it/s]"
],
"application/vnd.jupyter.widget-view+json": {
"version_major": 2,
"version_minor": 0,
"model_id": "80452cd7058849128735fa342497622e"
}
},
"metadata": {},
"output_type": "display_data"
}
],
"execution_count": 2
},
{
"cell_type": "markdown",
"id": "fbcf695b",
"metadata": {},
"source": [
"The `DeclareQueryChecker` class takes as input:\n",
"- the event log;\n",
"- the boolean parameter `consider_vacuity=true` that means that vacuously satisfied traces are considered as satisfied, violated otherwise.\n",
"- The query/setting parameters `template`, `activation`, `target`, `activation_condition`, `target_condition` and `time_condition` that set the parameters of the DECLARE constraints and the variables to ask. If a parameter is not set then it is treated as a variable to ask.\n",
"- The float `min_support` parameter sets the support to be satisfied in the log by the variable assignments.\n",
"- the boolean parameter `return_first` that if set to false it returns all the variables assignments that satisfy the support in the log. Otherwise, it returns only one variables assignment (the first) that satisfy the support in the log. This saves computational time when one is only interested in the existence of a variable assignment with a given support.\n",
"\n",
"In the following, we compute the assignments for the target by setting `template_str='Chain Response'`, `activation='IV Antibiotics'`, `act_cond='A.org:group is A'` and `min_support=0.2`:\n",
"\n",
"After this setting, the method `run` of the `DeclareQueryChecker` class will perform the query checking."
]
},
{
"cell_type": "code",
"id": "ab182fa1",
"metadata": {
"ExecuteTime": {
"end_time": "2026-05-29T14:06:44.872075100Z",
"start_time": "2026-05-29T14:06:41.231488Z"
}
},
"source": [
"query_checker = DeclareQueryChecker(log=event_log, consider_vacuity=False, template='Chain Response', activation='IV Antibiotics', activation_condition='A.org:group is A', min_support=0.2, return_first=False)\n",
"query_check_res: DeclareResultsBrowser = query_checker.run()"
],
"outputs": [],
"execution_count": 3
},
{
"cell_type": "markdown",
"id": "67577003",
"metadata": {},
"source": [
"The query checking results can be filtered with the `filter_query_checking` method whose parameter `queries` takes as input a list of variables to ask. In the above example, we set the template and activation and ask for the targets. The output of `filter_query_checking` is a Pandas dataframe."
]
},
{
"cell_type": "code",
"id": "8cdc0eed",
"metadata": {
"ExecuteTime": {
"end_time": "2026-05-29T14:06:44.906868800Z",
"start_time": "2026-05-29T14:06:44.886349600Z"
}
},
"source": [
"query_check_res.filter_query_checking(queries=['template', 'activation', 'target', 'activation_condition'])"
],
"outputs": [
{
"data": {
"text/plain": [
" template activation target activation_condition\n",
"0 Chain Response ER Registration ER Triage A.org:group is A\n",
"1 Chain Response ER Sepsis Triage Leucocytes A.org:group is A\n",
"2 Chain Response ER Sepsis Triage IV Liquid A.org:group is A\n",
"3 Chain Response IV Liquid IV Antibiotics A.org:group is A\n",
"4 Chain Response IV Antibiotics Admission NC A.org:group is A"
],
"text/html": [
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" template | \n",
" activation | \n",
" target | \n",
" activation_condition | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" Chain Response | \n",
" ER Registration | \n",
" ER Triage | \n",
" A.org:group is A | \n",
"
\n",
" \n",
" | 1 | \n",
" Chain Response | \n",
" ER Sepsis Triage | \n",
" Leucocytes | \n",
" A.org:group is A | \n",
"
\n",
" \n",
" | 2 | \n",
" Chain Response | \n",
" ER Sepsis Triage | \n",
" IV Liquid | \n",
" A.org:group is A | \n",
"
\n",
" \n",
" | 3 | \n",
" Chain Response | \n",
" IV Liquid | \n",
" IV Antibiotics | \n",
" A.org:group is A | \n",
"
\n",
" \n",
" | 4 | \n",
" Chain Response | \n",
" IV Antibiotics | \n",
" Admission NC | \n",
" A.org:group is A | \n",
"
\n",
" \n",
"
\n",
"
"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"execution_count": 4
},
{
"cell_type": "markdown",
"id": "efffc3c5",
"metadata": {},
"source": [
"In the following example we compute the assignments both for the activation and the target by setting `template_str='Response'` and `min_support=0.8`:"
]
},
{
"cell_type": "code",
"id": "7bd7cd74",
"metadata": {
"ExecuteTime": {
"end_time": "2026-05-29T14:06:46.417264900Z",
"start_time": "2026-05-29T14:06:44.969415900Z"
}
},
"source": [
"query_checker = DeclareQueryChecker(log=event_log, consider_vacuity=False, template='Response', min_support=0.8, return_first=False)\n",
"query_check_res: DeclareResultsBrowser = query_checker.run()\n",
" \n",
"query_check_res.filter_query_checking(queries=['template', 'activation', 'target'])"
],
"outputs": [
{
"data": {
"text/plain": [
" template activation target\n",
"0 Response ER Registration Leucocytes\n",
"1 Response ER Registration CRP\n",
"2 Response ER Registration LacticAcid\n",
"3 Response ER Registration ER Triage\n",
"4 Response ER Registration ER Sepsis Triage\n",
"5 Response ER Triage Leucocytes\n",
"6 Response ER Triage CRP\n",
"7 Response ER Triage ER Sepsis Triage\n",
"8 Response ER Sepsis Triage Leucocytes\n",
"9 Response ER Sepsis Triage CRP"
],
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" template | \n",
" activation | \n",
" target | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" Response | \n",
" ER Registration | \n",
" Leucocytes | \n",
"
\n",
" \n",
" | 1 | \n",
" Response | \n",
" ER Registration | \n",
" CRP | \n",
"
\n",
" \n",
" | 2 | \n",
" Response | \n",
" ER Registration | \n",
" LacticAcid | \n",
"
\n",
" \n",
" | 3 | \n",
" Response | \n",
" ER Registration | \n",
" ER Triage | \n",
"
\n",
" \n",
" | 4 | \n",
" Response | \n",
" ER Registration | \n",
" ER Sepsis Triage | \n",
"
\n",
" \n",
" | 5 | \n",
" Response | \n",
" ER Triage | \n",
" Leucocytes | \n",
"
\n",
" \n",
" | 6 | \n",
" Response | \n",
" ER Triage | \n",
" CRP | \n",
"
\n",
" \n",
" | 7 | \n",
" Response | \n",
" ER Triage | \n",
" ER Sepsis Triage | \n",
"
\n",
" \n",
" | 8 | \n",
" Response | \n",
" ER Sepsis Triage | \n",
" Leucocytes | \n",
"
\n",
" \n",
" | 9 | \n",
" Response | \n",
" ER Sepsis Triage | \n",
" CRP | \n",
"
\n",
" \n",
"
\n",
"
"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"execution_count": 5
},
{
"cell_type": "markdown",
"id": "1588738c",
"metadata": {},
"source": [
"As last example, we set the activation and the target and ask for the template:"
]
},
{
"cell_type": "code",
"id": "cba37244",
"metadata": {
"ExecuteTime": {
"end_time": "2026-05-29T14:06:47.431159700Z",
"start_time": "2026-05-29T14:06:47.268141Z"
}
},
"source": [
"query_checker = DeclareQueryChecker(log=event_log, consider_vacuity=False, activation='ER Registration', target='CRP', min_support=0.2, return_first=False)\n",
"query_check_res: DeclareResultsBrowser = query_checker.run()\n",
"\n",
"query_check_res.filter_query_checking(queries=['template', 'activation', 'target'])"
],
"outputs": [
{
"data": {
"text/plain": [
" template activation target\n",
"0 Choice ER Registration CRP\n",
"1 Responded Existence ER Registration CRP\n",
"2 Response ER Registration CRP\n",
"3 Alternate Response ER Registration CRP\n",
"4 Precedence ER Registration CRP\n",
"5 Alternate Precedence ER Registration CRP\n",
"6 Not Chain Response ER Registration CRP\n",
"7 Not Chain Precedence ER Registration CRP"
],
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" template | \n",
" activation | \n",
" target | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" Choice | \n",
" ER Registration | \n",
" CRP | \n",
"
\n",
" \n",
" | 1 | \n",
" Responded Existence | \n",
" ER Registration | \n",
" CRP | \n",
"
\n",
" \n",
" | 2 | \n",
" Response | \n",
" ER Registration | \n",
" CRP | \n",
"
\n",
" \n",
" | 3 | \n",
" Alternate Response | \n",
" ER Registration | \n",
" CRP | \n",
"
\n",
" \n",
" | 4 | \n",
" Precedence | \n",
" ER Registration | \n",
" CRP | \n",
"
\n",
" \n",
" | 5 | \n",
" Alternate Precedence | \n",
" ER Registration | \n",
" CRP | \n",
"
\n",
" \n",
" | 6 | \n",
" Not Chain Response | \n",
" ER Registration | \n",
" CRP | \n",
"
\n",
" \n",
" | 7 | \n",
" Not Chain Precedence | \n",
" ER Registration | \n",
" CRP | \n",
"
\n",
" \n",
"
\n",
"
"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"execution_count": 6
}
],
"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
}