def test_simple_model_checking2(self): path = "examples/rpython_performance/SigmaLoop.mch" if os.name=='nt': path="examples/rpython_performance\SigmaLoop" ast_string = file_to_AST_str(path) root = str_ast_to_python_ast(ast_string) # Test env = Environment() env._max_int = 2**31 mch = parse_ast(root, env) type_check_bmch(root, env, mch) # also checks all included, seen, used and extend solution_file_read = False bstates = set_up_constants(root, env, mch, solution_file_read) assert len(bstates)==0 # no setup possible bstates = exec_initialisation(root, env, mch, solution_file_read) assert len(bstates)==1 # only one possibility (sum:=45) assert len(env.state_space.seen_states)==0 assert isinstance(bstates[0], BState) env.state_space.set_current_state(bstates[0]) assert len(env.state_space.seen_states)==1 invatiant = root.children[2] assert isinstance(invatiant, AInvariantMachineClause) assert interpret(invatiant, env) assert len(env.state_space.stack)==2 next_states = calc_next_states(env, mch) assert len(next_states)==1 assert len(env.state_space.stack)==2 # init and empty setup assert env.get_value('sum')==55 env.state_space.set_current_state(next_states[0].bstate) assert env.get_value('sum')==55
def test_set_up_constants_nondeterministic2(self): string = ''' MACHINE Param2 PROPERTIES num:NAT & num <4 CONSTANTS num VARIABLES xx INVARIANT xx:NAT INITIALISATION xx:=num END''' # Build AST string_to_file(string, file_name) ast_string = file_to_AST_str(file_name) root = str_ast_to_python_ast(ast_string) # Test env = Environment() env._min_int = -16 env._max_int = 16 mch = parse_ast(root, env) type_check_bmch(root, env, mch) bstates = set_up_constants(root, env, mch) assert len(bstates)==4 for bstate in bstates: env.state_space.add_state(bstate) num = bstate.get_value("num", mch) assert num in [0,1,2,3] env.state_space.undo()
def test_simple_model_checking0(self): path = "examples/rpython_performance/Lift2.mch" if os.name=='nt': path="examples\rpython_performance\Lift2" ast_string = file_to_AST_str(path) root = str_ast_to_python_ast(ast_string) # Test env = Environment() mch = parse_ast(root, env) type_check_bmch(root, env, mch) # also checks all included, seen, used and extend env._max_int = 2**31 solution_file_read = False bstates = set_up_constants(root, env, mch, solution_file_read) assert len(bstates)==0 # no setup possible bstates = exec_initialisation(root, env, mch, solution_file_read) assert len(bstates)==1 # only one possibility (floor:=4) assert len(env.state_space.seen_states)==0 assert isinstance(bstates[0], BState) env.state_space.set_current_state(bstates[0]) assert len(env.state_space.seen_states)==1 invatiant = root.children[2] assert isinstance(invatiant, AInvariantMachineClause) assert interpret(invatiant, env) assert len(env.state_space.stack)==2 next_states = calc_next_states(env, mch) assert len(next_states)==2 assert len(env.state_space.stack)==2 # init and empty setup assert env.get_value('floor')==4 env.state_space.undo() assert len(env.state_space.stack)==1 # setup assert len(env.state_space.seen_states)==1 for n_state in next_states: bstate = n_state.bstate assert isinstance(bstate, BState) if not env.state_space.is_seen_state(bstate): env.state_space.set_current_state(bstate) assert len(env.state_space.stack)==3 # dec, inc, setup assert len(env.state_space.seen_states)==3 assert env.get_value('floor')==3 or env.get_value('floor')==5 # TODO: Bstate needs refactoring. # - Remove init state # - dont map None to values if parsing unit is no machine # - check empty on stack length 0 # model checking loop while not env.state_space.empty(): assert interpret(invatiant, env) next_states = calc_next_states(env, mch) env.state_space.undo() for n_state in next_states: bstate = n_state.bstate if not env.state_space.is_seen_state(bstate): env.state_space.set_current_state(bstate) assert len(env.state_space.seen_states)==100
def run_checking_mode(arguments): root, env, parse_object, solution_file_present = startup(arguments) if not isinstance(parse_object, BMachine): # #PREDICATE or #EXPRESSION result = interpret(parse_object.root, env) # eval predicate or expression print result else: assert isinstance(parse_object, BMachine) # 8. typecheck type_check_root_bmch(root, env, parse_object) # also checks all included, seen, used and extend mch = parse_object bstates = set_up_constants(root, env, mch, solution_file_present) # also evals properties if not bstates==[]: result = None for bstate in bstates: env.state_space.add_state(bstate) #if mch.has_properties_mc: # assert interpret(mch.aPropertiesMachineClause, env) init_bstates = exec_initialisation(root, env, mch, solution_file_present) for init_bstate in init_bstates: env.state_space.add_state(init_bstate) if mch.has_invariant_mc: # TODO: why not result=result and interpret ? Add comment or fix code result = interpret(mch.aInvariantMachineClause, env) env.state_space.undo() if mch.has_assertions_mc: interpret(mch.aAssertionsMachineClause, env) env.state_space.undo() return result else: # TODO: dont repeat yourself init_bstates = exec_initialisation(root, env, mch, solution_file_present) for bstate in init_bstates: env.state_space.add_state(bstate) if mch.has_invariant_mc: assert interpret(mch.aInvariantMachineClause, env) if mch.has_assertions_mc: interpret(mch.aAssertionsMachineClause, env) env.state_space.undo() if not init_bstates==[]: env.state_space.add_state(init_bstates[0]) return eval_Invariant(root, env, mch)
def __calc_states_and_print_ui(root, env, mch, solution_file_read): # Schneider Book page 62-64: # The parameters p make the constraints C True # #p.C # Sets St and constants k which meet the constraints c make the properties B True # C => #St,k.B if not env.set_up_done: next_states = set_up_constants(root, env, mch, solution_file_read) if len(next_states)>0: print_set_up_bstates(next_states, mch) return next_states else: # no bstates and no exception: set up to do (e.g no constants) env.set_up_done = True # If C and B is True there should be Variables v which make the Invaraiant I True # B & C => #v.I if not env.init_done: next_states = exec_initialisation(root, env, mch, solution_file_read) if len(next_states)>0: undo_possible = not env.state_space.empty() print_init_bstates(next_states, mch, undo_possible) return next_states else: # no bstates and no exception: no init to do (e.g no variables) env.init_done = True print mch.mch_name," - Invariant:", eval_Invariant(root, env, mch) # TODO: move print to animation_clui if EVAL_CHILD_INVARIANT: bstate = env.state_space.get_state() for bmachine in bstate.bmch_list: if not bmachine is None and not bmachine.mch_name==mch.mch_name : print bmachine.mch_name, " - Invariant:", interpret(bmachine.aInvariantMachineClause, env) bstate_lst = calc_next_states(env, mch) show_ui(env, mch, bstate_lst) next_states = [] for n in bstate_lst: # all other data inside bstate_lst has already been processed by show_ui next_states.append(n.bstate) return next_states
def run_with_pyb(bfile_name, dir=""): # Build b-mch AST ast_string = file_to_AST_str("%s%s.mch" % (dir, bfile_name)) ast_root = str_ast_to_python_ast(ast_string) # Get ProB Solution, write to env env = Environment() env._min_int = -2**31 env._max_int = 2**31 ast_str, err = solution_file_to_AST_str("%s%s_values.txt" % (dir, bfile_name)) assert err=='' root = str_ast_to_python_ast(ast_str) env.solution_root = root env.write_solution_nodes_to_env(root) # Init B-mch #dh = DefinitionHandler(env, remove_defs_and_parse_ast) dh = DefinitionHandler(env, str_ast_to_python_ast) dh.repl_defs(ast_root) mch = parse_ast(ast_root, env) type_check_root_bmch(ast_root, env, mch) # also checks all included, seen, used and extend #if env.solution_root: # idNodes = find_var_nodes(root.children[0]) # idNames = [n.idName for n in idNodes] # type_check_predicate(env.solution_root, env, idNames) # side-effect: check properties and invariant print "team-test:calc set up.." bstates = set_up_constants(root, env, mch, solution_file_read=True) env.state_space.add_state(bstates[0]) print "team-test:calc set init.." bstates = exec_initialisation(root, env, mch, solution_file_read=True) env.state_space.add_state(bstates[0]) if mch.has_properties_mc: print "team-test:eval properties..." assert interpret(mch.aPropertiesMachineClause, env) if mch.has_invariant_mc: print "team-test:eval invariant..." assert interpret(mch.aInvariantMachineClause, env)
def test_bool_law(self): path = "examples/BoolLaws.mch" if os.name=='nt': path="examples\BoolLaws.mch" ast_string = file_to_AST_str(path) root = str_ast_to_python_ast(ast_string) # Test env = Environment() mch = remove_defs_and_parse_ast(root, env) type_check_bmch(root, env, mch) # also checks all included, seen, used and extend env._max_int = 2**31 solution_file_read = False bstates = set_up_constants(root, env, mch, solution_file_read) assert len(bstates)==1 # only one possibility assert isinstance(bstates[0], BState) env.state_space.set_current_state(bstates[0]) bstates = exec_initialisation(root, env, mch, solution_file_read) assert len(bstates)==1 # only one possibility assert len(env.state_space.seen_states)==1 assert isinstance(bstates[0], BState) env.state_space.set_current_state(bstates[0]) assert len(env.state_space.seen_states)==2 invatiant = root.children[5] assert isinstance(invatiant, AInvariantMachineClause) assert interpret(invatiant, env) assert len(env.state_space.stack)==3 # init and setup and ref state next_states = calc_next_states(env, mch) assert len(next_states)==3 while not env.state_space.empty(): assert interpret(invatiant, env) next_states = calc_next_states(env, mch) env.state_space.undo() for n_state in next_states: bstate = n_state.bstate if not env.state_space.is_seen_state(bstate): env.state_space.set_current_state(bstate) if env.state_space.get_state().opName=="set up": assert len(env.state_space.stack)==2 # setup and ref state NOT init env.state_space.undo() #negative exponent bug: (x>0 => y**x=C) crashes # inconsistence between variables and constants """ MACHINE Test VARIABLES x INVARIANT x>0 => 10**x = 10 & -3>0 => 10**(-3) = 10 INITIALISATION x:=-3 END def test_arith_law(self): path = "examples/ArithmeticLaws.mch" if os.name=='nt': path="examples\ArithmeticLaws.mch" ast_string = file_to_AST_str(path) root = str_ast_to_python_ast(ast_string) # Test env = Environment() mch = remove_defs_and_parse_ast(root, env) type_check_bmch(root, env, mch) # also checks all included, seen, used and extend env._max_int = 2**31 solution_file_read = False bstates = set_up_constants(root, env, mch, solution_file_read) assert len(bstates)==0 # no setup possible bstates = exec_initialisation(root, env, mch, solution_file_read) assert len(bstates)==1 # only one possibility assert len(env.state_space.seen_states)==0 assert isinstance(bstates[0], BState) env.state_space.set_current_state(bstates[0]) assert len(env.state_space.seen_states)==1 invatiant = root.children[3] assert isinstance(invatiant, AInvariantMachineClause) assert interpret(invatiant, env) assert len(env.state_space.stack)==2 next_states = calc_next_states(env, mch) """
def run_model_checking_mode(arguments): print "WARNING: model checking still experimental" root, env, parse_object, solution_file_present = startup(arguments) if not isinstance(parse_object, BMachine): print "Error: only model checking of b machines" return assert isinstance(parse_object, BMachine) # 6. typecheck type_check_root_bmch(root, env, parse_object) # also checks all included, seen, used and extend mch = parse_object bstates = set_up_constants(root, env, mch, solution_file_read=False) # also evals properties # TODO: implement setup and init non determinism if len(bstates)==1: env.state_space.set_current_state(bstates[0]) elif len(bstates)>1: print "WARNING: non det. set up constants not supported yet" return bstates = exec_initialisation(root, env, mch, solution_file_read=False) for bstate in bstates: if not env.state_space.is_seen_state(bstate): env.state_space.set_current_state(bstate) #if not len(bstates)==1: # print "WARNING: only one init. expected" # print "real init number:", len(bstates) # return if not mch.has_invariant_mc: print "WARNING: no invariant present" return #env.state_space.set_current_state(bstates[0]) while not env.state_space.empty(): # 7. model check # FIXME: dirty fix to avoid invariant checking of set up states if env.state_space.get_state().opName=="set up": env.state_space.undo() continue if not DISABLE_INVARIANT_MC_CHECK: if not interpret(mch.aInvariantMachineClause, env): print "WARNING: invariant violation found after checking", len(env.state_space.seen_states),"states" violation = env.state_space.get_state() violation.print_bstate() #print violation.opName return False if EVAL_CHILD_INVARIANT: bstate = env.state_space.get_state() for bmachine in bstate.bmch_list: if not bmachine is None and not bmachine.mch_name==mch.mch_name : if not interpret(bmachine.aInvariantMachineClause, env): print "WARNING: invariant violation in",bmachine.mch_name ," found after checking", len(env.state_space.seen_states),"states" return False next_states = calc_next_states(env, mch) env.state_space.undo() for s in next_states: bstate = s.bstate #print s.opName #bstate.print_bstate() # TODO: check double values with Lift2.mch example if not env.state_space.is_seen_state(bstate): env.state_space.set_current_state(bstate) print "checked",len(env.state_space.seen_states),"states.\033[1m\033[92mNo invariant violation found.\033[00m" if DISABLE_INVARIANT_MC_CHECK: print "invariant check was disabled" return True