Inserting SWRL rules via Owlready

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

Inserting SWRL rules via Owlready

Marie
Hi,

I am currently trying to insert SWRL rules via Owlready into my ontology. Unfortunately, Protégé tells me that the ontology cannot be reloaded after I have saved the ontology including my new SWRL rule.

ONTOLOGY

<?xml version="1.0"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
         xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
         xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
         xmlns:owl="http://www.w3.org/2002/07/owl#"
         xml:base="http://www.example.org/onto"
         xmlns="http://www.example.org/onto#"
         xmlns:swrl="http://www.w3.org/2003/11/swrl#"
         xmlns:swrl2="http://swrl.stanford.edu/ontologies/3.3/swrla.owl#">

<owl:Ontology rdf:about="http://www.example.org/onto"/>

<owl:AnnotationProperty rdf:about="http://swrl.stanford.edu/ontologies/3.3/swrla.owl#isRuleEnabled"/>

<owl:ObjectProperty rdf:about="#op1"/>

<owl:ObjectProperty rdf:about="#op2"/>

<owl:Class rdf:about="#A"/>


  <rdf:type rdf:resource="http://www.w3.org/2002/07/owl#NamedIndividual"/>



  <rdf:type rdf:resource="http://www.w3.org/2002/07/owl#NamedIndividual"/>



  <rdf:type rdf:resource="http://www.w3.org/2002/07/owl#NamedIndividual"/>



  <rdf:type rdf:resource="http://www.w3.org/2002/07/owl#NamedIndividual"/>



</rdf:RDF>


CODE

ontology = or2.get_ontology("file://test2.owl").load()

with ontology:
    input_rule = or2.Imp()
    input_rule.set_as_rule("""op1(a,b) -> op2(a,b)""")

ontology.save("test2.owl")



Further, I get

input_rule.body    ->   Body[_:9]
input_rule.head    ->   Body[_:10]

Inserting the exact same rule directly in Protégé works fine. I also noticed, that inserting the rule via Protégé and Owlready produces different results in the .owl file.

Can you tell me what I am doing wrong?

Best,
Marie

Reply | Threaded
Open this post in threaded view
|

Re: Inserting SWRL rules via Owlready

Jiba
Administrator
Hi,

There are 2 things that seem strange to me in your example:

First, what are a and b in your rule? If they are variable, their name must start with "?", e.g. op1(?a,?b) -> op2(?a,?b).

Second, your ontology has four individual without any information (<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#NamedIndividual"/>), which is usually not very interesting. Are some of them supposed to be a and b ?

When I run your example, I get an error because "a" and "b" are not defined in the rule. If I use variable instead (?a and ?b), it seems to work.

Jiba
Reply | Threaded
Open this post in threaded view
|

Re: Inserting SWRL rules via Owlready

Marie
This post was updated on .
Hi Jiba,

thanks for your reply. I see that I have sent you the already modified (via owlready) version of the ontology by accident. Here is the original one

ONTOLOGY

<?xml version="1.0"?>
<rdf:RDF xmlns="http://www.example/or2_swrl#"
     xml:base="http://www.example/or2_swrl"
     xmlns:owl="http://www.w3.org/2002/07/owl#"
     xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
     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.example/or2_swrl"/>
   


   

   


   

    <owl:ObjectProperty rdf:about="http://www.example/or2_swrl#op1"/>
   


   

    <owl:ObjectProperty rdf:about="http://www.example/or2_swrl#op2"/>
   


   

   


   

    <owl:Class rdf:about="http://www.example/or2_swrl#A"/>
   


   

   


   

    <owl:NamedIndividual rdf:about="http://www.example/or2_swrl#individual1">
        <rdf:type rdf:resource="http://www.example/or2_swrl#A"/>
    </owl:NamedIndividual>
   


   

    <owl:NamedIndividual rdf:about="http://www.example/or2_swrl#individual2">
        <rdf:type rdf:resource="http://www.example/or2_swrl#A"/>
    </owl:NamedIndividual>
</rdf:RDF>





I renamed the individuals a and b to individual1 and individual2 to make things clearer. So 'a' and 'b' are not supposed to be variables but individuals.

The modified ontology seems a bit strange to me and it also cannot be opened with Protégé. Here is my code again:

CODE

ontology = or2.get_ontology(location).load()


with ontology:
    rule = or2.Imp()
    rule.set_as_rule("""op1(individual1,individual2) -> op2(individual1,individual2)""")
   
    print(str(rule))
    print("Body", rule.body)
    print("Head", rule.head)

ontology.save(location)


OUTPUT

op1(or2_swrl.individual1, or2_swrl.individual2) -> op2(or2_swrl.individual1, or2_swrl.individual2)
Body [_:4]
Head [_:5]


So as I my original ontology looks okay to me and the rule as well, I cannot see why the produced ontology looks like this:

ONTOLOGY (OUTPUT)

<?xml version="1.0"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
         xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
         xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
         xmlns:owl="http://www.w3.org/2002/07/owl#"
         xml:base="http://www.example/or2_swrl"
         xmlns="http://www.example/or2_swrl#"
         xmlns:swrl="http://www.w3.org/2003/11/swrl#">

<owl:Ontology rdf:about="http://www.example/or2_swrl"/>

<owl:ObjectProperty rdf:about="#op1"/>

<owl:ObjectProperty rdf:about="#op2"/>

<owl:Class rdf:about="#A"/>


  <rdf:type rdf:resource="http://www.w3.org/2002/07/owl#NamedIndividual"/>



  <rdf:type rdf:resource="http://www.w3.org/2002/07/owl#NamedIndividual"/>


<swrl:Imp>
  <swrl:body>
    <rdf:Description>
      <rdf:first>
        <swrl:IndividualPropertyAtom>
          <swrl:propertyPredicate rdf:resource="#op1"/>
          <swrl:arguments rdf:resource="#individual2"/>
          <swrl:arguments rdf:resource="#individual1"/>
        </swrl:IndividualPropertyAtom>
      </rdf:first>
      <rdf:rest rdf:resource="http://www.w3.org/1999/02/22-rdf-syntax-ns#nil"/>
    </rdf:Description>
  </swrl:body>
  <swrl:head>
    <rdf:Description>
      <rdf:first>
        <swrl:IndividualPropertyAtom>
          <swrl:propertyPredicate rdf:resource="#op2"/>
          <swrl:arguments rdf:resource="#individual2"/>
          <swrl:arguments rdf:resource="#individual1"/>
        </swrl:IndividualPropertyAtom>
      </rdf:first>
      <rdf:rest rdf:resource="http://www.w3.org/1999/02/22-rdf-syntax-ns#nil"/>
    </rdf:Description>
  </swrl:head>
</swrl:Imp>


</rdf:RDF>

EDIT: I also tried using variables, i.e.

rule.set_as_rule("""op1(?a, ?b) -> op2(?a, ?b)"""),

but this does not work either (same error as above).


Does my code work for you using my original ontology?

Thank you very much!

Best,
Marie

Reply | Threaded
Open this post in threaded view
|

Re: Inserting SWRL rules via Owlready

Jiba
Administrator
Hi,

There was a bug in Owlready, related to the creation of SWRL IndividualPropertyAtom. I fixed the bug in the development version on BitBucket and your example now works fine.

PS remind to call the reasoner as follows:

or2.sync_reasoner_pellet(infer_property_values = True, infer_data_property_values = True)

Jiba
Reply | Threaded
Open this post in threaded view
|

Re: Inserting SWRL rules via Owlready

Marie
Thanks a lot, it works fine for me, too.