Towards the future RDF library
September 11, 2015
RDF is an simple and powerful language to describe data. However, playing with RDF data can get very tricky - especially when doing simple things. Only by solving the user experience of RDF libraries we would really get developers to user RDF more11 In all of its forms, turtle, json-ld, and so on.. This blog post is directed to the community that works in this field and it is an open call to work on an ultimate, simpler library for RDF.
I will now show you three ways to do a very simple task (1) the current way (using rdf-ext22 I have been recently involved with the development of this library) (2) the way developers find familiar (3) the way the future RDF lib should be.
The current RDF way
Example 1: Creating a graph with a triple that represents my name
var rdf = require('rdf-ext')
var me = rdf.createGraph()
me.add(new rdf.Triple(
new rdf.NamedNode('http://nicola.io'),
new rdf.NamedNode('http://xmlns.com/foaf/0.1/name'),
new rdf.Literal('Nicola')
))
Example 2: Retrieving my name from the graph
me.match(
'http://nicola.io',
'http://xmlns.com/foaf/0.1/name')[0]
.object
.toString()
// Nicola
The Javascript Objects way
Now, to the RDF experts, this might sound about right, but let’s remember one second how real world developers would do something like that, let’s say using Javascript Objects.
Example 3: The equivalent of Example 1 and 2 using JSON
var me = {
name: 'Nicola'
}
me.name
// Nicola
The future RDF way
The future RDF library should provide a seamless experience to the developer that is used to the JS object.
Example 4: The equivalent of Example 1 and 2 as it should be
var r = require('simplerdf')
var me = r()
r['http://xmlns.com/foaf/0.1/name'] = 'Nicola'
r['http://xmlns.com/foaf/0.1/name']
// Nicola
Even simpler, I should be able to load an JSON-LD context to achieve the following
var me = r()
me.context({
'name': 'http://xmlns.com/foaf/0.1/name',
'homepage': {
'@id': 'http://xmlns.com/foaf/0.1/homepage',
'@type': '@id'
}
})
me.name = 'Nicola'
me.name
// Nicola
In other words, the key hack is on Object.defineProperty
which enable us to redefine the getters/setters of object properties. So, in other words, we can have the power of a graph, behind some simple operations.
As a last note, as many of you may point out, these examples only show how to do operations when both the subject and the predicate are known - what about the rest?
In my opinion, this is already a big step forward simplifying RDF, maybe there are some operations would not be possible to achieve, and we can always come back to the traditional way of handling a graph me.toGraph()
, or we can find together some new clever solutions that have the same spirit of simplicity.
Conclusion
I implemented most of what presented here in a library called simplerdf - please, have a look at further examples. However, as you may expect, this library is not stable at all, the APIs could change drastically in the next few days.
If you are interested, I really would like to hear from you, and maybe fork the Github repository and contribute, so that maybe, we could really write the future RDF library together.
Inspired by this repo that has been written 5 years ago.
- Nicola Greco,
Keep on rocking the decentralized web