def can_infer(expected_facts): """ Method that initiates the method chaining by constructing an ``InferenceAssertion`` object with the given expected facts. .. code-block:: python can_infer(":Icaro a :Mortal") This function is in ``diderot.__init__``, so it can be imported simply with ``from diderot import can_infer``. """ expected_facts = parse_facts(expected_facts) return InferenceAssertion(expected_facts=expected_facts)
def from_facts(self, facts): """ 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. .. code-block:: python can_infer(":Icaro a :Mortal").from_facts(":Icaro a :Human . :Human rdfs:subClassOf :Mortal") """ self.facts = parse_facts(facts) self._infer() return self
def from_ontology(self, ontology): """ This function is part of the method chaining and receives the ontology as argument. .. code-block:: python 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. """ ontology_graph = parse_facts(ontology) query_result = ontology_graph.query(self.question) if query_result.construct: raise RuntimeError("Only SELECT or ASK queries are accepted") if query_result.askAnswer: self.assertion_value = query_result.askAnswer[0] if not self.assertion_value: ASSERTION_ERROR_MESSAGE = "ASK query returned false.\n Query: {0}" self.assertion_error_message = ASSERTION_ERROR_MESSAGE.format(self.question) elif query_result.allVariables is not None: self.assertion_value = len(query_result.selected) > 0 if not self.assertion_value: ASSERTION_ERROR_MESSAGE = "SELECT query result is empty.\n Query: {0}" self.assertion_error_message = ASSERTION_ERROR_MESSAGE.format(self.question) else: self.query_result = query_result else: raise RuntimeError("Unexpected exception parsing SPARQL query results:\n {0}".format(self.query_result)) return self
def test_parse_facts_invalid_type(self): self.assertRaises(RuntimeError, parse_facts(10))
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))
def test_parse_facts_from_string(self, parse_ttl_string): mocked_open = mock_open() mocked_open.side_effect = IOError with patch("diderot.utils.urlopen", mocked_open): parse_facts(TTL_STRING) parse_ttl_string.assert_called_with(TTL_STRING)
def test_parse_facts_from_file_path(self, parse_ttl_file): parse_facts("db/test.n3", begin=2, end=3) parse_ttl_file.assert_called_with(os.getcwd() + "/db/test.n3", 2, 3)
def test_parse_facts_from_uri(self, parse_ttl_string): mocked_open = mock_open() mocked_open.return_value = StringIO(TTL_STRING) with patch("diderot.utils.urlopen", mocked_open): parse_facts("http://test") parse_ttl_string.assert_called_with(TTL_STRING)