openlink / virtuoso-opensource

Virtuoso is a high-performance and scalable Multi-Model RDBMS, Data Integration Middleware, Linked Data Deployment, and HTTP Application Server Platform
https://vos.openlinksw.com
Other
868 stars 210 forks source link

how to realize a knowledge inferencing using owl:sameAs? #1306

Closed candlecao closed 3 months ago

candlecao commented 4 months ago

In terms of OpenLink Virtuoso, I want to realize the knowledge inference based on OWL.

But I don't know how to realize this for the occasion as below: if in an RDF instance graph,there is a triple <InstanceA> <propertyX> <InstanceB>.

and in an OWL document, there is content as:

<Instance_a> owl:sameAs <InstanceA>.
<Instance_b> owl:sameAs <InstanceB>.

then,what I expect to supplement is: <Instance_a> <propertyX> <Instance_b>. --I mean I want to retrieve this triple with the activated inference function.

Regarding above, I tried operating like this:

  1. Prepare an OWL document in which I asserted <Instance_a> owl:sameAs <InstanceA>. And <Instance_b> owl:sameAs <InstanceB>.
  2. Import this OWL document to Virtuoso with a GRAPH name <http://sampleForReasoning/ontology> for it.
  3. Prepare another GRAPH named <http://sampleForReasoning/instance> where there is triple that is: <InstanceA> <propertyX> <InstanceB>. And also there are triples including <Instance_a> and <Instance_b>.
  4. Then create an inference rule that reference the <http://sampleForReasoning/ontology> named graph: rdfs_rule_set ('urn:owl.music', 'http://sampleForReasoning/ontology') ;
  5. Then in the SPARQL interface, I execute:
    DEFINE input:inference 'urn:owl.music'
    SELECT *
    FROM <http://sampleForReasoning/instance>
    WHERE {
       ?subject <propertyX> ?object.
    }

    However, the result returned is only still <InstanceA> <propertyX> <InstanceB>, there is no inferred triple such as <Instance_a> <propertyX> <Instance_b>.

How to solve the problem?

Actually I know how to realize inference in Virtuoso based on T-box (such as classes and properties) to generate latent triples of instances, but here the owl:sameAs is about A-box (instances). Then I just don't know how to deal with it.

imitko commented 3 months ago

@candlecao

Please read the Virtuoso OWL sameAs Support.

short explanation: The subject or object must be given in order sameAs traversal to start. Also, the sameAs relations should be in the instance graph(s).

HTH

candlecao commented 3 months ago

@candlecao

Please read the Virtuoso OWL sameAs Support.

short explanation: The subject or object must be given in order sameAs traversal to start. Also, the sameAs relations should be in the instance graph(s).

HTH

Thank you very much. I will try it.

candlecao commented 3 months ago

@imitko Hi, imitko. I tried. But it seemed the sameAs doesn't take effect: For example:

First, I prepared an .ttl file in which there are 2 triples like:

<http://example.com/dataspace/person/kidehen#this> <http://www.w3.org/2002/07/owl#sameAs> <http://www.openlinksw.com/dataspace/person/kidehen@openlinksw.com#this> .
<http://example.com/dataspace/person/kidehen#this> <http://xmlns.com/foaf/0.1/name> "Kingsley Idehen" .

Second, I imported the .ttl file to Virtuoso as named graph http://OWL_sameAs_example. Third, I input code in ISQL:

SPARQL
DEFINE input:same-as "yes"
SELECT distinct *
WHERE
 {GRAPH <http://OWL_sameAs_example>{
   ?s <http://xmlns.com/foaf/0.1/name> "Kingsley Idehen" .}
 };

and expected to get feedback as both <http://example.com/dataspace/person/kidehen#this> and <http://www.openlinksw.com/dataspace/person/kidehen@openlinksw.com#this>. However, there is only <http://example.com/dataspace/person/kidehen#this> fed back. Why? It seems the <http://www.w3.org/2002/07/owl#sameAs> doesn't work or DEFINE input:same-as "yes" doesn't take effect.

candlecao commented 3 months ago

I asked chatGPT, getting a reply:

you can enable OWL sameAs reasoning support using the Virtuoso SQL command line interface (iSQL) with the following command: DB.DBA.RDF_OBJ_FT_RULE_ADD (, 'http://www.w3.org/2002/07/owl#sameAs', 0, 0); This command adds the sameAs reasoning rule to the Virtuoso database.

However, I tried it, but it didn't work and reported an error: image

imitko commented 3 months ago

@candlecao

The minimal example you can try re:sameAs via command-line ISQL:

TTLP (
' @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
 <#ent1> <http://www.w3.org/2002/07/owl#sameAs> <#ent2> .
 <#ent2> <http://www.w3.org/2002/07/owl#sameAs> <#ent3> .
 <#ent3> <http://www.w3.org/2002/07/owl#sameAs> <#ent4> .
 <#ent1> rdfs:label "ent1" .
', '', 'g-sas', 0);

then

SPARQL define input:same-as "yes" SELECT ?s FROM <g-sas> WHERE  {    ?s ?p ?o ; ?p1  "ent1" };

compare with

SPARQL  SELECT ?s FROM <g-sas> WHERE  {    ?s ?p ?o ; ?p1 "ent1" };

BTW: GPT answer is absolutely wrong, hallucination as per usual, neither syntax nor API call are correct.

HTH

candlecao commented 3 months ago

Hello, @imitko. I tried your code and it worked. But something is so weird: If I replace your code SPARQL define input:same-as "yes" SELECT ?s FROM <g-sas> WHERE { ?s ?p ?o ; ?p1 "ent1" }; to SPARQL define input:same-as "yes" SELECT ?s FROM <g-sas> WHERE { ?s ?p "ent1" };, the feedback is different!? Why?!

And another my case is like this:

TTLP (
'@prefix wdt: <http://www.wikidata.org/prop/direct/> .
 <#InstanceA_local> <http://www.w3.org/2002/07/owl#sameAs> <#InstanceA_wiki> .
 <#InstanceB_local> <http://www.w3.org/2002/07/owl#sameAs> <#InstanceB_wiki> .
 <#InstanceA_local> wdt:P136 <#InstanceB_local> .
', '', 'http://g-sameAs', 0);

(you know, I just want to use this example to generate <#InstanceA_wiki> wdt:P136 <#InstanceB_wiki>.) then I query via ISQL by imitating your code:

SPARQL 
define input:same-as "yes" 
prefix wdt: <http://www.wikidata.org/prop/direct/>
SELECT * FROM <http://g-sameAs> 
WHERE  {
  ?s ?p ?o ;
       wdt:P136 <#InstanceB_local> };

The expected result should contain <#InstanceA_wiki> wdt:P136 <#InstanceB_local>. However it didn't appear in the feedback after execution. I don't know why. Would you please kindly further check it? Thank you.