0

When I am trying to more than 9 property objects to Resounce using Apache Jena API, ordering is not retained in the way I am adding. Can anyone explain the reason and how can I fix it?

hyma
  • 21
  • 1
  • 1
    Can you describe what exactly you mean? Perhaps show an example? An RDF graph is a *set* of triples, there's no ordering to them. And the same graph might be written in various ways, even in RDF/XML. See [this answer](http://stackoverflow.com/a/17052385/1281433) for some examples of that. – Joshua Taylor Apr 11 '16 at 13:51
  • You need to clarify the question (as @JoshuaTaylor said). What exactly are you doing, what is the expected result and what are you actually getting. Include code. – CaptSolo Apr 11 '16 at 19:42
  • It is not surprising that order is lost in RDF since RDF graph is a set of triples. Sets do not have ordering and you can not assume that the order that you add triples in will be the same as the order in which you get them out later on. – CaptSolo Apr 11 '16 at 19:45
  • Right. Set doesn't guarantee ordering. I thought if I create sequence on the model with model.createSeq(), it will retain the order of the triplet as I add. Does Seq interface only guarantees order of the subjects but not each triplet/statement? – hyma Apr 12 '16 at 12:04

1 Answers1

1

Although the question is very old, this is a recurring issue: The only way to maintain order in a set of triples is to reify the triples and to introduce an explicit property to encode the sequence. So, instead of doing

:my :first :triple.
:my :other :triple.

you can do

:s1 rdfs:subject :my; rdfs:predicate :first; rdfs:object :triple.
:s2 rdfs:subject :my; rdfs:predicate :other; rdfs:object :triple.
:s1 my:next :s2.

Or, more compact (but also less readable):

[ rdfs:subject :my; rdfs:predicate :first; rdfs:object :triple;
  my:next
  [ rdfs:subject :my; rdfs:predicate :other; rdfs:object :triple ] ]

The real trouble begins if you successfully encode a sequence but then, you are not able to maintain it in an aggregate. There is no reliable workaround.

The following may or may not work. No guarantees, and depending on your query optimiser, it can fail ... and this will be in accordance with SPARQL semantics.

SELECT ?preds
WHERE {
  ?first my:next [].
  MINUS { [] my:next ?first }
  # we establish that ?first is the first in a series of statements
  { SELECT ?first (GROUP_CONCAT(?pred; separator="/") as ?preds)
    WHERE { 
      ?first my:next*/rdfs:predicate ?p.
      BIND(str(?p) as ?pred)
    } GROUP BY ?first
  }
}

The naive expectation is that the order of occurrences in ?preds is preserved. This is not guaranteed.

Chiarcos
  • 324
  • 1
  • 10