问题
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
?
回答1:
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:
PREFIX : <http://example.org/ontology#>
PREFIX person: <http://example.org/data/person/>
person:Bob :loves person:Mary.
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:
PREFIX : <http://example.org/ontology#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX person: <http://example.org/data/person/>
PREFIX statement: <http://example.org/data/statement/>
person:Bob :loves person:Mary.
statement:1 a rdf:Statement;
rdf:subject person:Bob;
rdf:predicate :loves;
rdf:object person:Mary;
:reason <http://loveletters.com/123>.
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.
PREFIX relationship: <http://example.org/data/relationship/>
relationship:1 a :LovesRelationship;
:who person:Bob;
:whom person:Mary;
:reason <http://loveletters.com/123>.
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:
<<person:Bob :loves person:Mary>>
:reason <http://loveletters.com/123>.
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:
ASK { person:Bob :loves ?anyone }
Option 2 requires a different query, because of the changed model:
ASK {
?rel a :LovesRelationship;
:who person:Bob.
}
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:
SELECT ?whom ?why {
?statement a rdf:Statement;
rdf:subject person:Bob;
rdf:predicate :loves;
rdf:object ?whom;
:reason ?why.
}
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:
SELECT ?whom ?why {
?rel a :LovesRelationship;
:who person:Bob;
:whom ?whom;
:reason ?why.
}
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*:
SELECT ?whom ?why {
<<person:Bob :loves ?whom>>
:reason ?why.
}
This is concise and intuitive, so it's a shame we can't currently use it in most SPARQL systems!
来源:https://stackoverflow.com/questions/55885944/can-rdf-model-a-labeled-property-graph-with-edge-properties