# Rule isn't working

**URL:** <https://community.stardog.com/t/rule-isnt-working/829>\
**Category:** Support\
**Created:** [February 25, 2018, 2:59pm UTC](https://community.stardog.com/t/rule-isnt-working/829 "2018-02-25T14:59:17Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![cannata](https://avatars.discourse-cdn.com/v4/letter/c/54ee81/32.png) [@cannata](https://community.stardog.com/u/cannata)\
**Post date:** [February 25, 2018, 2:59pm UTC](https://community.stardog.com/t/rule-isnt-working/829/1 "2018-02-25T14:59:17Z")

</div>

I have the following rule in a file called 3\ Rules.ttl

```auto
@prefix rule: <tag:stardog:api:rule:> .
@prefix carnot: <http://Carnot.org/> .

[] a rule:SPARQLRule;
rule:content """
	IF {
	  ?e a carnot:Employee .
	  ?e carnot:employeeDepartment ?d .
	  ?a carnot:assignmentEmployees ?e .
	  ?a carnot:assignmentPairs ?p
	}
	THEN {
		?p carnot:pairDepartment ?d 
	}
""".

```

I load the file using: stardog data add testDB 3\ Rules.ttl

But it doesn’t seem to work.

However, if I do the same thing as a query, i.e.,

```auto
prefix rule: <tag:stardog:api:rule:>
prefix carnot: <http://Carnot.org/>

insert { ?p carnot:pairDepartment ?d }
where{
  ?e a carnot:Employee .
  ?e carnot:employeeDepartment ?d .
  ?a carnot:assignmentEmployees ?e .
  ?a carnot:assignmentPairs ?p
}

```

it works.

Am I doing something wrong w.r.t loading the Rules file?

---

<div class="post-metadata">

**Author:** ![jess](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/jess/32/10_2.png) [@jess](https://community.stardog.com/u/jess)\
**Post date:** [February 25, 2018, 3:24pm UTC](https://community.stardog.com/t/rule-isnt-working/829/2 "2018-02-25T15:24:12Z")

</div>

Hi Phil,

The first thing to check is always your `stardog.log` file.

It's also a bit simpler to use our "inline rules" syntax by writing your rule directly in the TTL file, not as a string. You can find an example in the [docs](https://www.stardog.com/docs/#_how_to_use_stardog_rules).

Jess

---

<div class="post-metadata">

**Author:** ![cannata](https://avatars.discourse-cdn.com/v4/letter/c/54ee81/32.png) [@cannata](https://community.stardog.com/u/cannata)\
**Post date:** [February 25, 2018, 3:51pm UTC](https://community.stardog.com/t/rule-isnt-working/829/3 "2018-02-25T15:51:57Z")

</div>

When I put this in the Rules file,

```auto
@prefix rule: <tag:stardog:api:rule:> .
@prefix carnot: <http://Carnot.org/> .

IF {
	  ?e a carnot:Employee .
	  ?e carnot:employeeDepartment ?d .
	  ?a carnot:assignmentEmployees ?e .
	  ?a carnot:assignmentPairs ?p
	}
THEN {
		?p carnot:pairDepartment ?d 
	}
.

```

I get the following error,  
`Expected ':', found ' ' [line 4]`

I guess I don’t understand the inline syntax.  
thanks  
phil

---

<div class="post-metadata">

**Author:** ![jess](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/jess/32/10_2.png) [@jess](https://community.stardog.com/u/jess)\
**Post date:** [February 25, 2018, 3:56pm UTC](https://community.stardog.com/t/rule-isnt-working/829/4 "2018-02-25T15:56:47Z")

</div>

The inline rules syntax was introduced in Stardog 5.1.0. Are you using an older version of Stardog?

Also, the `.` at the end of your file is invalid. The rules aren’t terminated as triples are.

Jess

---

<div class="post-metadata">

**Author:** ![cannata](https://avatars.discourse-cdn.com/v4/letter/c/54ee81/32.png) [@cannata](https://community.stardog.com/u/cannata)\
**Post date:** [February 25, 2018, 3:59pm UTC](https://community.stardog.com/t/rule-isnt-working/829/5 "2018-02-25T15:59:54Z")

</div>

I’m using 5.0.1, I’ll try to upgrade.  
I had submitted a request for 5.2 but never received an email. I’ll try again.  
thanks  
phil

---

<div class="post-metadata">

**Author:** ![jess](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/jess/32/10_2.png) [@jess](https://community.stardog.com/u/jess)\
**Post date:** [February 25, 2018, 4:05pm UTC](https://community.stardog.com/t/rule-isnt-working/829/6 "2018-02-25T16:05:54Z")

</div>

Let us know if you have an issue downloading. The upgrade should be seamless if you placed your Stardog home directory outside the binary distribution.

Jess

---

<div class="post-metadata">

**Author:** ![cannata](https://avatars.discourse-cdn.com/v4/letter/c/54ee81/32.png) [@cannata](https://community.stardog.com/u/cannata)\
**Post date:** [February 25, 2018, 4:16pm UTC](https://community.stardog.com/t/rule-isnt-working/829/7 "2018-02-25T16:16:17Z")

</div>

I'm still waiting for the email.

However if I insert the rule the old way, no problem appears in the stardog.log, and I do see the rule in the Browser,

 ![image](https://canada1.discourse-cdn.com/flex030/uploads/stardog/original/1X/1762f8588c7b52fd361ed16ac6e803fd70e45d7f.png)

---

<div class="post-metadata">

**Author:** ![jess](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/jess/32/10_2.png) [@jess](https://community.stardog.com/u/jess)\
**Post date:** [February 25, 2018, 4:18pm UTC](https://community.stardog.com/t/rule-isnt-working/829/8 "2018-02-25T16:18:25Z")

</div>

You said “it doesn’t seem to work” in your original post. Can you further explain how you came to that conclusion? Rules are evaluated at query time when reasoning is enabled for the query. Did you see any messages in your `stardog.log` file?

Jess

edit: Sorry just noticed you said no messages in the log file.

---

<div class="post-metadata">

**Author:** ![cannata](https://avatars.discourse-cdn.com/v4/letter/c/54ee81/32.png) [@cannata](https://community.stardog.com/u/cannata)\
**Post date:** [February 25, 2018, 4:28pm UTC](https://community.stardog.com/t/rule-isnt-working/829/9 "2018-02-25T16:28:38Z")

</div>

I just ran a query to use the rule and I got the following in the log file,  
WARN 2018-02-25 10:20:19,552 [XNIO-1 task-17] com.complexible.stardog.reasoning.rule.RuleExtractor:parseRule(157): Ignoring invalid rule:

```
    IF {
      ?e a carnot:Employee .
      ?e carnot:employeeDepartment ?d .
      ?a carnot:assignmentEmployees ?e .
      ?a carnot:assignmentPairs ?p
    }
    THEN {
            ?p carnot:pairDepartment ?d
    }

```

Parse error was: com.complexible.common.rdf.query.parser.sparql.ast.VisitorException: QName ‘carnot:Employee’ uses an undefined prefix

So I added the carnot namespace to the database, and now it works.

Why wasn’t the carnot prefix that I used to define the rule used? Why did I need to add carnot to the database?

Thanks  
phil

---

<div class="post-metadata">

**Author:** ![cannata](https://avatars.discourse-cdn.com/v4/letter/c/54ee81/32.png) [@cannata](https://community.stardog.com/u/cannata)\
**Post date:** [February 25, 2018, 4:39pm UTC](https://community.stardog.com/t/rule-isnt-working/829/12 "2018-02-25T16:39:16Z")

</div>

I’ve tried to reply 3 times and my reply doesn’t show up.

---

<div class="post-metadata">

**Author:** ![jess](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/jess/32/10_2.png) [@jess](https://community.stardog.com/u/jess)\
**Post date:** [February 25, 2018, 4:42pm UTC](https://community.stardog.com/t/rule-isnt-working/829/14 "2018-02-25T16:42:44Z")

</div>

Are you trying to post an image? Our anti-spam measures are a little aggressive for new users. I will have to check into it. Are you able to describe the problem with text?

---

<div class="post-metadata">

**Author:** ![cannata](https://avatars.discourse-cdn.com/v4/letter/c/54ee81/32.png) [@cannata](https://community.stardog.com/u/cannata)\
**Post date:** [February 25, 2018, 4:51pm UTC](https://community.stardog.com/t/rule-isnt-working/829/15 "2018-02-25T16:51:12Z")

</div>

I was pasting something from the stardog.log, maybe that was the problem.

Anyway, I tried running a query, and saw an error in the log “carnot:Employee’ uses an undefined prefix”

So, I added the carnot prefix to the database as carnot=http://Carnot.org/ and then the query worked. This is good grogress.

Why wasn’t the carnot prefix I used to define the rule saved with the rule?

Also, now the Browser doesn’t seem to be working and there’s no error in the log.

---

<div class="post-metadata">

**Author:** ![jess](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/jess/32/10_2.png) [@jess](https://community.stardog.com/u/jess)\
**Post date:** [February 25, 2018, 4:55pm UTC](https://community.stardog.com/t/rule-isnt-working/829/16 "2018-02-25T16:55:40Z")

</div>

For a prefix to be recognized in a rule string, it must be in the rule string itself, or defined in the database. The prefixes in the containing TTL file are not used. The inline rules syntax simplifies this. Here's the relevant section from the docs:

> However, the namespace URIs used by the literal embedded rules can be defined in two places: the string that contains the rule—​in the example above, you can see the default namespace is urn:test:--or in the Stardog database in which the rules are stored. Either place will work; if there are conflicts, the "closest definition wins", that is, if foo:Example is defined in both the rule content and in the Stardog database, the definition in the rule content is the one that Stardog will use.

What is not working with the browser? Can you explain it in more detail please?

Jess

---

<div class="post-metadata">

**Author:** ![cannata](https://avatars.discourse-cdn.com/v4/letter/c/54ee81/32.png) [@cannata](https://community.stardog.com/u/cannata)\
**Post date:** [February 25, 2018, 5:01pm UTC](https://community.stardog.com/t/rule-isnt-working/829/17 "2018-02-25T17:01:24Z")

</div>

With the carnot namespace not in the database, I see the following for pairing1"  
 ![image](https://canada1.discourse-cdn.com/flex030/uploads/stardog/original/1X/f6569636bea419bf8ca614f5151681216d119496.png)

With the carnot namespace in the database. I see the following for paring1:  
 ![image](https://canada1.discourse-cdn.com/flex030/uploads/stardog/original/1X/476a2f4fdb8329c42e1e15e3dfe7687a459e32f5.png)

I.e., the References are lost.  
phil

---

<div class="post-metadata">

**Author:** ![jess](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/jess/32/10_2.png) [@jess](https://community.stardog.com/u/jess)\
**Post date:** [February 25, 2018, 10:25pm UTC](https://community.stardog.com/t/rule-isnt-working/829/18 "2018-02-25T22:25:54Z")

</div>

I’m not sure exactly what’s going on here. Are you able to reproduce the issue with a SPARQL query?

---

<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:** [March 11, 2018, 10:26pm UTC](https://community.stardog.com/t/rule-isnt-working/829/19 "2018-03-11T22:26:12Z")

</div>

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