def test_model_list_extensions(): lang = tarski.language(theories=[]) p = lang.predicate('p', lang.Object, lang.Object) f = lang.function('f', lang.Object, lang.Object) o1 = lang.constant("o1", lang.Object) o2 = lang.constant("o2", lang.Object) model = Model(lang) model.evaluator = evaluate model.set(f, o1, o2) model.add(p, o1, o2) extensions = model.list_all_extensions() ext_f = extensions[f.signature] ext_p = extensions[p.signature] # We want to test that the `list_all_extensions` method correctly unwraps all TermReferences in the internal # representation of the model and returns _only_ actual Tarski terms. Testing this is a bit involved, as of # course we cannot just check for (o1, o2) in ext_f, because that will trigger the wrong __eq__ and __hash__ # methods - in other words, to test this we precisely need to wrap things back into TermReferences, so that we can # compare them properly assert len(ext_f) == 1 and len(ext_p) == 1 p, v = ext_f[0] assert TermReference(p) == TermReference(o1) and TermReference(v) == TermReference(o2) v1, v2 = ext_p[0] assert TermReference(v1) == TermReference(o1) and TermReference(v2) == TermReference(o2)
def test_numeric_function_set(): lang = numeric.generate_numeric_instance() lang.get_sort('particle') p1 = lang.get_constant('p1') x = lang.get_function('x') model = tarski.model.create(lang) model.set(x, p1, 1.0)
def test_blocksworld_set(): lang = blocksworld.generate_small_fstrips_bw_language() model = Model(lang) loc = lang.get_function('loc') b1, table = (lang.get_constant(s) for s in ('b1', 'table')) model.set(loc, b1, table) assert evaluate(loc(b1), model) == table
def test_blocksworld_set_via_square_brackets(): lang = blocksworld.generate_small_fstrips_bw_language() model = Model(lang) model.evaluator = evaluate loc = lang.get_function('loc') b1, table = (lang.get_constant(s) for s in ('b1', 'table')) model.set(loc, b1, table) assert model[loc(b1)] == table
def test_numeric_rel_formula_evaluation(): lang = numeric.generate_numeric_instance() p1 = lang.get_constant('p1') p2 = lang.get_constant('p2') x = lang.get_function('x') model = tarski.model.create(lang) model.evaluator = evaluate model.set(x, p1, 1.0) model.set(x, p2, 2.0) assert model[x(p1) < x(p2)]
def test_numeric_builtin_addition(): lang = numeric.generate_numeric_instance() p1 = lang.get_constant('p1') x = lang.get_function('x') model = tarski.model.create(lang) model.evaluator = evaluate model.set(x, p1, 1.0) expr = model[x(p1) + 1.0] assert isinstance(expr, Constant) assert expr.symbol == 2.0
def test_numeric_builtin_addition_int(): lang = language(theories=[Theory.EQUALITY, Theory.ARITHMETIC]) # The sorts particle = lang.sort('bowl') eggs = lang.function('eggs', lang.Object, lang.Integer) bowl_1 = lang.constant('bowl_1', particle) model = tarski.model.create(lang) model.evaluator = evaluate model.set(eggs(bowl_1), 1) expr = model[eggs(bowl_1) + 1] assert isinstance(expr, Constant) assert isinstance(expr.symbol, int) assert expr.symbol == 2
def test_predicate_without_equality(): lang = tarski.language(theories=[]) leq = lang.predicate('leq', lang.Integer, lang.Integer) f = lang.function('f', lang.Object, lang.Integer) o1 = lang.constant("o1", lang.Object) o2 = lang.constant("o2", lang.Object) model = Model(lang) model.evaluator = evaluate model.set(f, o1, 1) model.set(f, o2, 2) for x in range(0, 5): for y in range(x, 5): model.add(leq, x, y) assert model[leq(f(o1), f(o2))] is True assert model[leq(f(o2), f(o1))] is False
def test_model_as_atoms(): lang = tarski.language(theories=[]) p = lang.predicate('p', lang.Object, lang.Object) f = lang.function('f', lang.Object, lang.Object) o1 = lang.constant("o1", lang.Object) o2 = lang.constant("o2", lang.Object) model = Model(lang) model.evaluator = evaluate model.set(f, o1, o2) model.add(p, o1, o2) atoms = model.as_atoms() patom, fatom = atoms assert patom.is_syntactically_equal(p(o1, o2)) term, value = fatom assert term.is_syntactically_equal( f(o1)) and value.is_syntactically_equal(o2)
def test_predicate_without_equality_reals(): import numpy lang = tarski.language(theories=[Theory.ARITHMETIC]) leq = lang.predicate('leq', lang.Real, lang.Real) w = lang.function('w', lang.Object, lang.Real) o1 = lang.constant("o1", lang.Object) o2 = lang.constant("o2", lang.Object) model = Model(lang) model.evaluator = evaluate model.set(w(o1), 1.0) model.set(w(o2), 2.0) for x in numpy.arange(0.0, 5.0): for y in numpy.arange(x, 5.0): model.add(leq, x, y) assert model[leq(w(o1), w(o2))] is True assert model[leq(w(o2), w(o1))] is False
def test_ite(): lang = tarski.language('arith', [Theory.EQUALITY, Theory.ARITHMETIC]) c = 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) model = Model(lang) model.evaluator = evaluate model.set(x(), 1) model.set(y(), 2) assert model[tau].symbol == 5