Query Available SWRL Rules + Insert new ones

HI there,

I created some rules in Protege with my ontology and then I insert the all in Stardog,

The rules have names and comments in Protege and we can find them in the serialized RDF file.

<rdf:Description>
        <swrla:isRuleEnabled rdf:datatype="http://www.w3.org/2001/XMLSchema#boolean">true</swrla:isRuleEnabled>
        <rdfs:comment rdf:datatype="http://www.w3.org/2001/XMLSchema#string">Rule1</rdfs:comment>
        <rdfs:label rdf:datatype="http://www.w3.org/2001/XMLSchema#string">Rule1</rdfs:label>
        <rdf:type rdf:resource="http://www.w3.org/2003/11/swrl#Imp"/>
...

1- Is there any way, I can ask Stardog what are the supported rules?
2- How can I inject a new rule to Stardog?

Thank you for your help,
Best,

Charbel

  1. Can you elaborate a little bit on what you mean by asking Stardog about supported rules?

  2. You can “inject” a new rule by simply adding it to the database.

Hey Zack,
1- I would like to query stardog what are the available rules. So I would like to get back Rule1

<rdfs:comment rdf:datatype="http://www.w3.org/2001/XMLSchema#string">Rule1</rdfs:comment>
<rdfs:label rdf:datatype="http://www.w3.org/2001/XMLSchema#string">Rule1</rdfs:label>

Something like ./stardog available Rules or through a REST API

2- I would like to add the following new SWRL rule:
monitors(?DIP1, ?Prod1)
^ implements(?DIP2, ?CTRL1)
^ accepts(?CTRL1, ?Prod2) -> owl:sameAs(?Prod1, ?Prod2)

How can I add the rule to Stardog?

Thank,

There are a couple ways you can query the rules:

  1. Simply write a SPARQL query: select * where {?rule a swrl:Imp, rdfs:label ?label ...}
  2. Run the stardog reasoning schema myDb CLI command to get highlighted output of the rules Stardog can see

As for adding rules, you simply need to insert them into the database as talked about in the docs section on rule reasoning

You should take a quick look over the "User defined Rules" section of the documentations Home | Stardog Documentation Latest

You should be able to query the database based on the data you added. (I'm not sure exactly how you added your rules, Stardog rules or SWRL). I think you can get the rules by running stardog reasoning schema from the CLI but you'll get the entire TBOX. (I'm assuming that the rules would be included in there but I'm not entirely sure about that.)

You can simply add new rules at any time by including them in the database. They need to be either in the Stardog rules format or SWRL/XML. I'd suggest the Stardog format as it's much easier to work with.

@prefix rule: <tag:stardog:api:rule:> .
[] a rule:SPARQLRule;
   rule:content """
     @prefix myprefix: <http://....> .
    IF {
          ?DIP1 monitors ?Prod1 .
          ?DIP2 implements ?CTRL1 .
          ?CTRL1 accepts ?Prod2 .
    }
    THEN {
       ?Prod1 owl:sameAs ?Prod2
    }
   """.

Thank you @stephen.
That worked, I noticed that the SWRL rules are rewritten in Stardog Rules ? when i run the reasoning schema.

So I guess the query select * where {?rule a swrl:Imp, rdfs:label ?label ...} will return an rdf rule and not my initial SWRL rule?

^ implements(?DIP2, ?CTRL1) 
^ accepts(?CTRL1, ?Prod2) 
^ controls(?DIP3, ?Cons1 -> ...

Thank you @zachary.whitley!
I am trying to add the rule SRS but I am not successful

Invalid file type. 
Please ensure that the contents of the file are valid for the chosen format. 

This is the rule I am trying to add it is saved in a file with stardogRules.srs, I built it using VS Code and the Stardog extension:

@prefix rule: <tag:stardog:api:rule:> .

[] a rule:SPARQLRule ;
  rule:content """
    @prefix dsp: <http://www.se.com/dsp#> .
      IF {
           ?DIP11 dsp:monitors ?Prod1 .
           ?DIP2 dsp:implements ?CTRL1 .
           ?CTRL1 dsp:accepts ?Prod2 .
           ?DIP3 dsp:controls ?Cons1 .
           ?CTRL2 dsp:provides ?Cons2 .
           ?CTRL1 a dsp:ControlLoop .
           ?CTRL2 a dsp:ControlLoop .
           ?Prod1 a dsp:Producer  .
           ?Prod2 a dsp:Producer  .
           ?Cons1 a dsp:Consumer .
           ?Cons2 a dsp:Consumer .
          } THEN {
              ?Prod1 owl:sameAs ?Prod2 .
              ?DIP2 owl:sameAs ?DIP11 .
              ?Cons1 owl:sameAs ?Cons2 .
              ?DIP2 owl:sameAs ?DIP3 .
              ?CTRL2 owl:sameAs ?CTRL1 .
          }""".

It can’t figure out the file type based on the .srs extension. Try renaming it to stardogRules.srs.ttl

That worked ! thx, maybe an update the VS Code documentation section will help :slight_smile:

Hey @zachary.whitley @stephen,

Here are my observations on Stardog 5.0.3:
1- When I put the rule in SWRL with Protege, and import the rule in Stardog. I can see the rule when I run

>stardog reasoning schema myDB

However, when I import the owl file into Stardog and then add myRule.srs.ttl into Stardog, I don’t see any rule:

>stardog reasoning schema myDB

Any thoughts?

Thank you

My initial thought would be there is something wrong that is syntactically correct but isn’t allowing it to be recognized as a rule like the prefix is wrong but I don’t see the problem with what you’ve posted. Can you check the file that you’re using?

I don't know, I can see it in the WebUI under SPARQLRule but not in the reasoning schema.

Also. the Then part of the query is not executed since I dont see the changes applied.

I expect to see:
new sameAs results after I apply the query:

Select ?a ?b
{
?a a dsp:BootCampDIPBoard.
?b a dsp:BootCampDIPBoard.
?a owl:sameAs ?b.
}

The query is just returning the following results, I activated the sameAs reasoning in Stardog.

Did you enable reasoning for the query?

yes I did, I am trying to understand if I have cycles in my rule.

@prefix rule: tag:stardog:api:rule: .

a rule:SPARQLRule ;
rule:content """
@prefix dsp: http://www.se.com/dsp# .
IF {
?DIP11 dsp:monitors ?Prod1 .
?DIP2 dsp:implements ?CTRL1 .
?CTRL1 dsp:accepts ?Prod2 .
?DIP3 dsp:controls ?Cons1 .
?CTRL2 dsp:provides ?Cons2 .
?CTRL1 a dsp:ControlLoop .
?CTRL2 a dsp:ControlLoop .
?Prod1 a dsp:Producer .
?Prod2 a dsp:Producer .
?Cons1 a dsp:Consumer .
?Cons2 a dsp:Consumer .
?DIP11 a dsp:BootCampDIPBoard .
?DIP2 a dsp:BootCampDIPBoard .
?DIP3 a dsp:BootCampDIPBoard .
} THEN {
?Prod1 owl:sameAs ?Prod2 .
?DIP2 owl:sameAs ?DIP11 .
?Cons1 owl:sameAs ?Cons2 .
?DIP2 owl:sameAs ?DIP3 .
?CTRL2 owl:sameAs ?CTRL1 .
}""".

I have a difficult time identifying cycles as well. There should be warnings in the logs if you have them. From the docs “sameAs inferences are computed and indexed eagerly” so I’m not sure if sameAs reasoning is supported as the result of a rule. Someone from Stardog would have to weigh in on that.

The rule in protege with Pellet seems to be working well.
But I think, Stardog is based on ELK now so not sure how it is reasoning.

I can send the owl file offline to the Stardog team if needed.

Thank you for your help,
Best.

Stardog currently does not support rules in sameAs reasoning. Only OWL axioms (FunctionaProperty, InversefunctionalProperty and HasKey) and explicit sameAs triples are used. We have a ticket to add this feature but we don’t have a specific deadline at this point.

Best,
Evren