How to do twice reasoning

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

How to do twice reasoning

Jacky
Hi, Thank you for developing OWLready.  I tried twice reasoning, but I can not finish it. I just used the same example:
 
from owlready2 import *
import time
onto = get_ontology("http://test.org/onto.owl")

with onto:
   class Drug(Thing):
       def take(self): print("I took a drug")

   class ActivePrinciple(Thing):
       pass

   class has_for_active_principle(Drug >> ActivePrinciple):
       python_name = "active_principles"

   class Placebo(Drug):
       equivalent_to = [Drug & Not(has_for_active_principle.some(ActivePrinciple))]
       def take(self): print("I took a placebo")

   class SingleActivePrincipleDrug(Drug):
       equivalent_to = [Drug & has_for_active_principle.exactly(1, ActivePrinciple)]
       def take(self): print("I took a drug with a single active principle")

   class DrugAssociation(Drug):
       equivalent_to = [Drug & has_for_active_principle.min(2, ActivePrinciple)]
       def take(self): print("I took a drug with %s active principles" % len(self.active_principles))


acetaminophen   = ActivePrinciple("acetaminophen")
amoxicillin     = ActivePrinciple("amoxicillin")
clavulanic_acid = ActivePrinciple("clavulanic_acid")
test_acid = ActivePrinciple("test_acid")

AllDifferent([acetaminophen, amoxicillin, clavulanic_acid,test_acid])

drug1 = Drug(active_principles = [acetaminophen])
drug2 = Drug(active_principles = [amoxicillin, clavulanic_acid])
drug3 = Drug(active_principles = [])

close_world(Drug)

with onto:
     sync_reasoner()

print('First reasoning:',drug1.__class__)

drug1.active_principles.append(test_acid)
close_world(Drug)


with onto:
    sync_reasoner()

print('second reasoning:',drug1.__class__)

I got the results as follows:

* Owlready2 * Warning: optimized Cython parser module 'owlready2_optimized' is not available, defaulting to slower Python implementation
* Owlready2 * Running HermiT...
    java -Xmx2000M -cp E:\Work\test\venv\lib\site-packages\owlready2\hermit;E:\Work\test\venv\lib\site-packages\owlready2\hermit\HermiT.jar org.semanticweb.HermiT.cli.CommandLine -c -O -D -I file:///C:/Users/X/AppData/Local/Temp/tmpcx118ugk
First reasoning: onto.SingleActivePrincipleDrug
* Owlready2 * HermiT took 1.5297236442565918 seconds
* Owlready * Reparenting onto.drug2: {onto.Drug} => {onto.DrugAssociation}
* Owlready * Reparenting onto.drug1: {onto.Drug} => {onto.SingleActivePrincipleDrug}
* Owlready * Reparenting onto.drug3: {onto.Drug} => {onto.Placebo}
* Owlready * (NB: only changes on entities loaded in Python are shown, other changes are done but not listed)
* Owlready2 * Running HermiT...
    java -Xmx2000M -cp E:\Work\test\venv\lib\site-packages\owlready2\hermit;E:\Work\test\venv\lib\site-packages\owlready2\hermit\HermiT.jar org.semanticweb.HermiT.cli.CommandLine -c -O -D -I file:///C:/Users/X/AppData/Local/Temp/tmp42ebyn1a
Traceback (most recent call last):
  File "E:\Work\test\venv\lib\site-packages\owlready2\reasoning.py", line 134, in sync_reasoner_hermit
    output = subprocess.check_output(command, stderr = subprocess.STDOUT)
  File "C:\Users\X\AppData\Local\Programs\Python\Python37\lib\subprocess.py", line 395, in check_output
    **kwargs).stdout
  File "C:\Users\X\AppData\Local\Programs\Python\Python37\lib\subprocess.py", line 487, in run
    output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command '['java', '-Xmx2000M', '-cp', 'E:\\Work\\test\\venv\\lib\\site-packages\\owlready2\\hermit;E:\\Work\\test\\venv\\lib\\site-packages\\owlready2\\hermit\\HermiT.jar', 'org.semanticweb.HermiT.cli.CommandLine', '-c', '-O', '-D', '-I', 'file:///C:/Users/X/AppData/Local/Temp/tmp42ebyn1a']' returned non-zero exit status 1.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "E:/Work/test/test7.py", line 50, in <module>
    sync_reasoner()
  File "E:\Work\test\venv\lib\site-packages\owlready2\reasoning.py", line 137, in sync_reasoner_hermit
    raise OwlReadyInconsistentOntologyError()
owlready2.base.OwlReadyInconsistentOntologyError

Process finished with exit code 1.

I can not solve it. Could you help me if possible? Thanks in advance!
Reply | Threaded
Open this post in threaded view
|

Re: How to do twice reasoning

carlos
Hi, I have a similar problem and I do not know how to solve it. For example, I have the next code:

    onto = get_ontology("ontology_1.nt").load()

    sync_reasoner_pellet()

    onto_inferred = get_ontology("http://inferrences/")

    onto_inferred.save("./output/infered_triples_1.nt", format="ntriples")

    onto2 = get_ontology("ontology_2.nt").load()

    sync_reasoner_pellet()

    onto_inferred = get_ontology("http://inferrences/")

    onto_inferred.save("./output/infered_2.nt", format="ntriples")

I do not understand why, the second time that I use the reasoner, it use again the first ontology, so the two output are the same. I would like to know how can I use two times the reasoning with 2 different ontologies.
Reply | Threaded
Open this post in threaded view
|

Re: How to do twice reasoning

carlos
Hi, I have solved my problem of using two consecutive times the reasoning. The problem is that sync_reasoner() places all inferred facts in a special ontology, ‘http://inferrences/’. Therefore, if we try to use the reasoner with a new ontology, the special ontology used for keeping the inferred fact continues keeping the old facts, so the solution is removing all these ontology before applying again the reasoner. You can do it using the method destroy of the ontology:


    onto = get_ontology("ontology_1.nt").load()

    sync_reasoner_pellet()

    onto_inferred = get_ontology("http://inferrences/")

    onto_inferred.save("./output/infered_triples_1.nt", format="ntriples")

    onto_inferred.destroy()

    onto2 = get_ontology("ontology_2.nt").load()

    sync_reasoner_pellet()

    onto_inferred = get_ontology("http://inferrences/")

    onto_inferred.save("./output/infered_2.nt", format="ntriples")
Reply | Threaded
Open this post in threaded view
|

Re: How to do twice reasoning

Jiba
Administrator
In reply to this post by Jacky
Hi,

The problem is related to the use of close_world(). Calling close_world() creates new restrictions. Since you call it twice, 2 sets of restrictions are created.

In particular, the first call to close_world() creates a restriction on drug1 : it must have for active principle only acetaminophen.

Then, you add a second active principle to drug1, and call close_world() again. It now creates a second restriction : drug1 must have for active principle only acetaminophen and test_acid. But the previous restriction remains, and make the individual (and thus the ontology) inconsistent.

You can see the restrictions as follow:

>>> drug1.is_a
[onto.has_for_active_principle.only(OneOf([onto.acetaminophen])), onto.SingleActivePrincipleDrug, onto.has_for_active_principle.only(OneOf([onto.acetaminophen, onto.test_acid]))]


The solution is to add the restrictions created by close_world() in a separate ontology, and to destroy this ontology before calling close_world() a second time:

restr_onto = get_ontology("http://test.org/restr_onto1.owl")
with restr_onto: close_world(Drug) # First call

[...]

restr_onto.destroy()
restr_onto = get_ontology("http://test.org/restr_onto2.owl")
with restr_onto: close_world(Drug) # Second call


As mentioned by Carlos, you'll also need to destroy the ontology containing the inferences ("http://inferrences/" by default).

Jiba