Skip to content
This repository has been archived by the owner on Aug 28, 2023. It is now read-only.

VisualDFA constructor implicitly checks wrapped automaton cardinality #7

Open
no-preserve-root opened this issue Nov 28, 2022 · 1 comment

Comments

@no-preserve-root
Copy link

no-preserve-root commented Nov 28, 2022

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)
@no-preserve-root no-preserve-root changed the title VisualDFA constructors implicitly check wrapped automaton cardinality VisualDFA constructor implicitly checks wrapped automaton cardinality Nov 28, 2022
@lewiuberg
Copy link
Owner

lewiuberg commented Nov 29, 2022

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 🙂

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants