forked from linkeddata/rdflib.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Let formula, store/indexedFormula implement a proper datafactory
The current implementation contains some methods which are like the spec DataFactory interface but have small distinctions. E.g. `namedNode` is called `sym`, `literal` accepts three rather than two arguments. This commit applies the canonical methods of the rdfjs tf DataFactory interface to the store. End-users can also overwrite the used factory via a constructor argument.
- Loading branch information
Fletcher91
committed
Oct 15, 2019
1 parent
30a5bf7
commit 33bacf9
Showing
11 changed files
with
151 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import BlankNode from './blank-node' | ||
import DefaultGraph from './default-graph' | ||
import Literal from './literal' | ||
import NamedNode from './named-node' | ||
import Statement from './statement' | ||
import Variable from './variable' | ||
|
||
function blankNode (value) { | ||
return new BlankNode(value) | ||
} | ||
|
||
function defaultGraph () { | ||
return new DefaultGraph() | ||
} | ||
|
||
function literal (value, languageOrDatatype) { | ||
if (typeof value !== "string" && !languageOrDatatype) { | ||
return Literal.fromValue(value) | ||
} | ||
|
||
const strValue = typeof value === 'string' ? value : '' + value | ||
if (typeof languageOrDatatype === 'string') { | ||
if (languageOrDatatype.indexOf(':') === -1) { | ||
return new Literal(strValue, languageOrDatatype) | ||
} else { | ||
return new Literal(strValue, null, namedNode(languageOrDatatype)) | ||
} | ||
} else { | ||
return new Literal(strValue, null, languageOrDatatype) | ||
} | ||
} | ||
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 variable (name) { | ||
return new Variable(name) | ||
} | ||
|
||
/** Only contains the factory methods as defined in the spec */ | ||
export default { | ||
blankNode, | ||
defaultGraph, | ||
literal, | ||
namedNode, | ||
quad, | ||
variable, | ||
supports: { | ||
COLLECTIONS: false, | ||
DEFAULT_GRAPH_TYPE: true, | ||
EQUALS_METHOD: true, | ||
VARIABLE_TYPE: true, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,53 @@ | ||
'use strict' | ||
import BlankNode from './blank-node' | ||
import Collection from './collection' | ||
import DefaultGraph from './default-graph' | ||
import CanonicalDataFactory from './data-factory-internal' | ||
import Fetcher from './fetcher' | ||
import IndexedFormula from './store' | ||
import Literal from './literal' | ||
import NamedNode from './named-node' | ||
import Statement from './statement' | ||
import Variable from './variable' | ||
import IndexedFormula from './store' | ||
|
||
/** | ||
* Data factory which also supports Collections | ||
* | ||
* Necessary for preventing circular dependencies. | ||
*/ | ||
const ExtendedTermFactory = { | ||
...CanonicalDataFactory, | ||
collection, | ||
supports: { | ||
COLLECTIONS: true, | ||
DEFAULT_GRAPH_TYPE: true, | ||
EQUALS_METHOD: true, | ||
VARIABLE_TYPE: true, | ||
} | ||
} | ||
|
||
/** Full RDFLib.js Data Factory */ | ||
const DataFactory = { | ||
blankNode, | ||
defaultGraph, | ||
...ExtendedTermFactory, | ||
fetcher, | ||
graph, | ||
lit, | ||
literal, | ||
namedNode, | ||
quad, | ||
st, | ||
triple, | ||
variable, | ||
} | ||
export default DataFactory | ||
|
||
function blankNode (value) { | ||
return new BlankNode(value) | ||
} | ||
function collection (elements) { | ||
return new Collection(elements) | ||
} | ||
function defaultGraph () { | ||
return new DefaultGraph() | ||
} | ||
function fetcher (store, options) { | ||
return new Fetcher(store, options) | ||
} | ||
function graph () { | ||
return new IndexedFormula() | ||
function graph (features = undefined, opts = undefined) { | ||
return new IndexedFormula(features, opts || { rdfFactory: ExtendedTermFactory }) | ||
} | ||
function lit (val, lang, dt) { | ||
return new Literal('' + val, lang, dt) | ||
} | ||
function literal (value, languageOrDatatype) { | ||
if (typeof languageOrDatatype === 'string') { | ||
if (languageOrDatatype.indexOf(':') === -1) { | ||
return new Literal(value, languageOrDatatype) | ||
} else { | ||
return new Literal(value, null, namedNode(languageOrDatatype)) | ||
} | ||
} else { | ||
return new Literal(value, null, languageOrDatatype) | ||
} | ||
} | ||
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) { | ||
return new Statement(subject, predicate, object, graph) | ||
} | ||
function triple (subject, predicate, object) { | ||
return quad(subject, predicate, object) | ||
} | ||
function variable (name) { | ||
return new Variable(name) | ||
return CanonicalDataFactory.quad(subject, predicate, object) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.