Skip to content
This repository was archived by the owner on Oct 25, 2026. It is now read-only.
cristianvasquezPublic archive

About

A mini cli to inspect/manipulate RDF files

Topics

Resources

Stars

2 stars

Watchers

1 watching

Forks

Repository files navigation

uuid 7e28b748-491f-450a-a0d5-afb7f784df2b
repo-uri osg://repo/github.com/cristianvasquez/rdf-cli
repo-name rdf-cli
layout node.js
tags
repo/rdf
repo-group rdf
notes published

rdf-cli

rdf: RDF commands that compose over Unix pipes. Install: npm install -g rdf-cli.

Commands

Nodes are stream kinds; edges are commands.

flowchart TD
  RDF([RDF bytes]) -- read --> NQ([N-Quads])
  PL([path lines]) -- from-paths --> NQ
  IL([IRI lines]) -- fetch --> NQ
  NIL([nothing]) -- endpoint-select --> B([JSONL bindings])
  NIL -- endpoint-construct --> NQ
  NQ -- "filter · map · construct<br/>claim · validate · skolem<br/>canonicalize · dispatch" --> NQ
  NQ -- select --> B
  NQ -- ask --> T([text])
  NQ -- pretty --> RDF
  B -- table --> RD([results document])
Loading
command in → out purpose
read RDF bytes → N-Quads Parse RDF files or stdin into N-Quads.
from-paths path lines → N-Quads Parse the files named on stdin, one path per line, into N-Quads.
fetch IRI lines → N-Quads Dereference the IRIs on stdin, one per line, into N-Quads.
endpoint-select nothing → JSONL bindings Run a remote SPARQL SELECT and emit SPARQL JSON results as a head line followed by binding lines.
endpoint-construct nothing → N-Quads Run a SPARQL CONSTRUCT or DESCRIBE on a remote endpoint and emit its triples, graphless.
filter N-Quads → N-Quads Keep the quads where a SPARQL expression is true.
map N-Quads → N-Quads Rewrite the quads that match --where with SPARQL expressions.
select N-Quads → JSONL bindings Run a SPARQL SELECT and emit SPARQL JSON results as a head line followed by binding lines.
construct N-Quads → N-Quads Run a SPARQL CONSTRUCT and emit its triples, graphless.
ask N-Quads → text Run a SPARQL ASK and print true or false; exit 1 on false.
claim N-Quads → N-Quads Apply one claimer: claimed quads and views move into named graphs.
validate N-Quads → N-Quads Validate against SHACL shapes; append the report as a named graph; exit 1 on non-conformance.
skolem N-Quads → N-Quads Replace blank nodes with generated IRIs, one IRI per blank node in the run.
canonicalize N-Quads → N-Quads Emit the RDFC-1.0 canonical form.
dispatch N-Quads → N-Quads Write each named graph under ROOT to a file; pass the other quads on.
pretty N-Quads → RDF bytes Render as TriG (default), Turtle, N-Quads, or N-Triples; any other format is an error.
table JSONL bindings → results document Serialize SPARQL results as CSV, TSV, or JSON.

rdf <command> --help gives the arguments. spec/manifest.hs gives the contract: types, laws, library API.

Rules

  • RDF data between commands is N-Quads. select and endpoint-select emit JSON Lines: first {"head":{"vars":["s","missing","o"]}} with variable names in SELECT projection order, then one binding object per line, for example {"s":{"type":"uri","value":"urn:a"},"o":{"type":"uri","value":"urn:x"}}. Binding values are SPARQL JSON terms (uri, literal, or bnode, with value and optional xml:lang or datatype); unbound keys are absent, not null. The head is emitted even with zero bindings. table uses head.vars for column order: CSV has plain values, TSV has RDF term syntax and ?-prefixed headers, and JSON is a complete SPARQL results document. Absent bindings are empty CSV/TSV cells; zero rows still emits the header. JSON consumers must skip the first line to process bindings only.
  • Graphless quads stay graphless. Only map -g and read --graph-from path add graphs; map -g default removes them.
  • Each input file is its own blank-node scope. Labels do not change between commands.
  • In filter and map, an expression sees one quad: ?s ?p ?o ?g. ?g is unbound in the default graph.
  • An error goes to stderr and gives exit code 1. No failure is silent.
  • Input formats: trig, turtle/ttl, nquads/nq, ntriples/nt, jsonld/json, rdfxml/xml, n3. From the file extension, or --format.

Recipes

The smoke test runs each block from a copy of the repository.

# Add a graph to graphless quads; remove all graphs
rdf read examples/data/*.ttl | rdf map --where '!bound(?g)' -g '<urn:batch>' | rdf pretty
rdf read examples/data/two-graphs.trig | rdf map -g default | rdf pretty --format turtle
# Keep English literals and all non-literals
rdf read examples/data/*.ttl | rdf filter 'coalesce(langMatches(lang(?o), "en"), true)'
# Change a namespace
rdf read examples/data/*.ttl \
  | rdf map --where 'strstarts(str(?s), "http://example.org/")' \
            -s 'iri(replace(str(?s), "^http://example.org/", "https://example.com/"))'
# Query to CSV; test a condition with the exit code
rdf read examples/data/*.ttl | rdf select 'SELECT ?s ?name WHERE { ?s <http://xmlns.com/foaf/0.1/name> ?name }' | rdf table
rdf read examples/data/*.ttl | rdf ask 'ASK { ?s a <http://xmlns.com/foaf/0.1/Person> }'
# Compare two files, independent of blank-node labels, order, and syntax
diff <(rdf read examples/data/two-graphs.trig | rdf canonicalize) \
     <(rdf read examples/data/two-graphs.nq | rdf canonicalize)
# One graph per file, rename the graphs, write each graph to its own file
rdf read --graph-from path 'examples/data/*.ttl' \
  | rdf map -g 'iri(replace(str(?g), "^file://./examples/data/", "file://./out/"))' \
  | rdf dispatch file://./out/ --destination out
# Validate: the report is appended as a named graph; exit 1 on non-conformance
rdf read tests/fixtures/person-valid.ttl | rdf validate --shapes tests/fixtures/person-shape.ttl --markdown-report > /dev/null
# Claim: claimed quads and views move into named graphs; the rest stays graphless for the next claim
rdf read tests/fixtures/person-valid.ttl | rdf claim tests/fixtures/person-claimer.trig | rdf pretty

Library

import { sources, transforms } from 'rdf-cli'   // also: sinks, pipeline

const store = await transforms.materialize(sources.readFromGlob(['./data/**/*.ttl']))
const { vars, rows } = transforms.select(store, 'SELECT ?s ?o WHERE { ?s ?p ?o }')
console.log(vars) // ['s', 'o'], including when there are no rows
for await (const row of rows) console.log(row.s.value)

pipeline composes operations and records their provenance as PROV-O. Signatures: spec/manifest.hs.

About

A mini cli to inspect/manipulate RDF files

Topics

Resources

Stars

2 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages