# Insert current date

**URL:** <https://community.stardog.com/t/insert-current-date/834>\
**Category:** Support\
**Created:** [February 25, 2018, 10:27pm UTC](https://community.stardog.com/t/insert-current-date/834 "2018-02-25T22:27:38Z")\
**Posts on this page:** 20\
**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, 10:27pm UTC](https://community.stardog.com/t/insert-current-date/834/1 "2018-02-25T22:27:38Z")

</div>

Is there a way to insert the current date?

---

<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, 11:04pm UTC](https://community.stardog.com/t/insert-current-date/834/2 "2018-02-25T23:04:19Z")

</div>

You can use the `now()` function and cast it to a date with `date()` or `xsd:date()`.

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 26, 2018, 3:51pm UTC](https://community.stardog.com/t/insert-current-date/834/3 "2018-02-26T15:51:39Z")

</div>

Thanks Jess, I have it working but it looks like FILTER doesn’t work in a rule,

`WARN 2018-02-25 20:56:17,362 [XNIO-1 task-16] com.complexible.stardog.reasoning.rule.RuleExtractor:parseRule(160): Error parsing rule, ignoring it:`

```auto
prefix carnot: <http://Carnot.org/>
        IF {
          ?e a carnot:Event .
          BIND (now() as ?t) .
          FILTER NOT EXISTS { ?e schema:startDate ?z }
        }
        THEN {
                ?e schema:startDate ?t
        }

```

`java.lang.UnsupportedOperationException: null`

Is that expected?  
The rule works if I comment out the filter.

---

<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:** [February 26, 2018, 4:17pm UTC](https://community.stardog.com/t/insert-current-date/834/4 "2018-02-26T16:17:36Z")

</div>

The rule you constructed is not allowed. See this section in the documentation [Home | Stardog Documentation Latest](https://www.stardog.com/docs/#_rule_limitations_gotchas)

> "Rule body (IF) and only rule body may optionally contain UNION, BIND or FILTER clauses. However, functions EXISTS, NOT EXISTS, or NOW() cannot be used in rules. User-defined functions (UDF) may be used in rules but if the UDF is not a pure function then the results are undefined."

---

<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 26, 2018, 4:33pm UTC](https://community.stardog.com/t/insert-current-date/834/5 "2018-02-26T16:33:41Z")

</div>

Okay, thanks.  
however, now() seems to work.

---

<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 26, 2018, 4:40pm UTC](https://community.stardog.com/t/insert-current-date/834/6 "2018-02-26T16:40:15Z")

</div>

It’s a bit of an odd rule. What is your intention? Every time the rule is evaluated it will produce a different value. A rule may be evaluated multiple times within the same query which is the reason it is prohibited.

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 26, 2018, 4:50pm UTC](https://community.stardog.com/t/insert-current-date/834/7 "2018-02-26T16:50:08Z")

</div>

I’m using Stardog in my Data Management class at the University of Texas.

This is just for demonstration purposes.

However, if entailments could be materialized, this would be very useful for automatically adding timestamps to events.

Are entailments ever going to be materialized in Stardog?  
thanks  
phil

---

<div class="post-metadata">

**Author:** ![lorenz\_b](https://avatars.discourse-cdn.com/v4/letter/l/d78d45/32.png) [@lorenz\_b](https://community.stardog.com/u/lorenz_b)\
**Post date:** [February 27, 2018, 6:47am UTC](https://community.stardog.com/t/insert-current-date/834/8 "2018-02-27T06:47:55Z")

</div>

Stardog does only support backward-chaining. Just use SPARQL 1.1 Update, i.e. an `INSERT` query to modify the data.

---

<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 27, 2018, 2:23pm UTC](https://community.stardog.com/t/insert-current-date/834/9 "2018-02-27T14:23:04Z")

</div>

But I want a rule to do an insert. I tried,

```auto
[] a rule:SPARQLRule;
rule:content """
prefix carnot: <http://Carnot.org/> 
	IF {
	  ?e a carnot:Employee .
	  ?e carnot:employeeDepartment ?d .
	  ?a carnot:assignmentEmployees ?e .
	  ?a carnot:assignmentPairs ?p
	}
	THEN {
		insert { ?p carnot:pairDepartment ?d }
	}
""".

```

but it gives a syntax error.

```auto
prefix carnot: <http://Carnot.org/>
        IF {
          ?e a carnot:Employee .
          ?e carnot:employeeDepartment ?d .
          ?a carnot:assignmentEmployees ?e .
          ?a carnot:assignmentPairs ?p
        }
        THEN {
                insert ( ?p carnot:pairDepartment ?d )
        }

```

Parse error was: `Encountered " "insert" "insert "" at line 10, column 3. Was expecting one of: "(" ... "[" ... <NIL> ... <ANON> ...`

---

<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:** [February 27, 2018, 5:11pm UTC](https://community.stardog.com/t/insert-current-date/834/10 "2018-02-27T17:11:05Z")

</div>

Rules just don't support doing what you're trying to do. Stardog is very extensible so you might be able to achieve what you're looking to do with a custom trasaction listener. An [example](https://github.com/stardog-union/stardog-examples/tree/feature/materialization/examples/connectable) can be seen at the stardog-examples repo

---

<div class="post-metadata">

**Author:** ![lorenz\_b](https://avatars.discourse-cdn.com/v4/letter/l/d78d45/32.png) [@lorenz\_b](https://community.stardog.com/u/lorenz_b)\
**Post date:** [February 28, 2018, 7:35am UTC](https://community.stardog.com/t/insert-current-date/834/11 "2018-02-28T07:35:43Z")

</div>

And I told you that Stardog doesn’t support forward-chaining (which is materialization) via rules…that’s why I suggested using SPARQL to modify the dataset

Regarding syntax errors, well, you can’t simply write rules in a new language just because you want to have some features.

---

<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:** [February 28, 2018, 12:50pm UTC](https://community.stardog.com/t/insert-current-date/834/12 "2018-02-28T12:50:18Z")

</div>

This can be done fairly straightforward via a regular SPARQL INSERT WHERE query:

```auto
prefix carnot: <http://Carnot.org/>
  INSERT { ?p carnot:pairDepartment ?d }
  WHERE {
    ?e a carnot:Employee ;
      carnot:employeeDepartment ?d .
    ?a carnot:assignmentEmployees ?e ;
      carnot:assignmentPairs ?p
  }

```

---

<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 28, 2018, 1:59pm UTC](https://community.stardog.com/t/insert-current-date/834/13 "2018-02-28T13:59:31Z")

</div>

Thank you, but this is not a rule. I use sparql all the time to do insert, but I would like to be able to use a rule to do inserts based on something reasoned about the data.

---

<div class="post-metadata">

**Author:** ![pavel](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/pavel/32/18_2.png) [@pavel](https://community.stardog.com/u/pavel)\
**Post date:** [February 28, 2018, 2:07pm UTC](https://community.stardog.com/t/insert-current-date/834/14 "2018-02-28T14:07:55Z")

</div>

Rules do not do inserts: they only define which triples are true (inferred). It’s up to you to decide what to do with inferred triples. You can insert them into the database (serialize into a file, send over network, etc.):

```auto
[] a rule:SPARQLRule;
rule:content """
prefix carnot: &lt;http://Carnot.org/&gt; 
	IF {
	  ?e a carnot:Employee .
	  ?e carnot:employeeDepartment ?d .
	  ?a carnot:assignmentEmployees ?e .
	  ?a carnot:assignmentPairs ?p
	}
	THEN {
		?p carnot:pairDepartment ?d
	}
""".

```

and then:

```auto
prefix carnot: <http://Carnot.org/>

INSERT WHERE { ?p carnot:pairDepartment ?d }

```

The query should be run with reasoning for the rule to be applied.

Best,  
Pavel

---

<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 28, 2018, 2:11pm UTC](https://community.stardog.com/t/insert-current-date/834/15 "2018-02-28T14:11:54Z")

</div>

I understand this but not allowing rules to do inserts behind the scene without requiring an explicit query is a major deficiency.

---

<div class="post-metadata">

**Author:** ![evren](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/evren/32/1515_2.png) [@evren](https://community.stardog.com/u/evren)\
**Post date:** [February 28, 2018, 2:59pm UTC](https://community.stardog.com/t/insert-current-date/834/16 "2018-02-28T14:59:51Z")

</div>

Hi Phil,

It looks like what you want is something in the style of production rules whereas Stardog supports logical rules as in Datalog. There are certainly some use cases where production rules are useful since they can be more procedural and use arbitrary functions like `now` in your example. However, there are also many documented drawbacks with production rules (typically vague semantics, complexity of rule maintenance as things like priorities and overrides come into play, etc.).

In our experience, Datalog-style declarative rules are much more suitable for the data representation and unification problems that Stardog is used in. And if you have Datalog rules then materialization creates a truth maintenance problem which is why we provide query-time reasoning instead.

Best,  
Evren

---

<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 28, 2018, 3:31pm UTC](https://community.stardog.com/t/insert-current-date/834/17 "2018-02-28T15:31:03Z")

</div>

I understand, I’ve been teaching Data Management and a variety of other “data” courses at the University of Texas for 18 years. I believe this is a major deficiency for a “semantic” data management system, which needs to have a different (better) semantics than relational (datalog) systems. I’d love to discuss how you think Stardog might provide these semantics (automatically) and still preserve its declarative nature if this is possible.

---

<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:** [February 28, 2018, 3:51pm UTC](https://community.stardog.com/t/insert-current-date/834/18 "2018-02-28T15:51:23Z")

</div>

Take this example workflow. I create my .ttl file including the rule and some data:

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

[] a rule:SPARQLRule;
rule:content """
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
	}
""".

carnot:Employee1 a carnot:Employee ;
  rdfs:label "Jim" ;
  carnot:employeeDepartment carnot:Department1 .

carnot:Assignment1 carnot:assignmentEmployees carnot:Employee1 ;
  carnot:assignmentPairs carnot:Pair1 .

```

I create a db in Stardog loading this data:

```auto
Stephens-iMac:support stephen$ stardog-admin db create -n myDb myDb.ttl
Bulk loading data to new database myDb.
Loaded 7 triples to myDb from 1 file(s) in 00:00:00.290 @ 0.0K triples/sec.
Successfully created database 'myDb'.

```

Now I can query to prove that the triple from the rule is being inferred properly:

```auto
Stephens-iMac:support stephen$ stardog query --reasoning myDb "select * where {?p carnot:pairDepartment ?d}"
+--------------+--------------------+
| p | d |
+--------------+--------------------+
| carnot:Pair1 | carnot:Department1 |
+--------------+--------------------+

Query returned 1 results in 00:00:00.892

```

Which part of this do you see as deficient? Technically speaking the `carnot:Pair1 carnot: carnot:pairDepartment carnot:Department1` isn’t physically on disk, but when performing any read that would need to see it, it would, in practice, always be present.

---

<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 28, 2018, 3:55pm UTC](https://community.stardog.com/t/insert-current-date/834/19 "2018-02-28T15:55:23Z")

</div>

If I change Jim’s department to Department 2, I want all of the pairings that predated this change to remain as Department1.

---

<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:** [March 12, 2018, 11:13pm UTC](https://community.stardog.com/t/insert-current-date/834/20 "2018-03-12T23:13:37Z")

</div>

If I have the following in file.sparql:

```auto
prefix schema: <http://schema.org/>
prefix carnot: <http://Carnot.org/>

insert { ?s ?p ?o }
where {
  ?s a carnot:Pairing .
  ?s ?p ?o .
  }
;

```

And I execute the following as follows:  
`stardog query --reasoning companyDB file.sparql`

the insert doesn’t seem to do anything.

However, if I run the following in the Query Panel, the inserts happen:

```auto
prefix schema: <http://schema.org/>
prefix carnot: <http://Carnot.org/>

insert { ?s ?p ?o }
where {
  ?s a carnot:Pairing .
  ?s ?p ?o .
  }

```

Is there an explanation for this?  
thanks  
phil

[Next page](https://community.stardog.com/t/insert-current-date/834.md?page=2)
