Exemple #1
0
def test_basic_representation_queries():
    lang = generate_small_fstrips_bw_language(nblocks=5)
    clear, loc, b1, b2, b3 = lang.get('clear', 'loc', 'b1', 'b2', 'b3')
    x = lang.variable('x', lang.ns.block)

    assert rep.is_function_free(b1)
    assert rep.is_function_free(clear(b1))
    assert rep.is_function_free((clear(b1) & clear(b2)) | clear(b3))
    assert rep.is_function_free(exists(x, clear(x)))
    assert not rep.is_function_free(loc(b1) == b2)

    assert rep.is_conjunction_of_literals(clear(b1))
    assert rep.is_conjunction_of_literals(~clear(b1))
    assert rep.is_conjunction_of_literals(~~clear(b1))

    assert rep.is_conjunction_of_literals(clear(b1) & clear(b2))
    assert rep.is_conjunction_of_literals(clear(b1) & ~clear(b2))
    assert rep.is_conjunction_of_literals(clear(b1) & ~~clear(b2))

    assert rep.is_conjunction_of_literals(loc(b1) == b2)
    assert rep.is_conjunction_of_literals(loc(b1) != b2)
    assert rep.is_conjunction_of_literals(~(loc(b1) == b2))

    assert not rep.is_conjunction_of_literals(~(clear(b1) & clear(b2)))
    assert not rep.is_conjunction_of_literals((clear(b1) & clear(b2))
                                              | clear(b3))
    assert not rep.is_conjunction_of_literals(exists(x, clear(x)))
Exemple #2
0
def test_literal_collection():
    lang = generate_small_fstrips_bw_language(nblocks=5)
    clear, loc, b1, b2, b3 = lang.get('clear', 'loc', 'b1', 'b2', 'b3')
    x = lang.variable('x', lang.ns.block)

    assert rep.collect_literals_from_conjunction(clear(b1)) == {(clear(b1),
                                                                 True)}
    assert rep.collect_literals_from_conjunction(~clear(b1)) == {(clear(b1),
                                                                  False)}
    assert len(rep.collect_literals_from_conjunction(clear(b1)
                                                     & ~clear(b2))) == 2
    assert len(
        rep.collect_literals_from_conjunction(
            land(clear(b1), clear(b2), clear(b3)))) == 3

    assert len(
        rep.collect_literals_from_conjunction(clear(x) & clear(b1)
                                              & clear(x))) == 2

    # These ones are not conjunctions of literals, so should return None
    assert rep.collect_literals_from_conjunction(~(clear(b1)
                                                   & clear(b2))) is None
    assert rep.collect_literals_from_conjunction((clear(b1) & clear(b2))
                                                 | clear(b3)) is None
    assert rep.collect_literals_from_conjunction(exists(x, clear(x))) is None

    # ATM we don't want to deal with the complexity of nested negation, so we expect the method to return None for
    # "not not clear(b2)"
    assert rep.collect_literals_from_conjunction(clear(b1)
                                                 & ~~clear(b2)) is None
def test_ground_atom_transformation():
    lang = bw.generate_small_fstrips_bw_language()
    b1, b2, b3, clear, loc = lang.get('b1', 'b2', 'b3', 'clear', 'loc')
    atoms = transform_to_ground_atoms((clear(b1)) & (clear(b2)))
    assert atoms == [('clear', 'b1'), ('clear', 'b2')]

    with pytest.raises(TransformationError):
        transform_to_ground_atoms((clear(b1)) | (clear(b2)))
def test_nnf_double_negation():
    bw = blocksworld.generate_small_fstrips_bw_language()
    block = bw.get_sort('block')
    place = bw.get_sort('place')
    loc = bw.get_function('loc')
    clear = bw.get_predicate('clear')
    b1, b2, b3, b4 = [bw.get_constant('b{}'.format(k)) for k in range(1, 5)]
    table = bw.get_constant('table')

    phi = neg(neg(loc(b1) == loc(b2)))
    result = NNFTransformation.rewrite(phi)
    gamma = loc(b1) == loc(b2)

    assert str(result.nnf) == str(gamma)
def test_prenex_idempotency():
    bw = blocksworld.generate_small_fstrips_bw_language()
    block = bw.get_sort('block')
    place = bw.get_sort('place')
    loc = bw.get_function('loc')
    clear = bw.get_predicate('clear')
    b1, b2, b3, b4 = [bw.get_constant('b{}'.format(k)) for k in range(1, 5)]
    table = bw.get_constant('table')

    x = bw.variable('x', block)

    phi = loc(b1) == b2
    result = PrenexTransformation.rewrite(bw, phi)
    gamma = loc(b1) == b2

    assert str(result.prenex) == str(gamma)
def test_nnf_quantifier_flips():
    bw = blocksworld.generate_small_fstrips_bw_language()
    block = bw.get_sort('block')
    place = bw.get_sort('place')
    loc = bw.get_function('loc')
    clear = bw.get_predicate('clear')
    b1, b2, b3, b4 = [bw.get_constant('b{}'.format(k)) for k in range(1, 5)]
    table = bw.get_constant('table')

    x = bw.variable('x', block)

    phi = neg(exists(x, loc(x) == loc(b2)))
    result = NNFTransformation.rewrite(phi)
    gamma = forall(x, neg(loc(x) == loc(b2)))

    assert str(result.nnf) == str(gamma)
def test_builtin_negation_absorption():
    bw = blocksworld.generate_small_fstrips_bw_language()
    block = bw.get_sort('block')
    place = bw.get_sort('place')
    loc = bw.get_function('loc')
    clear = bw.get_predicate('clear')
    b1, b2, b3, b4 = [bw.get_constant('b{}'.format(k)) for k in range(1, 5)]
    table = bw.get_constant('table')

    x = bw.variable('x', block)

    phi = neg(loc(b1) == b2)
    psi = loc(b1) != b2

    r = NegatedBuiltinAbsorption.rewrite(bw, phi)
    assert str(r.formula) == str(psi)