Add split miner implementation
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,129 @@
|
||||
# Split Miner - BPMN process discovery from event logs.
|
||||
# Authors:
|
||||
# imacat@mail.imacat.idv.tw (imacat), 2026/3/10
|
||||
# AI assistance: Claude Code (Anthropic)
|
||||
|
||||
# Copyright (c) 2026 imacat.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
# implied. See the License for the specific language governing
|
||||
# permissions and limitations under the License.
|
||||
"""Tests for the DFG construction with a 5-task example.
|
||||
|
||||
Uses a small event log with 5 tasks to verify DFG construction,
|
||||
edge frequencies, and source/sink detection.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import unittest
|
||||
|
||||
from split_miner.bpmn import Node, Task
|
||||
from split_miner.dfg import DirectlyFollowsGraph
|
||||
|
||||
|
||||
def _make_tasks(
|
||||
labels: str,
|
||||
) -> dict[str, Task]:
|
||||
"""Create a Task for each single-character label.
|
||||
|
||||
:param labels: The labels as a string.
|
||||
:return: A dict mapping label to Task.
|
||||
"""
|
||||
return {ch: Task(ch, ch) for ch in labels}
|
||||
|
||||
|
||||
def _make_five_task_log() -> tuple[
|
||||
dict[tuple[Node, ...], int], dict[str, Task]
|
||||
]:
|
||||
"""Build a 5-task event log.
|
||||
|
||||
L = {<a,b,c,d>^3, <a,c,b,d>^2, <a,e,d>^1}
|
||||
|
||||
:return: The event log and the task map.
|
||||
"""
|
||||
t: dict[str, Task] = _make_tasks("abcde")
|
||||
traces: dict[tuple[Node, ...], int] = {
|
||||
(t["a"], t["b"], t["c"], t["d"]): 3,
|
||||
(t["a"], t["c"], t["b"], t["d"]): 2,
|
||||
(t["a"], t["e"], t["d"]): 1,
|
||||
}
|
||||
return traces, t
|
||||
|
||||
|
||||
class TestFiveTaskDFG(unittest.TestCase):
|
||||
"""Tests for DFG with 5 tasks (a, b, c, d, e)."""
|
||||
|
||||
def setUp(self) -> None:
|
||||
"""Set up the test.
|
||||
|
||||
:return: None.
|
||||
"""
|
||||
traces: dict[tuple[Node, ...], int]
|
||||
traces, self.__t = _make_five_task_log()
|
||||
self.__dfg: DirectlyFollowsGraph = (
|
||||
DirectlyFollowsGraph(traces)
|
||||
)
|
||||
|
||||
def test_nodes(self) -> None:
|
||||
"""DFG has the correct 5 nodes."""
|
||||
self.assertEqual(
|
||||
self.__dfg.nodes,
|
||||
set(self.__t.values()),
|
||||
)
|
||||
|
||||
def test_edge_frequencies(self) -> None:
|
||||
"""All 8 edge frequencies are correct."""
|
||||
t: dict[str, Task] = self.__t
|
||||
expected: dict[tuple[Node, Node], int] = {
|
||||
(t["a"], t["b"]): 3,
|
||||
(t["a"], t["c"]): 2,
|
||||
(t["a"], t["e"]): 1,
|
||||
(t["b"], t["c"]): 3,
|
||||
(t["b"], t["d"]): 2,
|
||||
(t["c"], t["b"]): 2,
|
||||
(t["c"], t["d"]): 3,
|
||||
(t["e"], t["d"]): 1,
|
||||
}
|
||||
for (src, tgt), freq in expected.items():
|
||||
self.assertEqual(
|
||||
self.__dfg.df_frequency(src, tgt),
|
||||
freq,
|
||||
f"|{src.node_id} -> {tgt.node_id}|"
|
||||
f" should be {freq}"
|
||||
)
|
||||
|
||||
def test_edges(self) -> None:
|
||||
"""DFG has the correct 8 edges."""
|
||||
t: dict[str, Task] = self.__t
|
||||
expected: set[tuple[Node, Node]] = {
|
||||
(t["a"], t["b"]), (t["a"], t["c"]),
|
||||
(t["a"], t["e"]),
|
||||
(t["b"], t["c"]), (t["b"], t["d"]),
|
||||
(t["c"], t["b"]), (t["c"], t["d"]),
|
||||
(t["e"], t["d"]),
|
||||
}
|
||||
self.assertEqual(self.__dfg.edges, expected)
|
||||
|
||||
def test_sources(self) -> None:
|
||||
"""Sources include a (first task of traces)."""
|
||||
self.assertIn(
|
||||
self.__t["a"], self.__dfg.sources
|
||||
)
|
||||
|
||||
def test_sinks(self) -> None:
|
||||
"""Sinks include d (last task of traces)."""
|
||||
self.assertIn(
|
||||
self.__t["d"], self.__dfg.sinks
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user