You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@jena.apache.org by Stephen Suffian <st...@gmail.com> on 2014/02/21 17:49:27 UTC

Ordering in model.write()

I have a question regarding ordering of data in both model.write() and when
rules fire.

I am writing statements to a file using model.write(). The statements look
like they do below:

<http://www.compscii.com/ontologies/0.1/AutoIE.owl##route1> <
http://www.compscii.com/ontologies/0.1/AutoIE.owl##isTrackedBy> <
http://www.compscii.com/ontologies/0.1/AutoIE.owl##point1> .

<http://www.compscii.com/ontologies/0.1/AutoIE.owl##route1> <
http://www.compscii.com/ontologies/0.1/AutoIE.owl##isTrackedBy> <
http://www.compscii.com/ontologies/0.1/AutoIE.owl##point2> .

<http://www.compscii.com/ontologies/0.1/AutoIE.owl##route1> <
http://www.compscii.com/ontologies/0.1/AutoIE.owl##isTrackedBy> <
http://www.compscii.com/ontologies/0.1/AutoIE.owl##point3> .

However, when writing these statements to a file, they don't show up in the
file in monotonically increasing order.

I then have a rule:

 [Rule1 (?r ie:isTrackedBy ?p) ->DoSomething(?r ie:lineSegment ?l)]

When I load the file later into an inference model and the rules fire, they
seem to fire from the statements at the bottom of the file first towards
the top.

I need the rules to fire on the statements in monotically increasing order.
Is there any way to ensure that when the model writes to a file it writes
the statements in order? Can I trust that the rules will always fire from
the statements at the bottom of the file first?

Re: Ordering in model.write()

Posted by Chris_Dollin <eh...@gmail.com>.
On Friday, February 21, 2014 11:49:27 AM Stephen Suffian wrote:
> I have a question regarding ordering of data in both model.write() and when
> rules fire.

Models are not ordered. I don't think rules-firings are either.

> I am writing statements to a file using model.write(). The statements look
> like they do below:
> 
> <http://www.compscii.com/ontologies/0.1/AutoIE.owl##route1> <
> http://www.compscii.com/ontologies/0.1/AutoIE.owl##isTrackedBy> <
> http://www.compscii.com/ontologies/0.1/AutoIE.owl##point1> .

(Is that really '##'? That looks wrong.)

> <http://www.compscii.com/ontologies/0.1/AutoIE.owl##route1> <
> http://www.compscii.com/ontologies/0.1/AutoIE.owl##isTrackedBy> <
> http://www.compscii.com/ontologies/0.1/AutoIE.owl##point2> .
> 
> <http://www.compscii.com/ontologies/0.1/AutoIE.owl##route1> <
> http://www.compscii.com/ontologies/0.1/AutoIE.owl##isTrackedBy> <
> http://www.compscii.com/ontologies/0.1/AutoIE.owl##point3> .
> 
> However, when writing these statements to a file, they don't show up in the
> file in monotonically increasing order.

That's right. There is no defined ordering of triples; a graph is a /set/ of
triples, there's no order to worry about.

Internally, models may store triples howsoever they please in whatever
order is convenient. For example, the Jena memory model stores
its triples in a hash-table; the ordering depends on details of the
hash function it uses.

(Actually it's more cunning than that, possibly unnecessarily so, since
resources map to collections of statements-with-that-as-subject,
and the collection is a list -- in insertion order -- if there aren't many,
and a not-java.util hash table if there are more than not many.)

So: if you want order, you have to put it in yourself.

> I then have a rule:
> 
>  [Rule1 (?r ie:isTrackedBy ?p) ->DoSomething(?r ie:lineSegment ?l)]
> 
> When I load the file later into an inference model and the rules fire, they
> seem to fire from the statements at the bottom of the file first towards
> the top.

That's almost certainly a accident of the implementation. You might,
for example, seeing a combination of the memory-model random
order and redording as items are copied from one queue/list to
another.

> I need the rules to fire on the statements in monotically increasing order.
> Is there any way to ensure that when the model writes to a file it writes
> the statements in order? 

You could write your own output method. Or you could sort the file after
writing it.

> Can I trust that the rules will always fire from  the statements at the
> bottom of the file first?

No.

Chris