psychoinformatics-de / shacl-vue

https://psychoinformatics-de.github.io/shacl-vue/
MIT License
0 stars 0 forks source link

List of useful commands from `rdf-ext` etc #30

Open jsheunis opened 5 days ago

jsheunis commented 5 days ago

Sources:

The repos, tutorial, and examples are useful but not extensive or informative enough. This issue will collect useful commands from these and other sources, and try to work them into a framework of usage specific to our shacl-vue needs.

Namespaces

const ns = {
  house: rdf.namespace('https://housemd.rdf-ext.org/person/'),
  housePlace: rdf.namespace('https://housemd.rdf-ext.org/place/'),
  schema: rdf.namespace('http://schema.org/'),
  rdf: rdf.namespace('http://www.w3.org/1999/02/22-rdf-syntax-ns#')
}

const house = ns.house('gregory-house')
const housePersonQuad = rdf.quad(house, ns.rdf.type, ns.schema.Person)

Nodes and quads


import rdf from 'rdf-ext'

const house = rdf.namedNode('https://housemd.rdf-ext.org/person/gregory-house')
const familyName = rdf.namedNode('http://schema.org/familyName')
const houseFamilyName = rdf.literal('House')

const houseFamilyNameQuad = rdf.quad(house, familyName, houseFamilyName)

const bakerStreet = rdf.namedNode('https://housemd.rdf-ext.org/place/221b-baker-street')
const address = rdf.namedNode('http://schema.org/address')
const bakerStreetAddress = rdf.blankNode()

const bakerStreetAddressQuad = rdf.quad(bakerStreet, address, bakerStreetAddress)

console.log(`bakerStreetAddress Quad: ${bakerStreetAddressQuad.toString()}`)

Datasets

const dataset = rdf.dataset()
dataset
  .add(houseFamilyNameQuad)
  .add(bakerStreetAddressQuad)
console.log(`dataset: ${dataset.toString()}`)

Datasets from URLs

const fileUrl = new URL('../../node_modules/housemd/dist/housemd.nt', import.meta.url)
const fileRelativeUrl = './node_modules/housemd/dist/housemd.nt'
const httpUrl = 'https://housemd.rdf-ext.org/person/gregory-house'

async function main () {
  // read a Dataset from a file URL
  const gregoryHouseFile = await rdf.io.dataset.fromURL(fileUrl)
  console.log(`read ${gregoryHouseFile.size} triples from ${fileUrl}`)

  // read a Dataset from a relative file URL
  const gregoryHouseFileRelative = await rdf.io.dataset.fromURL(fileRelativeUrl)
  console.log(`read ${gregoryHouseFileRelative.size} triples from ${fileRelativeUrl}`)

  // read a Dataset from a http URL
  const gregoryHouseHttp = await rdf.io.dataset.fromURL(httpUrl)
  console.log(`read ${gregoryHouseHttp.size} triples from ${httpUrl}`)
}

Serializing

import formatsPretty from '@rdfjs/formats/pretty.js'

// clone the default environment
const rdfPretty = rdf.clone()

// import pretty print serializers
rdfPretty.formats.import(formatsPretty)

// it's also possible import into the default like this:
// rdf.formats.import(formatsPretty)

// serialize a Dataset to plain text
console.log(await rdfPretty.io.dataset.toText('text/turtle', gregoryHouseHttp))

Dataset traversal

This uses grapoi

in()

// return all quads where someTerm is the object.
const subjects = rdf.grapoi({ dataset, term: someTerm }).in();

out()

// return all quads where someTerm is the subject
const objects = grapoi({ dataset, term: someTerm }).out();

trim()

// return a new Grapoi instance with a trimmed traversal history
const trimmed = grapoi({ dataset, term: someTerm }).out().trim();

quads()

// return an array of RDF quads
const quads = grapoi({ dataset, term: someTerm }).out().quads();

distinct()

const uniqueTerms = grapoi({ dataset, term: someTerm }).out().distinct();

hasIn(predicate, object)

hasOut(predicate, object)

// E.g. find all nodes with predicate rdf:type and object schema:Person
const results = grapoi({ dataset })
  .hasOut(ns.rdf.type, ns.schema.Person)
  .quads();