Skip to content

Partial behavior change and corresponding tests #228

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

Merged
merged 4 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 6 additions & 3 deletions automata/fa/dfa.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,9 +278,8 @@ def to_partial(self, *, retain_names: bool = False, minify: bool = True) -> Self
Self
An equivalent partial DFA.
"""
if self.allow_partial:
return self.copy()

# Still want to try and remove dead states in the case of a partial DFA
graph = self._get_digraph()
live_states = get_reachable_nodes(graph, [self.initial_state])
non_trap_states = get_reachable_nodes(graph, self.final_states, reversed=True)
Expand Down Expand Up @@ -338,8 +337,12 @@ def to_complete(self, trap_state: Optional[DFAStateT] = None) -> Self:
An equivalent complete DFA.

"""
is_partial = any(
len(lookup) != len(self.input_symbols)
for lookup in self.transitions.values()
)

if not self.allow_partial:
if not is_partial:
return self.copy()

if trap_state is None:
Expand Down
21 changes: 21 additions & 0 deletions tests/test_dfa.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,27 @@ def test_equivalence_partials(self) -> None:
self.assertEqual(self.partial_dfa, complete_dfa)
self.assertEqual(self.partial_dfa, complete_dfa.to_partial(minify=False))

# Make a fake partial DFA and check that the number of states is
# still reduced
test_dfa = DFA(
states=complete_dfa.states,
input_symbols=complete_dfa.input_symbols,
transitions=complete_dfa.transitions,
initial_state=complete_dfa.initial_state,
final_states=complete_dfa.final_states,
allow_partial=True,
)

# Checking for equivalent number of states
self.assertEqual(complete_dfa.states, complete_dfa.to_complete().states)
self.assertEqual(
len(self.partial_dfa.states), len(test_dfa.to_partial().states)
)

self.assertTrue(
set(test_dfa.to_partial(minify=False).states).issubset(complete_dfa.states)
)

def test_equivalence_minify(self) -> None:
"""Should be equivalent after minify."""
minimal_dfa = self.no_consecutive_11_dfa.minify()
Expand Down
Loading