I wanted to model partner relationship like the following, which I expressed in the format of labeled property graph.
I wanted to use RDF language to express the above graph, particularly I wanted to understand if I can express the label of the "loves" edge (which is an URI to an article/letter).
I am new to RDF, and I know the RDF can easily express the node properties in the LPG, but is it possible to conveniently express the edge properties?
A bit more context of this question: the reason I wanted use RDF (rather than Gremlin) is that I wanted to add some reasoning capability in the long run.
Further added question: if we choose an RDF model to represent the above LPG, in plain English, I wanted to answer the following questions with SPARQL query:
- Is Bob in love with any one?
- If so, who is he in love with and why?
How complex would the SPARQL statement be to query out the loveletters.com/123
?
RDF doesn't support edge properties, so the brief answer is no. But of course there are ways to model this kind of thing in RDF.
Plain RDF triple without edge properties
If we didn't want to annotate the edge, the relationship between Bob and Mary would simply be a triple with Bob as Subject, Mary as object, and “loves” as predicate:
So how can we add annotations?
Option 1: Using RDF Reification
RDF has a built-in solution called “RDF reification”. It allows making statements about statements:
So we say that there is a statement with Bob as subject, Mary as object, and “loves” as predicate. Then we can add properties to that statement. The downside is that it is kind of redundant. First we add the “loves” triple, then we add four more triples to replicate the “loves” triple.
Option 2: Modelling relationships as entities
Another approach is to change the model. Instead of considering “loves” an edge between people, we consider it a node in itself. A node that represents the relationship, and is connected to the two parties involved.
So in our model we created a class
:LovesRelationship
that represents “loves”, and properties:who
and:whom
to indicate the two parties. The downside of this approach is that the graph structure no longer directly represents our social network. So when querying how two people are related, we always have to go through those relationship entities instead of just dealing with edges connecting people.Option 3: Using RDF*
There is a proposal called RDF* that addresses this problem quite nicely. (Sometimes it's called RDR or Reification Done Right.) RDF*/RDR adds new syntax that allows triples to be the subject of other triples:
The downside is that it is non-standard and so far supported only by a few systems (Blazegraph, AnzoGraph, and an extension for Jena). As of April 2019, Neptune is not among them.
Query: Is Bob in love with anyone?
This is easy to do in the basic RDF version as well as in Option 1 and Option 3:
Option 2 requires a different query, because of the changed model:
This would match any
:LovesRelationship
where the:who
property is Bob, regardless of the:whom
and:reason
properties.Query: Who is Bob in love with and why?
Option 1, RDF Reification:
I find this query not very intuitive, because it talks about RDF statements, while we are really interested in people and relationships.
Option 2, relationship modelled as entity:
This is better in my eyes; once you have accepted that relationships are entities in this model, it becomes fairly intuitive.
Option 3, RDF*, using SPARQL*:
This is concise and intuitive, so it's a shame we can't currently use it in most SPARQL systems!
AnzoGraph supports RDF*/SPARQL*, so you could actually use the concise and most intuitive format to represent the data and query it.
Now query:
Result: