anusii / rdflib

A pure Dart package for working with RDF (resource description framework).
https://pub.dev/packages/rdflib
GNU General Public License v3.0
13 stars 3 forks source link

RDFLib: blank nodes support #6

Open tian3rd opened 1 year ago

tian3rd commented 1 year ago

This is created by @Sergio, when converted from draft to issue, it removed @Sergio from the list... Probably a bug in GitHub Kanban.

We need to consider the proper RDF Data Model components: IRIs, Literals, Blank nodes, etc.

https://www.w3.org/TR/rdf11-primer/#section-blank-node https://www.w3.org/TR/2014/REC-rdf11-concepts-20140225/#section-blank-nodes

tian3rd commented 1 year ago

Based on discussion with @Sergio, we can start from parsing simple triples with blank nodes using _:.

Refer to https://www.w3.org/TR/turtle/#unlabeled-bnodes for more details.

# example from https://www.w3.org/TR/turtle/#unlabeled-bnodes
_:a <http://xmlns.com/foaf/0.1/name> "Alice" .
_:a <http://xmlns.com/foaf/0.1/knows> _:b .
_:b <http://xmlns.com/foaf/0.1/name> "Bob" .
_:b <http://xmlns.com/foaf/0.1/knows> _:c .
_:c <http://xmlns.com/foaf/0.1/name> "Eve" .
_:b <http://xmlns.com/foaf/0.1/mbox> <bob@example.com> .

which is the full form of of the following abbreviated form:

@prefix foaf: <http://xmlns.com/foaf/0.1/> .

[ foaf:name "Alice" ] foaf:knows [
    foaf:name "Bob" ;
    foaf:knows [
        foaf:name "Eve" ] ;
    foaf:mbox <bob@example.com> ] .
tian3rd commented 1 year ago

This is a long shot as we are aiming for a complete parser for the turtle file based on the grammar: https://www.w3.org/TR/turtle/#sec-grammar-grammar .

  1. So the first step is to create a terminal or combinator parsers for parsing BlankNode ([137s] BlankNode ::= BLANK_NODE_LABEL | ANON)

  2. Next, add methods in the rdflib package to interpret the parsed result, and save the blank nodes to the Graph.

  3. Moreover, replace the simple parsers with more flexible parses based on GrammarDefinition (from petitparser)

  4. Add tests to ensure the right results.

gjwgit commented 1 year ago

Current updates not yet supporting blank nodes. We do not use blank nodes our data for now although may need it sometime in future. Sergio suggests we may need some time in future. Postpone for now.

tian3rd commented 1 year ago

Update:

Moved this back from backlog, and started to play with the parsed blank node.

Need to refer to the implementation of python rdflib to get more ideas like the BNode class

gjwgit commented 1 year ago

Status update?

tian3rd commented 1 year ago
  1. Now the package can parse the blank nodes properly
  2. One problem is how to represent these blank node in the graphs (as a blank node class instance or a special URIRef instance) and its related functions including serialization, finding related triples by criterial: e.g, Graph.subjects()

Working on this issue as I refactor the code and rewrite the examples.

tian3rd commented 1 year ago

Decide to go on with a new blank node class, and include to add a property of a unique URIRef id. This is good for storing the triples in consistency with previous format: Triple(URIRef, URIRef, dynamic).

tian3rd commented 1 year ago

Based on the grammar, the example above:

[ foaf:name "Alice" ] foaf:knows [
    foaf:name "Bob" ;
    foaf:knows [
        foaf:name "Eve" ] ;
    foaf:mbox <bob@example.com> ] .

is actually not a valid triple. With a slight change to the following, it's valid though:

[]
   foaf:name "Alice";
   foaf:knows [
    foaf:name "Bob" ;
    foaf:knows [
        foaf:name "Eve" ] ;
    foaf:mbox <bob@example.com> ] .
zheyxu commented 1 year ago

Hi @tian3rd, why is the following one valid?

[]
   foaf:name "Alice";
   foaf:knows [
    foaf:name "Bob" ;
    foaf:knows [
        foaf:name "Eve" ] ;
    foaf:mbox <bob@example.com> ] .

As the example provided is the first one.

tian3rd commented 1 year ago

Hi @tian3rd, why is the following one valid?

[]
   foaf:name "Alice";
   foaf:knows [
    foaf:name "Bob" ;
    foaf:knows [
        foaf:name "Eve" ] ;
    foaf:mbox <bob@example.com> ] .

As the example provided is the first one.

It's correct/incorrect according to the grammar specified here: https://www.w3.org/TR/turtle/#sec-grammar-grammar. We can have a chat if you have time, and go over some of the technical aspects of the links I sent you last week.