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 symref(p) == symref(o1) and symref(v) == symref(o2) v1, v2 = ext_p[0] assert symref(v1) == symref(o1) and symref(v2) == symref(o2)
def test_blocksworld_add(): lang = blocksworld.generate_small_fstrips_bw_language() model = Model(lang) clear = lang.get_predicate('clear') b1 = lang.get_constant('b1') b2 = lang.get_constant('b2') model.add(clear, b1) assert evaluate(clear(b1), model) is True assert evaluate(clear(b2), model) is False
def test_blocksworld_add_and_remove(): lang = tarski.benchmarks.blocksworld.generate_fstrips_bw_language() model = Model(lang) clear = lang.get_predicate('clear') b1 = lang.get_constant('b1') b2 = lang.get_constant('b2') model.add(clear, b1) model.add(clear, b2) model.remove(clear, b1) assert evaluate(clear(b1), model) is False
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_predicate_without_equality_reals(): import numpy lang = tarski.language(theories=[]) 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.setx(w(o1), 1.0) model.setx(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_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_extensions(): lang = tarski.language() pred = lang.predicate('pred', lang.Object, lang.Object) o1 = lang.constant("o1", lang.Object) o2 = lang.constant("o2", lang.Object) model = Model(lang) with pytest.raises(errors.ArityMismatch): # This should raise an error, as the predicate is binary model.add(pred) with pytest.raises(ValueError): # This should raise an error, as the predicate sort does not coincide with the sort of the parameters model.add(pred, 1, 2) model.add(pred, o1, o2) assert not model.holds(pred, (o2, o1)) # Make sure the order in which the elements were added is respected! assert model.holds(pred, (o1, o2)) with pytest.raises(KeyError): # This should raise an error, as the tuple does not belong to the predicate's extension model.remove(pred, o2, o1) model.remove(pred, o1, o2) with pytest.raises(KeyError): # This should raise an error, as the tuple has been removed model.remove(pred, o1, o2)