In this example we can check if unexpected inferred facts cannot be inferred from the ontology.
Consider this simple ontology (the same one used in Checking expected inferred facts):
@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.
We want to be sure that example:Icaro a example:Student cannot be inferred. We use the code below:
from diderot import DiderotTestCase, cannot_infer
class ExpectedFactsTestCase(DiderotTestCase):
def test_check_unexpected_facts(self):
UNEXPECTED_FACTS = "<http://example.onto/Icaro> a <http://example.onto/Student> ."
ONTOLOGY_FILE = "example/db/check_expected_facts/ontology.n3"
self.assert_that(cannot_infer(UNEXPECTED_FACTS).from_facts(ONTOLOGY_FILE))
EXPECTED_ANSWER = [(anyURI(), anyInt(), anyString())]
self.assert_that(can_answer(QUESTION).from_ontology(ONTOLOGY_FILE).with_answer(EXPECTED_ANSWER))
import sure
(5).should.be.an('int')
This is useful in a stage of ontology development that we have not added example:Student to the ontology yet but in the future it will exist, so this test will fail, or can be changed to use can_infer() instead of cannot_infer().
Now we want to see the test fail adding a fact we know it can be inferred, such as example:Icaro a example:Mortal ..
$ make test
======================================================================
FAIL: test_check_unexpected_facts_fail (test.test_unexpected_facts.ExpectedFactsTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/path/test_unexpected_facts.py", line 16, in test_check_unexpected_facts_fail
self.assert_that(cannot_infer(UNEXPECTED_FACTS).from_facts(ONTOLOGY_FILE))
File "/path/diderot/case.py", line 30, in assert_that
raise AssertionError(assertion.assertion_error_message)
AssertionError: Could infer some unexpected facts:
<http://example.onto/Icaro> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>
<http://example.onto/Mortal>.
----------------------------------------------------------------------