Oneiroe / PySimpleAutomata

Academic Python Library to manage DFA, NFA and AFW automata.
MIT License
23 stars 6 forks source link

DFA graphical representation not reflecting the JSON file #5

Closed aymeric75 closed 6 months ago

aymeric75 commented 6 months ago

Hello,

Running this code:

`from PySimpleAutomata import DFA, automata_IO

dfa_example = automata_IO.dfa_json_importer("examplee.json")

DFA.dfa_completion(dfa_example) new_dfa=DFA.dfa_minimization(dfa_example)

automata_IO.dfa_to_dot(new_dfa, 'outputttt-name', './')`

Where examplee.json is

{ "alphabet": [ "5c", "10c", "gum" ], "states": [ "s0", "s1", "s2", "s3", "s4" ], "initial_state": "s0", "accepting_states": [ "s0", "s2" ], "transitions": [ ["s0","5c","s1"], ["s0","10c","s4"], ["s1","5c","s2"], ["s1","10c","s3"], ["s2","5c","s3"], ["s2","10c","s3"], ["s4","5c","s3"], ["s4","10c","s3"], ["s3","gum","s0"] ] }

I get:

outputttt-name dot

It seems not in accordance with the JSON file... for instance why is there a "SINK" node ?

Thanks

Oneiroe commented 6 months ago

Hi aymeric75,

The output is correct because you used the dfa_completion function, which completes the automaton. To put it in simple terms, completing an automaton guarantees that every state has a transition defined for each element of the alphabet. The "sink" state is added to capture all the missing transitions in the original automaton.

I hope you'll find this helpful.

aymeric75 commented 6 months ago

Thanks, after having looked at the doc I cannot find a function where I could display only the nodes and transitions specified...

Oneiroe commented 6 months ago

The function dfa_to_dot does exactly that. Mind the input: in your example, you have to remove the completion (as already discussed) and the minimization (because it entails the completion) to output the image of the input automaton without alterations.

aymeric75 commented 6 months ago

Thanks I am going to try today and close the issue if it works

aymeric75 commented 6 months ago

Hello,

My code is now:


from PySimpleAutomata import DFA, automata_IO

dfa_example = automata_IO.dfa_json_importer("DFA_Hanoi_4_4.json")

new_dfa=DFA.dfa_reachable(dfa_example)

automata_IO.dfa_to_dot(new_dfa, 'DFA-Hanoi-4-4', './')

And the DFA_Hanoi_4_4.json is attached, DFA_Hanoi_4_4.json

But when I run this code I get:

graphviz.backend.execute.CalledProcessError: Command '[PosixPath('dot'), '-Kdot', '-Tsvg', '-O', 'DFA-Hanoi-4-4.dot']' returned non-zero exit status 1. [stderr: b"Error: DFA-Hanoi-4-4.dot: syntax error in line 259 near '['\n"]

[EDIT] the code also generates a .dot files (attached as a txt file DFA-Hanoi-4-4.txt ) which seems corrupted (look near l. 259 after the "fake" statement, the quotes disappear)