Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

List of useful commands from rdf-ext etc #30

Open
jsheunis opened this issue Jun 24, 2024 · 0 comments
Open

List of useful commands from rdf-ext etc #30

jsheunis opened this issue Jun 24, 2024 · 0 comments

Comments

@jsheunis
Copy link
Collaborator

jsheunis commented Jun 24, 2024

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()

  • Follow incoming edges to the current term, i.e. find subjects that point to the current object through any predicate.
  • useful when you want to find all entities that have a specific relationship with the current entity.
// return all quads where someTerm is the object.
const subjects = rdf.grapoi({ dataset: someDataset, term: someTerm }).in();

out()

  • traverse the graph by following outgoing edges from the current term, i.e. find objects that the current subject points to through any predicate
  • useful for discovering related entities starting from a known subject.
// return all quads where someTerm is the subject
const objects = grapoi({ dataset: someDataset, term: someTerm }).out();

trim()

  • removes unnecessary history of traversals, keeping only the relevant parts of the query
// return a new Grapoi instance with a trimmed traversal history
const trimmed = grapoi({ dataset: someDataset, term: someTerm }).out().trim();

quads()

  • returns all the quads that have been traversed so far
// return an array of RDF quads
const quads = grapoi({ dataset: someDataset, term: someTerm }).out().quads();

distinct()

  • filters the traversed terms to ensure they are unique
  • useful when you want to avoid processing the same term multiple times in your traversal
const uniqueTerms = grapoi({ dataset: someDataset, term: someTerm }).out().distinct();

hasIn(predicate, object)

  • Filters nodes that have an incoming edge with the specified predicate and object.

hasOut(predicate, object)

  • Filters nodes that have an outgoing edge with the specified predicate and object.
// E.g. find all nodes with predicate rdf:type and object schema:Person
const results = grapoi({ dataset: someDataset })
  .hasOut(ns.rdf.type, ns.schema.Person)
  .quads(); 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant