How to use Owlready for Ontology-based semantic recommendation

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

How to use Owlready for Ontology-based semantic recommendation

IWilliams
This a follow-up question to my previous question.
I want to write the following swrl rule in Owlready2:
Flow(?fl) ∧ hasAsset(?fl, ?ast) ∧ hasLink(?fl, ?lk) →  hasAttack(?fl, WebAttack1)

WebAttack1 is  the Sub-Class of WebAttack

If the antecedent true, then I want to display all individuals of WebAttack1 for the Flow. My problem is that I tried using SPARQL instead of creating the swrl rules, but I would not get an result when I used SPARQL queries. I read that SPARQL does not return inferred facts, so I want to build a rules engine that can recommend a web attack when certain conditions are true in the ontology based on specific rules.
Example is shown below:
T-Box
WebAttack
    WebAttack1
       WebAttack1-1
       WebAttack1-2
    WebAttack2

I then created individuals:
  wb1 ( a WebAttack1-1) has idOfWebAttack 32
  wb2 (a WebAttack1-2) has idOfWebAttack 198

Below is a SPARQL query:

def getAttactDesc(id):
    attack = list(graph.query_owlready(prefixes +
                                             """select ?attack where{
                                           ?attack rdf:type uc:WebAttack1;
                                                uc:idOfWebAttack '""" + str(id) + """'^^xsd:integer.
                                           }"""))
    return (attack[0][0].descriptionOfWebAttack[0], attack[0][0].attackFlow[0])

The above query does not return anything when I do getAttactDesc(32) . However, if I do:

def getAttactDesc(id):
    attackPatten = list(graph.query_owlready(prefixes +
                                             """select ?attack where{
                                           ?attack rdf:type uc:WebAttack1-1;
                                                uc:idOfWebAttack '""" + str(id) + """'^^xsd:integer.
                                           }"""))
    return (attackPatten[0][0].descriptionOfWebAttack[0], attackPatten[0][0].attackFlow[0])
and then getAttactDesc(32), I get results.

Is there a way to write a rule engine with SWRL that can give me the inferred results that SPAQRL cannot? If so, how can it be done using Owlready2?