def test_language_creation(): lang = theories.language() sorts = sorted(x.name for x in lang.sorts) assert sorts == ['object'] lang = fstrips.language("test", theories=[]) sorts = sorted(x.name for x in lang.sorts) assert sorts == ['object'] lang = fstrips.language("test") sorts = sorted(x.name for x in lang.sorts) assert sorts == ['object'] # The default equality theory should not import the arithmetic sorts either
def test_copying_and_equality(): lang = fstrips.language('arith', [Theory.EQUALITY, Theory.ARITHMETIC]) c = lang.constant(1, lang.Integer) x = lang.function('x', lang.Integer) y = lang.function('y', lang.Integer) _ = lang.function('f', lang.Integer, lang.Integer, lang.Integer) _ = copy.deepcopy(c) phi = (x() <= y()) & (y() <= x()) shallow = copy.copy(phi) deep = copy.deepcopy(phi) # phi3 = (x() <= y()) & (y() <= x()) assert isinstance(phi, CompoundFormula) assert isinstance(shallow, CompoundFormula) assert isinstance(deep, CompoundFormula) assert shallow.connective == phi.connective assert id(shallow.connective) == id(phi.connective) assert id(shallow.subformulas) == id(phi.subformulas) assert id(shallow.subformulas[0]) == id(phi.subformulas[0]) assert deep.connective == phi.connective assert id(deep.subformulas) != id(phi.subformulas) assert id(deep.subformulas[0]) != id(phi.subformulas[0])
def test_duplicate_detection_and_global_getter(): lang = fstrips.language("test") t1 = lang.sort('t1') c1 = lang.constant('c1', lang.Object) f1 = lang.function('f1', lang.Object) p1 = lang.predicate('p1') # Redeclaring things raises exceptions of appropriate types with pytest.raises(err.DuplicateSortDefinition): lang.sort('t1') with pytest.raises(err.DuplicateConstantDefinition): lang.constant('c1', lang.Object) with pytest.raises(err.DuplicateFunctionDefinition): lang.function('f1', lang.Object) with pytest.raises(err.DuplicatePredicateDefinition): lang.predicate('p1') # Declaring any language element with same name as a language element of a different type also raises exception with pytest.raises(err.DuplicateDefinition): lang.sort('p1') with pytest.raises(err.DuplicateDefinition): lang.constant('t1', lang.Object) with pytest.raises(err.DuplicateDefinition): lang.function('c1', lang.Object) with pytest.raises(err.DuplicateDefinition): lang.predicate('f1') assert id(lang.get('t1')) == id(t1) assert id(lang.get('c1')) == id(c1) assert id(lang.get('f1')) == id(f1) assert id(lang.get('p1')) == id(p1) assert len(lang.get('t1', 'c1', 'f1', 'p1')) == 4 assert (all(id(x) == id(y) for x, y in zip([t1, c1, f1, p1], lang.get('t1', 'c1', 'f1', 'p1'))))
def test_special_term_construction(): from tarski.syntax.arithmetic.special import max lang = fstrips.language(theories=[Theory.ARITHMETIC, Theory.SPECIAL]) ints = lang.Integer two, three = lang.constant(2, ints), lang.constant(3, ints) max_ = max(two, three) assert isinstance(max_, Term), "max_ should be the term max(Const(2), Const(3)), not the integer value 3"
def test_load_arithmetic_module_fails_when_language_frozen(): lang = fstrips.language(theories=[Theory.ARITHMETIC]) ints = lang.Integer two, three = lang.constant(2, ints), lang.constant(3, ints) with pytest.raises(err.DuplicateTheoryDefinition): # load_theory() should raise exception since arithmetic theory is already loaded theories.load_theory(lang, Theory.ARITHMETIC)
def test_complex_atom_from_expression_only_functions(): lang = fstrips.language('arith', [Theory.EQUALITY, Theory.ARITHMETIC]) x = lang.function('x', lang.Integer) y = lang.function('y', lang.Integer) z = lang.function('z', lang.Integer) phi = (x() <= y()) & (y() <= z()) assert isinstance(phi, CompoundFormula)
def test_arithmetic_terms_does_not_fail_with_load_theory(): lang = fstrips.language(theories=[Theory.ARITHMETIC]) ints = lang.Integer two, three = lang.constant(2, ints), lang.constant(3, ints) sum_ = two + three assert isinstance( sum_, Term ), "sum_ should be the term +(Const(2), Const(3)), not the integer value 5"
def test_matrices_constants(): lang = fstrips.language('test', [Theory.EQUALITY, Theory.ARITHMETIC]) A = lang.matrix([[0, 0, 1, 0], [0, 0, 0, 1], [0, 0, 0, 0], [0, 0, 0, 0]], lang.Real) assert isinstance(A, Matrix) N, M = A.shape for i in range(N): for j in range(M): assert isinstance(A.matrix[i, j], Term)
def test_term_refs(): lang = fstrips.language(theories=[Theory.ARITHMETIC]) _ = lang.function('f', lang.Object, lang.Integer) o1 = lang.constant("o1", lang.Object) o2 = lang.constant("o2", lang.Object) tr1 = symref(o1) tr2 = symref(o1) tr3 = symref(o2) assert tr1 == tr2 assert tr1 != tr3
def generate_atomic_bw_language(nblocks): """ A STRIPS encoding of BW with a single move action. """ lang = fstrips.language("blocksworld-atomic") lang.predicate('clear', 'object') lang.predicate('on', 'object', 'object') lang.predicate('diff', 'object', 'object') lang.constant('table', 'object') _ = [lang.constant(f'b{k}', 'object') for k in range(1, nblocks + 1)] return lang
def test_numeric_sort_deduction(): lang = fstrips.language(theories=[Theory.EQUALITY, Theory.ARITHMETIC]) plus0 = Constant(1, lang.Integer) + 1 # Disable the test temporarily until we address issue #93 # assert plus0.sort == lang.Integer # The sorts particle = lang.sort('bowl') eggs = lang.function('eggs', lang.Object, lang.Integer) bowl_1 = lang.constant('bowl_1', particle) plus1 = eggs(bowl_1) + 1
def test_prodterm(): from tarski.syntax.arithmetic import prodterm lang = fstrips.language('arith', [Theory.EQUALITY, Theory.ARITHMETIC]) _ = lang.constant(1, lang.Integer) _ = lang.constant('o1', lang.Object) _ = lang.constant('o2', lang.Object) x = lang.function('x', lang.Object, lang.Integer) y = lang.function('y', lang.Object, lang.Integer) o = lang.variable('o', lang.Object) product = prodterm(o, x(o) + y(o)) assert isinstance(product, AggregateCompoundTerm)
def test_object_function_arity(): fol = fstrips.language() block = fol.sort('block') # MRJ: Note that first argument is name of function, following arguments # denote the domain and codomain of the function. The previous definition # loc = fol.function('loc', 'block') # actually defines a 0-arity term, which denotes one element of the sort # block, or a mapping between the symbol loc() and a constant of sort block. # I think that what was intended, was an actual function like # loc: block -> block, a mapping between elements of the sort block. loc = fol.function('loc', 'block', 'block') b1 = fol.constant('b1', block) b2 = fol.constant('b2', block) _ = (loc(b1) == b2)
def test_term_refs_compound(): lang = fstrips.language(theories=[Theory.ARITHMETIC]) f = lang.function('f', lang.Object, lang.Integer) o1 = lang.constant("o1", lang.Object) o2 = lang.constant("o2", lang.Object) _ = lang.get('f') t1 = f(o1) t2 = f(o1) t3 = f(o2) assert t1.symbol == t2.symbol tr1 = symref(t1) tr2 = symref(t2) tr3 = symref(t3) assert tr1 == tr2 assert tr1 != tr3
def test_formula_refs(): lang = fstrips.language('arith', [Theory.EQUALITY, Theory.ARITHMETIC]) _ = lang.constant(1, lang.Integer) x = lang.function('x', lang.Integer) y = lang.function('y', lang.Integer) phi = (x() <= y()) & (y() <= x()) psi = (x() >= y()) & (y() <= x()) gamma = (x() <= y()) & (y() <= x()) fr1 = symref(phi) fr2 = symref(psi) fr3 = symref(gamma) assert fr1 == fr3 assert fr1 != fr2
def test_ite(): lang = fstrips.language('arith', [Theory.EQUALITY, Theory.ARITHMETIC]) _ = lang.constant(1, lang.Integer) x = lang.function('x', lang.Integer) y = lang.function('y', lang.Integer) phi = (x() <= y()) & (y() <= x()) t1 = x() + 2 t2 = y() + 3 tau = ite(phi, t1, t2) assert tau.condition.is_syntactically_equal(phi) assert tau.subterms[0].is_syntactically_equal(t1) assert tau.subterms[1].is_syntactically_equal(t2) assert tau.sort == t1.sort
def test_complex_atom_from_expression_function_and_constants(): lang = fstrips.language('artih', [Theory.EQUALITY, Theory.ARITHMETIC]) y = lang.function('y', lang.Integer) phi = (y() <= 4) & (-4 <= y()) assert isinstance(phi, CompoundFormula)
def test_equality_atom_from_expression(): lang = fstrips.language('arith', [Theory.EQUALITY, Theory.ARITHMETIC]) y = lang.function('y', lang.Integer) assert isinstance(y() == 4, Atom)
def generate_spider_language(): """ The FOL of the IPC18 Spider benchmark """ lang = fstrips.language("Spider", theories=[Theory.EQUALITY, Theory.ARITHMETIC]) lang.sort("cardposition", "object") lang.sort("card_or_tableau", "cardposition") lang.sort("card", "card_or_tableau") lang.sort("tableau", "card_or_tableau") lang.sort("deal", "cardposition") lang.constant("discard", "cardposition") # Domain constant lang.predicate("on", "card", "cardposition") lang.predicate("clear", "cardposition") lang.predicate("in-play", "card") lang.predicate("current-deal", "deal") # Note that the following predicates are defined in uppercase in the IPC PDDL, but we lowercase # for consistency with what the parser does lang.predicate("can-continue-group", "card", "cardposition") lang.predicate("can-be-placed-on", "card", "card") lang.predicate("is-ace", "card") lang.predicate("is-king", "card") lang.predicate("next-deal", "deal", "deal") lang.predicate("to-deal", "card", "tableau", "deal", "cardposition") lang.predicate("currently-dealing") lang.predicate("currently-collecting-deck") lang.predicate("collect-card", "cardposition") lang.predicate("part-of-tableau", "cardposition", "tableau") lang.predicate("movable", "card") lang.predicate("currently-updating-unmovable") lang.predicate("make-unmovable", "card") lang.predicate("currently-updating-movable") lang.predicate("make-movable", "cardposition") lang.predicate("currently-updating-part-of-tableau") lang.predicate("make-part-of-tableau", "card", "tableau") lang.function("total-cost", lang.Real) lang.constant("card-d0-s0-v0", "card") # The constants from the smallest instance p01.pddl lang.constant("card-d0-s0-v1", "card") lang.constant("card-d0-s0-v2", "card") lang.constant("card-d0-s1-v0", "card") lang.constant("card-d0-s1-v1", "card") lang.constant("card-d0-s1-v2", "card") lang.constant("card-d0-s2-v0", "card") lang.constant("card-d0-s2-v1", "card") lang.constant("card-d0-s2-v2", "card") lang.constant("card-d0-s3-v0", "card") lang.constant("card-d0-s3-v1", "card") lang.constant("card-d0-s3-v2", "card") lang.constant("pile-0", "tableau") lang.constant("pile-1", "tableau") lang.constant("pile-2", "tableau") lang.constant("deal-0", "deal") lang.constant("deal-1", "deal") lang.constant("deal-2", "deal") return lang
def test_special_terms_does_not_fail_with_load_theory(): from tarski.syntax.builtins import BuiltinFunctionSymbol lang = fstrips.language(theories=[Theory.ARITHMETIC, Theory.SPECIAL]) assert Theory.SPECIAL in lang.theories _ = lang.get_function(BuiltinFunctionSymbol.MAX)
def test_load_booleans(): lang = fstrips.language(theories=["boolean"]) assert len(list(lang.get('Boolean').domain())) == 2
def test_builtin_constants(): lang = fstrips.language(theories=[Theory.ARITHMETIC]) ints = lang.Integer two = lang.constant(2, ints) assert isinstance(two, Constant), "two should be the constant 2, not the integer value 2"