Checking expected inferred factsΒΆ

In this example we can check if expected facts can be inferred from the ontology.

Consider this simple ontology:

@prefix example: <http://example.onto/> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .

example:Human rdfs:subClassOf example:Mortal .
example:Icaro a example:Human .

It says that Human is a sub class of Mortal, and that Icaro is a human. From this one can simply infer that Icaro is mortal.

Moreover, as the domain and range of the property rdfs:subClassOf is rdfs:Class and owl:Class is sub class of rdfs:Class, both Mortal and Human are instances of owl:Class. So, the expected facts are:

@prefix example: <http://example.onto/> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .

example:Icaro a example:Mortal .
example:Mortal a owl:Class .
example:Human a owl:Class .

To test if the expected facts are indeed inferred by the given ontology we can implement this simple Python code:

from diderot import DiderotTestCase, can_infer


class ExpectedFactsTestCase(DiderotTestCase):

    def test_check_expected_facts(self):
        EXPECTED_FACTS_FILE = "example/db/check_expected_facts/expected_facts.n3"
        ONTOLOGY_FILE = "example/db/check_expected_facts/ontology.n3"
        self.assert_that(can_infer(EXPECTED_FACTS_FILE).from_facts(ONTOLOGY_FILE))

Now consider that we add the triple example:Icaro a example:Student to the expected facts file.

It is quite obvious that this fact can not be inferred from the ontology. Then, when running the tests, an AssertionError will be raised with the expected facts that could not be inferred.

$ make test
======================================================================
FAIL: test_some_expected_facts_can_not_be_inferred (test.test_expected_facts.ExpectedFactsTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/path_to_test/test_expected_facts.py", line 16, in test_some_expected_facts_can_not_be_inferred
    self.assertThat(can_infer(EXPECTED_FACTS_FILE).from_facts(ONTOLOGY_FILE))
  File "/path_to_diderot/diderot/case.py", line 37, in assertThat
    raise AssertionError(ASSERTION_ERROR_MESSAGE.format(not_inferred_graph.serialize(format="nt")))
AssertionError: Could not infer some expected facts:
  <http://example.onto/Icaro> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.onto/Student>.

Previous topic

Examples

Next topic

Checking unexpected inferred facts

This Page