Esempio n. 1
0
def test_rabin_trivial_loop():
    a = trl.default_rabin_automaton()
    a.acceptance = 'Rabin(1)'
    a.declare_variables(x='bool')
    a.varlist['sys'] = ['x']
    # solve
    assert len(a.win['<>[]']) == 1
    assert len(a.win['[]<>']) == 1
    zk, yki, xkijr = gr1.solve_rabin_game(a)
    assert zk[-1] == a.bdd.true, zk
    # transducer
    gr1.make_rabin_transducer(zk, yki, xkijr, a)
    # vars
    assert '_goal' in a.vars, a.vars
    vt = a.vars['_goal']['type']
    assert vt == 'int', vt
    dom = a.vars['_goal']['dom']
    assert dom == (0, 0), dom
    assert '_hold' in a.vars, a.vars
    vt = a.vars['_hold']['type']
    assert vt == 'int', vt
    dom = a.vars['_hold']['dom']
    assert dom == (0, 1), dom
    assert 'x' in a.vars, a.vars
    # init
    init = a.init['impl_env']
    init_ = a.add_expr('(_goal = 0) /\ (_hold = 1)')
    assert init == init_, a.bdd.to_expr(init)
    # action
    action = a.action['impl']
    s = ("(_goal = 0) /\ (_goal' = 0) /\ " "(_hold' = 0)")
    action_ = a.add_expr(s)
    assert action == action_, a.bdd.to_expr(action)
Esempio n. 2
0
def test_rabin_with_safety_assumption():
    aut = trl.default_rabin_automaton()
    aut.declare_variables(x='bool')
    aut.varlist['env'] = ['x']
    aut.init['env'] = aut.add_expr('x')
    aut.action['env'] = aut.add_expr("x' ")
    aut.action['sys'] = aut.add_expr("x")
    # solve
    zk, yki, xkijr = gr1.solve_rabin_game(aut)
    win_set = zk[-1]
    assert win_set == aut.add_expr('x'), win_set
    # transducer
    gr1.make_rabin_transducer(zk, yki, xkijr, aut)
    assert action_refined(aut)
    init = aut.init['impl_env']
    s = 'x /\ (_goal = 0) /\ (_hold = 1)'
    init_ = aut.add_expr(s)
    assert init == init_, aut.bdd.to_expr(init)
    action = aut.action['impl']
    s = "((_goal = 0) /\ (_goal' = 0) /\ (_hold' = 0) /\ x)"
    action_ = aut.add_expr(s)
    assert action == action_, aut.bdd.to_expr(action)
    #
    # negate action to make unrealizable
    aut.action['sys'] = aut.add_expr(' ~ x')
    # solve
    zk, yki, xkijr = gr1.solve_rabin_game(aut)
    win_set = zk[-1]
    assert win_set == aut.false, aut.bdd.to_expr(win_set)
    with assert_raises(AssertionError):
        gr1.make_rabin_transducer(zk, yki, xkijr, aut)
Esempio n. 3
0
def test_rabin_persistence():
    aut = trl.default_rabin_automaton()
    aut.declare_variables(x='bool')
    aut.varlist['sys'] = ['x']
    aut.init['env'] = aut.add_expr(' ~ x')
    aut.win['<>[]'] = [aut.add_expr('x')]
    # solve
    zk, yki, xkijr = gr1.solve_rabin_game(aut)
    win_set = zk[-1]
    assert win_set == aut.true, aut.bdd.to_expr(win_set)
    # tranducer
    gr1.make_rabin_transducer(zk, yki, xkijr, aut)
    assert action_refined(aut)
    init = aut.init['impl_env']
    s = '(_goal = 0) /\ (_hold = 1) /\ ~ x'
    init_ = aut.add_expr(s)
    assert init == init_, aut.bdd.to_expr(init)
    action = aut.action['impl']
    s = ("(_goal = 0) /\ (_goal' = 0) /\ "
         "ite(_hold' = 1, ~ x /\ x', x /\ x')")
    action_ = aut.add_expr(s)
    assert action == action_, aut.bdd.to_expr(action)
    #
    # unrealizable
    aut.win['[]<>'] = [aut.add_expr(' ~ x')]
    zk, _, _ = gr1.solve_rabin_game(aut)
    win_set = zk[-1]
    assert win_set == aut.false, aut.bdd.to_expr(win_set)
Esempio n. 4
0
def test_rabin_persistence_2():
    aut = trl.default_rabin_automaton()
    aut.declare_variables(x='bool')
    aut.varlist['sys'] = ['x']
    aut.init['sys'] = aut.true
    aut.win['<>[]'] = [aut.add_expr('x'), aut.add_expr(' ~ x')]
    # solve
    zk, yki, xkijr = gr1.solve_rabin_game(aut)
    win_set = zk[-1]
    assert win_set == aut.true, aut.bdd.to_expr(win_set)
    # transducer
    gr1.make_rabin_transducer(zk, yki, xkijr, aut)
    assert action_refined(aut)
    init = aut.init['impl_env']
    s = '(_goal = 0) /\ (_hold = 2)'
    init_ = aut.add_expr(s)
    assert init == init_, aut.bdd.to_expr(init)
    action = aut.action['impl']
    # enumeration.dump_relation(action, aut)
    s = ("(_goal = 0) /\ (_goal' = 0) /\ "
         "ite(_hold = 1,"
         "(_hold' = 1) /\ ~ x',"
         "ite(_hold = 0,"
         "(_hold' = 0) /\ x',"
         "(_hold = 2) /\ ("
         "( (_hold' = 0) /\ x' ) \/ "
         "( (_hold' = 1) /\ ~ x' )"
         ") ) )")
    action_ = aut.add_expr(s)
    assert action == action_, aut.bdd.to_expr(action)
Esempio n. 5
0
def test_rabin_with_safety_assumption():
    aut = trl.default_rabin_automaton()
    aut.declare_variables(x='bool')
    aut.varlist['env'] = ['x']
    aut.init['env'] = aut.add_expr('x')
    aut.action['env'] = aut.add_expr("x' ")
    aut.action['sys'] = aut.add_expr("x")
    # solve
    zk, yki, xkijr = gr1.solve_rabin_game(aut)
    win_set = zk[-1]
    assert win_set == aut.add_expr('x'), win_set
    # transducer
    gr1.make_rabin_transducer(zk, yki, xkijr, aut)
    assert action_refined(aut)
    init = aut.init['impl_env']
    s = 'x /\ (_goal = 0) /\ (_hold = 1)'
    init_ = aut.add_expr(s)
    assert init == init_, aut.bdd.to_expr(init)
    action = aut.action['impl']
    s = "((_goal = 0) /\ (_goal' = 0) /\ (_hold' = 0) /\ x)"
    action_ = aut.add_expr(s)
    assert action == action_, aut.bdd.to_expr(action)
    #
    # negate action to make unrealizable
    aut.action['sys'] = aut.add_expr(' ~ x')
    # solve
    zk, yki, xkijr = gr1.solve_rabin_game(aut)
    win_set = zk[-1]
    assert win_set == aut.false, aut.bdd.to_expr(win_set)
    with assert_raises(AssertionError):
        gr1.make_rabin_transducer(zk, yki, xkijr, aut)
Esempio n. 6
0
def test_rabin_counter():
    aut = trl.default_rabin_automaton()
    aut.declare_variables(x='bool')
    aut.varlist['sys'] = ['x']
    aut.plus_one = False
    aut.qinit = '\A \A'
    aut.action['sys'] = aut.add_expr("x => ~ x' ")
    aut.win['[]<>'] = [aut.add_expr('x')]
    # solve
    zk, yki, xkijr = gr1.solve_rabin_game(aut)
    win_set = zk[-1]
    assert win_set == aut.bdd.true, aut.bdd.to_expr(win_set)
    # transducer
    gr1.make_rabin_transducer(zk, yki, xkijr, aut)
    assert action_refined(aut)
    init = aut.init['impl_env']
    s = '(_goal = 0) /\ (_hold = 1)'
    init_ = aut.add_expr(s)
    assert init == init_, aut.bdd.to_expr(init)
    action = aut.action['impl']
    # regression
    s = ("(_goal = 0) /\ (_goal' = 0) /\ "
         "(_hold' = 0) /\ "
         "ite(_hold = 0, x <=> ~ x', x => ~ x')")
    action_ = aut.add_expr(s)
    assert action == action_, aut.bdd.to_expr(action)
Esempio n. 7
0
def test_rabin_trivial_loop():
    a = trl.default_rabin_automaton()
    a.acceptance = 'Rabin(1)'
    a.declare_variables(x='bool')
    a.varlist['sys'] = ['x']
    # solve
    assert len(a.win['<>[]']) == 1
    assert len(a.win['[]<>']) == 1
    zk, yki, xkijr = gr1.solve_rabin_game(a)
    assert zk[-1] == a.bdd.true, zk
    # transducer
    gr1.make_rabin_transducer(zk, yki, xkijr, a)
    # vars
    assert '_goal' in a.vars, a.vars
    vt = a.vars['_goal']['type']
    assert vt == 'int', vt
    dom = a.vars['_goal']['dom']
    assert dom == (0, 0), dom
    assert '_hold' in a.vars, a.vars
    vt = a.vars['_hold']['type']
    assert vt == 'int', vt
    dom = a.vars['_hold']['dom']
    assert dom == (0, 1), dom
    assert 'x' in a.vars, a.vars
    # init
    init = a.init['impl_env']
    init_ = a.add_expr('(_goal = 0) /\ (_hold = 1)')
    assert init == init_, a.bdd.to_expr(init)
    # action
    action = a.action['impl']
    s = (
        "(_goal = 0) /\ (_goal' = 0) /\ "
        "(_hold' = 0)")
    action_ = a.add_expr(s)
    assert action == action_, a.bdd.to_expr(action)
Esempio n. 8
0
def test_rabin_persistence_2():
    aut = trl.default_rabin_automaton()
    aut.declare_variables(x='bool')
    aut.varlist['sys'] = ['x']
    aut.init['sys'] = aut.true
    aut.win['<>[]'] = [aut.add_expr('x'), aut.add_expr(' ~ x')]
    # solve
    zk, yki, xkijr = gr1.solve_rabin_game(aut)
    win_set = zk[-1]
    assert win_set == aut.true, aut.bdd.to_expr(win_set)
    # transducer
    gr1.make_rabin_transducer(zk, yki, xkijr, aut)
    assert action_refined(aut)
    init = aut.init['impl_env']
    s = '(_goal = 0) /\ (_hold = 2)'
    init_ = aut.add_expr(s)
    assert init == init_, aut.bdd.to_expr(init)
    action = aut.action['impl']
    # enumeration.dump_relation(action, aut)
    s = (
        "(_goal = 0) /\ (_goal' = 0) /\ "
        "ite(_hold = 1,"
        "(_hold' = 1) /\ ~ x',"
        "ite(_hold = 0,"
        "(_hold' = 0) /\ x',"
        "(_hold = 2) /\ ("
        "( (_hold' = 0) /\ x' ) \/ "
        "( (_hold' = 1) /\ ~ x' )"
        ") ) )")
    action_ = aut.add_expr(s)
    assert action == action_, aut.bdd.to_expr(action)
Esempio n. 9
0
def test_rabin_persistence():
    aut = trl.default_rabin_automaton()
    aut.declare_variables(x='bool')
    aut.varlist['sys'] = ['x']
    aut.init['env'] = aut.add_expr(' ~ x')
    aut.win['<>[]'] = [aut.add_expr('x')]
    # solve
    zk, yki, xkijr = gr1.solve_rabin_game(aut)
    win_set = zk[-1]
    assert win_set == aut.true, aut.bdd.to_expr(win_set)
    # tranducer
    gr1.make_rabin_transducer(zk, yki, xkijr, aut)
    assert action_refined(aut)
    init = aut.init['impl_env']
    s = '(_goal = 0) /\ (_hold = 1) /\ ~ x'
    init_ = aut.add_expr(s)
    assert init == init_, aut.bdd.to_expr(init)
    action = aut.action['impl']
    s = (
        "(_goal = 0) /\ (_goal' = 0) /\ "
        "ite(_hold' = 1, ~ x /\ x', x /\ x')")
    action_ = aut.add_expr(s)
    assert action == action_, aut.bdd.to_expr(action)
    #
    # unrealizable
    aut.win['[]<>'] = [aut.add_expr(' ~ x')]
    zk, _, _ = gr1.solve_rabin_game(aut)
    win_set = zk[-1]
    assert win_set == aut.false, aut.bdd.to_expr(win_set)
Esempio n. 10
0
def test_rabin_counter():
    aut = trl.default_rabin_automaton()
    aut.declare_variables(x='bool')
    aut.varlist['sys'] = ['x']
    aut.plus_one = False
    aut.qinit = '\A \A'
    aut.action['sys'] = aut.add_expr("x => ~ x' ")
    aut.win['[]<>'] = [aut.add_expr('x')]
    # solve
    zk, yki, xkijr = gr1.solve_rabin_game(aut)
    win_set = zk[-1]
    assert win_set == aut.bdd.true, aut.bdd.to_expr(win_set)
    # transducer
    gr1.make_rabin_transducer(zk, yki, xkijr, aut)
    assert action_refined(aut)
    init = aut.init['impl_env']
    s = '(_goal = 0) /\ (_hold = 1)'
    init_ = aut.add_expr(s)
    assert init == init_, aut.bdd.to_expr(init)
    action = aut.action['impl']
    # regression
    s = (
        "(_goal = 0) /\ (_goal' = 0) /\ "
        "(_hold' = 0) /\ "
        "ite(_hold = 0, x <=> ~ x', x => ~ x')")
    action_ = aut.add_expr(s)
    assert action == action_, aut.bdd.to_expr(action)
Esempio n. 11
0
def test_streett_with_liveness_assumption():
    aut = trl.default_streett_automaton()
    aut.declare_variables(x='bool', y=(0, 2))
    aut.varlist = dict(env=['x'], sys=['y'])
    aut.init['env'] = aut.add_expr('y < 2')
    aut.action['sys'] = aut.add_expr(
        """
        /\ ( ((y = 0) /\ ~ x) => (y' = 0) )
        /\ ((y = 0) => (y' < 2))
        /\ ((y = 1) => (y' = 0))
        /\ ((y = 2) => FALSE)
        /\ y \in 0..2
        """)
    aut.win['<>[]'] = [aut.add_expr(' ~ x')]
    aut.win['[]<>'] = [aut.add_expr('y = 1')]
    # solve
    z, yij, xijk = gr1.solve_streett_game(aut)
    z_ = aut.add_expr('y < 2')
    e = aut.bdd.to_expr(z)
    e_ = aut.bdd.to_expr(z_)
    assert z == z_, (e, e_)
    # transducer
    gr1.make_streett_transducer(z, yij, xijk, aut)
    assert action_refined(aut)
    init = aut.init['impl_env']
    assert init == aut.add_expr('(y < 2) /\ (_goal = 0)'), aut.bdd.to_expr(init)
    action = aut.action['impl']
    s = (
        "( (y = 0) => ite(x, (y' = 1), (y' = 0)) ) /\ "
        "( (y = 1) => (y' = 0) ) /\ "
        "( (_goal = 0) /\ (_goal' = 0) ) /\ "
        "( (y /= 2) /\ (y /= 3) )")
    action_ = aut.add_expr(s)
    sat = list(aut.bdd.pick_iter(action))
    sys_action = aut.action['sys']
    sys_action = gr1._copy_bdd(sys_action, aut.bdd, aut.bdd)
    u = aut.apply('=>', action, sys_action)
    assert u == aut.bdd.true, u
    assert action == action_, (action, action_, pprint.pprint(sat))
    #
    # test complement
    b = trl.default_rabin_automaton()
    b.acceptance = 'Rabin(1)'
    b.declare_variables(x='bool', y=(0, 2))
    b.varlist = dict(env=['y'], sys=['x'])
    b.action['env'] = gr1._copy_bdd(aut.action['sys'], aut.bdd, b.bdd)
    b.win['<>[]'] = [b.add_expr('y /= 1')]
    b.win['[]<>'] = [b.add_expr('x')]
    zk, yki, xkijr = gr1.solve_rabin_game(b)
    rabin_win_set = zk[-1]
    bdd = b.bdd
    streett_win_set = gr1._copy_bdd(z, aut.bdd, bdd)
    assert rabin_win_set == bdd.apply('not', streett_win_set)
    with assert_raises(AssertionError):
        gr1.make_rabin_transducer(zk, yki, xkijr, b)
Esempio n. 12
0
def test_streett_with_liveness_assumption():
    aut = trl.default_streett_automaton()
    aut.declare_variables(x='bool', y=(0, 2))
    aut.varlist = dict(env=['x'], sys=['y'])
    aut.init['env'] = aut.add_expr('y < 2')
    aut.action['sys'] = aut.add_expr("""
        /\ ( ((y = 0) /\ ~ x) => (y' = 0) )
        /\ ((y = 0) => (y' < 2))
        /\ ((y = 1) => (y' = 0))
        /\ ((y = 2) => FALSE)
        /\ y \in 0..2
        """)
    aut.win['<>[]'] = [aut.add_expr(' ~ x')]
    aut.win['[]<>'] = [aut.add_expr('y = 1')]
    # solve
    z, yij, xijk = gr1.solve_streett_game(aut)
    z_ = aut.add_expr('y < 2')
    e = aut.bdd.to_expr(z)
    e_ = aut.bdd.to_expr(z_)
    assert z == z_, (e, e_)
    # transducer
    gr1.make_streett_transducer(z, yij, xijk, aut)
    assert action_refined(aut)
    init = aut.init['impl_env']
    assert init == aut.add_expr('(y < 2) /\ (_goal = 0)'), aut.bdd.to_expr(
        init)
    action = aut.action['impl']
    s = ("( (y = 0) => ite(x, (y' = 1), (y' = 0)) ) /\ "
         "( (y = 1) => (y' = 0) ) /\ "
         "( (_goal = 0) /\ (_goal' = 0) ) /\ "
         "( (y /= 2) /\ (y /= 3) )")
    action_ = aut.add_expr(s)
    sat = list(aut.bdd.pick_iter(action))
    sys_action = aut.action['sys']
    sys_action = gr1._copy_bdd(sys_action, aut.bdd, aut.bdd)
    u = aut.apply('=>', action, sys_action)
    assert u == aut.bdd.true, u
    assert action == action_, (action, action_, pprint.pprint(sat))
    #
    # test complement
    b = trl.default_rabin_automaton()
    b.acceptance = 'Rabin(1)'
    b.declare_variables(x='bool', y=(0, 2))
    b.varlist = dict(env=['y'], sys=['x'])
    b.action['env'] = gr1._copy_bdd(aut.action['sys'], aut.bdd, b.bdd)
    b.win['<>[]'] = [b.add_expr('y /= 1')]
    b.win['[]<>'] = [b.add_expr('x')]
    zk, yki, xkijr = gr1.solve_rabin_game(b)
    rabin_win_set = zk[-1]
    bdd = b.bdd
    streett_win_set = gr1._copy_bdd(z, aut.bdd, bdd)
    assert rabin_win_set == bdd.apply('not', streett_win_set)
    with assert_raises(AssertionError):
        gr1.make_rabin_transducer(zk, yki, xkijr, b)
Esempio n. 13
0
def test_rabin_always_x():
    # always x
    aut = trl.default_rabin_automaton()
    aut.declare_variables(x='bool')
    aut.varlist['sys'] = ['x']
    aut.acceptance = 'Rabin(1)'
    aut.action['sys'] = aut.add_expr("x' ")
    # solve
    zk, yki, xkijr = gr1.solve_rabin_game(aut)
    win_set = zk[-1]
    assert win_set == aut.bdd.true, win_set
    # transducer
    gr1.make_rabin_transducer(zk, yki, xkijr, aut)
    # init
    init = aut.init['impl_env']
    s = '(_goal = 0) /\ (_hold = 1)'
    init_ = aut.add_expr(s)
    assert init == init_, aut.bdd.to_expr(init)
    # action
    action = aut.action['impl']
    s = (
        "(_goal = 0) /\ (_goal' = 0) /\ "
        "(_hold' = 0) /\ "
        "x' ")
    action_ = aut.add_expr(s)
    assert action == action_, aut.bdd.to_expr(action)
    #
    # always ~ x
    aut.init['env'] = aut.add_expr(' ~ x')
    aut.action['sys'] = aut.add_expr(' ~ x')
    # solve
    zk, yki, xkijr = gr1.solve_rabin_game(aut)
    win_set = zk[-1]
    win_set_ = aut.add_expr(' ~ x')
    assert win_set == win_set_, aut.bdd.to_expr(win_set)
    # transducer
    gr1.make_rabin_transducer(zk, yki, xkijr, aut)
    assert action_refined(aut)
    init = aut.init['impl_env']
    s = (
        '(_goal = 0) /\ (_hold = 1) /\ '
        ' ~ x')
    init_ = aut.add_expr(s)
    assert init == init_, aut.bdd.to_expr(init)
    action = aut.action['impl']
    s = (
        "(_goal = 0) /\ (_goal' = 0) /\ "
        "(_hold' = 0) /\ "
        " ~ x /\ ~ x'")
    action_ = aut.add_expr(s)
    assert action == action_, aut.bdd.to_expr(action)
Esempio n. 14
0
def test_rabin_always_x():
    # always x
    aut = trl.default_rabin_automaton()
    aut.declare_variables(x='bool')
    aut.varlist['sys'] = ['x']
    aut.acceptance = 'Rabin(1)'
    aut.action['sys'] = aut.add_expr("x' ")
    # solve
    zk, yki, xkijr = gr1.solve_rabin_game(aut)
    win_set = zk[-1]
    assert win_set == aut.bdd.true, win_set
    # transducer
    gr1.make_rabin_transducer(zk, yki, xkijr, aut)
    # init
    init = aut.init['impl_env']
    s = '(_goal = 0) /\ (_hold = 1)'
    init_ = aut.add_expr(s)
    assert init == init_, aut.bdd.to_expr(init)
    # action
    action = aut.action['impl']
    s = ("(_goal = 0) /\ (_goal' = 0) /\ " "(_hold' = 0) /\ " "x' ")
    action_ = aut.add_expr(s)
    assert action == action_, aut.bdd.to_expr(action)
    #
    # always ~ x
    aut.init['env'] = aut.add_expr(' ~ x')
    aut.action['sys'] = aut.add_expr(' ~ x')
    # solve
    zk, yki, xkijr = gr1.solve_rabin_game(aut)
    win_set = zk[-1]
    win_set_ = aut.add_expr(' ~ x')
    assert win_set == win_set_, aut.bdd.to_expr(win_set)
    # transducer
    gr1.make_rabin_transducer(zk, yki, xkijr, aut)
    assert action_refined(aut)
    init = aut.init['impl_env']
    s = ('(_goal = 0) /\ (_hold = 1) /\ ' ' ~ x')
    init_ = aut.add_expr(s)
    assert init == init_, aut.bdd.to_expr(init)
    action = aut.action['impl']
    s = ("(_goal = 0) /\ (_goal' = 0) /\ " "(_hold' = 0) /\ " " ~ x /\ ~ x'")
    action_ = aut.add_expr(s)
    assert action == action_, aut.bdd.to_expr(action)
Esempio n. 15
0
def synthesize_rabin_controller(b):
    """Return enumerated graph with steps as edges.
     after solving Rabin(1) game.

    @param b: game with <>[] & []<> winning
    @type b: `symbolic.Automaton`
    """
    qinit = b.qinit
    aut_rabin = b.build()
    zk, yki, xkijr = gr1.solve_rabin_game(aut_rabin)
    t= gr1.make_rabin_transducer(zk, yki, xkijr, aut_rabin)
    g = enum.action_to_steps(t, qinit=qinit)
    return g