## Question Authors' Forum

### Graph UI documentation ### Re: Graph UI documentation

by Andreas Siebel -
Number of replies: 0
If someone wants to create a Finite-State-Automata-Question: I created a DFA-Validation with this library:

Here is my code:

import json
from automata.fa.dfa import DFA
import automata

student_answer = """{{ STUDENT_ANSWER | e('py') }}"""
SEPARATOR = "##"

error_count = 0
def error(s):
global error_count
print(s)
error_count += 1

class ValidationError(Exception):
pass

try:
node_id_to_name_map = {}
for i, node in enumerate(graph_rep['nodes']):
node_id_to_name_map[i] = node if node != '' else ('#' + str(i))
edges = graph_rep['edges']
nodes = [ node for node in graph_rep['nodes'] if node != "" ]
end_nodes = [ node for node in graph_rep['nodes'] if node != "" and node == True]
labels = [ edge for edge in graph_rep['edges'] if edge != ""]
graph = {}

start_nodes = set()
for node_id, node_name in sorted(node_id_to_name_map.items()):
edges = dict()
for source, target, edge_label in graph_rep['edges']:
if source == node_id:
edges[edge_label] = node_id_to_name_map[target]
if source == -1:
graph[node_name] = edges
if len(start_nodes) > 1:
raise ValidationError("Too many initial states")
dfa = DFA(
states= set(nodes),
input_symbols=set(labels),
transitions=graph,
initial_state=start_nodes.pop(),
final_states=set(end_nodes)
)

except json.JSONDecodeError as e:
raise Exception("Oops. Illegal graph received (exception {}). Please report (unless you did something silly yourself)".format(e))
except automata.base.exceptions.MissingStateError as e:
print("Validation Error:" + str(e))
except ValidationError as e:
print("Validation Error:" + str(e))

{% for TEST in TESTCASES %}
if dfa.accepts_input("{{ TEST.stdin }}"):
print(True)
else:
print(False)

{{ TEST.extra }}
{% if not loop.last %}
print(SEPARATOR)
{% endif %}
{% endfor %}
@17943918#@>