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:
    graph_rep = json.loads(student_answer)
    node_id_to_name_map = {}
    for i, node in enumerate(graph_rep['nodes']):
        node_id_to_name_map[i] = node[0] if node[0] != '' else ('#' + str(i))
    edges = graph_rep['edges']
    nodes = [ node[0] for node in graph_rep['nodes'] if node[0] != "" ]
    end_nodes = [ node[0] for node in graph_rep['nodes'] if node[0] != "" and node[1] == True]
    labels = [ edge[2] for edge in graph_rep['edges'] if edge[2] != ""]
    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:
                start_nodes.add(node_id_to_name_map[target])
        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#@>