Finite-state machines with dot
fsmdot is a Python package to create finite-state machines which can be exported to dot format. It uses the pygraphviz library which is a Python interface to the Graphviz graph layout and visualization package.
Installing
- First, you need to install Graphviz. See how to download it here.
- Then, fsmdot can be installed using pip:
pip3 install fsmdot
Usage
With the fsmdot library, you can create two different types of finite-state machine:
- Deterministic finite automaton (DFA)
- Nondeterministic finite automaton (NFA)
A finite-state machine is represented by a quintuple (Q, S, T, q0, F) where:
- Q is a set of states
- S is a set of input symbols (alphabet)
- d is a dictionnary containing the transitions
- q0 is the initial state
- F is the set of accept states
Deterministic finite automaton
This is how to create a deterministic finite automaton.
- First, we import the Dfa class:
from fsmdot.dfa import Dfa
- Create the set of states:
Q = {'S1', 'S2'}
- Create the set of symbols representing the input alphabet:
S = {'0', '1'}
- Create the transitions as a dictionnary.
d = {
'S1': {
'0': 'S2',
'1': 'S1'
},
'S2': {
'0': 'S1',
'1': 'S2'
}
}
- Create the initial state (the state must be in Q):
q0 = 'S1'
- Create the set of accept states (the states must be in Q):
F = {'S1'}
- Then, you can create the DFA:
a = Dfa(Q, S, d, q0, F)
- To see the state-transition table, use the print_table method:
a.print_table()
This is the result:
+---------+-----+-----+
| | 0 | 1 |
+=========+=====+=====+
| -> * S1 | S2 | S1 |
+---------+-----+-----+
| S2 | S1 | S2 |
+---------+-----+-----+
- You can check if a string is accepted by the automata using the accept method:
print(a.accept('11110'))
print(a.accept('110110110101'))
This is the result:
False
True
- To create the dot graph representing the DFA, use the dot_graph method. It creates a graph object.
G = a.dot_graph()
You can print the string representation of the graph using the to_string method or write this content in a file using the write method (see pygraphviz):
print(G.to_string())
G.write('graph1_dfa.dot')
Result:
strict digraph FSM {
graph [rankdir=LR];
node [shape=circle];
null [shape=point];
S1 [shape=doublecircle];
null -> S1;
S1 -> S1 [label=1];
S1 -> S2 [label=0];
S2 -> S1 [label=0];
S2 -> S2 [label=1];
}
File graph1_dfa.dot:
Nondeterministic finite automaton
You can also create nondeterministic finite automatons using the Nfa class.
- To add epsilon-moves, use the symbol Nfa.EPSILON.
Example:
from fsmdot.nfa import Nfa
Q = {1, 2, 3, 4}
S = {Nfa.EPSILON, '0', '1'}
d = {
1: {
Nfa.EPSILON: {3},
'0': {2}
},
2: {
'1': {2, 4}
},
3: {
Nfa.EPSILON: {2},
'0': {4}
},
4: {
'0': {3}
}
}
q0 = 1
F = {3, 4}
a = Nfa(Q, S, d, q0, F)
a.print_table()
G = a.dot_graph()
G.write('graph6_nfa.dot')
State-transition table:
+------+-----+--------+-----+
| | 0 | 1 | ε |
+======+=====+========+=====+
| -> 1 | {2} | {} | {3} |
+------+-----+--------+-----+
| 2 | {} | {2, 4} | {} |
+------+-----+--------+-----+
| * 3 | {4} | {} | {2} |
+------+-----+--------+-----+
| * 4 | {3} | {} | {} |
+------+-----+--------+-----+
File graph6_nfa.dot:
- To calculate the epsilon closure of a state, use the epsilon_closure method.
# Calculations of epsilon closure
for state in Q:
print(state, a.epsilon_closure(state))
Result:
1 {1, 2, 3}
2 {2}
3 {2, 3}
4 {4}
- To convert a NFA to a DFA, use the to_dfa method. It uses the powerset construction.
# Conversion to DFA
dfa = a.to_dfa()
dfa.print_table()
G2 = dfa.dot_graph()
G2.write('graph6_dfa.dot')
State-transition table of dfa:
+----------------+--------+--------+
| | 0 | 1 |
+================+========+========+
| -> * {1, 2, 3} | {2, 4} | {2, 4} |
+----------------+--------+--------+
| * {2, 3} | {4} | {2, 4} |
+----------------+--------+--------+
| * {2, 4} | {2, 3} | {2, 4} |
+----------------+--------+--------+
| * {4} | {2, 3} | {} |
+----------------+--------+--------+
File graph6_dfa.dot:
Examples
To see how the library works, look at the examples in the examples folder.
References
- Automata theory
- Finite-state machines
- Deterministic finite automaton
- Nondeterministic finite automaton
- Powerset construction
- DFA minimization
Author
License
Metadata
Release files for fsmdot 0.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| fsmdot-0.0.1.tar.gz | 8.5 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| fsmdot-0.0.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 17.1 kB
Release files / fsmdot-0.0.1.tar.gz
| Download URL | fsmdot-0.0.1.tar.gz |
|---|---|
| Size | 8.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
55ced4f562466cef1236628eb66022a0dab8f75aae64385046d05879a68feb4c
|
|
BLAKE2b-256 checksum How to use checksums |
be842278e3673ff9dde3e042a33b36ae36ffef4ce82866e4b239b5bb2e272261
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.5
|
Release files / fsmdot-0.0.1-py3-none-any.whl
| Download URL | fsmdot-0.0.1-py3-none-any.whl |
|---|---|
| Size | 8.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
202c4f2c654ba4569664790e60268308e6927d7e30d9c395853838a659d9c12e
|
|
BLAKE2b-256 checksum How to use checksums |
0cc646646efb472f6f7277ce8a4ffffba992e4d61cf995a0ce9b66f1914da85a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.5
|
Security for fsmdot 0.0.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.