Giter VIP home page Giter VIP logo

rdfhop's Introduction

RDF Hop

An RDF library that treats graphs like graphs.

Intallation

npm i rdfhop

Usage

Graphs can represent anything in the world, and for this usage example, it will represent a story. Specifically the story of "Avatar the Last Airbender"

Create a Graph

Let's start with some worldbuilding. Through JavaScript we can easily define a node representing each of the 4 elements. "Water", "Earth", "Fire", and "Air".

import {
  Store,
  SuperStore,
  NamedNode,
  NodeSet
} from 'rdfhop'
import rdf from 'rdfhop/vocab/rdf'
import foaf from 'rdfhop/vocab/foaf'

const ELEMENT_PREFIX = 'https://avatar.example/elements#'
const VOCAB_PREFIX = 'https://avatar.example/vocab#'

const elementStore = new Store()

const elements: NodeSet = elementStore.add([
  new NamedNode(ELEMENT_PREFIX + 'water').addOut(rdf.label, new Literal('water')),
  new NamedNode(ELEMENT_PREFIX + 'earth').addOut(rdf.label, new Literal('earth')),
  new NamedNode(ELEMENT_PREFIX + 'fire').addOut(rdf.label, new Literal('fire')),
  new NamedNode(ELEMENT_PREFIX + 'air').addOut(rdf.label, new Literal('air'))
])
elements.forEach((element: NamedNode) => {
  element.addOut(rdf.type, new NamedNode(VOCAB_PREFIX + 'Element'))
})

console.log(elementStore.toString())



import hop, {
  NamedNode,
  RDFNode
} from './index'
import rdf from 'rdfhop/vocab/rdf'
import foaf from 'rdfhop/vocab/foaf'

const ELEMENT_PREFIX = 'https://avatar.example/elements#'
const VOCAB_PREFIX = 'https://avatar.example/vocab#'

const elementGraph = hop.graph('https://avatar.example/elements')

const elements = [
  hop.namedNode(ELEMENT_PREFIX + 'water').addOut(rdf.label, hop.literal('water'), [elementGraph]),
  hop.namedNode(ELEMENT_PREFIX + 'earth').addOut(rdf.label, hop.literal('earth'), [elementGraph]),
  hop.namedNode(ELEMENT_PREFIX + 'fire').addOut(rdf.label, hop.literal('fire'), [elementGraph]),
  hop.namedNode(ELEMENT_PREFIX + 'air').addOut(rdf.label, hop.literal('air'), [elementGraph])
]

elements.forEach((element) => {
  element.addOut(rdf.type, hop.namedNode(VOCAB_PREFIX + 'Element'), [elementGraph])
})

console.log(elements.map((element) => element.toString()))

This will log:

@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>.
@prefix elements: <https://avatar.example/element#>.
@prefix vocab: https://avatar.example/vocab#>.
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

elements:water a vocab:Element ;
  rdf:label "water"^^xsd:string .
elements:earth a vocab:Element ;
  rdf:label "earth"^^xsd:string ,
elements:fire a vocab:Element ;
  rdf:label "fire"^^xsd:string .
elements:air a vocab:Element ;
  rdf:label "air"^^xsd:string .

Already we can see how intuitive it is to set up an RDF graph. RDF Hop is designed to work with all the JavaScript paridigms you're used to.

Travserse a the Graph

Now that we've defined a graph and put it into a store, we can traverse the graph.

const water = hop.namedNode(ELEMENT_PREFIX + "water")
const earth = hop.namedNode(ELEMENT_PREFIX + "earth")
const fire = hop.namedNode(ELEMENT_PREFIX + "fire")
const air = hop.namedNode(ELEMENT_PREFIX + 'air')

console.log(fire.out(rdf.label).one().value()) // "fire^^xsd:string"

Notice the out command. This will yeild all nodes that are the object of the provided predicate. Because this returns a collection of nodes, we call one() which picks a singular node from the collection. value yeilds the real string value of the literal.

const air = elementStore.getLiteral("air").in(rdf.label).one()

Similarly, you can use the in method to trace the graph backwards.

Multiple Stores

You might want to keep graphs with overlapping data separate in the case you have multiple RDF files. To do so create a new store:

const CHARACTER_PREFIX = 'https://avatar.example/characters#'
const characterGraph = hop.graph('https://avatar.example/characters')

// Define Characters
const rawCharacterData: { name: string, elems: RDFNode[] }[] = [
  { name: 'Katara', elems: [water] },
  { name: 'Sokka', elems: [] },
  { name: 'Aang', elems: [water, earth, fire, air]},
  { name: 'Zuko', elems: [fire]},
  { name: 'Jet', elems: []},
  { name: 'Toph', elems: [earth]}
]
const characters = rawCharacterData.map(({ name, elems }) => {
    const character = hop.namedNode(CHARACTER_PREFIX + name)
      .addOut(rdf.type, foaf.Person, [characterGraph])
      .addOut(foaf.name, hop.literal(name), [characterGraph])
    elems.forEach((elem: RDFNode) => character.addOut(hop.namedNode(VOCAB_PREFIX + 'bends'), elem, [characterGraph]))
    return character
})

// Ensure all characters know each other
characters.forEach((personA: NamedNode) => {
  characters.forEach((personB: NamedNode) => {
    if (personA !== personB) {
      personA.addOut(foaf.knows, personB)
    }
  })
})

console.log(characterGraph.nodes().map(node => node.toString()))

Notice that despite the elements being added as an object to the characters, the knows predicate will not be applied to them becasue these elements are not in the characterStore, and would not be included in the set yeilded by getAllNamedNodes()

Removing Data

Let's say it's mid season 2 and Toph doesn't know Zuko or Jet. We can remove her know connections:

const toph = hop.namedNode(CHARACTER_PREFIX + 'Toph')
const zuko = hop.namedNode(CHARACTER_PREFIX + 'Zuko')
const jet = hop.namedNode(CHARACTER_PREFIX + 'Jet')
toph.deleteOut(foaf.knows, zuko)
toph.deleteIn(foaf.knows, zuko)
toph.deleteOut(foaf.knows, jet)
toph.deleteIn(foaf.knows, jet)

console.log(characterGraph.nodes().map(node => node.toString()))

rdfhop's People

Contributors

jaxoncreed avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.