def test_difference(self): larger_graph = get_empty_graph() larger_graph.add((URIRef(":Icaro"), RDF.type, URIRef(":Mortal"))) larger_graph.add((URIRef(":Mortal"), RDF.type, OWL.Class)) larger_graph.add((URIRef(":Human"), RDF.type, OWL.Class)) subset_graph = get_empty_graph() subset_graph.add((URIRef(":Icaro"), RDF.type, URIRef(":Mortal"))) self.assertTrue(is_empty_graph(difference(subset_graph, larger_graph)))
def test_intersection(self): larger_graph = get_empty_graph() larger_graph.add((URIRef(":Icaro"), RDF.type, URIRef(":Mortal"))) larger_graph.add((URIRef(":Mortal"), RDF.type, OWL.Class)) larger_graph.add((URIRef(":Human"), RDF.type, OWL.Class)) subset_graph = get_empty_graph() subset_graph.add((URIRef(":Icaro"), RDF.type, URIRef(":Mortal"))) result = intersection(subset_graph, larger_graph) expected = set(subset_graph) self.assertEqual(result, expected)
def test_subclass_assertion(self): facts = get_empty_graph() facts.add((URIRef(":Human"), RDFS.subClassOf, URIRef(":Mortal"))) facts.add((URIRef(":Icaro"), RDF.type, URIRef(":Human"))) expected_facts = get_empty_graph() expected_facts.add((URIRef(":Icaro"), RDF.type, URIRef(":Mortal"))) expected_facts.add((URIRef(":Mortal"), RDF.type, OWL.Class)) expected_facts.add((URIRef(":Human"), RDF.type, OWL.Class)) assertion = can_infer(expected_facts).from_facts(facts) self.assertTrue(assertion.assertion_value)
def test_subclass_assertion(self, get_inferred_facts, add_facts, inference_init): facts = get_empty_graph() facts.add((URIRef(":Human"), RDFS.subClassOf, URIRef(":Mortal"))) facts.add((URIRef(":Icaro"), RDF.type, URIRef(":Human"))) assertion = cannot_infer(self.UNEXPECTED_FACTS).from_facts(facts) self.assertTrue(assertion.assertion_value)
def test_subclass_assertion_is_false(self, get_inferred_facts, add_facts, inference_init): facts = get_empty_graph() facts.add((URIRef(":Human"), RDFS.subClassOf, URIRef(":Mortal"))) assertion = can_infer(self.EXPECTED_FACTS).from_facts(facts) self.assertFalse(assertion.assertion_value)
def __init__(self): """ The ``Inference`` object is loaded with ``RDFS`` and ``OWL`` rules (``diderot.inference.RDFS_OWL_RULES``) and an empty graph to hold inferred facts. """ self.network = RDFS_OWL_RULES self.network.inferredFacts = get_empty_graph()
def test_difference_not_empty(self): larger_graph = get_empty_graph() larger_graph.add((URIRef(":Icaro"), RDF.type, URIRef(":Mortal"))) larger_graph.add((URIRef(":Mortal"), RDF.type, OWL.Class)) larger_graph.add((URIRef(":Human"), RDF.type, OWL.Class)) subset_graph = get_empty_graph() subset_graph.add((URIRef(":Icaro"), RDF.type, URIRef(":Mortal"))) subset_graph.add((URIRef(":Icaro"), RDF.type, URIRef(":Student"))) # Not in larger_graph difference_expected_graph = get_empty_graph() difference_expected_graph.add((URIRef(":Icaro"), RDF.type, URIRef(":Student"))) # Not in larger_graph difference_expected_graph_set = set(difference_expected_graph) difference_result_graph_set = difference(subset_graph, larger_graph) self.assertEqual(len(difference_result_graph_set), 1) self.assertEqual(difference_expected_graph_set, difference_result_graph_set)
def test_result_to_construct_query_raises_exception(self): mocked_graph = MagicMock() mocked_graph.query = MagicMock(return_value=SPARQLQueryResult(get_empty_graph())) # Graph object with patch("diderot.assertion.parse_facts", return_value=mocked_graph): try: can_answer("SELECT { ?s a ?o }").from_ontology("A_ONTOLOGY") except RuntimeError: pass else: self.fail("RuntimeError not raised")
def _build_assertion_error_message(self): """ Method that builds the AssertionError message. All expected and not inferred facts are built in a ``RDFlib.Graphh`` object and then serialized in NT format. """ ASSERTION_ERROR_MESSAGE = "Could infer some unexpected facts:\n\n {0}" unexpected_facts_graph = get_empty_graph() for triple in self.unexpected_inferred_facts: unexpected_facts_graph.add(triple) self.assertion_error_message = ASSERTION_ERROR_MESSAGE.format(unexpected_facts_graph.serialize(format="nt"))
def test_from_ttl_to_string(self): expected_graph = get_empty_graph() expected_graph = add_example_namespace(expected_graph) expected_graph.add((EXAMPLE.Icaro, RDF.type, EXAMPLE.Mortal)) expected_graph.add((EXAMPLE.Mortal, RDF.type, OWL.Class)) expected_graph.add((EXAMPLE.Human, RDF.type, OWL.Class)) expected_triples_list = graph_to_list_of_triples(expected_graph) result = parse_ttl_string(TTL_STRING) result = graph_to_list_of_triples(result) self.assertItemsEqual(result, expected_triples_list)
def test_parse_facts_rdflib_graph(self): graph = get_empty_graph() graph.add((URIRef(":Icaro"), RDF.type, URIRef(":Mortal"))) self.assertEqual(graph, parse_facts(graph))
from diderot import OWL from diderot.utils import parse_ttl_string, parse_ttl_file, get_empty_graph,\ parse_facts, is_empty_graph, difference, intersection from tests.utils import graph_to_list_of_triples, add_example_namespace, EXAMPLE TTL_STRING = """@prefix : <http://example.onto/> . @prefix owl: <http://www.w3.org/2002/07/owl#> . :Icaro a :Mortal . :Mortal a owl:Class . :Human a owl:Class .""" NON_EMPTY_GRAPH = get_empty_graph() NON_EMPTY_GRAPH.add((EXAMPLE.Icaro, RDF.type, EXAMPLE.Mortal)) class UtilsTestCase(TestCase): def test_from_ttl_to_string(self): expected_graph = get_empty_graph() expected_graph = add_example_namespace(expected_graph) expected_graph.add((EXAMPLE.Icaro, RDF.type, EXAMPLE.Mortal)) expected_graph.add((EXAMPLE.Mortal, RDF.type, OWL.Class)) expected_graph.add((EXAMPLE.Human, RDF.type, OWL.Class)) expected_triples_list = graph_to_list_of_triples(expected_graph) result = parse_ttl_string(TTL_STRING)
def test_difference_empty_subgraph(self): larger_graph = get_empty_graph() subset_graph = get_empty_graph() self.assertRaises(RuntimeError, difference, subset_graph, larger_graph)
def test_difference_larger_graph_None(self): subset_graph = get_empty_graph() self.assertRaises(RuntimeError, difference, subset_graph, None)
def test_intersection_empty_graphs(self): larger_graph = get_empty_graph() subset_graph = get_empty_graph() self.assertRaises(RuntimeError, intersection, subset_graph, larger_graph)
def test_intersection_larger_graph_None(self): subset_graph = get_empty_graph() self.assertRaises(RuntimeError, intersection, subset_graph, None)
def test_is_not_empty_graph(self): graph = get_empty_graph() graph.add((URIRef(":Icaro"), RDF.type, URIRef(":Mortal"))) self.assertFalse(is_empty_graph(graph))
def test_is_empty_graph(self): graph = get_empty_graph() self.assertTrue(is_empty_graph(graph))
def test_intersection_subset_graph_None(self): larger_graph = get_empty_graph() self.assertRaises(RuntimeError, difference, None, larger_graph)
def test_parse_facts_with_empty_rdflib_graph(self): graph = get_empty_graph() self.assertRaises(RuntimeError, parse_facts, graph)