geometry_tools.automata.kbmag_utils

Provides utility functions for working with the kbmag command-line tools and their output.

 1"""Provides utility functions for working with the kbmag command-line
 2tools and their output.
 3
 4"""
 5
 6from collections import deque
 7from os import path
 8import subprocess
 9
10from . import gap_parse
11
12KBMAG_PATH = "/home/teddy/math/tools/kbmag/bin"
13
14def build_dict(transitions, labels, to_filter=[]):
15    """Build a python dictionary from the data in the GAP record produced
16    by kbmag to describe a finite-state automaton.
17
18    Parameters
19    ----------
20
21    transitions : list
22        list of tuples of length `n`, where `n` is the number of
23        possible labels. If the `i`th entry of tuple `j` is `k`, then there is
24        an edge from vertex `j` to vertex `k` with label `i`. Note that this
25        implies that every vertex has outdegree `len(labels)`.
26
27    labels : list
28        ordered list of edge labels appearing in transitions.
29
30    to_filter : list
31        vertices to discard when building the automaton
32        (i.e. the "failure states" of the automaton).
33
34    Returns
35    -------
36    dict
37        Dictionary describing the finite-state automaton, in the format
38        expected by the `geometry_tools.automata.fsa.FSA` class.
39
40    """
41    v_dict = {}
42    for i, neighbors in enumerate(transitions):
43        n_dict = {}
44        for label, vertex in zip(labels, neighbors):
45            if vertex not in to_filter:
46                n_dict[label] = vertex
47
48        v_dict[i+1] = n_dict
49
50    return v_dict
51
52def dict_to_dot(fsa_dict, name="FSA", newlines=True):
53    if newlines:
54        newline_char = "\n"
55        tab_char = "\t"
56    else:
57        newline_char = ""
58        tab_char = ""
59
60    output = "digraph {} {{{}".format(name, newline_char)
61    for v, neighbors in fsa_dict.items():
62        if len(neighbors) == 0:
63            output += '{};{}'.format(v, newline_char)
64        else:
65            for neighbor, label in neighbors.items():
66                output += '{}{} -> {} [label="{}"];{}'.format(
67                    tab_char, v, neighbor, label[0], newline_char
68                )
69    output += "}}{}".format(newline_char)
70    return output
71
72def run_kbmag(filename):
73    autgroup = path.join(KBMAG_PATH, "autgroup")
74    gpgeowa = path.join(KBMAG_PATH, "gpgeowa")
75
76
77    subprocess.call([autgroup, filename])
78    subprocess.call([gpgeowa, filename])
KBMAG_PATH = '/home/teddy/math/tools/kbmag/bin'
def build_dict(transitions, labels, to_filter=[]):
15def build_dict(transitions, labels, to_filter=[]):
16    """Build a python dictionary from the data in the GAP record produced
17    by kbmag to describe a finite-state automaton.
18
19    Parameters
20    ----------
21
22    transitions : list
23        list of tuples of length `n`, where `n` is the number of
24        possible labels. If the `i`th entry of tuple `j` is `k`, then there is
25        an edge from vertex `j` to vertex `k` with label `i`. Note that this
26        implies that every vertex has outdegree `len(labels)`.
27
28    labels : list
29        ordered list of edge labels appearing in transitions.
30
31    to_filter : list
32        vertices to discard when building the automaton
33        (i.e. the "failure states" of the automaton).
34
35    Returns
36    -------
37    dict
38        Dictionary describing the finite-state automaton, in the format
39        expected by the `geometry_tools.automata.fsa.FSA` class.
40
41    """
42    v_dict = {}
43    for i, neighbors in enumerate(transitions):
44        n_dict = {}
45        for label, vertex in zip(labels, neighbors):
46            if vertex not in to_filter:
47                n_dict[label] = vertex
48
49        v_dict[i+1] = n_dict
50
51    return v_dict

Build a python dictionary from the data in the GAP record produced by kbmag to describe a finite-state automaton.

Parameters
  • transitions (list): list of tuples of length n, where n is the number of possible labels. If the ith entry of tuple j is k, then there is an edge from vertex j to vertex k with label i. Note that this implies that every vertex has outdegree len(labels).
  • labels (list): ordered list of edge labels appearing in transitions.
  • to_filter (list): vertices to discard when building the automaton (i.e. the "failure states" of the automaton).
Returns
def dict_to_dot(fsa_dict, name='FSA', newlines=True):
53def dict_to_dot(fsa_dict, name="FSA", newlines=True):
54    if newlines:
55        newline_char = "\n"
56        tab_char = "\t"
57    else:
58        newline_char = ""
59        tab_char = ""
60
61    output = "digraph {} {{{}".format(name, newline_char)
62    for v, neighbors in fsa_dict.items():
63        if len(neighbors) == 0:
64            output += '{};{}'.format(v, newline_char)
65        else:
66            for neighbor, label in neighbors.items():
67                output += '{}{} -> {} [label="{}"];{}'.format(
68                    tab_char, v, neighbor, label[0], newline_char
69                )
70    output += "}}{}".format(newline_char)
71    return output
def run_kbmag(filename):
73def run_kbmag(filename):
74    autgroup = path.join(KBMAG_PATH, "autgroup")
75    gpgeowa = path.join(KBMAG_PATH, "gpgeowa")
76
77
78    subprocess.call([autgroup, filename])
79    subprocess.call([gpgeowa, filename])