representing URIs and literals

Hi all,

A major design decision for an RDF library is how to represent URIs and literals.

For typed languages such as Java, the choice is pretty obvious:
a URI class and a Literal class, which both inherit from a common parent class.
A triple then has a constructor like Triple(URI subject, URI predicate, Entity object).
Unfortunately, this can lead to quite cumbersome code. Creating a triple is as awful as:
    new Triple(new URI("http://example.org/a"), new URI("http://example.org/b"), new Literal("c"))
The fact that only objects can be literals, could help to obtain a more compact overloaded constructor:
    new Triple("http://example.org/a", "http://example.org/b", new Literal("c"))
However, the verbosity for the literal still remains, and accessing properties always involves indirection:
    String value = ((Literal)triple.getObject()).getValue();
Languages such as C# can do some automated type conversion, but this does not always help.

Being a dynamic language, JavaScript gives us more possibilities.
We could follow the Java road and implement it with classes, but then we gain little.
This code is the slowest to write and execute (because of different runtime classes).

Alternatively, the JSON-LD uses annotations to indicate what is a URI and what is a literal [1].
This code fast to write and execute.
The major difference is that JSON-LD does not represent RDF on the triple level, but rather as a specific JSON tree.

A third option is what I have chosen in node-n3: URIs are regular strings; literals are double-quoted strings [2].
This code is fast to write and execute (all runtime triple classes are the same).
URI comparisons and literal comparisons are transparent; an extra step is required to get the literal value though.

There are possibly more options, and it could be interesting to see which library has chosen what and why.

Best,

Ruben

[1] http://json-ld.org/spec/latest/json-ld/#h3_the-context
[2] https://github.com/RubenVerborgh/node-n3#representing-uris-and-literals

Received on Saturday, 2 November 2013 15:56:31 UTC