Saving ontology with indirect facts

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

Saving ontology with indirect facts

falcaopetri
Hi everyone.

I am trying to save an ontology that has a TransitiveProperty relation. The 'problem' is that I want the saved ontology to contain even the INDIRECT_* facts.

I understand that this is not the default behavior since these facts could be 'recalculated'. But in my use case, I need all triples that could be infered from my relations to be exported.
!pip install owlready2
from owlready2 import *

onto = get_ontology("http://test.org/example.owl")
with onto:
  class Instance(Thing): pass
  class after(Instance >> Instance, TransitiveProperty): pass

  i1 = Instance("i1")
  i2 = Instance("i2", after=[i1])
  i3 = Instance("i3", after=[i2])
  
onto.save('test.nt', format='ntriples') 
By now, I am explicitly adding the indirect relations manually. But what I want is the code above to be enough to produce the following triples:
<http://test.org/example.owl#i2> <http://test.org/example.owl#after> <http://test.org/example.owl#i1> .
<http://test.org/example.owl#i3> <http://test.org/example.owl#after> <http://test.org/example.owl#i1> .
<http://test.org/example.owl#i3> <http://test.org/example.owl#after> <http://test.org/example.owl#i2> .

	
	
	
	
Reply | Threaded
Open this post in threaded view
|

Re: Saving ontology with indirect facts

Jiba
Administrator
Hi,

You can infer property values (including transitive property values) by calling the reasoning with the "infer_property_values = True" optional argument (you may use Hermit or Pellet).

You can also use the "with onto: ..." syntax to store the inferred facts in the given ontology.

For example :



from owlready2 import *
import sys, time

onto = get_ontology("http://test.org/t2.owl")

#set_datatype_iri(float, "http://www.w3.org/2001/XMLSchema#float")

with onto:
  class p(ObjectProperty, TransitiveProperty): pass
  class C(Thing): pass

  c1 = C()
  c2 = C()
  c3 = C()

  c1.p = [c2]
  c2.p = [c3]

with onto:
  sync_reasoner(infer_property_values = True)



You may also use a different ontology for storing the inferred facts, if you want so.

Jiba
Reply | Threaded
Open this post in threaded view
|

Re: Saving ontology with indirect facts

falcaopetri
Thank you, Jiba!

I guess I misunderstood the "infer_property_values" parameter at first and ended up not giving it a try.

Now it works as I wanted.