Skip to content

Commit

Permalink
feat: graph traversals
Browse files Browse the repository at this point in the history
  • Loading branch information
tpluscode committed Jul 16, 2020
1 parent b1a4500 commit 3ab31c0
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 2 deletions.
16 changes: 16 additions & 0 deletions lib/Clownface.js
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,22 @@ class Clownface {
return this
}

from (graph) {
const context = this._context.map(context => context.clone({
graph: graph === null ? rdf.defaultGraphInstance : graph || undefined
}))

return Clownface.fromContext(context)
}

fromDefaultGraph () {
return this.from(rdf.defaultGraphInstance)
}

fromUnionGraph () {
return this.from(undefined)
}

_toTermArray (predicates, type, languageOrDatatype) {
return toTermArray(predicates, type, languageOrDatatype, this.factory)
}
Expand Down
9 changes: 7 additions & 2 deletions lib/Context.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@ class Context {
this.term = term(value, undefined, undefined, factory)
}

clone ({ dataset = this.dataset, graph = this.graph, value, factory = this.factory, namespace = this.namespace }) {
return new Context({ dataset, graph, value, factory, namespace })
clone ({ dataset = this.dataset, graph, value, factory = this.factory, namespace = this.namespace }) {
let g = graph
if (typeof graph === 'undefined') {
g = undefined
}

return new Context({ dataset, graph: g, value, factory, namespace })
}

has (predicate, object) {
Expand Down
78 changes: 78 additions & 0 deletions test/Clownface/from.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/* global describe, it */

const assert = require('assert')
const { rdfs } = require('@tpluscode/rdf-ns-builders')
const clownface = require('../..')
const rdf = require('../support/factory')
const ns = require('../support/namespace')

describe('from', () => {
it('called with graph changes context to same term in named graph', () => {
const term = ns.ex.Foo
const dataset = rdf.dataset([
rdf.quad(term, rdfs.label, rdf.literal('default graph')),
rdf.quad(term, rdfs.label, rdf.literal('named graph'), ns.ex.NamedGraph)
])
const pointer = clownface({ dataset, term })

const movedPointer = pointer.from(ns.ex.NamedGraph)

assert.deepStrictEqual(movedPointer.out(rdfs.label).value, 'named graph')
})

it('called with undefined changes context to same term across all graphs', () => {
const term = ns.ex.Foo
const dataset = rdf.dataset([
rdf.quad(term, rdfs.label, rdf.literal('default graph')),
rdf.quad(term, rdfs.label, rdf.literal('named graph'), ns.ex.NamedGraph)
])
const pointer = clownface({ dataset, term, graph: ns.ex.NamedGraph })

const movedPointer = pointer.from(undefined)

assert.deepStrictEqual(movedPointer.out(rdfs.label).values.length, 2)
})

it('called with null changes context to same term in default graph', () => {
const term = ns.ex.Foo
const dataset = rdf.dataset([
rdf.quad(term, rdfs.label, rdf.literal('default graph')),
rdf.quad(term, rdfs.label, rdf.literal('named graph'), ns.ex.NamedGraph)
])
const pointer = clownface({ dataset, term })

const movedPointer = pointer.from(null)

assert.deepStrictEqual(movedPointer.out(rdfs.label).value, 'default graph')
})
})

describe('fromDefaultGraph', () => {
it('changes context to same term in default graph', () => {
const term = ns.ex.Foo
const dataset = rdf.dataset([
rdf.quad(term, rdfs.label, rdf.literal('default graph')),
rdf.quad(term, rdfs.label, rdf.literal('named graph'), ns.ex.NamedGraph)
])
const pointer = clownface({ dataset, term })

const movedPointer = pointer.fromDefaultGraph()

assert.deepStrictEqual(movedPointer.out(rdfs.label).value, 'default graph')
})
})

describe('fromUnionGraph', () => {
it('changes context to same term from all graphs', () => {
const term = ns.ex.Foo
const dataset = rdf.dataset([
rdf.quad(term, rdfs.label, rdf.literal('default graph')),
rdf.quad(term, rdfs.label, rdf.literal('named graph'), ns.ex.NamedGraph)
])
const pointer = clownface({ dataset, term, graph: ns.ex.NamedGraph })

const movedPointer = pointer.fromUnionGraph()

assert.deepStrictEqual(movedPointer.out(rdfs.label).values.length, 2)
})
})

0 comments on commit 3ab31c0

Please sign in to comment.