Accessing inferences and asserstions rather than just assertions

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

Accessing inferences and asserstions rather than just assertions

tyler
Using protege, I have several object properties, all of which have inverses (ie object property 'contains' is the inverse of 'is_part_of'). When using SPARQL queries, I am only able to pull out the individuals in which 'contains' was asserted and not inferred.

Is there a way to pull out all the individuals, regardless whether the relationship was asserted or inferred? Using http://owlready2.readthedocs.io/en/latest/reasoning.html hasn't helped so far. Code example below...

world = World()
world.get_ontology(filename).load()
sync_reasoner(world)
graph = world.as_rdflib_graph()
               
systems = "PREFIX base: <my_uri>" \
            "SELECT ?system " \
            "WHERE { " \
            "?system base:contains ?something . " \
            "}"

results = list(graph.query(attacked_systems))


Again, I need all asserted AND inferred individuals to be returned by the query. Is this possible?

Thanks in advance.

Tyler
Reply | Threaded
Open this post in threaded view
|

Re: Accessing inferences and asserstions rather than just assertions

Jiba
Administrator
I've just tested using a very simple script (see below), but it seems to work as expected.
Are you sure the problem is not somewhere else?

-----8<-----------

from owlready2 import *

world = World()
world.get_ontology("file:///tmp/t.owl").load()
sync_reasoner(world)
graph = world.as_rdflib_graph()
               
request = """SELECT ?x WHERE {
  ?x <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.semanticweb.org/jiba/ontologies/2018/1/untitled-ontology-40#C
}
"""

results = list(graph.query(request))
print(results)


Tested with this ontology: t.owl
Reply | Threaded
Open this post in threaded view
|

Re: Accessing inferences and asserstions rather than just assertions

tyler
Our examples look nearly identical, yet I am still unable to get inferred results.....

world = World()
world.get_ontology(self.filename).load()
sync_reasoner(world)
graph = world.as_rdflib_graph()
attacked_systems = "PREFIX ont: <http://www.semanticweb.org/tyler/ontologies/2018/0/untitled-ontology-14#>" \
            "SELECT * " \
            "WHERE { " \
            "?system ont:contains ?component ." \
            "?component ont:entered ?effect ." \
            "}"

results = list(graph.query(attacked_systems))


for item in results:
    print(item)

Any idea on what's going on? Anything I'm not doing? What could be causing this?

- I have shut down and closed protege while running this script
- the ontology is type rdf/xml
- owlready2 is running HermiT
- do I need to run the reasoner multiple times?

Thanks for the help
Reply | Threaded
Open this post in threaded view
|

Re: Accessing inferences and asserstions rather than just assertions

tyler
This post was updated on .
Do I need to sync the reasoner before creating the world? After? Sync the reasoner multiple times?

Sync the reasoner and then save the ontology, then run the reasoner again??

Do I need to save the quadstore to a sqlite file, then run sync_reasoner(world)? Is the sqlite3 file necessary?

Maybe a better question - what are the step by step mechanics involved in creating a world, syncing a reasoner, and querying an ontology (is saving the ontology or saving a quadstore a necessary part?)
Reply | Threaded
Open this post in threaded view
|

Re: Accessing inferences and asserstions rather than just assertions

Jiba
Administrator
> Do I need to sync the reasoner before creating the world? After? Sync the
> reasoner multiple times?
>
> Sync the reasoner and then save the ontology, then run the reasoner again??

No, just syncing the world once after everything is set up is OK.

You can try to add the following at the beginning of your program:

import owlready2
owlready2.set_log_level(9)

and run the reasoner in debug mode:

sync_reasoner(world, debug = 9)


In this mode, you should see on the output the new triples asserted. If no triple are inserted, there is a problem in the reasoning. Else, the problem is in the SPARQL request (are you sure the prefix is the right one?).


Jiba
Reply | Threaded
Open this post in threaded view
|

Re: Accessing inferences and asserstions rather than just assertions

tyler
This post was updated on .
At the top of my script:

from owlready2 import *
owlready2.set_log_level(9)

I get the error "NameError: name 'owlready2' is not defined" when calling owlready2.set_log_level(9)



When I change the top of the script to:

import owlready2
owlready2.set_log_level(9)

I get "NameError: name 'World' is not defined" when I attempt to instantiate an instance of World()


EDIT - adding "sync_reasoner(world, debug=9)" prints all triples....no need to set log level directly after import statement

Reply | Threaded
Open this post in threaded view
|

Re: Accessing inferences and asserstions rather than just assertions

tyler
this is the top of my .owl file:

<rdf:RDF xmlns="http://www.semanticweb.org/tyler/ontologies/2018/0/untitled-ontology-14#"
     xml:base="http://www.semanticweb.org/tyler/ontologies/2018/0/untitled-ontology-14"
     xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
     xmlns:owl="http://www.w3.org/2002/07/owl#"
     xmlns:xml="http://www.w3.org/XML/1998/namespace"
     xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
     xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
    <owl:Ontology rdf:about="http://www.semanticweb.org/tyler/ontologies/2018/0/untitled-ontology-14"/>

My SPARQL query:

effects = "PREFIX owl:<http://www.semanticweb.org/tyler/ontologies/2018/0/untitled-ontology-14#> " \
            "SELECT ?effect " \
            "WHERE { " \
            "?system owl:contains ?component ." \
            "?component owl:entered ?effect ." \
            "}"

From this query, I only get two asserted effects, and not the two additional inferred effects (as the result of being the inverse object properties)

I'm at a loss....
Reply | Threaded
Open this post in threaded view
|

Re: Accessing inferences and asserstions rather than just assertions

Jiba
Administrator
> I get the error NameError: name 'owlready2' is not defined when calling owlready2.set_log_level(9)

You need to import owlready2 as a module (not just from...import *)


> From this query, I only get two asserted effects, and not the two additional
> inferred effects (as the result of being the inverse object properties)
>
> I'm at a loss....

Could you email me your ontology? Or a part of it if it is big?
Reply | Threaded
Open this post in threaded view
|

Re: Accessing inferences and asserstions rather than just assertions

tyler
Just sent ontology....my sparql query I am using (sorry, it's changed a few times, but it's still only pulling 2 asserted and not two additional inferences)

effects = "PREFIX xmlns:<http://www.semanticweb.org/tyler/ontologies/2018/0/untitled-ontology-14#> " \
            "SELECT ?effect " \
            "WHERE { " \
            "?system xmlns:was_attacked_by ?hpm_attack ." \
            "?system xmlns:contains ?component ." \
            "?component xmlns:entered ?effect ." \
            "}"
Reply | Threaded
Open this post in threaded view
|

Re: Accessing inferences and asserstions rather than just assertions

tyler
Also, when I 'export my ontology with inferred axioms as ontology' in protege, and run owlready2 on that file, I get the expected results (asserted + inferred).....so somewhere along the line the Hermit reasoner used by owlready2 is not being synced
Reply | Threaded
Open this post in threaded view
|

Re: Accessing inferences and asserstions rather than just assertions

Jiba
Administrator
In reply to this post by tyler
Hi,

I've investigated this.
The problem is that:

 1) RDFlib works at the RDF level, not the OWL level. However, inverseProperties are defined in OWL, not in RDF. Thus, RDFlib ignores inverseProperties when performing SPARQL queries.

 2) HermiT is classifier: it classifies classes, but not relations. For example, if "inv" is the inverse  of "rel" and we assert that "A rel B", HermiT will not deduce that "B inv A". This is not needed for Owlready, because Owlready works at the OWL level and it will deal with inverseProperties as expected. So, asserting "A rel B" is enough in Owlready, and you do not need to use the reasoning for inverseProperty values.

In Protege, inverseProperty values are shown after classification, but I'm not sure they come from HermiT. Deducing inverseProperty values is rather trivial and do not require a reasoner. When exporting inferred axioms as ontology in Protege, you can export inverseProperty values, but by default this option is not checked, because inverseProperty values are usually not asserted in ontologies.


For your problem, maybe the easier solution is to use Owlready's search() function (which takes inverseProperties into account). It is also faster than RDFlib's SPARQL engine.
For example :


from owlready2 import *

world = World()
onto = world.get_ontology("file:///tmp/t2.owl").load()

# Search all instances that entered something
entered_sometings = world.search(entered = "*")

# Search all instances that contains something that entered something
contain_entered_sometings = []
for i in entered_sometings:
  contain_entered_sometings.extend(world.search(contains = i))
 
print(contain_entered_sometings)


In future version of Owlready, I might improve the RDFlib graph support so as it takes inverseProperties into account.

Best regards,
Jiba