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 startup(argv, offset): env = Environment() # 1. create env. file_name_str, solution_file_name_str = read_input_string(argv, offset) # 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) #env.parse_config_parameter(argv) root = str_ast_to_python_ast(ast_string) # 4. parse string to python ast # 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? return root, env, parse_object, solution_file_present
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_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