Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Determinized regex ordering #235

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion automata/fa/gnfa.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ def _find_min_connected_node(
if to_state != final_state:
state_degree[to_state] += 1

return min(state_degree, key=lambda x: state_degree.get(x, -1))
return min(state_degree, key=lambda x: (state_degree.get(x, -1), x))

def to_regex(self) -> str:
"""
Expand Down
37 changes: 37 additions & 0 deletions tests/test_gnfa.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,9 +387,46 @@ def test_to_regex(self) -> None:
# Test equality through DFA regex conversion
self.assertEqual(dfa_1, dfa_2)

def test_gnfa_deterministic(self) -> None:
"""
Check for deterministic conversion to GNFA.
From https://github.com/caleb531/automata/issues/231
"""

gt_symbols = {"Connect": "0", "Send_msg": "1", "Ack": "2", "Close": "3"}
gt_dfa = DFA.from_nfa(
NFA(
states={"q0", "q1", "q2"},
input_symbols={
gt_symbols["Connect"],
gt_symbols["Send_msg"],
gt_symbols["Close"],
gt_symbols["Ack"],
},
transitions={
"q0": {gt_symbols["Connect"]: {"q1"}},
"q1": {gt_symbols["Close"]: {"q0"}, gt_symbols["Send_msg"]: {"q2"}},
"q2": {gt_symbols["Ack"]: {"q1", "q0"}},
},
initial_state="q0",
final_states={"q0", "q1", "q2"},
)
)

# Repeat the test multiple times to account for possible non-determinism.
num_reps = 1_000
starting_gnfa = GNFA.from_dfa(gt_dfa)

for _ in range(num_reps):
regex = starting_gnfa.to_regex()
self.assertEqual(
"(012(012|12)*(03|3)|03)*(012(012|12)*(01?|1?)|(01?)?)", regex
)

def test_show_diagram(self) -> None:
"""
Should construct the diagram for a GNFA.

eliotwrobson marked this conversation as resolved.
Show resolved Hide resolved
"""

graph = self.gnfa.show_diagram()
Expand Down
Loading