Inference/Reasoning through Stardog

I’m not sure this is the place for my question but I’m trying.

I’ve an ontology that’s describing some vehicles and some clients. The ontology is based on three major classes, which are “Vehicle” , “Client” and a class joining Vehicle and Client (named “VehicleToClient”).

Anyway, in the “Client” class I have a property isTruckDriver (boolean). In the “Vehicle” class, I have a property kindOfVehicle (string, can be Truck, Car, …).

Now, and there’s why I’m posting there, I want to use the reasoning property of Stardog to determine if a Client is a “lyer” or not, based on his answer on isTruckDriver and on the value of kindOfVehicle (if it’s different he’s a lyer, we can trust him if not (this is an example, we won’t do that)).

The problem is : I don’t know where to start. I’ve read the whole documentation of Reasoning, Rules, … of Stardog but I don’t know where to start :

  • Am I supposed to start writing rules on Protégé (Ontology editor) ?
  • Am I supposed to use the “IF THEN” thing on my turtle file ?

And more important, how to do that ? I’m loosing my mind on it :’(

Thanks for all,


The easiest way to accomplish what you’re looking for would be to use Stardog Rules. Adding the IF…THEN syntax into your turtle files easily lets you accomplish this:

IF {
  ?client :isTruckDriver true .
  ?client :kindOfVehicle "Car"
  ?client a :Lyer

Loading this in one of your Turtle files and then running a query with reasoning enabled should give you results that use the rule.

Hello Stephen, thanks for your very quick answer.

I’ve tried a thing :

IF {
 ?client <> true ;
        <> "Car"^^xsd:string .  }
  ?client a <> } 

but when I try to add my data onto Stardog, it’s saying

The file is invalid. Please ensure that the file is correctly formed.

Moreover, my turtle file is well-formed (it works when I remove the “IF THEN” lines.

Thanks, Clément

Depending on the version of Stardog you are running, you may need to use the older, more formal representation of a rule:

[] a <tag:stardog:api:rule:SPARQLRule>;
   <tag:stardog:api:rule:content> """
      IF {
      	?client <> true ;
	        <> "Car"^^xsd:string .
      THEN { ?client a <> } 

Doesn’t work too :confused:

This is I think, the last Stardog release.

Maybe I have to clarify a thing : doesn’t exists in my ontology, but the kingOfVehicle and isTruckDriver does.

This is maybe the reason ?

If not, I don’t undersand :cry:

I’m unable to reproduce an error here, so I’ll show the input I’m using and the output I’m getting.


@prefix my: <> .

# Rule
IF {
  ?client my:isTruckDriver true ;
    my:kindOfVehicle "Car"^^xsd:string .
  ?client a my:lyer .

# Data
:Bob my:isTruckDriver true .
:Jim my:isTruckDriver false .
:Joe my:isTruckDriver true .

:Bob my:kindOfVehicle "Truck"^^xsd:string .
:Jim my:kindOfVehicle "Car"^^xsd:string .
:Joe my:kindOfVehicle "Car"^^xsd:string .

I load this into myDb and then run a simple query with reasoning:

Stephens-iMac:community stephen$ stardog query -r myDb "select ?client {?client a <>}"
| client |
| :Joe   |

Query returned 1 results in 00:00:00.076

Thanks for your answer.

I’m posting an anonymized sample too :

 IF {
 ?client <> true ;
        <> "Car"^^xsd:string .  }
  ?client a <> } 

  <> a <> ;
  <> true ;
  <> "Car"^^xsd:string .

The .ttl file is a bit huge, but it should be fine.

The error is still the same if I put the rule before or after the data.


I can show you a better example, or a better sample (because there’s more than 1 class in my ontology as explained)

It looks like you might have a mismatch between your rule (#kingOfVehicle) and your data (#kindOfVehicle)

Yes, my bad, but it’s an example and in my data it’s not that.

Indeed, i’m supposed to compare velocity and road types, but it was hard to explain.

Yes, if you are able to post actual data and an actual query that isn’t working, I should be able to provide better help

Here’s fake data :

Below a simpel request, which is taking the vehicle&clients numbers if they said they are driving on days (and not on nights).

PREFIX vco: <>

SELECT distinct ?numVehicule ?numClient
  ?entiteVehicule vco:aPourNumeroVehicule ?numVehicule ;
                  vco:aPourNumeroClient ?entiteClient .
  ?entiteClient vco:aPourNumeroClient ?numClient ;
          vco:estConducteurJour "O"^^xsd:string .
  ?entiteTrajet vco:aPourNumeroVehicule ?numVehicule .

I removed most of the data. The data properties are in french, sorry. The content is, for the most, numbers so it should be ok.

Thanks, Clément

What are you expecting to see when running this query, and what are you seeing instead?

This request sends me what I want: this is a basic request. So to answer your question: I’m seeing what I’m expecting to see.

As I said, now I want to use the reasoning tool. To use it, it looks like the most simple way is to add rules into the turtle file.
The .ttl I posted doesn’t have rules. When I add rules onto the turtle file, Stardog doesn’t like it and says : ‘The file is invalid. Please ensure that the file is correctly’

Thanks for all, again.

Is there an additional message in your stardog.log file that might show a syntax error in your rules? I am able to add a very simple rule to the file you provided and then load it with no issues:

<> a System:Context ;
	System:contextType "Provider" ;
	System:contextSrc :V2_CLIENT-provider ;
	System:contextState "Published" ;
	dc:date "2017-12-26T13:31:16.361" ;
	rdfs:label "Provider run" ;
	System:editable "false"^^xsd:boolean .

IF {
  ?c a <> ;
    <> "EXTRA_URBAN" ;
    <> "N"
  THEN {
   ?c a <>
Stephens-iMac:community stephen$ stardog query -r clement "select * {?c a <>} limit 5"
|                         c                         |
| |
| |
| |
| |
| |

Query returned 5 results in 00:00:00.065

What version of Stardog are you running? You only mentioned that you thought you were running the latest version. Inline rules in turtle files was only recently added in the latest 5.1 release.

You should be able to get the version by running stardom-admin server status

Stephen, thanks a lot. This is what I was looking for. A simple and understable answer ! :slight_smile:

Zachary : you found to solution to my problem ! I was in 5.0.3. In 5.1, it worked like a charm. My bad : I was sure I was on the latest update (that’s why I said that to Stephen …) :frowning:

I cannot try a very good example of reasoning because I doesn’t have remote access to my company server (I’ll be back there if any problems )

Another question (because you look strong on this subject) :
Do you think this is the best way to write rules ? (writing rules in the turtle file)
If no, can you show me those ways you think are better ?

Thanks.a.lot for what you’ve already done :slight_smile:


I’m going to assume you’re talking about Stephen here because I’m just here to make suggestions like, “Did you check to make sure there’s gas in the car?” but I’ll give it a go anyway. :wink:

Inline rules in turtle is new so I haven’t had a chance to play with it too much but I can give you my initial impressions. This is the kind of practical extension to a standard that I really appreciate from the Stardog people. They have a strong focus on supporting standards but know where to extend them when it means making it easier to get things done. Technically it’s not turtle anymore once you add the rules so that’s something to keep in mind if you’re planning on sharing the files with anyone not working with Stardog but converting it to turtle should be as easy as loading the data and exporting it back out was turtle. (I’ll have to check if there is an option to export as turtle+rules. sturtle? Searching for “turtle dog” on google is amusing if you want to give it a try)

If that’s something you plan on doing you can always save your stardog turtle rules in a separate file since you can always load or add multiple files. What it really means is unimposing options and options are nice things to have.

I would definitely recommend using the Stardog rules syntax (IF…THEN…) over SWRL. This is the, going beyond the standards thing that I was mentioning earlier. I like to think that people have a zero -sum cognitive budget to spend on their problems and some semantic web technologies spend way too much of it on the tech and don’t leave enough for the problem you’re working on. The Stardog enhancements change that equation and I love it. (ICV, PATHS, Stardog Rules Syntax, etc)

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.