def load_mealy_dot(path, parse_rules=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]) startnode = nodes[context['start']] return MealyMachine(startnode)
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: if self._is_valid(s + (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 MakeRandomMealyMachine(n_states, A_in, A_out): 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 MealyMachine(states[0])
with open(mappingpath, 'r') as file: for line in file: # remove double whitespace line = re.sub(' +', ' ', line) line = re.sub('\t', ' ', line).strip() a, b = line.split(' ') # Build mapping name_to_c[a] = b c_to_name[b] = a return name_to_c, c_to_name if __name__ == "__main__": s1 = MealyState('s1') s2 = MealyState('s2') s3 = MealyState('s3') s1.add_edge('a', 'nice', s2) s1.add_edge('b', 'a', s1) s2.add_edge('a', 'nice', s3) s2.add_edge('b', 'back_lol', s1) s3.add_edge('a', 'b', s3) s3.add_edge('b', 'c', s1) mm = MealyMachine(s1) constrpath = '/home/tom/projects/lstar/rers/TrainingSeqLtlRers2020/Problem1/constraints-Problem1.txt' mappingpath= '/home/tom/projects/lstar/rers/TrainingSeqLtlRers2020/Problem1/Problem1_alphabet_mapping_C_version.txt'
from suls.mealymachine import MealyMachine from suls.mealymachine import MealyState as State from util.mealy2nusmv import mealy2nusmv_withintermediate q0 = State('q0') q1 = State('q1') q0.add_edge('a', 'A', q1) mm = MealyMachine(q0) for line in mealy2nusmv_withintermediate(mm): print(line, end='')
fsm, access_sequence + cur) if not equivalent: return equivalent, counterexample # If not, keep building # else: if len(cur) < self.m: for a in A: if access_sequence + cur + a not in self.sul.invalid_cache: to_visit.append(cur + a) return equivalent, counterexample 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)
# Add the children to the corresponding branch of the new inner node new_inner.add(response_q_new, q_new_leaf) new_inner.add(response_q_old, q_old_leaf) print("splitty boi", q_old_state.name, q_new_state.name) # # if response_q_new == True: # new_inner.addTrue(q_new_leaf) # new_inner.addFalse(q_old_leaf) # else: # new_inner.addTrue(q_old_leaf) # new_inner.addFalse(q_new_leaf) if __name__ == "__main__": s1 = State('1') s2 = State('2') s3 = State('3') s1.add_edge('a', 'nice', s2) s1.add_edge('b', 'B', s1) s2.add_edge('a', 'nice', s3) s2.add_edge('b', 'back lol', s1) s3.add_edge('a', 'A', s3) s3.add_edge('b', 'B', s1) mm = MealyMachine(s1) #mm.render_graph() eqc = BFEquivalenceChecker(mm)
import tempfile from equivalencecheckers.bruteforce import BFEquivalenceChecker from equivalencecheckers.wmethod import WmethodEquivalenceChecker from learners.mealylearner import MealyLearner from suls.mealymachine import MealyState, MealyMachine from teachers.teacher 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 = MealyLearner(teacher)
from suls.mealymachine import MealyState, MealyMachine s1 = MealyState('q0') s2 = MealyState('q1') s3 = MealyState('q2') s1.add_edge('a', '1', s2) s1.add_edge('b', '0', s1) s2.add_edge('b', '2', s3) s2.add_edge('a', '0', s2) s3.add_edge('a', '3', s3) s3.add_edge('b', '3', s3) dfa = MealyMachine(s1) dfa.render_graph('example_mealy', format='png')