# Inference/Reasoning through Stardog

**URL:** <https://community.stardog.com/t/inference-reasoning-through-stardog/744>\
**Category:** Support\
**Created:** [December 26, 2017, 1:51pm UTC](https://community.stardog.com/t/inference-reasoning-through-stardog/744 "2017-12-26T13:51:40Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![barbotclem](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/barbotclem/32/183_2.png) [@barbotclem](https://community.stardog.com/u/barbotclem)\
**Post date:** [December 26, 2017, 1:51pm UTC](https://community.stardog.com/t/inference-reasoning-through-stardog/744/1 "2017-12-26T13:51:40Z")

</div>

Everyone,  
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,  
Clément

---

<div class="post-metadata">

**Author:** ![stephen](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/stephen/32/27_2.png) [@stephen](https://community.stardog.com/u/stephen)\
**Post date:** [December 26, 2017, 2:10pm UTC](https://community.stardog.com/t/inference-reasoning-through-stardog/744/2 "2017-12-26T14:10:58Z")

</div>

Hi,

The easiest way to accomplish what you're looking for would be to use [Stardog Rules](http://www.stardog.com/docs/#_stardog_rules_syntax). Adding the IF...THEN syntax into your turtle files easily lets you accomplish this:

```auto
IF {
  ?client :isTruckDriver true .
  ?client :kindOfVehicle "Car"
}
THEN {
  ?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.

---

<div class="post-metadata">

**Author:** ![barbotclem](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/barbotclem/32/183_2.png) [@barbotclem](https://community.stardog.com/u/barbotclem)\
**Post date:** [December 26, 2017, 2:18pm UTC](https://community.stardog.com/t/inference-reasoning-through-stardog/744/3 "2017-12-26T14:18:18Z")

</div>

Hello Stephen, thanks for your very quick answer.

I've tried a thing :

> ```
> IF {
> ?client <http://www.myuri.eu/myontology#isTruckDriver> true ;
> <http://www.myuri.eu/myontology#kingOfVehicle> "Car"^^xsd:string . }
> THEN {
> ?client a <http://www.myuri.eu/myontology#lyer> } 
> 
> ```

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

---

<div class="post-metadata">

**Author:** ![stephen](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/stephen/32/27_2.png) [@stephen](https://community.stardog.com/u/stephen)\
**Post date:** [December 26, 2017, 2:23pm UTC](https://community.stardog.com/t/inference-reasoning-through-stardog/744/4 "2017-12-26T14:23:47Z")

</div>

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

```auto
[] a <tag:stardog:api:rule:SPARQLRule>;
   <tag:stardog:api:rule:content> """
      IF {
      	?client <http://www.myuri.eu/myontology#isTruckDriver> true ;
	        <http://www.myuri.eu/myontology#kingOfVehicle> "Car"^^xsd:string .
      }
      THEN { ?client a <http://www.myuri.eu/myontology#lyer> } 
   """.

```

---

<div class="post-metadata">

**Author:** ![barbotclem](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/barbotclem/32/183_2.png) [@barbotclem](https://community.stardog.com/u/barbotclem)\
**Post date:** [December 26, 2017, 2:27pm UTC](https://community.stardog.com/t/inference-reasoning-through-stardog/744/5 "2017-12-26T14:27:50Z")

</div>

Doesn't work too 😕

This is I think, the last Stardog release.

Maybe I have to clarify a thing : _[http://www.myuri.eu/myontology#lyer](http://www.myuri.eu/myontology#lyer)_ doesn't exists in my ontology, but the _kingOfVehicle_ and _isTruckDriver_ does.

This is maybe the reason ?

If not, I don't undersand 😢

---

<div class="post-metadata">

**Author:** ![stephen](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/stephen/32/27_2.png) [@stephen](https://community.stardog.com/u/stephen)\
**Post date:** [December 26, 2017, 3:06pm UTC](https://community.stardog.com/t/inference-reasoning-through-stardog/744/6 "2017-12-26T15:06:51Z")

</div>

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

myInput.ttl

```auto
@prefix my: <http://www.myuri.eu/myontology#> .

# Rule
IF {
  ?client my:isTruckDriver true ;
    my:kindOfVehicle "Car"^^xsd:string .
}
THEN {
  ?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:

```auto
Stephens-iMac:community stephen$ stardog query -r myDb "select ?client {?client a <http://www.myuri.eu/myontology#lyer>}"
+--------+
| client |
+--------+
| :Joe |
+--------+

Query returned 1 results in 00:00:00.076

```

---

<div class="post-metadata">

**Author:** ![barbotclem](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/barbotclem/32/183_2.png) [@barbotclem](https://community.stardog.com/u/barbotclem)\
**Post date:** [December 26, 2017, 3:14pm UTC](https://community.stardog.com/t/inference-reasoning-through-stardog/744/7 "2017-12-26T15:14:26Z")

</div>

Thanks for your answer.

I’m posting an anonymized sample too :

```
 IF {
 ?client <http://www.myuri.eu/myontology#isTruckDriver> true ;
        <http://www.myuri.eu/myontology#kindOfVehicle> "Car"^^xsd:string . }
 THEN {
  ?client a <http://www.myuri.eu/myontology#lyer> } 

  <http://www.fluidops.com/resource/V2VEHICLE/558022> a <http://www.myuri.eu/myontology#Vehicle> ;
  <http://www.myuri.eu/myontology#isTruckDriver> true ;
  <http://www.myuri.eu/myontology#kindOfVehicle> "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.

Clément

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

---

<div class="post-metadata">

**Author:** ![stephen](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/stephen/32/27_2.png) [@stephen](https://community.stardog.com/u/stephen)\
**Post date:** [December 26, 2017, 3:15pm UTC](https://community.stardog.com/t/inference-reasoning-through-stardog/744/8 "2017-12-26T15:15:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![barbotclem](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/barbotclem/32/183_2.png) [@barbotclem](https://community.stardog.com/u/barbotclem)\
**Post date:** [December 26, 2017, 3:17pm UTC](https://community.stardog.com/t/inference-reasoning-through-stardog/744/9 "2017-12-26T15:17:21Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![stephen](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/stephen/32/27_2.png) [@stephen](https://community.stardog.com/u/stephen)\
**Post date:** [December 26, 2017, 3:20pm UTC](https://community.stardog.com/t/inference-reasoning-through-stardog/744/10 "2017-12-26T15:20:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![barbotclem](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/barbotclem/32/183_2.png) [@barbotclem](https://community.stardog.com/u/barbotclem)\
**Post date:** [December 26, 2017, 3:37pm UTC](https://community.stardog.com/t/inference-reasoning-through-stardog/744/11 "2017-12-26T15:37:12Z")

</div>

Here's fake data : [https://drive.google.com/file/d/1gov0H747d5xUFD0B0kuaPUkXhMunydwa/view?usp=drivesdk](https://drive.google.com/file/d/1gov0H747d5xUFD0B0kuaPUkXhMunydwa/view?usp=drivesdk)

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: <http://www.covea.eu/VoitureConnectee#>

SELECT distinct ?numVehicule ?numClient
WHERE {
  ?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

---

<div class="post-metadata">

**Author:** ![stephen](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/stephen/32/27_2.png) [@stephen](https://community.stardog.com/u/stephen)\
**Post date:** [December 26, 2017, 4:04pm UTC](https://community.stardog.com/t/inference-reasoning-through-stardog/744/12 "2017-12-26T16:04:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![barbotclem](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/barbotclem/32/183_2.png) [@barbotclem](https://community.stardog.com/u/barbotclem)\
**Post date:** [December 26, 2017, 4:14pm UTC](https://community.stardog.com/t/inference-reasoning-through-stardog/744/13 "2017-12-26T16:14:22Z")

</div>

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.  
Clément

---

<div class="post-metadata">

**Author:** ![stephen](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/stephen/32/27_2.png) [@stephen](https://community.stardog.com/u/stephen)\
**Post date:** [December 26, 2017, 4:29pm UTC](https://community.stardog.com/t/inference-reasoning-through-stardog/744/14 "2017-12-26T16:29:16Z")

</div>

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:

```auto
<http://www.fluidops.com/resource/V2_CLIENT-provider/1514291476361/Provider> 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 <http://www.covea.eu/VoitureConnectee#Client> ;
    <http://www.covea.eu/VoitureConnectee#estConducteurDeTypeRoute> "EXTRA_URBAN" ;
    <http://www.covea.eu/VoitureConnectee#estConducteurVille> "N"
  }
  THEN {
   ?c a <http://www.covea.eu/VoitureConnectee#Match>
  }

```

```auto
Stephens-iMac:community stephen$ stardog query -r clement "select * {?c a <http://www.covea.eu/VoitureConnectee#Match>} limit 5"
+---------------------------------------------------+
| c |
+---------------------------------------------------+
| http://www.fluidops.com/resource/V2CLIENT/E767682 |
| http://www.fluidops.com/resource/V2CLIENT/2782748 |
| http://www.fluidops.com/resource/V2CLIENT/G429992 |
| http://www.fluidops.com/resource/V2CLIENT/J045010 |
| http://www.fluidops.com/resource/V2CLIENT/E946626 |
+---------------------------------------------------+

Query returned 5 results in 00:00:00.065

```

---

<div class="post-metadata">

**Author:** ![zachary.whitley](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/zachary.whitley/32/94_2.png) [@zachary.whitley](https://community.stardog.com/u/zachary.whitley)\
**Post date:** [December 26, 2017, 5:59pm UTC](https://community.stardog.com/t/inference-reasoning-through-stardog/744/15 "2017-12-26T17:59:45Z")

</div>

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`

---

<div class="post-metadata">

**Author:** ![barbotclem](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/barbotclem/32/183_2.png) [@barbotclem](https://community.stardog.com/u/barbotclem)\
**Post date:** [December 26, 2017, 7:25pm UTC](https://community.stardog.com/t/inference-reasoning-through-stardog/744/16 "2017-12-26T19:25:10Z")

</div>

_Stephen_, thanks a lot. This is what I was looking for. A simple and understable answer ! 🙂

_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_ …) ☹

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 🙂

Clément

---

<div class="post-metadata">

**Author:** ![zachary.whitley](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/zachary.whitley/32/94_2.png) [@zachary.whitley](https://community.stardog.com/u/zachary.whitley)\
**Post date:** [December 27, 2017, 4:53pm UTC](https://community.stardog.com/t/inference-reasoning-through-stardog/744/17 "2017-12-27T16:53:39Z")

</div>

> [@barbotclem](#):
>
> Another question (because you look strong on this subject) :

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. 😉

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)

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex030/uploads/stardog/original/2X/e/ed66b48a616f505106a4acde3c9cee7e6da9bc67.svg) [@system](https://community.stardog.com/u/system)\
**Post date:** [January 10, 2018, 4:53pm UTC](https://community.stardog.com/t/inference-reasoning-through-stardog/744/18 "2018-01-10T16:53:45Z")

</div>

Closed on January 10, 2018, 4:53pm UTC

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