SWRL Rules in Owlready throwing AttributeError

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

SWRL Rules in Owlready throwing AttributeError

nas
Hello,

Hope everyone is doing well. I am working on a project and I am trying to implement some SWRL rules. I followed the documentation and wrote the following:

rule1 = Imp()
rule1.set_as_rule("MathModel(?m), Current(?m, ?c), Voltage(?m, ?v), multiply(?r, ?c, ?v) -> PowerElectrical(?m, ?r)")

but whenever I try to run it, it throws this error:

Traceback (most recent call last):
  File "ontologystuff.py", line 83, in <module>
    rule1.set_as_rule("MathModel(?m), Current(?m, ?c), Voltage(?m, ?v), multiply(?r, ?c, ?v) -> PowerElectrical(?m, ?r)")
  File "/Users/nasirsharaf/anaconda3/lib/python3.6/site-packages/owlready2/rule.py", line 146, in set_as_rule
    atom.arguments = arguments
AttributeError: 'str' object has no attribute 'arguments'

At one point, I tried this instead:

rule1 = Imp()
rule1.set_as_rule("MathModel(?m), Current(?m, ?c), Voltage(?m, ?v), multiply(?r, ?c, ?v) -> PowerElectrical(?m, ?r)", [])

but then it said that it could not find MathModel even though I made the class in the line right above it:

Traceback (most recent call last):
  File "ontologystuff.py", line 83, in <module>
    rule1.set_as_rule("MathModel(?m), Current(?m, ?c), Voltage(?m, ?v), multiply(?r, ?c, ?v) -> PowerElectrical(?m, ?r)",[])
  File "/Users/nasirsharaf/anaconda3/lib/python3.6/site-packages/owlready2/rule.py", line 135, in set_as_rule
    entity = _find_entity(atom, namespaces)
  File "/Users/nasirsharaf/anaconda3/lib/python3.6/site-packages/owlready2/rule.py", line 173, in _find_entity
    raise ValueError("Cannot find entity '%s'!" % name)
ValueError: Cannot find entity 'MathModel'!

Can anyone help me? I would greatly appreciate it
Reply | Threaded
Open this post in threaded view
|

Re: SWRL Rules in Owlready throwing AttributeError

Jiba
Administrator
Hi,

I think the problem is in the definition of your class and/or your properties. The second argument of set_as_rule() is the list of namespaces (or ontologies) where entities are searched. If you pass an empty list, no ontologies are searched and thus the class and properties cannot be found.

Here is a way to implement your ontology:


from owlready2 import *

onto = get_ontology("http://test.org/t.owl")
with onto:
  class MathModel(Thing): pass
  class Current(Thing >> float, FunctionalProperty): pass
  class Voltage(Thing >> float, FunctionalProperty): pass
  class PowerElectrical(Thing >> float, FunctionalProperty): pass

  m = MathModel(Current = 2.0, Voltage = 3.0)
 
  rule1 = Imp()
  rule1.set_as_rule("MathModel(?m), Current(?m, ?c), Voltage(?m, ?v), multiply(?r, ?c, ?v) -> PowerElectrical(?m, ?r)")

onto.save("/tmp/t.owl")

sync_reasoner_pellet(infer_property_values = True, infer_data_property_values = True)
print(m.PowerElectrical)


Jiba
nas
Reply | Threaded
Open this post in threaded view
|

Re: SWRL Rules in Owlready throwing AttributeError

nas
Hey, that worked! Thanks so much Jiba, you're a godsend.