def get_non_crashing_cover_set(fsm: MealyMachine): scs = get_state_cover_set(fsm) non_crashing = set() for seq in scs: fsm.reset() output = fsm.process_input(seq) if (output is not None) and ("error" not in output): non_crashing.add(seq) return non_crashing
def get_dset_outputs(fsm, dset): states = fsm.get_states() outputs = {} for state in states: mm = MealyMachine(state) out = [] for dseq in dset: out.append(mm.process_input(dseq)) mm.reset() outputs[state] = tuple(out.copy()) return outputs
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)
def __init__(self, fsm: MealyMachine): self.fsm = fsm self.A = fsm.get_alphabet() self.states = self.fsm.get_states() self.root = PartitionNode(self.states, self.A, self) self.nodes = [self.root] self.wanted = set([state.name for state in fsm.get_states()]) self.closed = set() self.solution = set()
def _render(fsm: MealyMachine, filename): states = sorted(fsm.get_states(), key=lambda x: int(x.name.strip('s'))) alphabet = sorted(fsm.get_alphabet()) g = Digraph('G', filename=filename) g.attr(rankdir='LR') # Add states for state in states: g.node(state.name) # Add transitions: for state in states: for action, (other_state, output) in sorted(state.edges.items(), key=lambda x: x[0]): g.edge(state.name, other_state.name, label=f'{action}/{output}') g.save()
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])
class LearnSimpleMealy(unittest.TestCase): 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) def test_lstar_wmethod(self): eqc = WmethodEquivalenceChecker(self.mm, m=len(self.mm.get_states())) teacher = Teacher(self.mm, eqc) learner = LStarMealyLearner(teacher) hyp = learner.run() equivalent, _ = eqc.test_equivalence(hyp) self.assertTrue(equivalent) self.assertEqual( len(self.mm.get_states()), len(hyp.get_states()), ) def test_lstar_bruteforce(self): eqc = BFEquivalenceChecker(self.mm, max_depth=len(self.mm.get_states())) teacher = Teacher(self.mm, eqc) learner = LStarMealyLearner(teacher) hyp = learner.run() equivalent, _ = WmethodEquivalenceChecker( self.mm, m=len(self.mm.get_states())).test_equivalence(hyp) self.assertTrue(equivalent) self.assertEqual( len(self.mm.get_states()), len(hyp.get_states()), )
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)
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)
def get_dset_outputs(fsm, dset): states = fsm.get_states() outputs = {} for state in states: if isinstance(fsm, MealyMachine): mm = MealyMachine(state) elif isinstance(fsm, DFA): mm = DFA(state, fsm.accepting_states) out = [] for dseq in dset: out.append(mm.process_input(dseq)) mm.reset() outputs[state] = tuple(out.copy()) return outputs
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)
def construct_hypothesis(self): # Keep track of the initial state initial_state = self.S[()] # Keep track of the amount of states, so we can sift again if # the sifting process created a new state n = len(list(self.S.items())) items_added = True # Todo: figure out a neater way to handle missing states during sifting than to just redo the whole thing while items_added: # Add transitions for access_seq, cur_state in list(self.S.items()): for a in self.A: next_state = self.sift(access_seq + a) output = self.query(access_seq + a) cur_state.add_edge(a[0], output, next_state, override=True) # Check if no new state was added n2 = len((self.S.items())) items_added = n != n2 # print("items added", items_added) n = n2 # Add spanning tree transitions for access_seq, state in self.S.items(): if len(access_seq) > 0: ancestor_acc_seq = access_seq[0:-1] ancestor_state = self.S[ancestor_acc_seq] a = access_seq[-1] output = self.query(ancestor_acc_seq + (a,)) ancestor_state.add_edge(a, output, state, override=True) # Find accepting states # accepting_states = [state for access_seq, state in self.S.items() if self.query(access_seq)] return MealyMachine(initial_state)
def _minimize(mm: MealyMachine): dset = get_distinguishing_set(mm) dset_outputs = get_dset_outputs(mm, dset) # Find non-unique states: state_map = {} for state, outputs in dset_outputs.items(): if outputs not in state_map: state_map[outputs] = [state] else: state_map[outputs].append(state) for outputs, states in state_map.items(): if len(states) > 1: og_state = states[0] rest_states = states[1:] states = mm.get_states() for state in states: for action, (other_state, output) in state.edges.items(): if other_state in rest_states: state.edges[action] = og_state, output return mm
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) s4.add_edge('b', 'nice', s5) s5.add_edge('a', 'loop', s5) s5.add_edge('b', 'loop', s5) mm = MealyMachine(s1) mm.render_graph(tempfile.mktemp('.gv')) print(get_state_cover_set(mm))
from stmlearn.equivalencecheckers import WmethodEquivalenceChecker from stmlearn.learners import TTTMealyLearner 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) # Use the W method equivalence checker eqc = WmethodEquivalenceChecker(mm, m=len(mm.get_states())) teacher = Teacher(mm, eqc) # We are learning a mealy machine learner = TTTMealyLearner(teacher) hyp = learner.run() hyp.render_graph(tempfile.mktemp('.gv')) learner.DTree.render_graph(tempfile.mktemp('.gv'))