def run_model_checking_mode(argv): print "\033[1m\033[91mWARNING\033[00m: model checking still experimental" root, env, parse_object, solution_file_present = startup(argv, offset=1) if not isinstance(parse_object, BMachine): print "Error: only model checking of b machines" return -1 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 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 -1 bstates = exec_initialisation(root, env, mch, solution_file_read=False) # TODO: volvo example 0 instead of 2 states for bstate in bstates: if not env.state_space.is_seen_state(bstate): env.state_space.set_current_state(bstate) if not mch.has_invariant_mc: print "WARNING: no invariant present" return -1 return _run_model_checking_mode(env, mch)
def run_checking_mode(): env = Environment() # 1. create env. file_name_str, solution_file_name_str = read_input_string(argv, 1) # 2. read filenames ast_string, error = file_to_AST_str_no_print(file_name_str) # 3. parse input-file to string if error: print error #env.set_search_dir(file_name_str) root = str_ast_to_python_ast(ast_string) # 4. parse string to python ast TODO: JSON #if solution_file_name_str: # 5. parse solution-file and write to env. # read_solution_file(env, solution_file_name_str) # The concreate solution values are added at # the bmachine object-init time to the respective mch # 6. replace defs and extern-functions inside mch and solution-file (if present) parse_object = remove_defs_and_parse_ast(root, env) # 7. which kind of ast? 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 #solution_file_read = not solution_file_name_str=="" bstates = set_up_constants(root, env, mch, solution_file_read=False) # also evals properties if not bstates==[]: bool = True 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, not solution_file_name_str=="") for init_bstate in init_bstates: env.state_space.add_state(init_bstate) if mch.has_invariant_mc: w_bool = interpret(mch.aInvariantMachineClause, env) bool = bool and w_bool.bvalue env.state_space.undo() if mch.has_assertions_mc: interpret(mch.aAssertionsMachineClause, env) env.state_space.undo() return bool else: # TODO: dont repeat yourself init_bstates = exec_initialisation(root, env, mch, not solution_file_name_str=="") for bstate in init_bstates: env.state_space.add_state(bstate) if mch.has_invariant_mc: w_bool = interpret(mch.aInvariantMachineClause, env) assert w_bool.bvalue 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 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 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 run_animation_mode(argv): env = Environment() # 1. create env. file_name_str, solution_file_name_str = read_input_string(argv, 1) # 2. read filenames ast_string, error = file_to_AST_str_no_print(file_name_str) # 3. parse input-file to string if error: print error #env.set_search_dir(file_name_str) root = str_ast_to_python_ast(ast_string) # 4. parse string to python ast TODO: JSON # uncomment for profiling (e.g. performance tests) #import cProfile #cProfile.run('mch = interpret(root, env)','pyB_profile_out.txt') solution_file_present = not solution_file_name_str=="" #if solution_file_present: # 5. parse solution-file and write to env. # read_solution_file(env, solution_file_name_str) # The concreate solution values are added at # the bmachine object-init time to the respective mch # 6. replace defs and extern-functions inside mch and solution-file (if present) parse_object = remove_defs_and_parse_ast(root, env) # 7. which kind of ast? if not isinstance(parse_object, BMachine): is_ppu = isinstance(parse_object, PredicateParseUnit) is_epu = isinstance(parse_object, ExpressionParseUnit) assert is_ppu or is_epu result = interpret(parse_object.root, env) # eval predicate or expression print result # TODO: print_values_b_style needs symbolic set impl #print print_values_b_style(result) return 0 assert isinstance(parse_object, BMachine) # 8. typecheck mch = parse_object type_check_root_bmch(root, env, mch) # also checks all included, seen, used and extend # TODO: Check with B spec # 9. animate if ops are present # DO-WHILE Loop while True: next_states = __calc_states_and_print_ui(root, env, mch, solution_file_present) if next_states==[]: # BUG: no enabled ops doesnt mean there are none (deadlock-state) pass undo_possible = not env.state_space.empty() number_of_options = len(next_states) if undo_possible: number_of_options = number_of_options + 1 input_str = "Input (0-"+str(number_of_options)+"):" #number = raw_input(input_str) # XXXX number = number_of_options number = int(number) # quit if number == number_of_options: print "goodbye" break elif undo_possible and number == number_of_options-1: x = env.state_space.get_stack_size() x = x-1 # BUGFIX: empty state on stack if 2==x and env.init_state_on_stack and env.set_up_state_on_stack: env.init_state_on_stack==False env.init_done = False elif 1==x and env.init_state_on_stack and env.set_up_state_on_stack==False: env.init_state_on_stack==False env.init_done = False elif 1==x and env.set_up_state_on_stack: env.set_up_done = False env.set_up_state_on_stack = False env.set_up_bmachines_names = [] env.state_space.undo() elif not env.set_up_done: env.set_up_done = True env.set_up_state_on_stack = True bstate = next_states[number] env.state_space.add_state(bstate) elif not env.init_done: env.init_done = True env.init_state_on_stack = True bstate = next_states[number] env.state_space.add_state(bstate) # init and set_up done. Exec operation: elif len(next_states)>number and number >= 0: # switch state ( bstate = next_states[number] env.state_space.add_state(bstate) else: print "Error! Wrong input:", number return 0
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
def run_animation_mode(arguments): root, env, parse_object, solution_file_present = startup(arguments) if not isinstance(parse_object, BMachine): is_ppu = isinstance(parse_object, PredicateParseUnit) is_epu = isinstance(parse_object, ExpressionParseUnit) assert is_ppu or is_epu result = interpret(parse_object.root, env) # eval predicate or expression print print_values_b_style(result) else: assert isinstance(parse_object, BMachine) # 8. typecheck mch = parse_object type_check_root_bmch(root, env, mch) # also checks all included, seen, used and extend # TODO: Check with B spec # 9. animate if ops are present # DO-WHILE Loop while True: next_states = __calc_states_and_print_ui(root, env, mch, solution_file_present) if next_states==[]: # BUG: no enabled ops doesnt mean there are none (deadlock-state) pass undo_possible = not env.state_space.empty() number_of_options = len(next_states) if undo_possible: number_of_options = number_of_options + 1 input_str = "Input (0-"+str(number_of_options)+"):" number = raw_input(input_str) number = int(number) # quit if number == number_of_options: print "goodbye" break elif undo_possible and number == number_of_options-1: x = env.state_space.get_stack_size() x = x-1 # BUGFIX: empty state on stack if 2==x and env.init_state_on_stack and env.set_up_state_on_stack: env.init_state_on_stack==False env.init_done = False elif 1==x and env.init_state_on_stack and env.set_up_state_on_stack==False: env.init_state_on_stack==False env.init_done = False elif 1==x and env.set_up_state_on_stack: env.set_up_done = False env.set_up_state_on_stack = False env.set_up_bmachines_names = [] env.state_space.undo() elif not env.set_up_done: env.set_up_done = True env.set_up_state_on_stack = True bstate = next_states[number] env.state_space.add_state(bstate) elif not env.init_done: env.init_done = True env.init_state_on_stack = True bstate = next_states[number] env.state_space.add_state(bstate) # init and set_up done. Exec operation: elif len(next_states)>number and number >= 0: # switch state ( bstate = next_states[number] env.state_space.add_state(bstate) else: print "Error! Wrong input:", number