Skip to content

A new RDF serialisation format that supports RDF 1.2 aka RDF Star.

License

Notifications You must be signed in to change notification settings

doga/rdf-dataflow

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

RDF logo

RDF Dataflow reader/writer

This library introduces a new RDF serialisation format called RDF Dataflow that is based on the SPARQL 1.1 Query Results JSON Format, but the use case for RDF Dataflow is somewhat different:

  • RDF Dataflow is only for serialising RDF datasets, whereas the other format represents any SPARQL query result, including results that that do not contain an RDF dataset, such as SELECT results.
  • RDF Dataflow supports the reification/annotation feature of RDF 1.2 aka RDF Star where the subject of an RDF statement can be another RDF statement.

The content type for RDF Dataflow documents is application/x-rdf-dataflow+json.

Usage example

RDF dataset to RDF-Dataflow and back.
description = '''
Serialise an RDF dataset to an RDF-Dataflow object and read it back.
'''
import { fromDataset, toDataset, contentType } from 'https://esm.sh/gh/doga/[email protected]/mod.mjs';
import rdf from 'https://esm.sh/gh/rdfjs/[email protected]';
import t from 'https://esm.sh/gh/rdfjs/[email protected]';

// Create an in-memory RDF dataset.
const
datasetIn = rdf.dataset(),

// RDF statement #1
subject1   = t.quad(
  t.namedNode('http://site.example/user123'),
  t.namedNode('http://xmlns.com/foaf/0.1/age'),
  t.namedNode('23', t.namedNode('http://www.w3.org/2001/XMLSchema#integer'))
),
predicate1 = t.namedNode('http://site.example/certainty'),
object1    = t.literal('0.9', t.namedNode('http://www.w3.org/2001/XMLSchema#decimal')),

// RDF statement #2
subject2   = t.namedNode('http://site.example/user567'),
predicate2 = t.namedNode('http://www.w3.org/2006/vcard/ns#hasEmail'),
object2    = t.namedNode('mailto:[email protected]');

// Fill up the dataset.
datasetIn.add(t.quad(subject1, predicate1, object1));
datasetIn.add(t.quad(subject2, predicate2, object2));

// Produce an RDF Dataflow object from the dataset.
const
dataflowObject = fromDataset(datasetIn),

// Read the dataset back from Dataflow.
datasetOut = toDataset(dataflowObject);

console.group(`RDF dataset written as a Dataflow object (content type "${contentType}"):`);
console.info(JSON.stringify(dataflowObject, null, 2));
console.groupEnd();

console.group('\nRDF dataset read from the Dataflow object:');
for (const quad of datasetOut) {
  console.group('Quad:');
  if(quad.subject.termType === 'Quad') {
    console.group('Subject quad:');
    console.info(`Subject:   ${quad.subject.subject.value}`)
    console.info(`Predicate: ${quad.subject.predicate.value}`)
    console.info(`Object:    ${quad.subject.object.value}`)
    console.groupEnd();
  } else {
    console.info(`Subject:   ${quad.subject.value}`)
  }
  console.info(`Predicate: ${quad.predicate.value}`)
  console.info(`Object:    ${quad.object.value}`)
  console.groupEnd();
}
console.groupEnd();

Sample output for the code above:

RDF dataset written as a Dataflow object (content type "application/x-rdf-dataflow+json"):
    {
      "head": {
        "terms": [
          "s",
          "p",
          "o",
          "g"
        ]
      },
      "dataset": [
        {
          "s": {
            "type": "quad",
            "value": {
              "s": {
                "type": "uri",
                "value": "http://site.example/user123"
              },
              "p": {
                "type": "uri",
                "value": "http://xmlns.com/foaf/0.1/age"
              },
              "o": {
                "type": "uri",
                "value": "23"
              }
            }
          },
          "p": {
            "type": "uri",
            "value": "http://site.example/certainty"
          },
          "o": {
            "type": "literal",
            "value": "0.9",
            "datatype": "http://www.w3.org/2001/XMLSchema#decimal"
          }
        },
        {
          "s": {
            "type": "uri",
            "value": "http://site.example/user567"
          },
          "p": {
            "type": "uri",
            "value": "http://www.w3.org/2006/vcard/ns#hasEmail"
          },
          "o": {
            "type": "uri",
            "value": "mailto:[email protected]"
          }
        }
      ]
    }

RDF dataset read from the Dataflow object:
    Quad:
        Subject quad:
            Subject:   http://site.example/user123
            Predicate: http://xmlns.com/foaf/0.1/age
            Object:    23
        Predicate: http://site.example/certainty
        Object:    0.9
    Quad:
        Subject:   http://site.example/user567
        Predicate: http://www.w3.org/2006/vcard/ns#hasEmail
        Object:    mailto:[email protected]

Running the usage example

Run the example above by typing this in your terminal (requires Deno 2+):

deno run --allow-net --allow-run --allow-env --allow-read jsr:@andrewbrey/[email protected] --dax=false --mode=isolated 'https://raw.githubusercontent.com/doga/rdf-dataflow/refs/heads/main/README.md'

About

A new RDF serialisation format that supports RDF 1.2 aka RDF Star.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published