def _get_variables_by_instances(agents): """ Return a dictionary of instance->list of variables """ st = symb_table() flatHierarchy = nscompile.cvar.mainFlatHierarchy # Populate variables with instances variables = {} for agent in agents: variables[agent] = [] varset = nscompile.FlatHierarchy_get_vars(flatHierarchy) varlist = nsset.Set_Set2List(varset) ite = nsutils.NodeList_get_first_iter(varlist) while not nsutils.ListIter_is_end(ite): var = nsutils.NodeList_get_elem_at(varlist, ite) varname = nsnode.sprint_node(var) isVar = nssymb_table.SymbTable_is_symbol_state_var(st._ptr, var) if isVar: # Put the var in the variables dictionary, under the right instance topcontext = varname.partition(".")[0] if topcontext in variables: variables[topcontext].append(var) ite = nsutils.ListIter_get_next(ite) return variables
def _peek(self): """ :return: the item at the current position in the iteration or the sentinel if no value could be found """ if _utils.ListIter_is_end(self._ptr): return self._SENTINEL else: node = _utils.NodeList_get_elem_at(self._lst._ptr, self._ptr) return self._lst._conv.to_object(node)
def insert_after(self, iterator, node): """ inserts `node` right after the position pointed by iterator :param iterator: the iterator pointing the position where to insert :param node: the node to insert in the list """ if _utils.ListIter_is_end(iterator._ptr): raise ValueError("impossible to add after an end iterator") else: _utils.NodeList_insert_after(self._ptr, iterator._ptr, self._conv.to_pointer(node))
def test_trans(self): # Parse a model #nsparser.ReadSMVFromFile("tests/pynusmv/models/modules.smv") #parsed_tree = nsparser.cvar.parsed_tree nscmd.Cmd_SecureCommandExecute( "read_model -i tests/pynusmv/models/modules.smv") nscmd.Cmd_SecureCommandExecute("flatten_hierarchy") st = nscompile.Compile_get_global_symb_table() # main = nsnode.find_node( # nsparser.ATOM, # nsnode.string2node(nsutils.find_string("main")), # None) # # # Flatten # nscompile.CompileFlatten_init_flattener() # st = nscompile.Compile_get_global_symb_table() # layer = nssymb_table.SymbTable_create_layer(st, "model", # nssymb_table.SYMB_LAYER_POS_BOTTOM) # nssymb_table.SymbTable_layer_add_to_class(st, "model", "Model Class") # nssymb_table.SymbTable_set_default_layers_class_name(st, "Model Class") # # #hierarchy = nscompile.Compile_FlattenHierarchy( # # st, layer, main, None, None, 1, 0, None) # # hierarchy = nscompile.FlatHierarchy_create(st) # instances = nsutils.new_assoc() # # nscompile.Compile_ConstructHierarchy(st, layer, main, None, None, # hierarchy, None, instances) # # fhtrans = nscompile.FlatHierarchy_get_trans(hierarchy) # print("NON FLATTENED") # print(nsnode.sprint_node(fhtrans)) # # print(fhtrans.type) #169 = AND # print(car(fhtrans)) #None # print(cdr(fhtrans).type) #130 = CONTEXT # print(car(cdr(fhtrans)).type) #208 = DOT # print(car(car(cdr(fhtrans)))) #None # print(cdr(car(cdr(fhtrans))).type) #161 = ATOM # print(nsnode.sprint_node(cdr(car(cdr(fhtrans))))) #m # # print(cdr(cdr(fhtrans)).type) #192 = EQUAL # # # # trans = nscompile.Compile_FlattenSexp(st, cdr(fhtrans), None) # print("FLATTENED") # print(nsnode.sprint_node(trans)) layers = nssymb_table.SymbTable_get_class_layer_names(st, None) variables = nssymb_table.SymbTable_get_layers_sf_i_vars(st, layers) ite = nsutils.NodeList_get_first_iter(variables) while not nsutils.ListIter_is_end(ite): variable = nsutils.NodeList_get_elem_at(variables, ite) print(nsnode.sprint_node(variable)) ite = nsutils.ListIter_get_next(ite) top = nsnode.find_node(nsparser.ATOM, nsnode.string2node(nsutils.find_string("top")), None) trans = nssexp.Expr_equal(nssexp.Expr_next(top, st), top, st) flattrans = nscompile.Compile_FlattenSexp(st, trans, None) inmod = nsnode.find_node( nsparser.ATOM, nsnode.string2node(nsutils.find_string("inmod")), None) t = nsnode.find_node(nsparser.ATOM, nsnode.string2node(nsutils.find_string("t")), None) m = nsnode.find_node(nsparser.ATOM, nsnode.string2node(nsutils.find_string("m")), None) my = nsnode.find_node(nsparser.ATOM, nsnode.string2node(nsutils.find_string("my")), None) n = nsnode.find_node(nsparser.ATOM, nsnode.string2node(nsutils.find_string("n")), None) mymod = nsnode.find_node( nsparser.ATOM, nsnode.string2node(nsutils.find_string("mymod")), None) main = nsnode.find_node( nsparser.ATOM, nsnode.string2node(nsutils.find_string("main")), None) minmod = nsnode.find_node(nsparser.DOT, nsnode.find_node(nsparser.DOT, None, m), inmod) ftrans = nssexp.Expr_equal( nssexp.Expr_next(inmod, st), nssexp.Expr_or(inmod, nsnode.find_node(nsparser.DOT, my, inmod)), st) print(nsnode.sprint_node(ftrans)) res, err = nsparser.ReadNextExprFromString( "next(inmod) = (inmod | my.inmod) IN n") self.assertEqual(err, 0) trans = car(res) print(nsnode.sprint_node(trans)) conttrans = nsnode.find_node(nsparser.CONTEXT, nsnode.find_node(nsparser.DOT, None, n), ftrans) print(nsnode.sprint_node(conttrans)) fflattrans = nscompile.Compile_FlattenSexp(st, conttrans, None) flattrans = nscompile.Compile_FlattenSexp(st, trans, None) print(nsnode.sprint_node(fflattrans)) print(nsnode.sprint_node(flattrans))
def print_instances(self, filepath): print("----- Instances for", filepath) car = nsnode.car cdr = nsnode.cdr glob.load_from_file(filepath) # Get parsed tree tree = nsparser.cvar.parsed_tree print("--- Main instances") # main module is car(tree) main_vars = self.get_instances_for_module(car(tree)) instances_args = {} for var in main_vars: # var = COLON(ATOM, MODTYPE(ATOM, CONS)) varname = nsnode.sprint_node(car(var)) instances_args[varname] = [] args = cdr(cdr(var)) argslist = [] while args is not None: arg = car(args) instances_args[varname].append(arg) argslist.append(nsnode.sprint_node(arg)) args = cdr(args) print(varname, ":", nsnode.sprint_node(car(cdr(var))), argslist) # Get FSM and stuff glob.compute_model() fsm = glob.prop_database().master.bddFsm self.assertIsNotNone(fsm) flatH = nscompile.cvar.mainFlatHierarchy st = nscompile.Compile_get_global_symb_table() self.assertIsNotNone(flatH) print("--- Check arguments instances") for instance in instances_args: print("INSTANCE", instance) for arg in instances_args[instance]: arg, err = nscompile.FlattenSexp(st, arg, None) self.assertEqual(err, 0) isVar = nssymb_table.SymbTable_is_symbol_var(st, arg) if isVar: print("VAR", nsnode.sprint_node(arg)) else: print("NOT VAR", nsnode.sprint_node(arg)) print("--- All vars") varset = nscompile.FlatHierarchy_get_vars(flatH) self.assertIsNotNone(varset) varlist = nsset.Set_Set2List(varset) self.assertIsNotNone(varlist) ite = nsutils.NodeList_get_first_iter(varlist) while not nsutils.ListIter_is_end(ite): var = nsutils.NodeList_get_elem_at(varlist, ite) isInput = nssymb_table.SymbTable_is_symbol_input_var(st, var) isVar = nssymb_table.SymbTable_is_symbol_var(st, var) if isInput: print("IVAR", "\t", "IN", "'" + nsnode.sprint_node(car(var)) + "'", "\t", nsnode.sprint_node(var)) elif isVar: print("VAR", "\t", "IN", "'" + nsnode.sprint_node(car(var)) + "'", "\t", nsnode.sprint_node(var)) else: print("[ERROR] Unknown type:", nsnode.sprint_node(var)) ite = nsutils.ListIter_get_next(ite) print("------------------------------------------------------")