Skip to content

Commit

Permalink
Refactor flow handling in BPMN process transformer, update prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
jtlicardo committed Dec 16, 2024
1 parent 1e6fb7f commit 27e315a
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 68 deletions.
2 changes: 2 additions & 0 deletions src/bpmn_assistant/prompts/create_bpmn.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ If a branch has an empty "path", it leads to the first element after the exclusi
If the branch does not lead to the next element in the process (for example, it goes back to a previous element), specify the next element id.
If the branch leads to the next element in the process, do not specify the next element id.
If the process needs to end under a specific condition, you must explicitly include an end event in that branch's "path". If no end event is provided, the process will automatically continue to the next task in the sequence.
If the process description does not explicitly mention the 'else' branch or specify the outcome for an unmet condition, assume it leads to an end event.


```json
{
Expand Down
96 changes: 33 additions & 63 deletions src/bpmn_assistant/services/bpmn_process_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,24 @@ def transform(
elements = []
flows = []

def add_flow(source_ref, target_ref, flow_id=None, condition=None):
"""
Helper function to append a flow to the flows list.
"""
for flow in flows:
if flow["sourceRef"] == source_ref and flow["targetRef"] == target_ref:
return

flow_id = flow_id or f"{source_ref}-{target_ref}"
flows.append(
{
"id": flow_id,
"sourceRef": source_ref,
"targetRef": target_ref,
"condition": condition,
}
)

def handle_exclusive_gateway(
element: dict, next_element_id: Optional[str] = None
) -> Optional[str]:
Expand All @@ -58,13 +76,10 @@ def handle_exclusive_gateway(
if not branch.get("path"):
# Connect the exclusive gateway to the next element in the process
if next_element_id:
flows.append(
{
"id": f"{element['id']}-{next_element_id}",
"sourceRef": element["id"],
"targetRef": next_element_id,
"condition": branch.get("condition", None),
}
add_flow(
element["id"],
next_element_id,
condition=branch.get("condition", None),
)
continue # Skip further processing for empty branches

Expand All @@ -81,14 +96,7 @@ def handle_exclusive_gateway(
source_ref = branch_structure["elements"][-1]["id"]
condition = None

flows.append(
{
"id": f"{source_ref}-{branch['next']}",
"sourceRef": source_ref,
"targetRef": branch["next"],
"condition": condition,
}
)
add_flow(source_ref, branch["next"], condition=condition)

# Add the flow from the exclusive gateway to the first element in the branch
first_element = (
Expand All @@ -97,13 +105,10 @@ def handle_exclusive_gateway(
else None
)
if first_element:
flows.append(
{
"id": f"{element['id']}-{first_element['id']}",
"sourceRef": element["id"],
"targetRef": first_element["id"],
"condition": branch["condition"],
}
add_flow(
element["id"],
first_element["id"],
condition=branch["condition"],
)

return join_gateway_id
Expand All @@ -120,31 +125,17 @@ def handle_parallel_gateway(element: dict) -> str:
)

for branch in element["branches"]:
branch_structure = self.transform(branch)
branch_structure = self.transform(branch, join_gateway_id)
elements.extend(branch_structure["elements"])
flows.extend(branch_structure["flows"])

# Add the flow from the parallel gateway to the first element in the branch
first_element = branch_structure["elements"][0]
flows.append(
{
"id": f"{element['id']}-{first_element['id']}",
"sourceRef": element["id"],
"targetRef": first_element["id"],
"condition": None,
}
)
add_flow(element["id"], first_element["id"])

# Add the flow from the last element in the branch to the join gateway
last_element = branch_structure["elements"][-1]
flows.append(
{
"id": f"{last_element['id']}-{join_gateway_id}",
"sourceRef": last_element["id"],
"targetRef": join_gateway_id,
"condition": None,
}
)
add_flow(last_element["id"], join_gateway_id)

return join_gateway_id

Expand All @@ -168,37 +159,16 @@ def handle_parallel_gateway(element: dict) -> str:

# Connect the join gateway to the next element in the process
if join_gateway_id and next_element_id:
flows.append(
{
"id": f"{join_gateway_id}-{next_element_id}",
"sourceRef": join_gateway_id,
"targetRef": next_element_id,
"condition": None,
}
)
add_flow(join_gateway_id, next_element_id)
elif element["type"] == "parallelGateway":
join_gateway_id = handle_parallel_gateway(element)

# Connect the join gateway to the next element in the process
if next_element_id:
flows.append(
{
"id": f"{join_gateway_id}-{next_element_id}",
"sourceRef": join_gateway_id,
"targetRef": next_element_id,
"condition": None,
}
)
add_flow(join_gateway_id, next_element_id)
elif next_element_id and element["type"] != "endEvent":
# Add the flow between the current element and the next element in the process
flows.append(
{
"id": f"{element['id']}-{next_element_id}",
"sourceRef": element["id"],
"targetRef": next_element_id,
"condition": None,
}
)
add_flow(element["id"], next_element_id)

# Add incoming and outgoing flows to each element
for element in elements:
Expand Down
Loading

0 comments on commit 27e315a

Please sign in to comment.