Skip to content

Commit d55a878

Browse files
committed
Partial behavior change and corresponding tests
1 parent 6246b26 commit d55a878

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

automata/fa/dfa.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -278,9 +278,8 @@ def to_partial(self, *, retain_names: bool = False, minify: bool = True) -> Self
278278
Self
279279
An equivalent partial DFA.
280280
"""
281-
if self.allow_partial:
282-
return self.copy()
283281

282+
# Still want to try and remove dead states in the case of a partial DFA
284283
graph = self._get_digraph()
285284
live_states = get_reachable_nodes(graph, [self.initial_state])
286285
non_trap_states = get_reachable_nodes(graph, self.final_states, reversed=True)
@@ -338,8 +337,12 @@ def to_complete(self, trap_state: Optional[DFAStateT] = None) -> Self:
338337
An equivalent complete DFA.
339338
340339
"""
340+
is_partial = any(
341+
len(lookup) != len(self.input_symbols)
342+
for lookup in self.transitions.values()
343+
)
341344

342-
if not self.allow_partial:
345+
if not is_partial:
343346
return self.copy()
344347

345348
if trap_state is None:

tests/test_dfa.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,21 @@ def test_equivalence_partials(self) -> None:
310310
self.assertEqual(self.partial_dfa, complete_dfa)
311311
self.assertEqual(self.partial_dfa, complete_dfa.to_partial(minify=False))
312312

313+
# Checking for equivalent number of states
314+
self.assertEqual(complete_dfa.states, complete_dfa.to_complete().states)
315+
316+
# Make a fake partial DFA and check that the number of states is
317+
# still reduced
318+
test_dfa = DFA(
319+
states=complete_dfa.states,
320+
input_symbols=complete_dfa.input_symbols,
321+
transitions=complete_dfa.transitions,
322+
initial_state=complete_dfa.initial_state,
323+
final_states=complete_dfa.final_states,
324+
allow_partial=True,
325+
)
326+
self.assertTrue(test_dfa.to_partial(minify=False).states.issubset(complete_dfa.states))
327+
313328
def test_equivalence_minify(self) -> None:
314329
"""Should be equivalent after minify."""
315330
minimal_dfa = self.no_consecutive_11_dfa.minify()

0 commit comments

Comments
 (0)