Skip to content

Commit

Permalink
#169 Let the n3 parser listen to the datafactory options
Browse files Browse the repository at this point in the history
Toggling the collections flag on the datafactory supports object will
disable the generation of collection objects in-memory. It is still
enabled by default for rdflib.
  • Loading branch information
Fletcher91 committed Oct 18, 2019
1 parent 1ab9015 commit 9ea7462
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 10 deletions.
28 changes: 21 additions & 7 deletions src/formula.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Namespace from './namespace'
import Node from './node'
import Serializer from './serialize'
import Statement from './statement'
import { appliedFactoryMethods, isStatement } from './util'
import { appliedFactoryMethods, arrayToStatements, isStatement } from './util'
import Variable from './variable'

/** @module formula */
Expand Down Expand Up @@ -496,12 +496,26 @@ export default class Formula extends Node {
holdsStatement (st) {
return this.holds(st.subject, st.predicate, st.object, st.why)
}
list (values) {
let collection = new Collection()
values.forEach(function (val) {
collection.append(val)
})
return collection

/**
* Used by the n3parser to generate list elements
* @param values - The values of the collection
* @param context - The store
* @return {BlankNode|Collection} - The term for the statement
*/
list (values, context) {
if (context.rdfFactory.supports["COLLECTIONS"]) {
const collection = context.rdfFactory.collection()
values.forEach(function (val) {
collection.append(val)
})
return collection
} else {
const node = context.rdfFactory.blankNode()
const statements = arrayToStatements(context.rdfFactory, node, values)
context.addAll(statements)
return node
}
}
/**
* transform a collection of NTriple URIs into their URI strings
Expand Down
42 changes: 39 additions & 3 deletions tests/unit/parse-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,43 @@ describe('Parse', () => {
expect(store.statements[0].object.lang).to.eql('be-x-old')
})
})

describe('collections', () => {
describe('with collection term support', () => {
let base = 'https://www.wikidata.org/wiki/Special:EntityData/Q2005.ttl'
let mimeType = 'text/turtle'
let store = DataFactory.graph()
let content = '<http://www.wikidata.org/entity/Q328> <http://www.w3.org/2000/01/rdf-schema#label> ( "0" "1"^^<http://www.w3.org/2001/XMLSchema#number> <http://example.org/> ) .'
parse(content, store, base, mimeType)
expect(store.statements[0].object.termType).to.eql('Collection')
})

describe('without collection term support', () => {
let base = 'https://www.wikidata.org/wiki/Special:EntityData/Q2005.ttl'
let mimeType = 'text/turtle'
let store = DataFactory.graph(undefined, { rdfFactory: CanonicalDataFactory })
let content = '<http://www.wikidata.org/entity/Q328> <http://www.w3.org/2000/01/rdf-schema#label> ( "0" "1"^^<http://www.w3.org/2001/XMLSchema#number> <http://example.org/> ) .'
parse(content, store, base, mimeType)
expect(store.statements.length).to.eql(1 + 3 * 2)

expect(store.statements[0].predicate.value).to.eql('http://www.w3.org/1999/02/22-rdf-syntax-ns#first')
expect(store.statements[0].object.value).to.eql('0')

expect(store.statements[2].predicate.value).to.eql('http://www.w3.org/1999/02/22-rdf-syntax-ns#first')
expect(store.statements[2].object.value).to.eql('1')
expect(store.statements[2].object.datatype.value).to.eql('http://www.w3.org/2001/XMLSchema#number')

expect(store.statements[4].predicate.value).to.eql('http://www.w3.org/1999/02/22-rdf-syntax-ns#first')
expect(store.statements[4].object.termType).to.eql('NamedNode')
expect(store.statements[4].object.value).to.eql('http://example.org/')

expect(store.statements[6].predicate.value).to.eql('http://www.w3.org/2000/01/rdf-schema#label')
expect(store.statements[6].object.termType).to.eql('BlankNode')
expect(store.statements[6].object.value).to.eql(store.statements[0].subject.value)
})
})
})

describe('ttl with charset', () => {
describe('literals', () => {
it('handles language subtags', () => {
Expand All @@ -32,6 +68,7 @@ describe('Parse', () => {
})
})
})

describe('a JSON-LD document', () => {
describe('with a base IRI', () => {
let store
Expand Down Expand Up @@ -80,7 +117,7 @@ describe('Parse', () => {
expect(store.rdfFactory.supports["COLLECTIONS"]).to.be.false
const homePageHeight = 5 // homepage + height + 3 x name
const list = 2 * 3 + 1 // (rdf:first + rdf:rest) * 3 items + listProp
expect(store.statements).to.have.length(homePageHeight + list);
expect(store.statements).to.have.length(homePageHeight + list)

const height = store.statements[0]
expect(height.subject.value).to.equal('https://www.example.org/#me')
Expand Down Expand Up @@ -172,8 +209,7 @@ describe('Parse', () => {

it('uses the specified base IRI', () => {
expect(store.rdfFactory.supports["COLLECTIONS"]).to.be.true
console.log(store.statements)
expect(store.statements).to.have.length(1);
expect(store.statements).to.have.length(1)

const collection = store.statements[0]
expect(collection.subject.value).to.equal('https://www.example.org/#me')
Expand Down

0 comments on commit 9ea7462

Please sign in to comment.