def test_genAST_sub_any(self): string = ''' MACHINE Test VARIABLES xx INVARIANT xx:NAT INITIALISATION BEGIN xx:=1; ANY r1, r2 WHERE r1 : NAT & r2 : NAT & r1*r1 + r2*r2 = 25 THEN xx := r1 + r2 END END END''' 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 = -1 env._max_int = 5 mch = parse_ast(root, env) type_check_bmch(root, env, mch) # also checks all included, seen, used and extend arbitrary_init_machine(root, env, mch) # init VARIABLES and eval INVARIANT assert isinstance(root.children[2], AInvariantMachineClause) assert interpret(root.children[2], env) assert env.get_value("xx")==5 or env.get_value("xx")==7 # 3+4 or 5+0
def test_genAST_sub_var(self): # Build AST string = ''' MACHINE Test VARIABLES xx INVARIANT xx:NAT INITIALISATION BEGIN xx:=1; VAR varLoc1, varLoc2 IN varLoc1 := xx + 1 ; varLoc2 := 2 * varLoc1 ; xx := varLoc2 END END END''' 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 = -1 env._max_int = 5 mch = parse_ast(root, env) type_check_bmch(root, env, mch) # also checks all included, seen, used and extend arbitrary_init_machine(root, env, mch) # init VARIABLES and eval INVARIANT assert isinstance(root.children[2], AInvariantMachineClause) assert interpret(root.children[2], env) assert env.get_value("xx")==4
def test_genAST_sub_let2(self): string = ''' MACHINE Test VARIABLES X, Y INVARIANT X:NAT & Y:NAT INITIALISATION BEGIN X:=10; LET r1, X BE X = 6 & r1 = X / 2 IN Y := r1 END END END''' 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 = -1 env._max_int = 5 mch = parse_ast(root, env) type_check_bmch(root, env, mch) # also checks all included, seen, used and extend arbitrary_init_machine(root, env, mch) # init VARIABLES and eval INVARIANT assert isinstance(root.children[2], AInvariantMachineClause) assert interpret(root.children[2], env) assert env.get_value("X")==10 assert env.get_value("Y")==3
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_constraint_forAll2(self): # !x.(P=>Q) # Build AST: string_to_file("#PREDICATE f={(1,7),(2,8),(3,9)} & S={1,2,3} & !(x,y).(y:INTEGER &(x:S & f(x)<y) & y<42 =>y:T)", file_name) ast_string = file_to_AST_str(file_name) root = str_ast_to_python_ast(ast_string) # Test env = Environment() lst = [("S", PowerSetType(IntegerType())),("f", PowerSetType(CartType(PowerSetType(IntegerType()), PowerSetType(IntegerType()))))] type_with_known_types(root, env, lst, ["T"]) assert isinstance(get_type_by_name(env, "x"), IntegerType) assert isinstance(get_type_by_name(env, "y"), IntegerType) assert isinstance(get_type_by_name(env, "T"), PowerSetType) assert isinstance(get_type_by_name(env, "T").data, IntegerType) env.add_ids_to_frame(["f","S","T"]) env.set_value("f", frozenset([(1,7),(2,8),(3,9)])) env.set_value("S", frozenset([1,2,3])) env.set_value("T", frozenset(range(10,42))) env._min_int = -2**8 env._max_int = 2**8 unqantPred = root.children[0].children[1] assert isinstance(unqantPred, AForallPredicate) varList = unqantPred.children[0:-1] P = unqantPred.children[-1].children[0] Q = unqantPred.children[-1].children[1] domain = compute_using_external_solver(P, env, varList) assert frozenset([x["x"] for x in domain])==frozenset([1,2,3]) domain = compute_using_external_solver(P, env, varList) assert frozenset([x["y"] for x in domain])==frozenset(range(8,42))
def test_genAST_sub_let(self): string = ''' MACHINE Test VARIABLES SumR, DifferenceR, Var1, Var2 INVARIANT SumR:NAT & DifferenceR:NAT & Var1:NAT & Var2:NAT INITIALISATION BEGIN Var1:=2; Var2:=3; LET r1, r2 BE r1 = (Var1 + Var2) / 2 & r2 = (Var1 - Var2) / 2 IN SumR := r1 + r2 || DifferenceR := r1 - r2 END END END''' 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 = -1 env._max_int = 5 mch = parse_ast(root, env) type_check_bmch(root, env, mch) # also checks all included, seen, used and extend arbitrary_init_machine(root, env, mch) # init VARIABLES and eval INVARIANT assert isinstance(root.children[2], AInvariantMachineClause) assert interpret(root.children[2], env) assert env.get_value("SumR")==1 assert env.get_value("DifferenceR")==3 assert env.get_value("Var1")==2 assert env.get_value("Var2")==3
def test_genAST_pred_fun_app5(self): # Build AST: string_to_file("#PREDICATE f={(2,42),(1,777)} & #z.(z:NAT & 42=f(z))", file_name) ast_string = file_to_AST_str(file_name) root = str_ast_to_python_ast(ast_string) env = Environment() env._min_int = -2**8 env._max_int = 2**8 type_with_known_types(root.children[0], env, [], ["f"]) assert interpret(root,env)
def test_symbolic_sequences_last(self): # Build AST string_to_file("#PREDICATE last(%x.(x:NAT|x))=MAXINT", file_name) ast_string = file_to_AST_str(file_name) root = str_ast_to_python_ast(ast_string) # Test env = Environment() env._min_int = -1 env._max_int = 5 assert interpret(root, env)
def test_symbolic_sequences_rev(self): # Build AST string_to_file("#PREDICATE rev(%x.(x:NATURAL & x<5|x))={(2,4),(3,3),(4,2),(5,1),(6,0)}", file_name) ast_string = file_to_AST_str(file_name) root = str_ast_to_python_ast(ast_string) # Test env = Environment() env._min_int = -1 env._max_int = 5 assert interpret(root, env)
def test_symbolic_sequences_take(self): # Build AST string_to_file("#PREDICATE (%x.(x:NAT1|x+1))/|\\3=[2,3,4]", file_name) ast_string = file_to_AST_str(file_name) root = str_ast_to_python_ast(ast_string) # Test env = Environment() env._min_int = -1 env._max_int = 5 assert interpret(root, env)
def test_symbolic_sequences_drop(self): # Build AST string_to_file("#PREDICATE (%x.(x:NAT1|x+1))\\|/MAXINT-3=[MAXINT-1,MAXINT,MAXINT+1]", file_name) ast_string = file_to_AST_str(file_name) root = str_ast_to_python_ast(ast_string) # Test env = Environment() env._min_int = -1 env._max_int = 5 assert interpret(root, env)
def test_symbolic_sequences_seq_perm(self): # Build AST string_to_file("#PREDICATE %x.(x:{1,2,3} | %x.(x:{1,2,3}|x)):seq(perm({1,2,3}))", file_name) ast_string = file_to_AST_str(file_name) root = str_ast_to_python_ast(ast_string) # Test env = Environment() env._min_int = -1 env._max_int = 5 assert interpret(root, env)
def test_symbolic_sequences_conc(self): # Build AST string_to_file("#EXPRESSION (%x.(x:NAT1|x+1))^(%x.(x:NAT1|x*x))", file_name) ast_string = file_to_AST_str(file_name) root = str_ast_to_python_ast(ast_string) # Test env = Environment() env._min_int = -1 env._max_int = 5 assert interpret(root, env)
def test_symbolic_sequences_append(self): # Build AST # {(1,2)...(127,128),(128,42)} string_to_file("#EXPRESSION (%x.(x:NAT1|x+1))<-42", file_name) ast_string = file_to_AST_str(file_name) root = str_ast_to_python_ast(ast_string) # Test env = Environment() env._min_int = -1 env._max_int = 5 assert interpret(root, env)
def test_symbolic_sequences_prepend(self): # Build AST # {(1,32),(2,2)...(128,128)} string_to_file("#PREDICATE (42->(%x.(x:NAT1|x+1)))(128)=128 ", file_name) ast_string = file_to_AST_str(file_name) root = str_ast_to_python_ast(ast_string) # Test env = Environment() env._min_int = -1 env._max_int = 5 assert interpret(root, env)
def test_genAST_pred_forall2(self): # Build AST: string_to_file("#PREDICATE !(x,y,z).( x>0 & y>0 & z<4 => x+y=z)", file_name) ast_string = file_to_AST_str(file_name) root = str_ast_to_python_ast(ast_string) # Test env = Environment() env._min_int = -1 env._max_int = 5 type_with_known_types(root.children[0], env, [], []) assert not interpret(root.children[0], env)
def test_genAST_pred_exist3(self): # Build AST: string_to_file("#PREDICATE #(x,y).( x<0 & y>0 & y>x)", file_name) ast_string = file_to_AST_str(file_name) root = str_ast_to_python_ast(ast_string) # Test env = Environment() env._min_int = -1 env._max_int = 5 type_with_known_types(root.children[0], env, [], []) assert interpret(root.children[0], env)
def test_genAST_lambda(self): # Build AST string_to_file("#PREDICATE f="+"%"+"x.(x>0 & x<4|x*x)", file_name) ast_string = file_to_AST_str(file_name) root = str_ast_to_python_ast(ast_string) env = Environment() env._min_int = -2**8 env._max_int = 2**8 env.add_ids_to_frame(["f"]) type_with_known_types(root, env, [], ["f","x"]) env.set_value("f", frozenset([(1,1),(2,4),(3,9)])) assert interpret(root.children[0],env)
def test_genAST_lambda2(self): # Build AST string_to_file("#PREDICATE f="+"%"+"x,y,z.(x:1..2 & y:1..2 & z:1..2|x+y+z)", file_name) ast_string = file_to_AST_str(file_name) root = str_ast_to_python_ast(ast_string) env = Environment() env._min_int = -2**8 env._max_int = 2**8 env.add_ids_to_frame(["f"]) type_with_known_types(root, env, [], ["f","x"]) env.set_value("f", frozenset([(((1,1),1),3),(((1,1),2),4),(((1,2),1),4),(((2,1),1),4),(((1,2),2),5),(((2,2),1),5),(((2,1),2),5),(((2,2),2),6)])) assert interpret(root.children[0],env)
def test_genAST_pred_seq_of_seq(self): # Build AST: string_to_file("#PREDICATE s:perm(perm(S))", file_name) ast_string = file_to_AST_str(file_name) root = str_ast_to_python_ast(ast_string) env = Environment() env._min_int = -1 env._max_int = 5 env.add_ids_to_frame(["S","s"]) env.set_value("S", frozenset(["a","b"])) env.set_value("s", frozenset([(2, frozenset([(1, 'a'), (2, 'b')])), (1, frozenset([(2, 'a'), (1, 'b')]))])) assert interpret(root.children[0],env)
def test_symbolic_sequences_conc2(self): # Build AST string_to_file("#PREDICATE conc(%x.(x:{1,2,3} | %x.(x:{1,2,3}|x)))=[1,2,3,1,2,3,1,2,3]", file_name) ast_string = file_to_AST_str(file_name) root = str_ast_to_python_ast(ast_string) # Test env = Environment() env._min_int = -1 env._max_int = 5 assert interpret(root, env) # C578.EML.014/019_100 DontKnowIfEqualException
def test_genAST_pred_forall4(self): # Build AST: string_to_file("#PREDICATE S:POW(ID) & !(X,y).(X<:S => card(X)=y)", file_name) ast_string = file_to_AST_str(file_name) root = str_ast_to_python_ast(ast_string) # Test env = Environment() env._min_int = -1 env._max_int = 5 env.add_ids_to_frame(["S","ID"]) env.set_value("ID", frozenset(["a","b"])) env.set_value("S", frozenset(["a","b"])) lst = [("ID", PowerSetType(SetType("ID")))] type_with_known_types(root.children[0], env, lst, ["S"]) assert not interpret(root.children[0],env)
def test_constraint_set_comp(self): # {x|P} # Build AST: string_to_file("#PREDICATE card({x|x:NAT & x=12})=1", file_name) ast_string = file_to_AST_str(file_name) root = str_ast_to_python_ast(ast_string) # Test env = Environment() env._min_int = -2**8 env._max_int = 2**8 type_with_known_types(root, env, [], [""]) assert isinstance(get_type_by_name(env, "x"), IntegerType) setexpr = root.children[0].children[0].children[0] assert isinstance(setexpr, AComprehensionSetExpression) varList = setexpr.children[0:-1] P = setexpr.children[-1] assert isinstance(P, Predicate) domain = compute_using_external_solver(P, env, varList) assert frozenset([x["x"] for x in domain])==frozenset([12])
def test_constraint_pi(self): # PI (z).(P|E) # Build AST: string_to_file("#PREDICATE PI(x).(x:-4..4 & x/=0 | x)=576", file_name) ast_string = file_to_AST_str(file_name) root = str_ast_to_python_ast(ast_string) # Test env = Environment() env._min_int = -2**8 env._max_int = 2**8 type_with_known_types(root, env, [], [""]) assert isinstance(get_type_by_name(env, "x"), IntegerType) setexpr = root.children[0].children[0] assert isinstance(setexpr, AGeneralProductExpression) varList = setexpr.children[0:-2] P = setexpr.children[-2] E = setexpr.children[-1] assert isinstance(P, Predicate) domain = compute_using_external_solver(P, env, varList) assert frozenset([x["x"] for x in domain])==frozenset([-4,-3,-2,-1,1,2,3,4])
def test_constraint_ex(self): # #x.(P & Q) # Build AST: string_to_file("#PREDICATE #(z).((z:NAT & z>=2 & z<=5) & (z>1 & z<=10))", file_name) ast_string = file_to_AST_str(file_name) root = str_ast_to_python_ast(ast_string) # Test env = Environment() env._min_int = -1 env._max_int = 5 type_with_known_types(root, env, [], [""]) assert isinstance(get_type_by_name(env, "z"), IntegerType) exqantPred = root.children[0] assert isinstance(exqantPred, AExistsPredicate) varList = exqantPred.children[0:-1] P = exqantPred.children[-1].children[0] Q = exqantPred.children[-1].children[1] assert isinstance(P, Predicate) assert isinstance(Q, Predicate) domain = compute_using_external_solver(P, env, varList) assert frozenset([x["z"] for x in domain])== frozenset([2,3,4,5])
def test_examples_schneider_club(self): string = ''' MACHINE Club(capacity) CONSTRAINTS capacity : NAT1 & capacity <= 2 SETS NAME={billy, bobby} CONSTANTS total PROPERTIES total : NAT1 & total > 2 VARIABLES member, waiting INVARIANT member <: NAME & waiting <: NAME & member /\ waiting = {} & card(member) <= 4096 & card(waiting) <= total INITIALISATION member := {} || waiting := {} 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) # also checks all included, seen, used and extend arbitrary_init_machine(root, env, mch) # init VARIABLES and eval INVARIANT assert isinstance(root.children[6], AInvariantMachineClause) assert interpret(root.children[6], env) assert isinstance(root.children[1], AConstraintsMachineClause) assert interpret(root.children[1], env)
def test_genAST_set_GEN_INTER2(self): string = ''' MACHINE Test VARIABLES xx, E2 INVARIANT xx:NAT & E2:POW(NAT) INITIALISATION E2:={2,4} ; xx:: INTER (x1).(x1 : E2 | {y1 | y1 : NAT & y1 <= x1}) END''' 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 = -1 env._max_int = 5 mch = parse_ast(root, env) type_check_bmch(root, env, mch) # also checks all included, seen, used and extend arbitrary_init_machine(root, env, mch) # init VARIABLES and eval INVARIANT assert isinstance(root.children[2], AInvariantMachineClause) assert interpret(root.children[2], env) assert env.get_value("xx")==0 or env.get_value("xx")==1 or env.get_value("xx")==2
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_constraint_lambda(self): # %x.(P|E) # Build AST: string_to_file("#PREDICATE card(%x.(x:1..100|x*x))=100", file_name) ast_string = file_to_AST_str(file_name) root = str_ast_to_python_ast(ast_string) # Test env = Environment() env._min_int = -2**8 env._max_int = 2**8 type_with_known_types(root, env, [], [""]) assert isinstance(get_type_by_name(env, "x"), IntegerType) lambdaexpr = root.children[0].children[0].children[0] assert isinstance(lambdaexpr, ALambdaExpression) varList = lambdaexpr.children[0:-2] P = lambdaexpr.children[-2] E = lambdaexpr.children[-1] assert isinstance(P, Predicate) assert isinstance(E, Expression) domain = compute_using_external_solver(P, env, varList) #print domain assert frozenset([x["x"] for x in domain])== frozenset(range(1,100+1))
def test_genAST_sub_choice_by_primed(self): # Build AST string = ''' MACHINE Test VARIABLES xx INVARIANT xx:NAT INITIALISATION xx:=3 ; xx: (xx+xx$0>=5) END''' 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 = -1 env._max_int = 5 mch = parse_ast(root, env) type_check_bmch(root, env, mch) # also checks all included, seen, used and extend arbitrary_init_machine(root, env, mch) # init VARIABLES and eval INVARIANT assert isinstance(root.children[2], AInvariantMachineClause) assert interpret(root.children[2], env) assert env.get_value("xx")>=2 assert isinstance(get_type_by_name(env, "xx"), IntegerType)