Esempio n. 1
0
    def test_genAST_pred_total_fun(self):
        # Build AST:
        string_to_file("#PREDICATE F=S-->T", file_name)
        ast_string = file_to_AST_str(file_name)
        root = str_ast_to_python_ast(ast_string)

        l = []
        l.append(frozenset([("a","x"),("b","x")]))
        l.append(frozenset([("a","y"),("b","y")]))
        l.append(frozenset([("a","x"),("b","y")]))
        l.append(frozenset([("a","y"),("b","x")]))
        env = Environment()
        env.add_ids_to_frame(["S","T","F"])
        env.set_value("S", frozenset(["a","b"]))
        env.set_value("T", frozenset(["x","y"]))
        env.set_value("F", frozenset(l))
        assert interpret(root.children[0],env)

        l = []
        l.append(frozenset([("1","hallo_welt"),("2","hallo_welt")]))
        env.set_value("S", frozenset(["1","2"]))
        env.set_value("T", frozenset(["hallo_welt",]))
        env.set_value("F", frozenset(l))
        assert interpret(root.children[0],env)
        
        l = []
        l.append(frozenset([("1","hallo_welt"),("2","hallo_welt")]))
        env.set_value("S", frozenset(["1","2"]))
        env.set_value("T", frozenset(["42",])) #no hallo_welt
        env.set_value("F", frozenset(l))
        assert not interpret(root.children[0],env) #False, total but wrong image
        
        # membership tests:
        env = Environment()
        env.add_ids_to_frame(["S"])
        env.set_value("S", frozenset([1,2]))
        
        string_to_file("#PREDICATE S={1,2} & {(1,1),(2,-1)}:S-->NAT", file_name)
        ast_string = file_to_AST_str(file_name)
        root = str_ast_to_python_ast(ast_string)
        assert not interpret(root,env)        

        string_to_file("#PREDICATE S={1,2} & {(1,1),(3,2)}:S-->NAT", file_name)
        ast_string = file_to_AST_str(file_name)
        root = str_ast_to_python_ast(ast_string)
        assert not interpret(root,env)    
                  
        string_to_file("#PREDICATE S={1,2} & {(1,1),(2,2)}:S-->NAT", file_name)
        ast_string = file_to_AST_str(file_name)
        root = str_ast_to_python_ast(ast_string)
        assert interpret(root,env)
Esempio n. 2
0
    def test_genAST_pred_rel_comp(self):
        # Build AST:
        string_to_file("#PREDICATE S= (p ; q)", file_name)
        ast_string = file_to_AST_str(file_name)
        root = str_ast_to_python_ast(ast_string)

        # Test
        env = Environment()
        env.add_ids_to_frame(["S","p","q"])
        env.set_value("S", frozenset([("1","x")]))
        env.set_value("p", frozenset([("1","a")]))
        env.set_value("q", frozenset([("a","x")]))
        assert interpret(root.children[0],env)

        env.set_value("S", frozenset([("1","a")]))
        env.set_value("p", frozenset([("1","x"),("2","y"),("3","z"),("1","y")]))
        env.set_value("q", frozenset([("x","a")]))
        assert interpret(root.children[0],env)

        env.set_value("S", frozenset([("1","a"),("2","a")]))
        env.set_value("p", frozenset([("1","x"),("2","x"),("3","z")]))
        env.set_value("q", frozenset([("x","a")]))
        assert interpret(root.children[0],env)
        
        # Build AST:
        string_to_file("#PREDICATE p={(1,\"a\")} & q={(\"a\",\"x\")} & {(1,\"x\")}=(p;q)", file_name)
        ast_string = file_to_AST_str(file_name)
        root = str_ast_to_python_ast(ast_string)
        
        # Test
        env = Environment()
        assert interpret(root,env)
 
        # Build AST:
        string_to_file("#PREDICATE p={(1,\"x\"),(2,\"y\"),(3,\"z\"),(1,\"y\")} & q={(\"x\",\"a\")} & {(1,\"a\")}=(p;q)", file_name)
        ast_string = file_to_AST_str(file_name)
        root = str_ast_to_python_ast(ast_string)
        
        # Test
        env = Environment()
        assert interpret(root,env)   
        
         # Build AST:
        string_to_file("#PREDICATE p={(1,\"x\"),(2,\"x\"),(3,\"z\")} & q={(\"x\",\"a\")} & {(1,\"a\"),(2,\"a\")}=(p;q) ", file_name)
        ast_string = file_to_AST_str(file_name)
        root = str_ast_to_python_ast(ast_string)
        
        # Test
        env = Environment()
        assert interpret(root,env)        
Esempio n. 3
0
    def test_examples_simple_bakery1(self):
        string ='''
        MACHINE Bakery1

        ABSTRACT_VARIABLES  p1, p2, y1, y2

        INVARIANT  
                p1:0..2 & p2:0..2 & y1:NATURAL & y2:NATURAL &
                (p1=2 => p2<2) &
                (p2=2 => p1<2) 

        INITIALISATION  p1,p2,y1,y2 := 0,0,0,0

        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()
        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)
Esempio n. 4
0
    def test_types_lambda2(self):
        # Build AST
        string_to_file("#PREDICATE f=" + "%" + "(x,y).(x=0 & y=10|TRUE)", file_name)
        ast_string = file_to_AST_str(file_name)
        root = str_ast_to_python_ast(ast_string)

        # Type
        env = Environment()
        type_with_known_types(root, env, [], ["f", "x", "y"])
        assert isinstance(get_type_by_name(env, "f"), PowerSetType)
        assert isinstance(get_type_by_name(env, "f").data, CartType)
        assert isinstance(get_type_by_name(env, "f").data.left, PowerSetType)
        assert isinstance(get_type_by_name(env, "f").data.right, PowerSetType)
        dom_type = get_type_by_name(env, "f").data.left
        img_type = get_type_by_name(env, "f").data.right  # only present if lambda is ass. to var
        assert isinstance(img_type.data, BoolType)
        assert isinstance(dom_type.data, CartType)
        assert isinstance(dom_type.data.left, PowerSetType)
        assert isinstance(dom_type.data.right, PowerSetType)
        assert isinstance(dom_type.data.left.data, IntegerType)
        assert isinstance(dom_type.data.right.data, IntegerType)
        lambda_node = root.children[0].children[1]
        assert isinstance(lambda_node, ALambdaExpression)
        image_type = env.get_lambda_type_by_node(lambda_node)  # this function always returns a type
        assert isinstance(image_type, BoolType)
Esempio n. 5
0
    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
Esempio n. 6
0
    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
Esempio n. 7
0
    def test_genAST_pred_rel_direct_prod(self):
        # Build AST:
        string_to_file("#PREDICATE f= p >< q", file_name)
        ast_string = file_to_AST_str(file_name)
        root = str_ast_to_python_ast(ast_string)

        # Test
        env = Environment()
        env.add_ids_to_frame(["q","p","f"])
        env.set_value("f", frozenset([]))
        env.set_value("p", frozenset([]))
        env.set_value("q", frozenset([]))
        assert interpret(root.children[0],env)

        env.set_value("f", frozenset([]))
        env.set_value("p", frozenset([("x","1"),("y","2")]))
        env.set_value("q", frozenset([("a","3"),("b","4")]))
        assert interpret(root.children[0],env)

        env.set_value("f", frozenset([("x",("1","3"))]))
        env.set_value("p", frozenset([("x","1"),("y","2")]))
        env.set_value("q", frozenset([("x","3"),("b","4")]))
        assert interpret(root.children[0],env)

        env.set_value("f", frozenset([(7,(11,20)), (2,(11,21))]))
        env.set_value("p", frozenset([(8,10), (7,11), (2,11), (6,12)]))
        env.set_value("q", frozenset([(1,20), (7,20), (2,21), (1,22)]))
        assert interpret(root.children[0],env)
Esempio n. 8
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
Esempio n. 9
0
 def test_types_complex_union_empty_set(self):          
     string = '''MACHINE Test
     SETS U = {g, h, i}; R={j,k,l}        
     CONSTANTS gg
     PROPERTIES
     gg : U +-> (R >+> R) & gg = { g |-> {j |-> l}, h |-> {k |-> k}, i |-> {}} 
     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()
     mch = parse_ast(root, env)
     type_check_bmch(root, env, mch)
     assert isinstance(get_type_by_name(env, "gg"), PowerSetType)
     assert isinstance(get_type_by_name(env, "gg").data, CartType)
     assert isinstance(get_type_by_name(env, "gg").data.left, PowerSetType)
     assert isinstance(get_type_by_name(env, "gg").data.right, PowerSetType)
     assert isinstance(get_type_by_name(env, "gg").data.left.data, SetType)
     assert get_type_by_name(env, "gg").data.left.data.name =="U"
     image_type = get_type_by_name(env, "gg").data.right.data.data
     assert isinstance(image_type, CartType)
     assert isinstance(image_type.left.data, SetType)
     assert isinstance(image_type.right.data, SetType)
     assert image_type.left.data.name=="R"
     assert image_type.right.data.name=="R"
Esempio n. 10
0
    def test_genAST_pred_rel_image(self):
        # Build AST:
        string_to_file("#PREDICATE f=r[S]", file_name)
        ast_string = file_to_AST_str(file_name)
        root = str_ast_to_python_ast(ast_string)

        # Test
        env = Environment()
        env.add_ids_to_frame(["S","r","f"])
        env.set_value("S", frozenset(["a"]))
        env.set_value("f", frozenset(["1"]))
        env.set_value("r", frozenset([("a","1"),("b","42"),("c","777")]))
        assert interpret(root.children[0],env)

        env.set_value("f", frozenset(["1","42"]))
        env.set_value("r", frozenset([("a","1"),("a","42"),("c","777")]))
        assert interpret(root.children[0],env)

        env.set_value("S", frozenset(["a","c"]))
        env.set_value("f", frozenset(["1","42","777"]))
        env.set_value("r", frozenset([("a","1"),("a","42"),("c","777")]))
        assert interpret(root.children[0],env)

        env.set_value("S", frozenset(["c"]))
        env.set_value("f", frozenset(["777"]))
        env.set_value("r", frozenset([("a","1"),("a","42"),("c","777")]))
        assert interpret(root.children[0],env)

        env.set_value("r", frozenset([(3,5), (3,9), (6,3), (9,2)]))
        env.set_value("S", frozenset([1, 2, 3]))
        env.set_value("f", frozenset([5,9]))
        assert interpret(root.children[0],env)
Esempio n. 11
0
    def test_genAST_pred_part_bij_fun(self):
        # Build AST
        string_to_file("#PREDICATE F=S>+>>T", file_name)
        ast_string = file_to_AST_str(file_name)
        root = str_ast_to_python_ast(ast_string)

        env = Environment()
        env.add_ids_to_frame(["S","T","F"])
        env.set_value("S", frozenset(["a","b"]))
        env.set_value("T", frozenset(["x","y","z"]))
        env.set_value("F", frozenset([]))
        assert interpret(root.children[0],env)

        env.set_value("S", frozenset(["a","b"]))
        env.set_value("T", frozenset(["x","y"]))
        l = [frozenset([("a","x"),("b","y")])]
        l.append(frozenset([("a","y"),("b","x")]))
        env.set_value("F", frozenset(l))
        assert interpret(root.children[0],env)

        env.set_value("S", frozenset(["a","b","c"]))
        env.set_value("T", frozenset(["x","y"]))
        l = [frozenset([("a","x"),("b","y")])]
        l.append(frozenset([("a","x"),("c","y")]))
        l.append(frozenset([("a","y"),("b","x")]))
        l.append(frozenset([("a","y"),("c","x")]))
        l.append(frozenset([("b","x"),("c","y")]))
        l.append(frozenset([("b","y"),("c","x")]))
        env.set_value("F", frozenset(l))
        assert interpret(root.children[0],env)
Esempio n. 12
0
    def test_types_seq_conc3(self):
        # Build AST
        string_to_file("#PREDICATE S=[[2, 5], [-1, -2, 9], [], [5]] & s=conc(S)", file_name)
        ast_string = file_to_AST_str(file_name)
        root = str_ast_to_python_ast(ast_string)

        # Type
        env = Environment()
        type_with_known_types(root, env, [], ["s", "S"])
        assert isinstance(get_type_by_name(env, "s"), PowerSetType)
        assert isinstance(get_type_by_name(env, "s").data, CartType)
        assert isinstance(get_type_by_name(env, "s").data.left, PowerSetType)
        assert isinstance(get_type_by_name(env, "s").data.right, PowerSetType)
        assert isinstance(get_type_by_name(env, "s").data.left.data, IntegerType)
        assert isinstance(get_type_by_name(env, "s").data.right.data, IntegerType)
        assert isinstance(get_type_by_name(env, "S"), PowerSetType)
        assert isinstance(get_type_by_name(env, "S").data, CartType)
        assert isinstance(get_type_by_name(env, "S").data.left, PowerSetType)
        assert isinstance(get_type_by_name(env, "S").data.right, PowerSetType)
        assert isinstance(get_type_by_name(env, "S").data.left.data, IntegerType)
        assert isinstance(get_type_by_name(env, "S").data.right.data.data, CartType)
        assert isinstance(get_type_by_name(env, "S").data.right.data.data.left, PowerSetType)
        assert isinstance(get_type_by_name(env, "S").data.right.data.data.right, PowerSetType)
        assert isinstance(get_type_by_name(env, "S").data.right.data.data.left.data, IntegerType)
        assert isinstance(get_type_by_name(env, "S").data.right.data.data.right.data, IntegerType)
Esempio n. 13
0
    def test_examples_no_query_op(self):
        string = '''
        MACHINE Query

        VARIABLES xx

        INVARIANT  xx:NAT

        INITIALISATION xx:=1
        
        OPERATIONS
        
        no_query = xx:=2

        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()
        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)
Esempio n. 14
0
 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()
Esempio n. 15
0
    def test_CartesianProductOverride(self):
        string = '''
        MACHINE CartesianProductOverride
        SETS
         S;T
        CONSTANTS a,b,c
        PROPERTIES
         /* Rule Hypotheses */
         a :  S <-> T &
         dom(a) = b &
         c <: T & 
        
         /* Rule Conclusion */
         not( a <+ b * c = b * c )
        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()
        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) # eval CONSTANTS and PROPERTIES
        assert isinstance(root.children[3], APropertiesMachineClause)
        assert interpret(root.children[3], env)
Esempio n. 16
0
    def test_examples_knights_knaves(self):
        string ='''
        MACHINE KnightsKnaves
        /* Puzzle from Smullyan:
        Knights: always tell the truth
        Knaves: always lie

        1: A says: “B is a knave or C is a knave”
        2: B says “A is a knight”

        What are A & B & C?
        */
        CONSTANTS A,B,C
        PROPERTIES
        A:BOOL & B:BOOL & C:BOOL /* TRUE if they are a Knight */
        &
        (A=TRUE <=> (B=FALSE or C=FALSE)) &
        (B=TRUE <=> A=TRUE)
        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()
        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) # search for CONSTANTS which make PROPERTIES True
        assert env.get_value("A") == True
        assert env.get_value("B") == True
        assert env.get_value("C") == False
Esempio n. 17
0
    def test_examples_simple_testset(self):
        string = '''
        MACHINE TestSet
        SETS
        ID={aa, bb, cc}

        CONSTANTS iv
        PROPERTIES
        iv:ID
        VARIABLES xx
        INVARIANT
        xx:ID
        INITIALISATION xx:=iv
        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()
        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[5], AInvariantMachineClause)
        assert interpret(root.children[5], env)
Esempio n. 18
0
    def test_types_simple_parprod(self):
        # Build AST
        string_to_file("#PREDICATE r1:A<->B & r2:C<->D & r3=(r1 || r2)", file_name)
        ast_string = file_to_AST_str(file_name)
        root = str_ast_to_python_ast(ast_string)

        # Type
        env = Environment()
        lst = [("A", PowerSetType(SetType("X"))),("B", PowerSetType(SetType("Y"))),("C", PowerSetType(SetType("M"))),("D", PowerSetType(SetType("N")))]
        type_with_known_types(root, env, lst, ["r1","r2","r3"])
        assert isinstance(get_type_by_name(env, "r3"), PowerSetType)
        assert isinstance(get_type_by_name(env, "r3").data, CartType)
        assert isinstance(get_type_by_name(env, "r3").data.left.data, CartType)
        assert isinstance(get_type_by_name(env, "r3").data.right.data, CartType)
        x = get_type_by_name(env, "r3").data.left.data.left.data
        y = get_type_by_name(env, "r3").data.left.data.right.data
        m = get_type_by_name(env, "r3").data.right.data.left.data
        n = get_type_by_name(env, "r3").data.right.data.right.data
        assert isinstance(x, SetType)
        assert isinstance(m, SetType)
        assert isinstance(y, SetType)
        assert isinstance(n, SetType)
        assert x.name == "X"
        assert y.name == "M"
        assert m.name == "Y"
        assert n.name == "N"
Esempio n. 19
0
 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))
Esempio n. 20
0
    def test_genAST_pred_simple_closure(self):
        # Build AST
        string_to_file("#PREDICATE f=closure(r)", file_name)
        ast_string = file_to_AST_str(file_name)
        root = str_ast_to_python_ast(ast_string)

        # Test
        env = Environment()
        env.add_ids_to_frame(["r","f"])
        env.set_value("f", frozenset([(1,3),(3,1),(1,1),(3,3)]))
        env.set_value("r", frozenset([(1,3),(3,1)]))
        assert interpret(root.children[0],env)

        env.set_value("f", frozenset([(1,7),(6,2),(8,4),(1,1),(6,6),(8,8),(2,2),(4,4),(7,7)]))
        env.set_value("r", frozenset([(1,7),(6,2),(8,4)]))
        assert interpret(root.children[0],env)

        env.set_value("f", frozenset([(1,7),(6,4),(8,4),(1,1),(6,6),(8,8),(4,4),(7,7)]))
        env.set_value("r", frozenset([(1,7),(6,4),(8,4)]))
        assert interpret(root.children[0],env)

        env.set_value("f", frozenset([(3,1),(1,1),(3,3)]))
        env.set_value("r", frozenset([(3,1),(1,1)]))
        assert interpret(root.children[0],env)

        env.set_value("f", frozenset([(1,3),(1,1),(3,3)]))
        env.set_value("r", frozenset([(1,3),(1,1)]))
        assert interpret(root.children[0],env)
Esempio n. 21
0
    def test_genAST_sub_case3(self):
        # Build AST
        string = '''
        MACHINE Test
        VARIABLES xx
        INVARIANT xx:NAT 
        INITIALISATION 
            BEGIN xx:=1; 
                CASE 1+1 OF 
                EITHER 4,5,6 THEN xx:=2 
                OR 7,8,9 THEN xx:=3
                ELSE xx:=4 END 
                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()
        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
Esempio n. 22
0
    def test_genAST_pred_rel_overriding(self):
        # Build AST:
        string_to_file("#PREDICATE f=r1 <+ r2", file_name)
        ast_string = file_to_AST_str(file_name)
        root = str_ast_to_python_ast(ast_string)

        # Test
        env = Environment()
        env.add_ids_to_frame(["r1","r2","f"])
        env.set_value("f", frozenset([("a","1"),("b","42"),("c","777"),("d","17")]))
        env.set_value("r1", frozenset([("d","17")]))
        env.set_value("r2", frozenset([("a","1"),("b","42"),("c","777")]))
        assert interpret(root.children[0],env)

        env.set_value("f", frozenset([("a","1"),("b","41"),("c","777"),("d","17")]))
        env.set_value("r2", frozenset([("d","17"),("b","41")]))
        env.set_value("r1", frozenset([("a","1"),("b","42"),("c","777")]))
        assert interpret(root.children[0],env)

        env.set_value("r1", frozenset([(2,7), (3,4), (5,1), (9,5)]))
        env.set_value("r2", frozenset([(3,5), (3,9), (6,3), (9,2)]))
        env.set_value("f", frozenset([(3,5), (3,9), (6,3), (9,2), (2,7), (5,1)]))
        assert interpret(root.children[0],env)
        
        env.set_value("f", frozenset([("a","1"),("b","42"),("c","777")]))
        env.set_value("r2", frozenset([]))
        env.set_value("r1", frozenset([("a","1"),("b","42"),("c","777")]))
        assert interpret(root.children[0],env)
        
        env.set_value("f", frozenset([("a","1"),("b","42"),("c","777")]))
        env.set_value("r2", frozenset([("a","1"),("b","42"),("c","777")]))
        env.set_value("r1", frozenset([]))
        assert interpret(root.children[0],env)
Esempio n. 23
0
    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
Esempio n. 24
0
    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  
Esempio n. 25
0
 def test_all_ids_known3(self):
     string_to_file("#PREDICATE #(x).(x>0 & x<10)", file_name)
     ast_string = file_to_AST_str(file_name)
     root = str_ast_to_python_ast(ast_string)
     
     env = Environment()
     assert all_ids_known(root, env)==False
Esempio n. 26
0
    def test_library_length(self):
        string = '''
        MACHINE LibraryStrings
        CONSTANTS length
        PROPERTIES
          /* compute the length of a string */
          length: STRING --> INTEGER &
          length = %x.(x:STRING|STRING_LENGTH(x)) 
        DEFINITIONS
          STRING_LENGTH(x) == length(x);
          EXTERNAL_FUNCTION_STRING_LENGTH == STRING --> INTEGER;
        ASSERTIONS
          length("abc") = 3;
          length("") = 0;
          length("hello") = 5
        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()
        dh = DefinitionHandler(env, str_ast_to_python_ast)                                   
        dh.repl_defs(root)
        mch = parse_ast(root, env)
        type_check_bmch(root, env, mch)
        assert isinstance(get_type_by_name(env, "length"), PowerSetType)
        assert isinstance(get_type_by_name(env, "length").data, CartType)
        assert isinstance(get_type_by_name(env, "length").data.left.data, StringType)
        assert isinstance(get_type_by_name(env, "length").data.right.data, IntegerType)
        arbitrary_init_machine(root, env, mch) # init VARIABLES and eval INVARIANT
        assert isinstance(root.children[4], AAssertionsMachineClause)
        interpret(root.children[4], env)
Esempio n. 27
0
    def test_genAST_set_gen_inter(self):
        # Build AST:
        string_to_file("#PREDICATE U:POW(POW(S)) & u=inter(U)", file_name)
        ast_string = file_to_AST_str(file_name)
        root = str_ast_to_python_ast(ast_string)

        # Test
        env = Environment()
        env.add_ids_to_frame(["S","U","u"])
        env.set_value("S", frozenset(["a","b","c"]))
        env.set_value("U", frozenset([frozenset(["a","b","c"])]))
        env.set_value("u", frozenset(["a","b","c"]))
        assert interpret(root.children[0],env)
        env.set_value("U", frozenset([frozenset(["a","b","c"]),frozenset(["a"])]))
        env.set_value("u", frozenset(["a"]))
        assert interpret(root.children[0],env)
        env.set_value("U", frozenset([frozenset(["a","b"]),frozenset(["c"])]))
        env.set_value("u", frozenset([]))
        assert interpret(root.children[0],env)
        env.set_value("U", frozenset([frozenset(["a","b"]),frozenset(["c"]),frozenset(["a"])]))
        env.set_value("u", frozenset([]))
        assert interpret(root.children[0],env)
        env.set_value("U", frozenset([frozenset(["a","b"]),frozenset(["c","b"]),frozenset(["a","b"])]))
        env.set_value("u", frozenset(["b"]))
        assert interpret(root.children[0],env)
Esempio n. 28
0
    def test_genAST_para_def(self):
        # Build AST
        string ='''
        MACHINE Test
        VARIABLES z
        INVARIANT z:MyType 
        INITIALISATION z:= Expr(2)
        DEFINITIONS
        Expr(X) == 1+X;
        MyType == NAT;
        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()
        dh = DefinitionHandler(env, str_ast_to_python_ast)
        dh.repl_defs(root)
        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
        invariant = root.children[2]
        assert isinstance(invariant, AInvariantMachineClause)
        assert interpret(invariant, env)
        assert env.get_value("z")==3
Esempio n. 29
0
    def test_genAST_subst_def2(self):
        string='''
        MACHINE Test
        VARIABLES z, b, x
        INVARIANT x:NAT & z:NAT & b:BOOL
        INITIALISATION x:=2 ; Assign(x+1, z) ; Assign(TRUE, b)
        DEFINITIONS Assign(Expr, VarName) == VarName := Expr;
        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()
        dh = DefinitionHandler(env, str_ast_to_python_ast)
        dh.repl_defs(root)
        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
        invariant = root.children[2]
        assert isinstance(invariant, AInvariantMachineClause)
        assert interpret(invariant, env)
        assert env.get_value("z")==3
        assert env.get_value("b")==True
        assert env.get_value("x")==2
Esempio n. 30
0
    def test_genAST_pred_seq_empty(self):
        # Build AST:
        string_to_file("#PREDICATE []={}", file_name)
        ast_string = file_to_AST_str(file_name)
        root = str_ast_to_python_ast(ast_string)

        env = Environment()
        assert interpret(root,env)