def get_edges_labels(a): bddict = a.get_dict() edges = [] labels = [] for s in range(0, a.num_states()): print("State :{}".format(s)) for t in a.out(s): ud = a.is_univ_dest(t) if not ud: for dest in a.univ_dests(t): print("adding edge {}".format((int(t.src), int(dest)))) print("with label {}".format( spot.bdd_to_formula(t.cond, bddict))) edges.append((int(t.src), int(dest))) labels.append(spot.bdd_to_formula(t.cond, bddict))
def accepting_word(self): acc_word = self.get_aut().accepting_word() if acc_word is None: return None acc_word.simplify() var_names = [] for formula in list(acc_word.prefix) + list(acc_word.cycle): for f in spot.atomic_prop_collect(spot.bdd_to_formula(formula)): var_names.append(f.ap_name()) for var, aps in self.var_map.items(): for ap in aps: if not ap in var_names: var_names.append(ap) var_names = sorted(list(set(var_names))) prefixes = self.to_binary(var_names, acc_word.prefix) cycles = self.to_binary(var_names, acc_word.cycle) ap_result = {} for var_name in var_names: ap_result[var_name] = (prefixes[var_name], cycles[var_name]) result = {} for var, aps in self.var_map.items(): result[var] = [ap_result[ap] for ap in aps] return result
def to_binary(self, var_names, bdd_list): var_vals = {k: [] for k in var_names} for bdd in bdd_list[::-1]: formula = spot.bdd_to_formula(bdd) next_vals = {} self.process_formula(next_vals, formula) # If we didn't find a value for a variable in this part of the formula, that means it can be either True or False. # We arbitrarily choose False. for var_name in var_names: var_vals[var_name].insert(0, next_vals.get(var_name, False)) return var_vals
def _to_automaton(self): self._spot_formula = spot.formula(self._formula) # Follow https://spot.lrde.epita.fr/tut12.html to convert to finite semantics aut = spot.from_ltlf(self._formula).translate('low', 'sbacc') rem = spot.remove_ap() rem.add_ap('alive') aut = rem.strip(aut) aut = spot.postprocess(aut, 'low', 'sbacc') self._spot_automaton = aut init_states = [d for d in aut.univ_dests(aut.get_init_state_number())] bdd_dict = aut.get_dict() for s in range(aut.num_states()): is_init = s in init_states is_accepting = aut.state_is_accepting(s) self.add_state(str(s), init=is_init, accept=is_accepting) state_id = aut.num_states() for ed in aut.edges(): label = spot.bdd_to_formula(ed.cond, bdd_dict) if self._add_flexible_state and str(ed.src) != str(ed.dst): state_name = 'e_' + str(state_id) self.add_state(state_name) # add transition to the new state self.add_transition(str(ed.src), state_name, label='1') # add transition of the self loop self.add_transition(state_name, state_name, label='1') # add transition from the new state to destination self.add_transition(state_name, str(ed.dst), label=str(label)) state_id += 1 else: self.add_transition(str(ed.src), str(ed.dst), label=str(label)) if self._alphabets is None: self._alphabets = set() for ap in spot.atomic_prop_collect(self._spot_formula): self._alphabets.add(str(ap)) # replace all '1' labels to be all possible alphabets for src, dst, label in self._graph.edges(data='label'): if self._graph[src][dst]['label'] == '1': self._graph[src][dst]['label'] = self._get_alphabet_str() self._graph[src][dst]['print_label'] = '1' elif self._graph[src][dst]['label'] == '0': self._graph[src][dst]['label'] = self._get_neg_alphabet_str() self._graph[src][dst]['print_label'] = '0' else: self._graph[src][dst]['print_label'] = self._graph[src][dst][ 'label']
dir(dra) type(accset) type(dra.state_acc_sets(0)) v[0].fin print(b, v) e = edge_list[0] dir(e) e.src e.dst e.cond spot.bdd_to_formula(e.cond, ra.get_dict()) f = spot.formula(ltl) dir(f) a = spot.translate(ltl, "deterministic", "generic", "sbacc") bddict = a.get_dict() init = a.get_init_state_number() print("Number of states: ", a.num_states()) print("Initial state: ", init) # get initial states initial_states = [] for i in a.univ_dests(init):
res = [] while c != buddy.bddtrue: var = buddy.bdd_var(c) h = buddy.bdd_high(c) if h == buddy.bddfalse: res.append(-var) c = buddy.bdd_low(c) else: res.append(var) c = h assert res == [0, -1] res2 = [] for i in run.aut.ap(): res2.append((i, run.aut.register_ap(i))) assert str(res2) == '[(a, 0), (b, 1)]' f = spot.bdd_to_formula(b) assert f._is(spot.op_And) assert f[0]._is(spot.op_ap) assert f[1]._is(spot.op_Not) assert f[1][0]._is(spot.op_ap) assert str(f) == 'a & !b' try: f = spot.bdd_to_formula(b, spot.make_bdd_dict()) sys.exit(2) except RuntimeError as e: assert "not in the dictionary" in str(e)
while c != buddy.bddtrue: var = buddy.bdd_var(c) h = buddy.bdd_high(c) if h == buddy.bddfalse: res.append(-var) c = buddy.bdd_low(c) else: res.append(var) c = h assert res == [0, -1] res2 = [] for i in run.aut.ap(): res2.append((i, run.aut.register_ap(i))) assert str(res2) == '[(a, 0), (b, 1)]' f = spot.bdd_to_formula(b) assert f._is(spot.op_And) assert f[0]._is(spot.op_ap) assert f[1]._is(spot.op_Not) assert f[1][0]._is(spot.op_ap) assert str(f) == 'a & !b' try: f = spot.bdd_to_formula(b, spot.make_bdd_dict()) sys.exit(2) except RuntimeError as e: assert "not in the dictionary" in str(e)
def spotToBuchi(aut, state_map=None): """ Translate spot representation back to Buchi object or GenBuchi object :param aut: input spot buchi automaton :return: a Buchi object or GenBuchi object """ buchi = GenBuchi() buchi.setAccNum(aut.num_sets()) for ap in list(aut.ap()): buchi.addAp(ap.to_str().replace( '"', '')) # ugly fix, '"' might be used to bypass # and @ init_index = aut.get_init_state_number() state_stack = [init_index] s_list = [init_index] if not state_map: buchi.addState(init_index, 'State %d' % init_index, []) buchi.setInitState(init_index) else: buchi.addState(state_map[init_index], 'State %d' % state_map[init_index], []) buchi.setInitState(state_map[init_index]) while state_stack: index_src = state_stack.pop() for edge in aut.out(index_src): if edge.dst not in s_list: s_list.append(edge.dst) state_stack.append(edge.dst) if not state_map: buchi.addState(edge.dst, 'State %d' % edge.dst, []) else: buchi.addState(state_map[edge.dst], 'State %d' % state_map[edge.dst], []) acc = list() for index_set in range(aut.num_sets()): if edge.acc.has(index_set): acc.append(index_set) if not state_map: buchi.setStateAcc(edge.src, acc) else: buchi.setStateAcc(state_map[edge.src], acc) cond_formula = spot.bdd_to_formula(edge.cond, aut.get_dict()) cond_formula_str = cond_formula.to_str().replace( '"', '') # ugly fix, when '@' appears there is '"' if not state_map: buchi.addEdge(edge.src, edge.dst, cond_formula_str, description=str(edge.cond)) else: buchi.addEdge(state_map[edge.src], state_map[edge.dst], cond_formula_str, description=str(edge.cond)) return buchi