示例#1
0
    def setUp(self):
        # Set up an example mealy machine
        states = [MealyState(f'{i}') for i in range(100)]

        for state_a, state_b in [
                states[i:i + 2] for i in range(len(states) - 1)
        ]:
            state_a.add_edge('a', state_a.name, state_b)
            state_a.add_edge('b', 'loop', state_a)
            state_a.add_edge('c', 'loop', state_a)
            state_a.add_edge('d', 'loop', state_a)

        states[-1].add_edge('a', states[-1].name, states[0])
        states[-1].add_edge('b', 'loop', states[-1])
        states[-1].add_edge('c', 'loop', states[-1])
        states[-1].add_edge('d', 'loop', states[-1])

        self.mm = MealyMachine(states[0])
    def build_dfa(self):
        # Gather states from S
        S = self.S

        # The rows can function as index to the 'state' objects
        state_rows = set([tuple(self._get_row(s)) for s in S])
        initial_state_row = tuple(self._get_row(tuple()))

        # Generate state names for convenience
        state_names = {
            state_row: f's{n + 1}'
            for (n, state_row) in enumerate(state_rows)
        }

        # Build the state objects and get the initial and accepting states
        states: Dict[Tuple, MealyState] = {
            state_row: MealyState(state_names[state_row])
            for state_row in state_rows
        }
        initial_state = states[initial_state_row]

        # Add the connections between states
        A = [a for (a, ) in self.A]
        # Keep track of states already visited
        visited_rows = []
        for s in S:
            s_row = tuple(self._get_row(s))
            if s_row not in visited_rows:
                for a in A:
                    sa_row = tuple(self._get_row(s + (a, )))
                    if sa_row in states.keys():
                        try:
                            cur_output = self.query(s + (a, ))
                            states[s_row].add_edge(a, cur_output,
                                                   states[sa_row])
                        except:
                            # Can't add the same edge twice
                            pass
            else:
                visited_rows.append(s_row)

        return MealyMachine(initial_state)
示例#3
0
def load_mealy_dot(
        path,
        parse_rules=industrial_mealy_parser):  # industrial_mealy_parser):
    # Parse the dot file
    context = {'nodes': [], 'edges': []}
    with open(path, 'r') as file:
        for line in file.readlines():
            _parse(parse_rules, line, context)

    # Build the mealy graph
    nodes = {name: MealyState(name) for (name, _) in context['nodes']}
    for (frm, to), edge_properties in context['edges']:
        input, output = edge_properties['label'].strip('"').split('/')
        nodes[frm].add_edge(input, output, nodes[to])

    if 'start' in context:
        startnode = nodes[context['start']]
    else:
        startnode = nodes["0"]

    return MealyMachine(startnode)
示例#4
0
def MakeRandomMealyMachine(n_states, A_in, A_out, minimize=True):
    states = [MealyState(f's{x + 1}') for x in range(n_states)]

    def get_reachable(start_state, states):
        to_visit = [start_state]
        visited = []

        while len(to_visit) > 0:
            cur_state = to_visit.pop()
            if cur_state not in visited:
                visited.append(cur_state)

            for action, (other_state, output) in cur_state.edges.items():
                if other_state not in visited and other_state not in to_visit:
                    to_visit.append(other_state)

        return visited, list(set(states).difference(set(visited)))

    def fix_missing(states):
        for state in states:
            for a in A_in:
                if a not in state.edges.keys():
                    state.add_edge(a, "error", state)

    reached, unreached = get_reachable(states[0], states)
    while len(unreached) > 0:
        x = random.choice(reached)
        y = random.choice(unreached)
        a = random.choice(A_in)
        o = random.choice(A_out)

        x.add_edge(a, o, y, override=True)

        reached, unreached = get_reachable(states[0], states)

    fix_missing(states)

    return _minimize(MealyMachine(states[0])) if minimize else MealyMachine(
        states[0])
示例#5
0
        with q.open('w') as f:
            for a in seq:
                f.write(f'{a} ')
            f.write('0')


# Do we need state cover or transition cover??
# TODO
def get_transition_cover_set(fsm):
    pass
    states = fsm.get_states()
    alphabet = fsm.get_alphabet()


if __name__ == "__main__":
    s1 = MealyState('1')
    s2 = MealyState('2')
    s3 = MealyState('3')
    s4 = MealyState('4')
    s5 = MealyState('5')

    s1.add_edge('a', 'nice', s2)
    s1.add_edge('b', 'nice', s3)

    s2.add_edge('a', 'nice!', s4)
    s2.add_edge('b', 'back', s1)

    s3.add_edge('a', 'nice', s4)
    s3.add_edge('b', 'back', s1)

    s4.add_edge('a', 'nice', s5)
示例#6
0
import tempfile

from stmlearn.equivalencecheckers import WmethodEquivalenceChecker
from stmlearn.learners import LStarMealyLearner
from stmlearn.suls import MealyState, MealyMachine
from stmlearn.teachers import Teacher

# Set up an example mealy machine
s1 = MealyState('1')
s2 = MealyState('2')
s3 = MealyState('3')

s1.add_edge('a', 'nice', s2)
s1.add_edge('b', 'B', s1)
s2.add_edge('a', 'nice', s3)
s2.add_edge('b', 'back', s1)
s3.add_edge('a', 'A', s3)
s3.add_edge('b', 'back', s1)

mm = MealyMachine(s1)
mm.render_graph(tempfile.mktemp('.gv'))

# Use the W method equivalence checker
eqc = WmethodEquivalenceChecker(mm)

teacher = Teacher(mm, eqc)

# We are learning a mealy machine
learner = LStarMealyLearner(teacher)

hyp = learner.run()
示例#7
0
    def setUp(self):
        # Set up an example mealy machine
        s1 = MealyState('1')
        s2 = MealyState('2')
        s3 = MealyState('3')

        s1.add_edge('a', '1', s2)
        s1.add_edge('b', 'next', s1)
        s2.add_edge('a', '2', s3)
        s2.add_edge('b', 'next', s1)
        s3.add_edge('a', '3', s3)
        s3.add_edge('b', 'next', s1)

        self.mm = MealyMachine(s1)
示例#8
0
 def setUp(self):
     s1 = MealyState('1')
     s2 = MealyState('2')
     s3 = MealyState('3')
     s1.add_edge('a', 'nice', s2)
     s1.add_edge('b', 'B', s1)
     s2.add_edge('a', 'nice', s3)
     s2.add_edge('b', 'back', s1)
     s3.add_edge('a', 'A', s3)
     s3.add_edge('b', 'back', s1)
     self.mm = MealyMachine(s1)