lewiuberg/visual-automata

VisualDFA constructor implicitly checks wrapped automaton cardinality

no-preserve-root opened this issue · 1 comments

The VisualDFA constructor checks the dfa parameter using

This checks if dfa is truthy. Since the DFA class defines a __len__ method (and no __bool__), is is truthy iff len(dfa) != 0. Unfortunately, the length checks the dfa's cardinality, i.e., the size if the input language. For infinite-language DFAs, an exception is then raised. As a result, infinite DFAs cannot be visualized.

This could be fixed by testing if dfa is None. VisualNFA is not affected since NFA does not define a __len__ method at the moment, but would fail if a similar method would be added to NFA.

MRE

Using most recent versions:

  • automata-lib 7.0.1
  • visual_automata 1.1.1
from automata.fa.dfa import DFA
from visual_automata.fa.dfa import VisualDFA

dfa = DFA(states={"q0"}, input_symbols={"i0"}, transitions={"q0": {"i0": "q0"}}, initial_state="q0",
          final_states={"q0"})
VisualDFA(dfa).show_diagram(view=True)

Expected Behavior

The automaton is shown.

Actual Behavior

Traceback (most recent call last):
  File "/path/to/scratch_1.py", line 6, in <module>
    VisualDFA(dfa).show_diagram(view=True)
  File "/path/to/site-packages/visual_automata/fa/dfa.py", line 34, in __init__
    if dfa:
  File "/path/to/site-packages/automata/fa/dfa.py", line 160, in __len__
    return self.cardinality()
  File "/path/to/site-packages/automata/fa/dfa.py", line 792, in cardinality
    raise exceptions.InfiniteLanguageException("The language represented by the DFA is infinite.")
automata.base.exceptions.InfiniteLanguageException: The language represented by the DFA is infinite.

Workaround

Manually copying the automaton works:

VisualDFA(states=dfa.states, input_symbols=dfa.input_symbols, transitions=dfa.transitions,
          initial_state=dfa.initial_state, final_states=dfa.final_states).show_diagram(view=True)

Good morning @no-preserve-root! I will have a look at this after work. If you have a fix, feel free to submit a PR to the Dev branch 🙂