Skip to content

Commit

Permalink
Added error handling for ambiguous outcome_codes -> A** (#127)
Browse files Browse the repository at this point in the history
cf. #38
  • Loading branch information
DeltaDaniel authored Sep 18, 2023
1 parent 371b7ce commit 063d80a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
18 changes: 18 additions & 0 deletions src/ebdtable2graph/graph_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from ebdtable2graph.models.errors import (
EbdCrossReferenceNotSupportedError,
EndeInWrongColumnError,
OutcomeCodeAmbiguousError,
OutcomeNodeCreationError,
)

Expand Down Expand Up @@ -89,6 +90,8 @@ def get_all_edges(table: EbdTable) -> List[EbdGraphEdge]:
first_node_after_start = _get_key_and_node_with_lowest_step_number(table)[1]
result: List[EbdGraphEdge] = [EbdGraphEdge(source=nodes["Start"], target=first_node_after_start, note=None)]

outcome_nodes_duplicates: dict[str, OutcomeNode] = {} # map to check for duplicate outcome nodes

for row in table.rows:
decision_node = _convert_row_to_decision_node(row)
for sub_row in row.sub_rows:
Expand All @@ -100,6 +103,7 @@ def get_all_edges(table: EbdTable) -> List[EbdGraphEdge]:
)
else:
outcome_node: Optional[OutcomeNode] = _convert_sub_row_to_outcome_node(sub_row)

if outcome_node is None:
if all(sr.result_code is None for sr in row.sub_rows) and any(
sr.note is not None and sr.note.startswith("EBD ") for sr in row.sub_rows
Expand All @@ -110,6 +114,20 @@ def get_all_edges(table: EbdTable) -> List[EbdGraphEdge]:
):
raise EndeInWrongColumnError(row=row)
raise OutcomeNodeCreationError(decision_node=decision_node, sub_row=sub_row)

# check for ambiguous outcome nodes, i.e. A** with different notes
is_ambiguous_outcome_node = (
outcome_node.result_code in outcome_nodes_duplicates
and outcome_nodes_duplicates[outcome_node.result_code].note != outcome_node.note
)

if not is_ambiguous_outcome_node:
outcome_nodes_duplicates[outcome_node.result_code] = outcome_node
else:
raise OutcomeCodeAmbiguousError(
outcome_node1=outcome_nodes_duplicates[outcome_node.result_code], outcome_node2=outcome_node
)

edge = _yes_no_edge(
sub_row.check_result.result,
source=decision_node,
Expand Down
12 changes: 11 additions & 1 deletion src/ebdtable2graph/models/errors/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"""
from typing import Optional

from ebdtable2graph.models import DecisionNode, EbdTableRow, EbdTableSubRow
from ebdtable2graph.models import DecisionNode, EbdTableRow, EbdTableSubRow, OutcomeNode


class NotExactlyTwoOutgoingEdgesError(NotImplementedError):
Expand Down Expand Up @@ -109,3 +109,13 @@ def __init__(self, decision_node: DecisionNode, sub_row: EbdTableSubRow):
super().__init__(f"Cannot create outcome node from sub row {sub_row} for DecisionNode {decision_node}.")
self.sub_row = sub_row
self.decision_node = decision_node


class OutcomeCodeAmbiguousError(ValueError):
"""
Raised when the result nodes are ambiguous. This can be the case for "A**" results.
"""

def __init__(self, outcome_node1: OutcomeNode, outcome_node2: OutcomeNode):
super().__init__(f"Ambiguous result codes: for [{outcome_node1, outcome_node2}].")
self.outcome_nodes = [outcome_node1, outcome_node2]

0 comments on commit 063d80a

Please sign in to comment.