Here we document Diderot’s programming interface
Diderot’s main class. To use our framework your tests cases should extend this class.
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))
Function that uses a assertion object (marvin.assertion.Assertion) to check if the test will pass or not.
Users should use this function instead of the usual assertEqual() or assertTrue() from unittest.TestCase.
Assertion objects can be created by using utilitary functions such as can_infer().
If the passed object is not an Assertion, a RuntimeError is raised.
Method that initiates the method chaining by constructing an InferenceAssertion object with the given expected facts.
can_infer(":Icaro a :Mortal")
This function is in diderot.__init__, so it can be imported simply with from diderot import can_infer.
Method that initiates the method chaining by constructing an CompetencyQuestionAssertion object with a SPARQL query.
can_answer("SELECT ?human WHERE { ?human a <http://example.onto/Human> }")
This function is in diderot.__init__, so it can be imported simply with from diderot import can_answer.
Generic class for assertion
Initializes assertion_value as False and assertion_error_message as None.
These are the two parameters used in client classes, such as diderot.DiderotTestCase.
Class that holds inference assertion values, facts (rdflib.Graph objects), with known facts and expected inferred facts.
This class also triggers the inference, on inference module.
The constructor for InferenceAssertion generally gets a expected_facts object as argument, as this is the use in can_infer function.
Known facts (hereby called facts) can be also passed.
self.assertion_value is initialized as False.
This function is part of the method chaining and receives facts as argument, so that the inference is triggered using self.expected_facts and facts.
As part of the method chaining this function returns the object itself, after running the inference process, which updates the self.assertion_value member.
can_infer(":Icaro a :Mortal").from_facts(":Icaro a :Human . :Human rdfs:subClassOf :Mortal")
Class that holds assertion values for competency question answering.
Write something
This function is part of the method chaining and receives the ontology as argument.
can_answer("SELECT * WHERE {?s ?p ?o}").from_ontology(":Icaro a :Human . :Human rdfs:subClassOf :Mortal")
If the query to the selected ontology returns True (for ASK queries) or a non-empty result (for SELECT queries), self.assertion_value is set to True. Otherwise, self.assertion_value is set to False.
If the query is not a ASK or SELECT query, a RuntimeError is raised.
As part of the method chaining this function returns the object itself.
Matches the expected_answers given as parameter with the self.query_result.
Note: It is required that the expected_answers list of tuples is in the same order of the variables passed in the SELECT query.
It is also required that references to URIs must be passed in expected_answers as a rdflib.URIRef.
question = """
SELECT ?human ?age ?name
WHERE {
?human a <http://example.onto/Human> ;
<http://example.onto/age> ?age ;
<http://example.onto/name> ?name .
}
"""
expected_answers = [(rdflib.URIRef("http://example.onto/Human"), "Icaro", 26)]
can_infer(QUESTION).from_ontology(ONTOLOGY).with_answer(expected_answers)
A RuntimeError will be raised if:
Utility function that loads RDFS and OWL semantics in a FuXi RETE.Network object.
This attribute is loaded on diderot import to build RDFS and OWL rules only one time.
Class that wraps FuXi RETE.Network to hold the network of facts, trigger inference, and retrieve inferred facts.
The Inference object is loaded with RDFS and OWL rules (diderot.inference.RDFS_OWL_RULES) and an empty graph to hold inferred facts.
Add facts (a RDFlib.Graph object) to inference network thus triggering the inference.
Get inferred facts from network.
Utility function that compares graph1 and graph2 to return a graph that is the difference between graph1 and graph2.
If any parameter is None or if graph1 is empty a RuntimeError is raised.
Utility function to create an empty RDFlib.Graph with the OWL namespace enabled.
Utility function that compares graph1 and graph2 to return a graph that is the intersection between graph1 and graph2.
If any parameter is None or if graph1 is empty a RuntimeError is raised.
Utility function to check if a given RDFlib.Graph is empty.
Utility function to parse facts in multiple formats.
Available formats for input: a string URI, a string relative file path, a string in N3/Turtle format, or a rdflib.Graph object.
A RuntimeError will be raised if the input is:
The optional parameters begin and end are used to extract a fragment of the original file, if the input is a relative file path.
Utility function that given a file path return the rdflib.Graph correspondent to the file content.
The optional parameters begin and end are used to extract a fragment of the original file.
If parameters begin or end are not positive integers a RuntimeError will be raised.
Utility function that, given a string in N3/Turtle format will parse it and return the equivalent rdflib.Graph object.
Utility model to create OWL namespace, not include in RDFlib 2.4.2, so that users can refer to OWL constructs easily.
from diderot import OWL
from diderot.utils import get_empty_graph
graph = get_empty_graph
graph.add((URIRef(":Mortal"), RDF.type, OWL.Class))