Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions example/people/social_book.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var cardTemplate = ""

/**
* run on initialization
*/
*/
function initialize() {
cardTemplate = $("#user_wrapper").clone()
//todo: place the proxy default somewhere else ( where?) so it can be easy to configure
Expand All @@ -39,7 +39,7 @@ function card(who,kb) {
/** mailboxes in foaf are usually written as <mailto:henry.story@bblfish.net> . This
* function removes the 'mailto:' part, if it exists */
function removeProtocol(uri) {
if (uri && uri.termType === 'symbol') {
if (uri && uri.termType === 'NamedNode') {
var parts= uri.split(":")
if (parts.length > 1) return parts[1]
else return "unset"
Expand Down Expand Up @@ -145,7 +145,7 @@ function friends (person,kb,col) {

for (i = 0; i < n; i++) {
friend = friends[i];
if (friend && friend.termType === 'symbol') { //only show people with a WebID for the moment.
if (friend && friend.termType === 'NamedNode') { //only show people with a WebID for the moment.
var name = kb.any(friend, FOAF('name'))
if (!name) {
name = friend.uri
Expand All @@ -164,9 +164,9 @@ function friends (person,kb,col) {
}

/**
* redraw the screen when the selected user identified by webid is pressed in
* redraw the screen when the selected user identified by webid is pressed in
* explorer column col
*/
*/
function redraw(webid, col) {
if (!col) col = 0
var person = $rdf.sym(webid);
Expand All @@ -176,7 +176,7 @@ function redraw(webid, col) {
docURI = webid.slice(0, indexOf)
else docURI = webid
var kb = graphs[docURI]
if (!kb) {
if (!kb) {
//if the knowledge base was not initialised fetch info from the web (if need CORS go through CORS proxy)
kb = graphs[docURI] = new $rdf.IndexedFormula();
var fetch = $rdf.fetcher(kb);
Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ $rdf.variable = $rdf.DataFactory.variable

// RDFJS DataFactory interface
$rdf.blankNode = $rdf.DataFactory.blankNode
$rdf.defaultGraph = $rdf.DataFactory.defaultGraph
$rdf.literal = $rdf.DataFactory.literal
$rdf.namedNode = $rdf.DataFactory.namedNode
$rdf.quad = $rdf.DataFactory.quad
Expand Down
5 changes: 4 additions & 1 deletion src/blank-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,15 @@ class BlankNode extends Node {
formula.copyTo(this, bnodeNew)
return bnodeNew
}
toCanonical () {
return '_:' + this.value
}
toString () {
return BlankNode.NTAnonymousNodePrefix + this.id
}
}
BlankNode.nextId = 0
BlankNode.termType = 'bnode'
BlankNode.termType = 'BlankNode'
BlankNode.NTAnonymousNodePrefix = '_:n'
BlankNode.prototype.classOrder = ClassOrder['BlankNode']
BlankNode.prototype.isBlank = 1
Expand Down
6 changes: 6 additions & 0 deletions src/data-factory.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'
const BlankNode = require('./blank-node')
const Collection = require('./collection')
const DefaultGraph = require('./default-graph')
const Fetcher = require('./fetcher')
const IndexedFormula = require('./indexed-formula')
const Literal = require('./literal')
Expand All @@ -14,6 +15,9 @@ function blankNode (value) {
function collection (elements) {
return new Collection(elements)
}
function defaultGraph () {
return new DefaultGraph()
}
function fetcher (store, timeout, async) {
return new Fetcher(store, timeout, async)
}
Expand All @@ -38,6 +42,7 @@ function namedNode (value) {
return new NamedNode(value)
}
function quad (subject, predicate, object, graph) {
graph = graph || new DefaultGraph()
return new Statement(subject, predicate, object, graph)
}
function st (subject, predicate, object, graph) {
Expand All @@ -52,6 +57,7 @@ function variable (name) {

// rdfjs spec factory methods
module.exports.blankNode = blankNode
module.exports.defaultGraph = defaultGraph
module.exports.graph = graph
module.exports.literal = literal
module.exports.namedNode = namedNode
Expand Down
15 changes: 15 additions & 0 deletions src/default-graph.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict'
const Node = require('./node')

class DefaultGraph extends Node {
constructor () {
super()
this.termType = 'DefaultGraph'
this.value = ''
}
toCanonical () {
return this.value
}
}

module.exports = DefaultGraph
13 changes: 0 additions & 13 deletions src/formula.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,6 @@ class Formula extends Node {
addStatement (st) {
return this.statements.push(st)
}
any (s, p, o, g) {
var st = this.anyStatementMatching(s, p, o, g)
if (st == null) {
return void 0
} else if (s == null) {
return st.subject
} else if (p == null) {
return st.predicate
} else if (o == null) {
return st.object
}
return void 0
}
bnode (id) {
return new BlankNode(id)
}
Expand Down
20 changes: 16 additions & 4 deletions src/indexed-formula.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,19 @@ class IndexedFormula extends Formula {
this.add(quad.subject, quad.predicate, quad.object, quad.graph)
})
}

any (s, p, o, g) {
var st = this.anyStatementMatching(s, p, o, g)
if (st == null) {
return void 0
} else if (s == null) {
return st.subject
} else if (p == null) {
return st.predicate
} else if (o == null) {
return st.object
}
return void 0
}
anyStatementMatching (subj, pred, obj, why) {
var x = this.statementsMatching(subj, pred, obj, why, true)
if (!x || x.length === 0) {
Expand Down Expand Up @@ -361,11 +373,11 @@ class IndexedFormula extends Formula {
for (var i = 0;i < statList.length;i++) {
var st = statList[i]
switch (st.object.termType) {
case 'symbol':
case 'NamedNode':
this.add(target, st.predicate, st.object)
break
case 'literal':
case 'bnode':
case 'Literal':
case 'BlankNode':
case 'collection':
this.add(target, st.predicate, st.object.copy(this))
}
Expand Down
4 changes: 2 additions & 2 deletions src/jsonparser.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ var jsonParser = (function () {
if (obj.type === 'uri') {
object = store.sym(obj.value)
store.add(subject, predicate, object, why)
} else if (obj.type === 'bnode') {
} else if (obj.type === 'BlankNode') {
if (bnodes[obj.value]) {
object = bnodes[obj.value]
} else {
object = store.bnode(obj.value)
bnodes[obj.value] = object
}
store.add(subject, predicate, object, why)
} else if (obj.type === 'literal') {
} else if (obj.type === 'Literal') {
// var datatype
if (obj.datatype) {
object = store.literal(obj.value, undefined, store.sym(obj.datatype))
Expand Down
39 changes: 27 additions & 12 deletions src/literal.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'
const ClassOrder = require('./class-order')
const NamedNode = require('./named-node')
const Node = require('./node')
const XSD = require('./xsd')

Expand All @@ -8,9 +9,14 @@ class Literal extends Node {
super()
this.termType = Literal.termType
this.value = value
this.lang = language // property currently used by rdflib
this.language = language // rdfjs property
this.datatype = datatype
if (language) {
this.lang = language
datatype = XSD.langString
}
// If not specified, a literal has the implied XSD.string default datatype
if (datatype) {
this.datatype = NamedNode.fromValue(datatype)
}
}
copy () {
return new Literal(this.value, this.lang, this.datatype)
Expand All @@ -25,6 +31,12 @@ class Literal extends Node {
((!this.datatype && !other.datatype) ||
(this.datatype && this.datatype.equals(other.datatype)))
}
get language () {
return this.lang
}
set language (language) {
this.lang = language || ''
}
toNT () {
if (typeof this.value === 'number') {
return this.toString()
Expand All @@ -37,11 +49,12 @@ class Literal extends Node {
str = str.replace(/\"/g, '\\"')
str = str.replace(/\n/g, '\\n')
str = '"' + str + '"'
if (this.datatype) {
str += '^^' + this.datatype.toNT()
}

if (this.language) {
str += '@' + this.language
} else if (!this.datatype.equals(XSD.string)) {
// Only add datatype if it's not a string
str += '^^' + this.datatype.toCanonical()
}
return str
}
Expand All @@ -56,7 +69,7 @@ class Literal extends Node {
*/
static fromBoolean (value) {
let strValue = value ? '1' : '0'
return new Literal(strValue, void 0, XSD.boolean)
return new Literal(strValue, null, XSD.boolean)
}
/**
* @method fromDate
Expand All @@ -74,7 +87,7 @@ class Literal extends Node {
let date = '' + value.getUTCFullYear() + '-' + d2(value.getUTCMonth() + 1) +
'-' + d2(value.getUTCDate()) + 'T' + d2(value.getUTCHours()) + ':' +
d2(value.getUTCMinutes()) + ':' + d2(value.getUTCSeconds()) + 'Z'
return new Literal(date, void 0, XSD.dateTime)
return new Literal(date, null, XSD.dateTime)
}
/**
* @method fromNumber
Expand All @@ -94,18 +107,18 @@ class Literal extends Node {
} else {
datatype = XSD.integer
}
return new Literal('' + value, void 0, datatype)
return new Literal('' + value, null, datatype)
}
/**
* @method fromValue
* @param value
* @return {Literal}
*/
static fromValue (value) {
if (value instanceof Node) {
if (typeof value === 'undefined' || value === null) {
return value
}
if (typeof value === 'undefined' || value === null) {
if (value && value.termType) { // this is a Node instance
return value
}
switch (typeof value) {
Expand All @@ -125,8 +138,10 @@ class Literal extends Node {

}
}
Literal.termType = 'literal'
Literal.termType = 'Literal'
Literal.prototype.classOrder = ClassOrder['Literal']
Literal.prototype.datatype = XSD.string
Literal.prototype.lang = ''
Literal.prototype.isVar = 0

module.exports = Literal
23 changes: 21 additions & 2 deletions src/named-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class NamedNode extends Node {
constructor (iri) {
super()
this.termType = NamedNode.termType
this.uri = iri
this.value = iri
}
/**
Expand All @@ -37,8 +36,28 @@ class NamedNode extends Node {
toString () {
return '<' + this.uri + '>'
}

/**
* Legacy getter and setter alias, node.uri
*/
get uri () {
return this.value
}
set uri (uri) {
this.value = uri
}
static fromValue (value) {
if (typeof value === 'undefined' || value === null) {
return value
}
const isNode = value && value.termType
if (isNode) {
return value
}
return new NamedNode(value)
}
}
NamedNode.termType = 'symbol'
NamedNode.termType = 'NamedNode'
NamedNode.prototype.classOrder = ClassOrder['NamedNode']
NamedNode.prototype.isVar = 0

Expand Down
5 changes: 3 additions & 2 deletions src/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,11 @@ Node.fromValue = function fromValue (value) {
const Collection = require('./collection')
const Literal = require('./literal')
const NamedNode = require('./named-node')
if (value instanceof Node || value instanceof Collection) {
if (typeof value === 'undefined' || value === null) {
return value
}
if (typeof value === 'undefined' || value === null) {
const isNode = value && value.termType
if (isNode) { // a Node subclass or a Collection
return value
}
if (Array.isArray(value)) {
Expand Down
Loading